Common: Add SetPlatformSocketOptions function and SEND_FLAGS constant to Network.h to deal with OS-specific SIGPIPE behavior.
This commit is contained in:
1 parent
2ffa7cec07
commit
5d2e93fa3e
4 files changed
+35
-16
No files matched your search
@@ -9,8 +9,9 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
#include <cstring>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <string.h>
|
#include <sys/socket.h>
|
||||||
#else
|
#else
|
||||||
#include <WinSock2.h>
|
#include <WinSock2.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -19,6 +20,7 @@
|
|||||||
|
|
||||||
#include "Common/BitUtils.h"
|
#include "Common/BitUtils.h"
|
||||||
#include "Common/CommonFuncs.h"
|
#include "Common/CommonFuncs.h"
|
||||||
|
#include "Common/Logging/Log.h"
|
||||||
#include "Common/Random.h"
|
#include "Common/Random.h"
|
||||||
#include "Common/StringUtil.h"
|
#include "Common/StringUtil.h"
|
||||||
|
|
||||||
@@ -586,4 +588,18 @@ const char* StrNetworkError()
|
|||||||
#endif
|
#endif
|
||||||
return DecodeNetworkError(error_code);
|
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
|
} // namespace Common
|
||||||
@@ -10,6 +10,10 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#if defined(__linux__) || defined(__HAIKU__)
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "Common/CommonTypes.h"
|
#include "Common/CommonTypes.h"
|
||||||
|
|
||||||
struct sockaddr_in;
|
struct sockaddr_in;
|
||||||
@@ -279,4 +283,15 @@ NetworkErrorState SaveNetworkErrorState();
|
|||||||
void RestoreNetworkErrorState(const NetworkErrorState& state);
|
void RestoreNetworkErrorState(const NetworkErrorState& state);
|
||||||
const char* DecodeNetworkError(s32 error_code);
|
const char* DecodeNetworkError(s32 error_code);
|
||||||
const char* StrNetworkError();
|
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
|
} // namespace Common
|
||||||
@@ -31,11 +31,7 @@ using ws_ssize_t = int;
|
|||||||
using ws_ssize_t = ssize_t;
|
using ws_ssize_t = ssize_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __linux__
|
using Common::SEND_FLAGS;
|
||||||
#define SEND_FLAGS MSG_NOSIGNAL
|
|
||||||
#else
|
|
||||||
#define SEND_FLAGS 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
TAPServerConnection::TAPServerConnection(const std::string& destination,
|
TAPServerConnection::TAPServerConnection(const std::string& destination,
|
||||||
std::function<void(std::string&&)> recv_cb,
|
std::function<void(std::string&&)> recv_cb,
|
||||||
@@ -114,11 +110,7 @@ static int ConnectToDestination(const std::string& destination)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __APPLE__
|
Common::SetPlatformSocketOptions(fd);
|
||||||
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
|
|
||||||
|
|
||||||
if (connect(fd, reinterpret_cast<sockaddr*>(&ss), ss_size) == -1)
|
if (connect(fd, reinterpret_cast<sockaddr*>(&ss), ss_size) == -1)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -893,11 +893,7 @@ s32 WiiSockMan::AddSocket(s32 fd, bool is_rw)
|
|||||||
sock.SetWiiFd(wii_fd);
|
sock.SetWiiFd(wii_fd);
|
||||||
m_ios.GetSystem().GetPowerPC().GetDebugInterface().NetworkLogger()->OnNewSocket(fd);
|
m_ios.GetSystem().GetPowerPC().GetDebugInterface().NetworkLogger()->OnNewSocket(fd);
|
||||||
|
|
||||||
#ifdef __APPLE__
|
Common::SetPlatformSocketOptions(fd);
|
||||||
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
|
|
||||||
|
|
||||||
// Wii UDP sockets can use broadcast address by default
|
// Wii UDP sockets can use broadcast address by default
|
||||||
if (!is_rw)
|
if (!is_rw)
|
||||||
|
|||||||
Reference in new issue
Block a user