From cb0ea38d74fe3228d7ddffc04996c82126ecc4f7 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 11 Mar 2010 17:45:39 +0000 Subject: [PATCH] Bugfix for connecting to invalid IP Address will now properly detect a failed connection attempt. --- .../include/platform/posix/socket.h | 1 + .../sources/platform/posix/socket.cpp | 37 ++++++++++++++----- .../sources/platform/win32/socket.cpp | 5 ++- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/source/shared_lib/include/platform/posix/socket.h b/source/shared_lib/include/platform/posix/socket.h index 9fbdecd00..afa735f63 100644 --- a/source/shared_lib/include/platform/posix/socket.h +++ b/source/shared_lib/include/platform/posix/socket.h @@ -52,6 +52,7 @@ public: class Socket { protected: int sock; + long lastDebugEvent; public: Socket(int sock); diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index 8191ad6a9..5a455392c 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -402,10 +402,15 @@ bool Socket::isReadable() int i= select(sock+1, &set, NULL, NULL, &tv); if(i < 0) { - //throwException("Error selecting socket"); - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] error while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); - printf("%s",szBuf); + if(difftime(time(NULL),lastDebugEvent) >= 1) + { + lastDebugEvent = time(NULL); + + //throwException("Error selecting socket"); + char szBuf[1024]=""; + sprintf(szBuf,"[%s::%s] error while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); + printf("%s",szBuf); + } } //return (i == 1 && FD_ISSET(sock, &set)); return (i == 1); @@ -429,18 +434,27 @@ bool Socket::isWritable(bool waitOnDelayedResponse) int i = select(sock+1, NULL, &set, NULL, &tv); if(i < 0 ) { - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] error while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); - printf("%s",szBuf); + if(difftime(time(NULL),lastDebugEvent) >= 1) + { + lastDebugEvent = time(NULL); + + char szBuf[1024]=""; + sprintf(szBuf,"[%s::%s] error while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); + printf("%s",szBuf); + } waitOnDelayedResponse = false; //throwException("Error selecting socket"); } else if(i == 0) { - char szBuf[1024]=""; - sprintf(szBuf,"[%s::%s] TIMEOUT while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); - printf("%s",szBuf); + if(difftime(time(NULL),lastDebugEvent) >= 1) + { + lastDebugEvent = time(NULL); + char szBuf[1024]=""; + sprintf(szBuf,"[%s::%s] TIMEOUT while selecting socket data, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,i,errno,strerror(errno)); + printf("%s",szBuf); + } if(waitOnDelayedResponse == false) { @@ -611,6 +625,8 @@ void ClientSocket::connect(const Ip &ip, int port) sprintf(szBuf, "In [%s::%s] Timeout in select() - Cancelling!\n",__FILE__,__FUNCTION__); //throwException(szBuf); fprintf(stderr, "%s", szBuf); + + disconnectSocket(); break; } } while (1); @@ -620,6 +636,7 @@ void ClientSocket::connect(const Ip &ip, int port) { fprintf(stderr, "In [%s::%s] Before END sock = %d, err = %d, errno = %d [%s]\n",__FILE__,__FUNCTION__,sock,err,errno,strerror(errno)); //throwException(szBuf); + disconnectSocket(); } else { diff --git a/source/shared_lib/sources/platform/win32/socket.cpp b/source/shared_lib/sources/platform/win32/socket.cpp index 97633a7c2..df4e076fa 100644 --- a/source/shared_lib/sources/platform/win32/socket.cpp +++ b/source/shared_lib/sources/platform/win32/socket.cpp @@ -683,7 +683,7 @@ void ClientSocket::connect(const Ip &ip, int port) fprintf(stderr, "%s\n", WSAGetLastErrorMessage(szBuf)); //fprintf(stderr, "%s", szBuf); - if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK) + if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK) { fd_set myset; struct timeval tv; @@ -746,7 +746,8 @@ void ClientSocket::connect(const Ip &ip, int port) if(err < 0) { fprintf(stderr, "In [%s::%s] Before END sock = %d, err = %d, errno = %d\n",__FILE__,__FUNCTION__,sock,err,WSAGetLastError()); - //throwException(szBuf); + //throwException(szBuf); + disconnectSocket(); } fprintf(stderr, "Valid recovery for connection sock = %d, err = %d, WSAGetLastError() = %d\n",sock,err,WSAGetLastError());