Example #1
0
void * connect_thread(void * param)
{
	printf ( "--->connect_thread 0x%x \n", pthread_self() );
	
	Connector	* pClass = (Connector *) param;
	Session 	* pSession;
	
	while (true)
	{
		sem_wait( &pClass->m_semConnect );
		//printf("After sem_wait.Begin to handle ConnectingList \n");
		if ( pClass->m_bShutdown )
		{
			printf ( "\n connect_thread 0x%x exit \n", pthread_self() );
			pthread_exit (NULL);
		}
		
		while ( !pClass->m_pConnectingList->empty() ) 
		{
			if ( pClass->m_bShutdown )
			{
				printf ( "\n connect_thread 0x%x exit \n", pthread_self() );
				pthread_exit (NULL);
			}
			
			pClass->m_pConnectingList->Lock();
			pSession = pClass->m_pConnectingList->front();
			pClass->m_pConnectingList->pop_front();
			pClass->m_pConnectingList->Unlock();
			
			int err = connect( pSession->GetSocket(), (SOCKADDR*)( pSession->GetSockAddr() ),sizeof(SOCKADDR_IN));
			
			if (err == SOCKET_ERROR)
			{
				//printf ( "connect fail, errno = %d\n", errno );
				
				pClass->m_pSync->m_pConnectFailList->Lock();
				pClass->m_pSync->m_pConnectFailList->push_back( pSession );			
				pClass->m_pSync->m_pConnectFailList->Unlock();
			}
			else 
			{
				printf("connect success\n");
				SocketOpt::Nonblocking( pSession->GetSocket() );
				SocketOpt::DisableBuffering( pSession->GetSocket() );

				pClass->m_pSync->m_pConnectSuccessList->Lock();
				pClass->m_pSync->m_pConnectSuccessList->push_back( pSession );			
				pClass->m_pSync->m_pConnectSuccessList->Unlock();
			}
		}
	}
}
Example #2
0
//=============================================================================================================================
unsigned __stdcall connect_thread( LPVOID param )
{
	Connector	*pClass = (Connector*)param;
	Session		*pSession;

	while( !pClass->m_bShutdown )
	{
		DWORD dwRet = WaitForMultipleObjects( 2, pClass->m_hEvent, FALSE, INFINITE );

		if( dwRet - WAIT_OBJECT_0 == 0 )
		{
			// 
			while( !pClass->m_pConnectingList->empty() )
			{
				//
				pClass->m_pConnectingList->Lock();				
				pSession = pClass->m_pConnectingList->front();
				pClass->m_pConnectingList->pop_front();
				pClass->m_pConnectingList->Unlock();

				// 
				int err = connect( pSession->GetSocket(), (SOCKADDR*)( pSession->GetSockAddr() ), sizeof(SOCKADDR_IN) );
								   
				if( err == SOCKET_ERROR )
				{
					// 
					int x = WSAGetLastError();
					pClass->m_pIoHandler->m_pConnectFailList->Lock();
					pClass->m_pIoHandler->m_pConnectFailList->push_back( pSession );
					pClass->m_pIoHandler->m_pConnectFailList->Unlock();
				}
				else
				{
					// 
					pClass->m_pIoHandler->m_pConnectSuccessList->Lock();
					pClass->m_pIoHandler->m_pConnectSuccessList->push_back( pSession );
					pClass->m_pIoHandler->m_pConnectSuccessList->Unlock();
				}
			}
		}
		else if( dwRet - WAIT_OBJECT_0 == 1 )
		{
			//
			break;
		}
	}

	return 0;
}