From ab95abb6bde0073ebb869624332adf028aac8dd7 Mon Sep 17 00:00:00 2001 From: faketruth Date: Sun, 5 Feb 2012 16:24:16 +0000 Subject: Now showing proper error messages when sockets fail :) Crossplatform GetLastError function in cSocket git-svn-id: http://mc-server.googlecode.com/svn/trunk@234 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cBlockingTCPLink.cpp | 2 +- source/cClientHandle.cpp | 2 +- source/cServer.cpp | 6 +++--- source/cSocket.cpp | 35 +++++++++++++++++++++++------------ source/cSocket.h | 3 ++- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/source/cBlockingTCPLink.cpp b/source/cBlockingTCPLink.cpp index 1145e0e56..5a36536bc 100644 --- a/source/cBlockingTCPLink.cpp +++ b/source/cBlockingTCPLink.cpp @@ -84,7 +84,7 @@ bool cBlockingTCPLink::Connect(const char * iAddress, unsigned int iPort) server.sin_port = htons( (unsigned short)iPort); if (connect(m_Socket, (struct sockaddr *)&server, sizeof(server))) { - LOGWARN("cTCPLink: Connection to \"%s:%d\" failed (%s)", iAddress, iPort, m_Socket.GetLastErrorString()); + LOGWARN("cTCPLink: Connection to \"%s:%d\" failed (%s)", iAddress, iPort, cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); CloseSocket(); return false; } diff --git a/source/cClientHandle.cpp b/source/cClientHandle.cpp index 5ecac4516..78fac80ae 100644 --- a/source/cClientHandle.cpp +++ b/source/cClientHandle.cpp @@ -1832,7 +1832,7 @@ void cClientHandle::ReceiveThread(void *lpParam) } else { - LOGERROR("Something went wrong during PacketID 0x%02x (%s)", temp, cSocket::GetLastErrorString()); + LOGERROR("Something went wrong during PacketID 0x%02x (%s)", temp, cSocket::GetErrorString( cSocket::GetLastError() ).c_str()); LOG("CLIENT %s DISCONNECTED", self->m_Username.c_str()); break; } diff --git a/source/cServer.cpp b/source/cServer.cpp index f14a419a9..36fb71875 100644 --- a/source/cServer.cpp +++ b/source/cServer.cpp @@ -179,7 +179,7 @@ bool cServer::InitServer( int a_Port ) if( !m_pState->SListenClient.IsValid() ) { - LOGERROR("m_SListenClient==INVALID_SOCKET (%s)", cSocket::GetLastErrorString() ); + LOGERROR("m_SListenClient==INVALID_SOCKET (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); return false; } @@ -196,13 +196,13 @@ bool cServer::InitServer( int a_Port ) if( m_pState->SListenClient.Bind( local ) != 0 ) { - LOGERROR("bind fail (%s)", cSocket::GetLastErrorString() ); + LOGERROR("bind fail (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); return false; } if( m_pState->SListenClient.Listen( 10 ) != 0) { - LOGERROR("listen fail (%s)", cSocket::GetLastErrorString() ); + LOGERROR("listen fail (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); return false; } diff --git a/source/cSocket.cpp b/source/cSocket.cpp index 10ccec3c3..f8b0bed20 100644 --- a/source/cSocket.cpp +++ b/source/cSocket.cpp @@ -92,7 +92,7 @@ void cSocket::CloseSocket() -const char * cSocket::GetLastErrorString() +AString cSocket::GetErrorString( int a_ErrNo ) { #define CASE_AND_RETURN(x) case x: return #x @@ -118,7 +118,27 @@ const char * cSocket::GetLastErrorString() } return "No Error"; #else - return "GetLastErrorString() only works on Windows"; + char buffer[ 256 ]; + if( strerror_r( errno, buffer, 256 ) == 0 ) + { + return AString( buffer ); + } + else + { + return "Error on getting error string!"; + } +#endif +} + + + + +int cSocket::GetLastError() +{ +#ifdef _WIN32 + return WSAGetLastError(); +#else + return errno; #endif } @@ -176,16 +196,7 @@ int cSocket::Bind(SockAddr_In& a_Address) local.sin_port=htons((u_short)a_Address.Port); - int res = bind(m_Socket, (sockaddr*)&local, sizeof(local)); - if (res != 0) - { - #ifdef _WIN32 - LOGWARNING("bind() failed for port %d, WSAGLE = %d", a_Address.Port, WSAGetLastError()); - #else // _WIN32 - LOGWARNING("bind() failed for port %d, errno = %d", a_Address.Port, errno); - #endif // else _WIN32 - } - return res; + return bind(m_Socket, (sockaddr*)&local, sizeof(local)); } diff --git a/source/cSocket.h b/source/cSocket.h index 5f373edac..98fc5fdc4 100644 --- a/source/cSocket.h +++ b/source/cSocket.h @@ -28,7 +28,8 @@ public: int SetReuseAddress(); static int WSAStartup(); - static const char* GetLastErrorString(); + static AString GetErrorString( int a_ErrNo ); + static int GetLastError(); static cSocket CreateSocket(); inline static bool IsSocketError( int a_ReturnedValue ) -- cgit v1.2.3