Skip to content

Commit

Permalink
cNetwork: Fixed Linux compilation.
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxoft committed Jan 22, 2015
1 parent a2aa37b commit 9ffca12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions src/OSSupport/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@ class cNetworkSingleton



////////////////////////////////////////////////////////////////////////////////
// Globals:

bool IsValidSocket(evutil_socket_t a_Socket)
{
#ifdef _WIN32
return (a_Socket != INVALID_SOCKET);
#else // _WIN32
return (a_Socket >= 0);
#endif // else _WIN32
}





////////////////////////////////////////////////////////////////////////////////
// cHostnameLookup:

Expand Down Expand Up @@ -683,7 +699,7 @@ void cTCPLinkImpl::UpdateAddress(const sockaddr * a_Address, int a_AddrLen, AStr
void cTCPLinkImpl::UpdateLocalAddress(void)
{
sockaddr_storage sa;
int salen = static_cast<int>(sizeof(sa));
socklen_t salen = static_cast<socklen_t>(sizeof(sa));
getsockname(bufferevent_getfd(m_BufferEvent), reinterpret_cast<sockaddr *>(&sa), &salen);
UpdateAddress(reinterpret_cast<const sockaddr *>(&sa), salen, m_LocalIP, m_LocalPort);
}
Expand All @@ -695,7 +711,7 @@ void cTCPLinkImpl::UpdateLocalAddress(void)
void cTCPLinkImpl::UpdateRemoteAddress(void)
{
sockaddr_storage sa;
int salen = static_cast<int>(sizeof(sa));
socklen_t salen = static_cast<socklen_t>(sizeof(sa));
getpeername(bufferevent_getfd(m_BufferEvent), reinterpret_cast<sockaddr *>(&sa), &salen);
UpdateAddress(reinterpret_cast<const sockaddr *>(&sa), salen, m_LocalIP, m_LocalPort);
}
Expand Down Expand Up @@ -753,12 +769,14 @@ bool cServerHandleImpl::Listen(UInt16 a_Port)
// It should listen on IPv6 with IPv4 fallback, when available; IPv4 when IPv6 is not available.
bool NeedsTwoSockets = false;
evutil_socket_t MainSock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (MainSock == SOCKET_ERROR)
if (!IsValidSocket(MainSock))
{
// Failed to create IPv6 socket, create an IPv4 one instead:
MainSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (MainSock == SOCKET_ERROR)
if (!IsValidSocket(MainSock))
{
int err = EVUTIL_SOCKET_ERROR();
LOGWARNING("%s: Cannot create a socket for neither IPv6 nor IPv4: %d (%s)", __FUNCTION__, err, evutil_socket_error_to_string(err));
return false;
}

Expand Down Expand Up @@ -822,7 +840,7 @@ bool cServerHandleImpl::Listen(UInt16 a_Port)
if (NeedsTwoSockets)
{
evutil_socket_t SecondSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (SecondSock != SOCKET_ERROR)
if (!IsValidSocket(SecondSock))
{
evutil_make_socket_nonblocking(SecondSock);
m_SecondaryConnListener = evconnlistener_new(cNetworkSingleton::Get().m_EventBase, Callback, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 0, SecondSock);
Expand Down
2 changes: 1 addition & 1 deletion tests/Network/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class cEchoServerCallbacks:
{
virtual void OnAccepted(cTCPLink & a_Link) override
{
LOGD("New client accepted (%s:%p), sending welcome message.", a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort());
LOGD("New client accepted (%s:%d), sending welcome message.", a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort());
// Send a welcome message to each connecting client:
a_Link.Send("Welcome to the simple echo server.\r\n");
LOGD("Welcome message queued.");
Expand Down

0 comments on commit 9ffca12

Please sign in to comment.