1
0

Extracted cSocket::GetErrorString into GetOSErrorString

This commit is contained in:
Tycho 2014-01-25 05:51:03 -08:00
parent 7419579140
commit 59b8205f02
6 changed files with 62 additions and 59 deletions

View File

@ -2,7 +2,7 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "BlockingTCPLink.h" #include "BlockingTCPLink.h"
#include "Errors.h"
@ -75,7 +75,7 @@ bool cBlockingTCPLink::Connect(const char * iAddress, unsigned int iPort)
server.sin_port = htons( (unsigned short)iPort); server.sin_port = htons( (unsigned short)iPort);
if (connect(m_Socket, (struct sockaddr *)&server, sizeof(server))) if (connect(m_Socket, (struct sockaddr *)&server, sizeof(server)))
{ {
LOGWARN("cTCPLink: Connection to \"%s:%d\" failed (%s)", iAddress, iPort, cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); LOGWARN("cTCPLink: Connection to \"%s:%d\" failed (%s)", iAddress, iPort,GetOSErrorString( cSocket::GetLastError() ).c_str() );
CloseSocket(); CloseSocket();
return false; return false;
} }

52
src/OSSupport/Errors.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "Globals.h"
#include "Errors.h"
AString GetOSErrorString( int a_ErrNo )
{
char buffer[ 1024 ];
AString Out;
#ifdef _WIN32
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_ErrNo, 0, buffer, ARRAYCOUNT(buffer), NULL);
Printf(Out, "%d: %s", a_ErrNo, buffer);
if (!Out.empty() && (Out[Out.length() - 1] == '\n'))
{
Out.erase(Out.length() - 2);
}
return Out;
#else // _WIN32
// According to http://linux.die.net/man/3/strerror_r there are two versions of strerror_r():
#if ( _GNU_SOURCE ) && !defined(ANDROID_NDK) // GNU version of strerror_r()
char * res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) );
if( res != NULL )
{
Printf(Out, "%d: %s", a_ErrNo, res);
return Out;
}
#else // XSI version of strerror_r():
int res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) );
if( res == 0 )
{
Printf(Out, "%d: %s", a_ErrNo, buffer);
return Out;
}
#endif // strerror_r() version
else
{
Printf(Out, "Error %d while getting error string for error #%d!", errno, a_ErrNo);
return Out;
}
#endif // else _WIN32
}

3
src/OSSupport/Errors.h Normal file
View File

@ -0,0 +1,3 @@
AString GetOSErrorString(int a_ErrNo);

View File

@ -105,58 +105,6 @@ void cSocket::ShutdownReadWrite(void)
AString cSocket::GetErrorString( int a_ErrNo )
{
char buffer[ 1024 ];
AString Out;
#ifdef _WIN32
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_ErrNo, 0, buffer, ARRAYCOUNT(buffer), NULL);
Printf(Out, "%d: %s", a_ErrNo, buffer);
if (!Out.empty() && (Out[Out.length() - 1] == '\n'))
{
Out.erase(Out.length() - 2);
}
return Out;
#else // _WIN32
// According to http://linux.die.net/man/3/strerror_r there are two versions of strerror_r():
#if ( _GNU_SOURCE ) && !defined(ANDROID_NDK) // GNU version of strerror_r()
char * res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) );
if( res != NULL )
{
Printf(Out, "%d: %s", a_ErrNo, res);
return Out;
}
#else // XSI version of strerror_r():
int res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) );
if( res == 0 )
{
Printf(Out, "%d: %s", a_ErrNo, buffer);
return Out;
}
#endif // strerror_r() version
else
{
Printf(Out, "Error %d while getting error string for error #%d!", errno, a_ErrNo);
return Out;
}
#endif // else _WIN32
}
int cSocket::GetLastError() int cSocket::GetLastError()
{ {
#ifdef _WIN32 #ifdef _WIN32

View File

@ -14,7 +14,7 @@
#endif #endif
#include "Errors.h"
class cSocket class cSocket
@ -57,11 +57,10 @@ public:
/// Initializes the network stack. Returns 0 on success, or another number as an error code. /// Initializes the network stack. Returns 0 on success, or another number as an error code.
static int WSAStartup(void); static int WSAStartup(void);
static AString GetErrorString(int a_ErrNo);
static int GetLastError(); static int GetLastError();
static AString GetLastErrorString(void) static AString GetLastErrorString(void)
{ {
return GetErrorString(GetLastError()); return GetOSErrorString(GetLastError());
} }
/// Creates a new socket of the specified address family /// Creates a new socket of the specified address family

View File

@ -7,6 +7,7 @@
#include "Globals.h" #include "Globals.h"
#include "SocketThreads.h" #include "SocketThreads.h"
#include "Errors.h"
@ -556,7 +557,7 @@ void cSocketThreads::cSocketThread::WriteToSockets(fd_set * a_Write)
if (Sent < 0) if (Sent < 0)
{ {
int Err = cSocket::GetLastError(); int Err = cSocket::GetLastError();
LOGWARNING("Error %d while writing to client \"%s\", disconnecting. \"%s\"", Err, m_Slots[i].m_Socket.GetIPString().c_str(), cSocket::GetErrorString(Err).c_str()); LOGWARNING("Error %d while writing to client \"%s\", disconnecting. \"%s\"", Err, m_Slots[i].m_Socket.GetIPString().c_str(), GetOSErrorString(Err).c_str());
m_Slots[i].m_Socket.CloseSocket(); m_Slots[i].m_Socket.CloseSocket();
if (m_Slots[i].m_Client != NULL) if (m_Slots[i].m_Client != NULL)
{ {