From 8e7787e31512e32e7c89c56ca713f376ee8e2faa Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 4 Apr 2012 16:02:19 +0200 Subject: [PATCH] Improved socket error handling. --- base/socket.cpp | 2 +- base/socket.h | 1 + base/tcpclient.cpp | 39 +++++++++++++++++++++++++++++++++++++-- base/tcpsocket.cpp | 25 ++++++++++++++++++++++--- 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/base/socket.cpp b/base/socket.cpp index aba6d054c..42f603aa8 100644 --- a/base/socket.cpp +++ b/base/socket.cpp @@ -99,7 +99,7 @@ int Socket::ExceptionEventHandler(EventArgs::Ptr ea) if (opt != 0) { SocketErrorEventArgs::Ptr ea = make_shared(); ea->Code = opt; - ea->Message = FormatErrorCode(opt); + ea->Message = FormatErrorCode(ea->Code); OnError(ea); Close(); diff --git a/base/socket.h b/base/socket.h index 1583621ea..ca6179b43 100644 --- a/base/socket.h +++ b/base/socket.h @@ -19,6 +19,7 @@ private: int ExceptionEventHandler(EventArgs::Ptr ea); +protected: string FormatErrorCode(int errorCode); protected: diff --git a/base/tcpclient.cpp b/base/tcpclient.cpp index 19fb82095..9dcd4d2e7 100644 --- a/base/tcpclient.cpp +++ b/base/tcpclient.cpp @@ -37,11 +37,22 @@ void TCPClient::Connect(const string& hostname, unsigned short port) int rc = connect(GetFD(), (sockaddr *)&sin, sizeof(sin)); #ifdef _WIN32 - if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK) + if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK) { #else /* _WIN32 */ - if (rc < 0 && errno != EINPROGRESS) + if (rc < 0 && errno != EINPROGRESS) { #endif /* _WIN32 */ + SocketErrorEventArgs::Ptr ea = make_shared(); +#ifdef _WIN32 + ea->Code = WSAGetLastError(); +#else /* _WIN32 */ + ea->Code = errno; +#endif /* _WIN32 */ + ea->Message = FormatErrorCode(ea->Code); + + OnError(ea); + Close(); + } m_PeerHost = hostname; m_PeerPort = port; @@ -84,6 +95,18 @@ int TCPClient::ReadableEventHandler(EventArgs::Ptr ea) return 0; if (rc <= 0) { + if (rc < 0) { + SocketErrorEventArgs::Ptr ea = make_shared(); +#ifdef _WIN32 + ea->Code = WSAGetLastError(); +#else /* _WIN32 */ + ea->Code = errno; +#endif /* _WIN32 */ + ea->Message = FormatErrorCode(ea->Code); + + OnError(ea); + } + Close(); return 0; } @@ -104,6 +127,18 @@ int TCPClient::WritableEventHandler(EventArgs::Ptr ea) rc = send(GetFD(), (const char *)m_SendQueue->GetReadBuffer(), m_SendQueue->GetSize(), 0); if (rc <= 0) { + if (rc < 0) { + SocketErrorEventArgs::Ptr ea = make_shared(); +#ifdef _WIN32 + ea->Code = WSAGetLastError(); +#else /* _WIN32 */ + ea->Code = errno; +#endif /* _WIN32 */ + ea->Message = FormatErrorCode(ea->Code); + + OnError(ea); + } + Close(); return 0; } diff --git a/base/tcpsocket.cpp b/base/tcpsocket.cpp index cfc373954..009d95194 100644 --- a/base/tcpsocket.cpp +++ b/base/tcpsocket.cpp @@ -8,8 +8,16 @@ void TCPSocket::MakeSocket(void) int fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd == INVALID_SOCKET) - throw exception(/*"socket() failed."*/); + if (fd == INVALID_SOCKET) { + SocketErrorEventArgs::Ptr ea = make_shared(); +#ifdef _WIN32 + ea->Code = WSAGetLastError(); +#else /* _WIN32 */ + ea->Code = errno; +#endif /* _WIN32 */ + ea->Message = FormatErrorCode(ea->Code); + OnError(ea); + } SetFD(fd); } @@ -30,6 +38,17 @@ void TCPSocket::Bind(const char *hostname, unsigned short port) int rc = ::bind(GetFD(), (sockaddr *)&sin, sizeof(sin)); - if (rc < 0) + if (rc < 0) { + SocketErrorEventArgs::Ptr ea = make_shared(); +#ifdef _WIN32 + ea->Code = WSAGetLastError(); +#else /* _WIN32 */ + ea->Code = errno; +#endif /* _WIN32 */ + ea->Message = FormatErrorCode(ea->Code); + + OnError(ea); + Close(); + } }