From 7b41fa400370560b0e7978164648cbf4cc6e6165 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sun, 22 Jan 2012 16:32:07 +0000 Subject: Improve logging of socket errors (create, bind, accept, and poll) --- lib/server/SocketListen.h | 32 ++++++++++++++++++++++++-------- lib/server/SocketStream.cpp | 15 ++++++--------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/server/SocketListen.h b/lib/server/SocketListen.h index 170c56a1..39c60ba6 100644 --- a/lib/server/SocketListen.h +++ b/lib/server/SocketListen.h @@ -87,12 +87,16 @@ public: { Close(); } + private: SocketListen(const SocketListen &rToCopy) { } -public: + int mType, mPort; + std::string mName; + +public: enum { MaxMultipleListenSockets = MaxMultiListenSockets @@ -108,8 +112,8 @@ public: if(::close(mSocketHandle) == -1) #endif { - BOX_LOG_SYS_ERROR("Failed to close network " - "socket"); + BOX_LOG_SOCKET_ERROR(mType, mName, mPort, + "Failed to close network socket"); THROW_EXCEPTION(ServerException, SocketCloseError) } @@ -127,6 +131,10 @@ public: // ------------------------------------------------------------------ void Listen(Socket::Type Type, const char *Name, int Port = 0) { + mType = Type; + mName = Name; + mPort = Port; + if(mSocketHandle != -1) { THROW_EXCEPTION(ServerException, SocketAlreadyOpen); @@ -144,7 +152,8 @@ public: 0 /* let OS choose protocol */); if(mSocketHandle == -1) { - BOX_LOG_SYS_ERROR("Failed to create a network socket"); + BOX_LOG_SOCKET_ERROR(Type, Name, Port, + "Failed to create a network socket"); THROW_EXCEPTION(ServerException, SocketOpenError) } @@ -158,7 +167,8 @@ public: &option, sizeof(option)) == -1) #endif { - BOX_LOG_SYS_ERROR("Failed to set socket options"); + BOX_LOG_SOCKET_ERROR(Type, Name, Port, + "Failed to set socket options"); THROW_EXCEPTION(ServerException, SocketOpenError) } @@ -167,9 +177,14 @@ public: || ::listen(mSocketHandle, ListenBacklog) == -1) { int err_number = errno; + + BOX_LOG_SOCKET_ERROR(Type, Name, Port, + "Failed to bind socket"); + // Dispose of the socket ::close(mSocketHandle); mSocketHandle = -1; + THROW_SYS_FILE_ERRNO("Failed to bind or listen " "on socket", Name, err_number, ServerException, SocketBindError); @@ -233,8 +248,8 @@ public: } else { - BOX_LOG_SYS_ERROR("Failed to poll " - "connection"); + BOX_LOG_SOCKET_ERROR(mType, mName, mPort, + "Failed to poll connection"); THROW_EXCEPTION(ServerException, SocketPollError) } @@ -253,7 +268,8 @@ public: // Got socket (or error), unlock (implicit in destruction) if(sock == -1) { - BOX_LOG_SYS_ERROR("Failed to accept connection"); + BOX_LOG_SOCKET_ERROR(mType, mName, mPort, + "Failed to accept connection"); THROW_EXCEPTION(ServerException, SocketAcceptError) } diff --git a/lib/server/SocketStream.cpp b/lib/server/SocketStream.cpp index 95b4b4f4..6ef4b8d1 100644 --- a/lib/server/SocketStream.cpp +++ b/lib/server/SocketStream.cpp @@ -154,14 +154,16 @@ void SocketStream::Open(Socket::Type Type, const std::string& rName, int Port) int sockDomain = 0; SocketAllAddr addr; int addrLen = 0; - Socket::NameLookupToSockAddr(addr, sockDomain, Type, rName, Port, addrLen); + Socket::NameLookupToSockAddr(addr, sockDomain, Type, rName, Port, + addrLen); // Create the socket mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */); if(mSocketHandle == INVALID_SOCKET_VALUE) { - BOX_LOG_SYS_ERROR("Failed to create a network socket"); + BOX_LOG_SOCKET_ERROR(Type, rName, Port, + "Failed to create a network socket"); THROW_EXCEPTION(ServerException, SocketOpenError) } @@ -169,16 +171,11 @@ void SocketStream::Open(Socket::Type Type, const std::string& rName, int Port) if(::connect(mSocketHandle, &addr.sa_generic, addrLen) == -1) { // Dispose of the socket + BOX_LOG_SOCKET_ERROR(Type, rName, Port, + "Failed to connect to socket"); #ifdef WIN32 - DWORD err = WSAGetLastError(); ::closesocket(mSocketHandle); - BOX_LOG_WIN_ERROR_NUMBER("Failed to connect to socket " - "(type " << Type << ", name " << rName << - ", port " << Port << ")", err); #else // !WIN32 - BOX_LOG_SYS_ERROR("Failed to connect to socket (type " << - Type << ", name " << rName << ", port " << Port << - ")"); ::close(mSocketHandle); #endif // WIN32 -- cgit v1.2.3