From 951a0212d8ac12520eaa8236d4b6405d9ddd512c Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 18 Jul 2020 14:20:31 +0100 Subject: [PATCH] Move IsValidSocket out of global namespace --- src/OSSupport/ServerHandleImpl.cpp | 17 ++++++++++------- src/OSSupport/UDPEndpointImpl.cpp | 23 +++++++++++++---------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/OSSupport/ServerHandleImpl.cpp b/src/OSSupport/ServerHandleImpl.cpp index fb5e16216..1550e38f6 100644 --- a/src/OSSupport/ServerHandleImpl.cpp +++ b/src/OSSupport/ServerHandleImpl.cpp @@ -15,13 +15,16 @@ //////////////////////////////////////////////////////////////////////////////// // 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); - #else // _WIN32 +#else // _WIN32 return (a_Socket >= 0); - #endif // else _WIN32 +#endif // else _WIN32 + } } @@ -129,13 +132,13 @@ bool cServerHandleImpl::Listen(UInt16 a_Port) int err = 0; 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: err = EVUTIL_SOCKET_ERROR(); LOGD("Failed to create IPv6 MainSock: %d (%s)", err, evutil_socket_error_to_string(err)); MainSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (!IsValidSocket(MainSock)) + if (!ServerHandleImplHelper::IsValidSocket(MainSock)) { m_ErrorCode = EVUTIL_SOCKET_ERROR(); 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"); evutil_socket_t SecondSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (!IsValidSocket(SecondSock)) + if (!ServerHandleImplHelper::IsValidSocket(SecondSock)) { err = EVUTIL_SOCKET_ERROR(); LOGD("socket(AF_INET, ...) failed for secondary socket: %d, %s", err, evutil_socket_error_to_string(err)); diff --git a/src/OSSupport/UDPEndpointImpl.cpp b/src/OSSupport/UDPEndpointImpl.cpp index 5984bc4e2..3f04dd4f5 100644 --- a/src/OSSupport/UDPEndpointImpl.cpp +++ b/src/OSSupport/UDPEndpointImpl.cpp @@ -14,13 +14,16 @@ //////////////////////////////////////////////////////////////////////////////// // 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); - #else // _WIN32 +#else // _WIN32 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(&sa)->sin_port = htons(a_Port); if (m_IsMainSockIPv6) { - if (IsValidSocket(m_SecondarySock)) + if (UDPEndpointImplHelper::IsValidSocket(m_SecondarySock)) { // The secondary socket, which is always IPv4, is present: NumSent = static_cast(sendto(m_SecondarySock, a_Payload.data(), a_Payload.size(), 0, reinterpret_cast(&sa), static_cast(salen))); @@ -339,7 +342,7 @@ void cUDPEndpointImpl::EnableBroadcasts(void) } // 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) { @@ -351,7 +354,7 @@ void cUDPEndpointImpl::EnableBroadcasts(void) } // 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(&broadcastInt), sizeof(broadcastInt)) == -1) { @@ -379,14 +382,14 @@ void cUDPEndpointImpl::Open(UInt16 a_Port) m_MainSock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); int err; - if (!IsValidSocket(m_MainSock)) + if (!UDPEndpointImplHelper::IsValidSocket(m_MainSock)) { // Failed to create IPv6 socket, create an IPv4 one instead: m_IsMainSockIPv6 = false; err = EVUTIL_SOCKET_ERROR(); 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); - if (!IsValidSocket(m_MainSock)) + if (!UDPEndpointImplHelper::IsValidSocket(m_MainSock)) { 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))); @@ -492,7 +495,7 @@ void cUDPEndpointImpl::Open(UInt16 a_Port) LOGD("Creating a second UDP socket for IPv4"); 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 err = EVUTIL_SOCKET_ERROR();