1
0
Fork 0

Move IsValidSocket out of global namespace

This commit is contained in:
Tiger Wang 2020-07-18 14:20:31 +01:00
parent eb0f640fa0
commit 951a0212d8
2 changed files with 23 additions and 17 deletions

View File

@ -15,13 +15,16 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Globals: // Globals:
static bool IsValidSocket(evutil_socket_t a_Socket) namespace ServerHandleImplHelper
{ {
#ifdef _WIN32 static bool IsValidSocket(evutil_socket_t a_Socket)
{
#ifdef _WIN32
return (a_Socket != INVALID_SOCKET); return (a_Socket != INVALID_SOCKET);
#else // _WIN32 #else // _WIN32
return (a_Socket >= 0); return (a_Socket >= 0);
#endif // else _WIN32 #endif // else _WIN32
}
} }
@ -129,13 +132,13 @@ bool cServerHandleImpl::Listen(UInt16 a_Port)
int err = 0; int err = 0;
evutil_socket_t MainSock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); evutil_socket_t MainSock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (!IsValidSocket(MainSock)) if (!ServerHandleImplHelper::IsValidSocket(MainSock))
{ {
// Failed to create IPv6 socket, create an IPv4 one instead: // Failed to create IPv6 socket, create an IPv4 one instead:
err = EVUTIL_SOCKET_ERROR(); err = EVUTIL_SOCKET_ERROR();
LOGD("Failed to create IPv6 MainSock: %d (%s)", err, evutil_socket_error_to_string(err)); LOGD("Failed to create IPv6 MainSock: %d (%s)", err, evutil_socket_error_to_string(err));
MainSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); MainSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (!IsValidSocket(MainSock)) if (!ServerHandleImplHelper::IsValidSocket(MainSock))
{ {
m_ErrorCode = EVUTIL_SOCKET_ERROR(); m_ErrorCode = EVUTIL_SOCKET_ERROR();
Printf(m_ErrorMsg, "Cannot create socket for port %d: %s", a_Port, evutil_socket_error_to_string(m_ErrorCode)); Printf(m_ErrorMsg, "Cannot create socket for port %d: %s", a_Port, evutil_socket_error_to_string(m_ErrorCode));
@ -227,7 +230,7 @@ bool cServerHandleImpl::Listen(UInt16 a_Port)
LOGD("Creating a second socket for IPv4"); LOGD("Creating a second socket for IPv4");
evutil_socket_t SecondSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); evutil_socket_t SecondSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (!IsValidSocket(SecondSock)) if (!ServerHandleImplHelper::IsValidSocket(SecondSock))
{ {
err = EVUTIL_SOCKET_ERROR(); err = EVUTIL_SOCKET_ERROR();
LOGD("socket(AF_INET, ...) failed for secondary socket: %d, %s", err, evutil_socket_error_to_string(err)); LOGD("socket(AF_INET, ...) failed for secondary socket: %d, %s", err, evutil_socket_error_to_string(err));

View File

@ -14,13 +14,16 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Globals: // Globals:
static bool IsValidSocket(evutil_socket_t a_Socket) namespace UDPEndpointImplHelper
{ {
#ifdef _WIN32 static bool IsValidSocket(evutil_socket_t a_Socket)
{
#ifdef _WIN32
return (a_Socket != INVALID_SOCKET); return (a_Socket != INVALID_SOCKET);
#else // _WIN32 #else // _WIN32
return (a_Socket >= 0); return (a_Socket >= 0);
#endif // else _WIN32 #endif // else _WIN32
}
} }
@ -281,7 +284,7 @@ bool cUDPEndpointImpl::Send(const AString & a_Payload, const AString & a_Host, U
reinterpret_cast<sockaddr_in *>(&sa)->sin_port = htons(a_Port); reinterpret_cast<sockaddr_in *>(&sa)->sin_port = htons(a_Port);
if (m_IsMainSockIPv6) if (m_IsMainSockIPv6)
{ {
if (IsValidSocket(m_SecondarySock)) if (UDPEndpointImplHelper::IsValidSocket(m_SecondarySock))
{ {
// The secondary socket, which is always IPv4, is present: // The secondary socket, which is always IPv4, is present:
NumSent = static_cast<int>(sendto(m_SecondarySock, a_Payload.data(), a_Payload.size(), 0, reinterpret_cast<const sockaddr *>(&sa), static_cast<socklen_t>(salen))); NumSent = static_cast<int>(sendto(m_SecondarySock, a_Payload.data(), a_Payload.size(), 0, reinterpret_cast<const sockaddr *>(&sa), static_cast<socklen_t>(salen)));
@ -339,7 +342,7 @@ void cUDPEndpointImpl::EnableBroadcasts(void)
} }
// Enable broadcasts on the secondary socket, if opened (use char, it worked for primary): // Enable broadcasts on the secondary socket, if opened (use char, it worked for primary):
if (IsValidSocket(m_SecondarySock)) if (UDPEndpointImplHelper::IsValidSocket(m_SecondarySock))
{ {
if (setsockopt(m_SecondarySock, SOL_SOCKET, SO_BROADCAST, &broadcastChar, sizeof(broadcastChar)) == -1) if (setsockopt(m_SecondarySock, SOL_SOCKET, SO_BROADCAST, &broadcastChar, sizeof(broadcastChar)) == -1)
{ {
@ -351,7 +354,7 @@ void cUDPEndpointImpl::EnableBroadcasts(void)
} }
// Enable broadcasts on the secondary socket, if opened (use int, it worked for primary): // Enable broadcasts on the secondary socket, if opened (use int, it worked for primary):
if (IsValidSocket(m_SecondarySock)) if (UDPEndpointImplHelper::IsValidSocket(m_SecondarySock))
{ {
if (setsockopt(m_SecondarySock, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<const char *>(&broadcastInt), sizeof(broadcastInt)) == -1) if (setsockopt(m_SecondarySock, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<const char *>(&broadcastInt), sizeof(broadcastInt)) == -1)
{ {
@ -379,14 +382,14 @@ void cUDPEndpointImpl::Open(UInt16 a_Port)
m_MainSock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); m_MainSock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
int err; int err;
if (!IsValidSocket(m_MainSock)) if (!UDPEndpointImplHelper::IsValidSocket(m_MainSock))
{ {
// Failed to create IPv6 socket, create an IPv4 one instead: // Failed to create IPv6 socket, create an IPv4 one instead:
m_IsMainSockIPv6 = false; m_IsMainSockIPv6 = false;
err = EVUTIL_SOCKET_ERROR(); err = EVUTIL_SOCKET_ERROR();
LOGD("UDP: Failed to create IPv6 MainSock: %d (%s)", err, evutil_socket_error_to_string(err)); LOGD("UDP: Failed to create IPv6 MainSock: %d (%s)", err, evutil_socket_error_to_string(err));
m_MainSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); m_MainSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (!IsValidSocket(m_MainSock)) if (!UDPEndpointImplHelper::IsValidSocket(m_MainSock))
{ {
err = EVUTIL_SOCKET_ERROR(); err = EVUTIL_SOCKET_ERROR();
m_Callbacks.OnError(err, Printf("Cannot create UDP socket for port %d: %s", a_Port, evutil_socket_error_to_string(err))); m_Callbacks.OnError(err, Printf("Cannot create UDP socket for port %d: %s", a_Port, evutil_socket_error_to_string(err)));
@ -492,7 +495,7 @@ void cUDPEndpointImpl::Open(UInt16 a_Port)
LOGD("Creating a second UDP socket for IPv4"); LOGD("Creating a second UDP socket for IPv4");
m_SecondarySock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); m_SecondarySock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (!IsValidSocket(m_SecondarySock)) if (!UDPEndpointImplHelper::IsValidSocket(m_SecondarySock))
{ {
// Don't report as an error, the primary socket is working // Don't report as an error, the primary socket is working
err = EVUTIL_SOCKET_ERROR(); err = EVUTIL_SOCKET_ERROR();