Merge pull request #11003 from sepalani/decode-error

Move SocketManager's DecodeError to Common
This commit is contained in:
Admiral H. Curtiss 2022-08-24 17:01:07 +02:00 committed by GitHub
commit a7d358a97b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 23 deletions

View File

@ -9,6 +9,7 @@
#ifndef _WIN32 #ifndef _WIN32
#include <netinet/in.h> #include <netinet/in.h>
#include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#else #else
@ -545,4 +546,24 @@ void RestoreNetworkErrorState(const NetworkErrorState& state)
WSASetLastError(state.wsa_error); WSASetLastError(state.wsa_error);
#endif #endif
} }
const char* DecodeNetworkError(s32 error_code)
{
thread_local char buffer[1024];
#define IS_BSD_STRERROR \
defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(ANDROID) || \
defined(__APPLE__)
#ifdef _WIN32
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_MAX_WIDTH_MASK,
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer,
sizeof(buffer), nullptr);
return buffer;
#elif (IS_BSD_STRERROR) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
strerror_r(error_code, buffer, sizeof(buffer));
return buffer;
#else
return strerror_r(error_code, buffer, sizeof(buffer));
#endif
}
} // namespace Common } // namespace Common

View File

@ -265,4 +265,5 @@ u16 ComputeTCPNetworkChecksum(const IPAddress& from, const IPAddress& to, const
u16 length, u8 protocol); u16 length, u8 protocol);
NetworkErrorState SaveNetworkErrorState(); NetworkErrorState SaveNetworkErrorState();
void RestoreNetworkErrorState(const NetworkErrorState& state); void RestoreNetworkErrorState(const NetworkErrorState& state);
const char* DecodeNetworkError(s32 error_code);
} // namespace Common } // namespace Common

View File

@ -38,23 +38,6 @@
namespace IOS::HLE namespace IOS::HLE
{ {
char* WiiSockMan::DecodeError(s32 ErrorCode)
{
#ifdef _WIN32
// NOT THREAD SAFE
static char Message[1024];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_MAX_WIDTH_MASK,
nullptr, ErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), Message,
sizeof(Message), nullptr);
return Message;
#else
return strerror(ErrorCode);
#endif
}
// The following functions can return // The following functions can return
// - EAGAIN / EWOULDBLOCK: send(to), recv(from), accept // - EAGAIN / EWOULDBLOCK: send(to), recv(from), accept
// - EINPROGRESS: connect, bind // - EINPROGRESS: connect, bind
@ -117,7 +100,7 @@ s32 WiiSockMan::GetNetErrorCode(s32 ret, std::string_view caller, bool is_rw)
} }
ERROR_LOG_FMT(IOS_NET, "{} failed with error {}: {}, ret= {}", caller, error_code, ERROR_LOG_FMT(IOS_NET, "{} failed with error {}: {}, ret= {}", caller, error_code,
DecodeError(error_code), ret); Common::DecodeNetworkError(error_code), ret);
const s32 return_value = TranslateErrorCode(error_code, is_rw); const s32 return_value = TranslateErrorCode(error_code, is_rw);
WiiSockMan::GetInstance().SetLastNetError(return_value); WiiSockMan::GetInstance().SetLastNetError(return_value);
@ -763,12 +746,11 @@ WiiSocket::ConnectingState WiiSocket::GetConnectingState() const
FD_SET(fd, &write_fds); FD_SET(fd, &write_fds);
FD_SET(fd, &except_fds); FD_SET(fd, &except_fds);
auto& sm = WiiSockMan::GetInstance();
if (select(nfds, &read_fds, &write_fds, &except_fds, &t) < 0) if (select(nfds, &read_fds, &write_fds, &except_fds, &t) < 0)
{ {
const s32 error = get_errno(); const s32 error = get_errno();
ERROR_LOG_FMT(IOS_SSL, "Failed to get socket (fd={}) connection state (err={}): {}", wii_fd, ERROR_LOG_FMT(IOS_SSL, "Failed to get socket (fd={}) connection state (err={}): {}", wii_fd,
error, sm.DecodeError(error)); error, Common::DecodeNetworkError(error));
return ConnectingState::Error; return ConnectingState::Error;
} }
@ -781,14 +763,14 @@ WiiSocket::ConnectingState WiiSocket::GetConnectingState() const
{ {
error = get_errno(); error = get_errno();
ERROR_LOG_FMT(IOS_SSL, "Failed to get socket (fd={}) error state (err={}): {}", wii_fd, error, ERROR_LOG_FMT(IOS_SSL, "Failed to get socket (fd={}) error state (err={}): {}", wii_fd, error,
sm.DecodeError(error)); Common::DecodeNetworkError(error));
return ConnectingState::Error; return ConnectingState::Error;
} }
if (error != 0) if (error != 0)
{ {
ERROR_LOG_FMT(IOS_SSL, "Non-blocking connect (fd={}) failed (err={}): {}", wii_fd, error, ERROR_LOG_FMT(IOS_SSL, "Non-blocking connect (fd={}) failed (err={}): {}", wii_fd, error,
sm.DecodeError(error)); Common::DecodeNetworkError(error));
return ConnectingState::Error; return ConnectingState::Error;
} }
@ -799,7 +781,7 @@ WiiSocket::ConnectingState WiiSocket::GetConnectingState() const
{ {
error = get_errno(); error = get_errno();
ERROR_LOG_FMT(IOS_SSL, "Non-blocking connect (fd={}) failed to get peername (err={}): {}", ERROR_LOG_FMT(IOS_SSL, "Non-blocking connect (fd={}) failed to get peername (err={}): {}",
wii_fd, error, sm.DecodeError(error)); wii_fd, error, Common::DecodeNetworkError(error));
return ConnectingState::Error; return ConnectingState::Error;
} }