Code/ServerApp/Source/ServerNetworking.cpp
//=================================================================================================
// Thread trying to reach out new Clients with a connection
//=================================================================================================
Thread trying to reach out new Clients with a connection
Thread trying to reach out new Clients with a connection
void NetworkConnectRequest_Send()
{		
	uint64_t loopIndex(0);
	ClientConfig clientConfig;
	SOCKET ConnectSocket(INVALID_SOCKET);
	while( !gbShutdown )
	{						
		addrinfo* pAdrResults(nullptr);
		uint32_t clientConfigID(ClientConfig::kInvalidRuntimeID);
		if( ConnectSocket == INVALID_SOCKET ) 	
			ConnectSocket = socket(AF_INET, SOCK_STREAM, 0);
		uint64_t configCount	= static_cast<uint64_t>(ClientConfig::GetConfigCount());
		uint32_t configIdx		= configCount ? static_cast<uint32_t>(loopIndex++ % configCount) : 0;
		if( ClientConfig::GetConfigByIndex(configIdx, clientConfig) )
		{
			if( (clientConfig.ConnectAuto || clientConfig.ConnectRequest) && !clientConfig.Connected )
			{
				char zPortName[10];
				sprintf_s(zPortName, "%i", clientConfig.HostPort);
				getaddrinfo(clientConfig.HostName, zPortName, nullptr, &pAdrResults);
				ClientConfig::SetProperty_ConnectRequest(clientConfig.RuntimeID, false);	
				clientConfigID = clientConfig.RuntimeID;									
			}
		}			
		bool bConnected(false);
		addrinfo* pAdrResultCur(pAdrResults);
		while( ConnectSocket != INVALID_SOCKET && pAdrResultCur && !bConnected )
		{
			bConnected		= connect(ConnectSocket, pAdrResultCur->ai_addr, static_cast<int>(pAdrResultCur->ai_addrlen)) == 0;
			pAdrResultCur	= bConnected ? pAdrResultCur : pAdrResultCur->ai_next;
		}
		if( bConnected )
		{			
			uint32_t freeIndex = ClientRemote::GetFreeIndex();
			if( freeIndex != ClientRemote::kInvalidClient )
			{			
				ClientRemote& newClient = ClientRemote::Get(freeIndex);
				if( ClientConfig::GetConfigByID(clientConfigID, clientConfig) )
				{
					strcpy_s(newClient.mInfoName, clientConfig.ClientName);
					newClient.mConnectPort		= clientConfig.HostPort;
					newClient.mClientConfigID	= clientConfigID;					
				}
				getnameinfo(pAdrResultCur->ai_addr, static_cast<socklen_t>(pAdrResultCur->ai_addrlen), newClient.mConnectHost, sizeof(newClient.mConnectHost), nullptr, 0, 0);
				NetworkConnectionNew(ConnectSocket, &newClient);
				ConnectSocket = INVALID_SOCKET;
			}
		}
		freeaddrinfo(pAdrResults);
		std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	}	
	closesocket( ConnectSocket );
}
void NetworkConnectRequest_Send ( ) { uint64_t loopIndex ( 0 ) ; ClientConfig clientConfig ; SOCKET ConnectSocket ( INVALID_SOCKET ) ; while ( ! gbShutdown ) { addrinfo * pAdrResults ( nullptr ) ; uint32_t clientConfigID ( ClientConfig :: kInvalidRuntimeID ) ; if ( ConnectSocket == INVALID_SOCKET ) ConnectSocket = socket ( AF_INET , SOCK_STREAM , 0 ) ; uint64_t configCount = static_cast < uint64_t > ( ClientConfig :: GetConfigCount ( ) ) ; uint32_t configIdx = configCount ? static_cast < uint32_t > ( loopIndex ++ % configCount ) : 0 ; if ( ClientConfig :: GetConfigByIndex ( configIdx , clientConfig ) ) { if ( ( clientConfig . ConnectAuto || clientConfig . ConnectRequest ) && ! clientConfig . Connected ) { char zPortName [ 10 ] ; sprintf_s ( zPortName , " " , clientConfig . HostPort ) ; getaddrinfo ( clientConfig . HostName , zPortName , nullptr , & pAdrResults ) ; ClientConfig :: SetProperty_ConnectRequest ( clientConfig . RuntimeID , false ) ; clientConfigID = clientConfig . RuntimeID ; } } bool bConnected ( false ) ; addrinfo * pAdrResultCur ( pAdrResults ) ; while ( ConnectSocket != INVALID_SOCKET && pAdrResultCur && ! bConnected ) { bConnected = connect ( ConnectSocket , pAdrResultCur -> ai_addr , static_cast < int > ( pAdrResultCur -> ai_addrlen ) ) == 0 ; pAdrResultCur = bConnected ? pAdrResultCur : pAdrResultCur -> ai_next ; } if ( bConnected ) { uint32_t freeIndex = ClientRemote :: GetFreeIndex ( ) ; if ( freeIndex != ClientRemote :: kInvalidClient ) { ClientRemote & newClient = ClientRemote :: Get ( freeIndex ) ; if ( ClientConfig :: GetConfigByID ( clientConfigID , clientConfig ) ) { strcpy_s ( newClient . mInfoName , clientConfig . ClientName ) ; newClient . mConnectPort = clientConfig . HostPort ; newClient . mClientConfigID = clientConfigID ; } getnameinfo ( pAdrResultCur -> ai_addr , static_cast < socklen_t > ( pAdrResultCur -> ai_addrlen ) , newClient . mConnectHost , sizeof ( newClient . mConnectHost ) , nullptr , 0 , 0 ) ; NetworkConnectionNew ( ConnectSocket , & newClient ) ; ConnectSocket = INVALID_SOCKET ; } } freeaddrinfo ( pAdrResults ) ; std :: this_thread :: sleep_for ( std :: chrono :: milliseconds ( 1000 ) ) ; } closesocket ( ConnectSocket ) ; }
Thread trying to reach out new Clients with a connection
Thread trying to reach out new Clients with a connection
// Find next client configuration to attempt connection to // Reset the Connection request, we are processing it // Keep track of ClientConfig we are attempting to connect to				 // Attempt to reach the Client, over all resolved addresses // Connection successful, find an available client slot