- attempt to make accepting socket connections and dealing with certain connection problems more stable

This commit is contained in:
Mark Vejvoda
2012-03-24 01:09:55 +00:00
parent 5e3420b8c9
commit b14431385f

View File

@@ -1483,6 +1483,9 @@ int Socket::peek(void *data, int dataSize,bool mustGetData,int *pLastSocketError
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) if(chrono.getMillis() > 1) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) if(chrono.getMillis() > 1) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
} }
else {
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] SOCKET appears to be invalid [%d]\n",__FILE__,__FUNCTION__,__LINE__,sock);
}
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
int lastSocketError = getLastSocketError(); int lastSocketError = getLastSocketError();
@@ -1535,6 +1538,9 @@ int Socket::peek(void *data, int dataSize,bool mustGetData,int *pLastSocketError
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) if(chrono.getMillis() > 1) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) if(chrono.getMillis() > 1) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
} }
else {
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #2 SOCKET appears to be invalid [%d]\n",__FILE__,__FUNCTION__,__LINE__,sock);
}
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
@@ -1545,7 +1551,7 @@ int Socket::peek(void *data, int dataSize,bool mustGetData,int *pLastSocketError
int iErr = lastSocketError; int iErr = lastSocketError;
disconnectSocket(); disconnectSocket();
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTED SOCKET error while peeking socket data, err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,err,getLastSocketErrorFormattedText(&iErr).c_str()); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"[%s::%s Line: %d] DISCONNECTED SOCKET error while peeking socket data for socket [%d], err = %d, error = %s\n",__FILE__,__FUNCTION__,__LINE__,socket,err,getLastSocketErrorFormattedText(&iErr).c_str());
} }
} }
@@ -2250,11 +2256,15 @@ Socket *ServerSocket::accept(bool errorOnFail) {
} }
} }
PLATFORM_SOCKET newSock=0;
char client_host[100]="";
const int max_attempts = 100;
for(int attempt = 0; attempt < max_attempts; ++attempt) {
struct sockaddr_in cli_addr; struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr); socklen_t clilen = sizeof(cli_addr);
char client_host[100]=""; client_host[0]='\0';
MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE); MutexSafeWrapper safeMutex(dataSynchAccessorRead,CODE_AT_LINE);
PLATFORM_SOCKET newSock= ::accept(sock, (struct sockaddr *) &cli_addr, &clilen); newSock= ::accept(sock, (struct sockaddr *) &cli_addr, &clilen);
safeMutex.ReleaseLock(); safeMutex.ReleaseLock();
if(isSocketValid(&newSock) == false) { if(isSocketValid(&newSock) == false) {
@@ -2264,8 +2274,13 @@ Socket *ServerSocket::accept(bool errorOnFail) {
int lastSocketError = getLastSocketError(); int lastSocketError = getLastSocketError();
if(lastSocketError == PLATFORM_SOCKET_TRY_AGAIN) { if(lastSocketError == PLATFORM_SOCKET_TRY_AGAIN) {
if(attempt+1 >= max_attempts) {
return NULL; return NULL;
} }
else {
sleep(0);
}
}
if(errorOnFail == true) { if(errorOnFail == true) {
throwException(szBuf); throwException(szBuf);
} }
@@ -2294,6 +2309,8 @@ Socket *ServerSocket::accept(bool errorOnFail) {
return NULL; return NULL;
} }
break;
}
Socket *result = new Socket(newSock); Socket *result = new Socket(newSock);
result->setIpAddress((client_host[0] != '\0' ? client_host : "")); result->setIpAddress((client_host[0] != '\0' ? client_host : ""));
return result; return result;