diff --git a/Source/Core/Common/Network.cpp b/Source/Core/Common/Network.cpp index 0ed2360267..f79b4e6d84 100644 --- a/Source/Core/Common/Network.cpp +++ b/Source/Core/Common/Network.cpp @@ -9,8 +9,9 @@ #include #ifndef _WIN32 +#include #include -#include +#include #else #include #endif @@ -19,6 +20,7 @@ #include "Common/BitUtils.h" #include "Common/CommonFuncs.h" +#include "Common/Logging/Log.h" #include "Common/Random.h" #include "Common/StringUtil.h" @@ -586,4 +588,18 @@ const char* StrNetworkError() #endif return DecodeNetworkError(error_code); } + +bool SetPlatformSocketOptions(int fd [[maybe_unused]]) +{ +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) + int opt_no_sigpipe = 1; + if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &opt_no_sigpipe, sizeof(opt_no_sigpipe)) < 0) + { + ERROR_LOG_FMT(COMMON, "Failed to set SO_NOSIGPIPE on socket: {}", StrNetworkError()); + return false; + } +#endif + return true; +} + } // namespace Common diff --git a/Source/Core/Common/Network.h b/Source/Core/Common/Network.h index 8f446d7a38..4ad18d3368 100644 --- a/Source/Core/Common/Network.h +++ b/Source/Core/Common/Network.h @@ -10,6 +10,10 @@ #include #include +#if defined(__linux__) || defined(__HAIKU__) +#include +#endif + #include "Common/CommonTypes.h" struct sockaddr_in; @@ -279,4 +283,15 @@ NetworkErrorState SaveNetworkErrorState(); void RestoreNetworkErrorState(const NetworkErrorState& state); const char* DecodeNetworkError(s32 error_code); const char* StrNetworkError(); + +// Sets SO_NOSIGPIPE when available. +bool SetPlatformSocketOptions(int fd); + +// Pass this to all `send` calls to avoid SIGPIPE. +#if defined(__linux__) || defined(__HAIKU__) +static constexpr int SEND_FLAGS = MSG_NOSIGNAL; +#else +static constexpr int SEND_FLAGS = 0; +#endif + } // namespace Common diff --git a/Source/Core/Core/HW/EXI/BBA/TAPServerConnection.cpp b/Source/Core/Core/HW/EXI/BBA/TAPServerConnection.cpp index f2bd89e6b2..570221c72b 100644 --- a/Source/Core/Core/HW/EXI/BBA/TAPServerConnection.cpp +++ b/Source/Core/Core/HW/EXI/BBA/TAPServerConnection.cpp @@ -31,11 +31,7 @@ using ws_ssize_t = int; using ws_ssize_t = ssize_t; #endif -#ifdef __linux__ -#define SEND_FLAGS MSG_NOSIGNAL -#else -#define SEND_FLAGS 0 -#endif +using Common::SEND_FLAGS; TAPServerConnection::TAPServerConnection(const std::string& destination, std::function recv_cb, @@ -114,11 +110,7 @@ static int ConnectToDestination(const std::string& destination) return -1; } -#ifdef __APPLE__ - int opt_no_sigpipe = 1; - if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &opt_no_sigpipe, sizeof(opt_no_sigpipe)) < 0) - INFO_LOG_FMT(SP1, "Failed to set SO_NOSIGPIPE on socket\n"); -#endif + Common::SetPlatformSocketOptions(fd); if (connect(fd, reinterpret_cast(&ss), ss_size) == -1) { diff --git a/Source/Core/Core/IOS/Network/Socket.cpp b/Source/Core/Core/IOS/Network/Socket.cpp index b2f72945e3..6baf371204 100644 --- a/Source/Core/Core/IOS/Network/Socket.cpp +++ b/Source/Core/Core/IOS/Network/Socket.cpp @@ -893,11 +893,7 @@ s32 WiiSockMan::AddSocket(s32 fd, bool is_rw) sock.SetWiiFd(wii_fd); m_ios.GetSystem().GetPowerPC().GetDebugInterface().NetworkLogger()->OnNewSocket(fd); -#ifdef __APPLE__ - int opt_no_sigpipe = 1; - if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &opt_no_sigpipe, sizeof(opt_no_sigpipe)) < 0) - ERROR_LOG_FMT(IOS_NET, "Failed to set SO_NOSIGPIPE on socket"); -#endif + Common::SetPlatformSocketOptions(fd); // Wii UDP sockets can use broadcast address by default if (!is_rw)