BOOL CMasterTestDlg::RequestGUIDValidation(CHLMasterAsyncSocket *pHLMasterSocket)
{
	if (!pHLMasterSocket->bInitialized)
	{
		BOOL bNoError;
		bNoError = pHLMasterSocket->Create(0,SOCK_DGRAM);
		// Check for errors
		if (!bNoError)
			return FALSE;
	
		bNoError = pHLMasterSocket->AsyncSelect(FD_CONNECT);
		if (!bNoError)
			return FALSE;

		// Try instantaneous connection
		bNoError = pHLMasterSocket->Connect("207.153.132.168", 27010);
		if (!bNoError)
			return FALSE;
	}

	pHLMasterSocket->bInitialized = TRUE;
	// Fixme, add an outgoing buffer?
	pHLMasterSocket->pMessageBuffer->SZ_Clear();
	//pHLMasterSocket->pSendMessageBuffer->SZ_Clear();
	//pHLMasterSocket->pSendBuffer->MSG_WriteByte(C2M_GUIDHEARTBEAT);

	char szGUID[GUID_LEN];
	CreateFakeGUID( szGUID, GUID_LEN );

	pHLMasterSocket->pSendBuffer->MSG_WriteBuf( GUID_LEN, szGUID );
	pHLMasterSocket->AsyncSelect(FD_READ | FD_WRITE);

	int iSent;
	iSent = pHLMasterSocket->Send(
		pHLMasterSocket->pSendBuffer->GetData(),
		pHLMasterSocket->pSendBuffer->GetCurSize());

	if (iSent != SOCKET_ERROR)
	{
		DataSent(iSent);
		// Clear message
		pHLMasterSocket->pSendBuffer->SZ_Clear();
	}
	else
	{
		DataSent( 0 );
	}


	return TRUE;
}
BOOL CMasterTestDlg::RequestServerList(CHLMasterAsyncSocket *pHLMasterSocket)
{
	UpdateData( TRUE );

	if (!pHLMasterSocket->bInitialized)
	{
		BOOL bNoError;
		bNoError = pHLMasterSocket->Create(0,SOCK_DGRAM);
		// Check for errors
		if (!bNoError)
			return FALSE;
	
		bNoError = pHLMasterSocket->AsyncSelect(FD_CONNECT);
		if (!bNoError)
			return FALSE;

		// Try instantaneous connection
		bNoError = pHLMasterSocket->Connect( (char *)(LPCSTR)m_hIPAddress, atoi( ( char * )(LPCSTR)m_hPort ) );
		if (!bNoError)
			return FALSE;
	}

	pHLMasterSocket->bInitialized = TRUE;
	pHLMasterSocket->pMessageBuffer->SZ_Clear();
	pHLMasterSocket->pSendBuffer->MSG_WriteByte( A2M_GET_SERVERS_BATCH2 );
	// Next id
	pHLMasterSocket->pSendBuffer->MSG_WriteLong( rand() % 9999 );
	// Search criteria
	pHLMasterSocket->pSendBuffer->MSG_WriteString( GetRandomCriteria() );
	pHLMasterSocket->AsyncSelect(FD_READ | FD_WRITE);

	int iSent;
	iSent = pHLMasterSocket->Send(
		pHLMasterSocket->pSendBuffer->GetData(),
		pHLMasterSocket->pSendBuffer->GetCurSize());

	if (iSent != SOCKET_ERROR)
	{
		DataSent(iSent);
		// Clear message
		pHLMasterSocket->pSendBuffer->SZ_Clear();
	}
	else
	{
		DataSent(0);
	}

	return TRUE;
}
Exemplo n.º 3
0
//
// Not called by the user ... called by the asynchronous notification object
//
void WSocket::OnSend(int error_code)
{
	if(m_sending_data==false)
	{
		return;
	}
	
	// Check to see if the send transfer is complete
	if(m_num_sent==m_send_data_buf_len)
	{
		delete [] p_send_data_buf;	// free the buffer
		p_send_data_buf=NULL;
		m_sending_data=false;
		SendQueuedDataBuffers();
		return;
	}

	// If it hasn't, then there is more data to send (this should always be the case on an OnSend)
	unsigned int num_sent;
	while (m_num_sent<m_send_data_buf_len)
	{
		// Send as much data as we can. If the buffer's full, it'll return SOCKET_ERROR and we'll go to the OnSend
		num_sent=Send((char *)p_send_data_buf+m_num_sent,m_send_data_buf_len-m_num_sent);

		if(num_sent==SOCKET_ERROR)
		{
			if(GetLastError() == WSAEWOULDBLOCK)	// another OnSend will be fired later when the buffer's empty
			{
				return;
			}
			else
			{
				// Something bad has happened here, like the remote socket has died or something
				delete [] p_send_data_buf;
				p_send_data_buf=NULL;
				m_sending_data=false;
				Close();
				return;
			}
		}

		m_num_sent+=num_sent;
	}

	// If the transfer is complete, free the buffer
	if(m_num_sent==m_send_data_buf_len)
	{
		delete [] p_send_data_buf;
		p_send_data_buf=NULL;
		m_sending_data=false;

		DataSent(m_num_sent);
	
		SendQueuedDataBuffers();
//		SendNextDataBuffer();	// send the next one, if there is one
	}
}
BOOL CMasterTestDlg::GetChallenge(CHLMasterAsyncSocket *pHLMasterSocket)
{
	if (!pHLMasterSocket->bInitialized)
	{
		BOOL bNoError;
		bNoError = pHLMasterSocket->Create(0,SOCK_DGRAM);
		// Check for errors
		if (!bNoError)
			return FALSE;
	
		bNoError = pHLMasterSocket->AsyncSelect(FD_CONNECT);
		if (!bNoError)
			return FALSE;

		// Try instantaneous connection
		bNoError = pHLMasterSocket->Connect( (char *)(LPCSTR)m_hIPAddress, atoi( ( char * )(LPCSTR)m_hPort ) );
		if (!bNoError)
			return FALSE;
	}

	pHLMasterSocket->bInitialized = TRUE;
	pHLMasterSocket->pMessageBuffer->SZ_Clear();
	pHLMasterSocket->pSendBuffer->MSG_WriteByte( A2A_GETCHALLENGE );
	pHLMasterSocket->AsyncSelect(FD_READ | FD_WRITE);

	int iSent;
	iSent = pHLMasterSocket->Send(
		pHLMasterSocket->pSendBuffer->GetData(),
		pHLMasterSocket->pSendBuffer->GetCurSize());

	if (iSent != SOCKET_ERROR)
	{
		DataSent(iSent);
		// Clear message
		pHLMasterSocket->pSendBuffer->SZ_Clear();
	}
	else
	{
		DataSent(0);
	}

	return TRUE;
}
Exemplo n.º 5
0
int WSocket::SendNextDataBuffer()
{
	// Check to see if there is a buffer to send
	if(v_send_data_buffers.size()==0)
	{
		return 0;
	}
	
	// Get next buffer to send
	vector<WSocketSendData *>::iterator send_data_iter=v_send_data_buffers.begin();
	
	// Now I am sending data
	m_sending_data=true;
	
	// Asynchronous Send that will continue in OnSend.
	m_num_sent=0;
	m_send_data_buf_len=(*send_data_iter)->GetDataLength();

	if(p_send_data_buf!=NULL)
	{
		delete [] p_send_data_buf;
		p_send_data_buf=NULL;
	}

	p_send_data_buf=new unsigned char [m_send_data_buf_len];	// this better be deleted in OnSend
	memcpy(p_send_data_buf,(*send_data_iter)->GetDataBuffer(),m_send_data_buf_len);		// copy buf to data member buffer
	
	// Remove the buffer from the vector
	delete (*send_data_iter);
	v_send_data_buffers.erase(send_data_iter);

	unsigned int num_sent;
	while(m_num_sent<m_send_data_buf_len)
	{
		// Send as much data as we can. If the buffer's full, it'll return SOCKET_ERROR and we'll go to the OnSend
		num_sent=Send((char *)p_send_data_buf+m_num_sent,m_send_data_buf_len-m_num_sent);
		
		if(num_sent==SOCKET_ERROR)
		{
			int error_code=GetLastError();
			if(error_code == WSAEWOULDBLOCK)
			{
				return SOCKET_ERROR;
			}
			else
			{
				// Something bad has happened here, like the remote socket has died or something
				delete [] p_send_data_buf;
				p_send_data_buf=NULL;
				m_sending_data=false;
				Close();

				return 0;
			}
		}

		m_num_sent+=num_sent;
	}

	// If we sent it all this time without having to go to the OnSend
	if(m_num_sent==m_send_data_buf_len)
	{
		delete [] p_send_data_buf;
		p_send_data_buf=NULL;
		m_sending_data=false;

		DataSent(m_num_sent);
	}

	return m_num_sent;	// should only be returned when we've sent all the data without having to go to the OnSend
}
BOOL CMasterTestDlg::SendHeartbeat( int challenge, CHLMasterAsyncSocket *pHLMasterSocket)
{
	if (!pHLMasterSocket->bInitialized)
	{
		BOOL bNoError;
		bNoError = pHLMasterSocket->Create(0,SOCK_DGRAM);
		// Check for errors
		if (!bNoError)
			return FALSE;
	
		bNoError = pHLMasterSocket->AsyncSelect(FD_CONNECT);
		if (!bNoError)
			return FALSE;

		// Try instantaneous connection
		bNoError = pHLMasterSocket->Connect( (char *)(LPCSTR)m_hIPAddress, atoi( ( char * )(LPCSTR)m_hPort ) );
		if (!bNoError)
			return FALSE;
	}

	pHLMasterSocket->bInitialized = TRUE;
	pHLMasterSocket->pMessageBuffer->SZ_Clear();
	pHLMasterSocket->pSendBuffer->MSG_WriteByte( S2M_HEARTBEAT2 );
	pHLMasterSocket->pSendBuffer->MSG_WriteByte( '\n' );
	
	char	string[4096];    // Buffer for sending heartbeat
	char	info[ MAX_SINFO ];
	int		iHasPW = 0;
	char	szOS[2];

	memset(string, 0, 2048);
	
#if _WIN32
	strcpy( szOS, "w" );
#else
	strcpy( szOS, "l" );
#endif

	info[0]='\0';

	Info_SetValueForKey( info, "protocol", va( "%i", 43 ), MAX_SINFO );
	Info_SetValueForKey( info, "challenge", va( "%i", challenge ), MAX_SINFO );

	int max = rand() % 25 + 5;

	Info_SetValueForKey( info, "max", va( "%i", max ), MAX_SINFO );
	Info_SetValueForKey( info, "players", va( "%i", rand() % max ), MAX_SINFO );
	Info_SetValueForKey( info, "gamedir", va( "mod%03i", rand() % 20 ), MAX_SINFO );
	Info_SetValueForKey( info, "map", va( "map%03i", rand() % 20 ), MAX_SINFO );
	Info_SetValueForKey( info, "dedicated", va( "%i", rand() % 2 ) , MAX_SINFO );
	Info_SetValueForKey( info, "password", va( "%i", iHasPW ) , MAX_SINFO );
	Info_SetValueForKey( info, "os", szOS , MAX_SINFO );

	pHLMasterSocket->pSendBuffer->MSG_WriteString( info );
	pHLMasterSocket->pSendBuffer->MSG_WriteByte( '\n' );


	pHLMasterSocket->AsyncSelect(FD_READ | FD_WRITE);

	int iSent;
	iSent = pHLMasterSocket->Send(
		pHLMasterSocket->pSendBuffer->GetData(),
		pHLMasterSocket->pSendBuffer->GetCurSize());

	if (iSent != SOCKET_ERROR)
	{
		DataSent(iSent);
		// Clear message
		pHLMasterSocket->pSendBuffer->SZ_Clear();
	}
	else
	{
		DataSent(0);
	}

	return TRUE;
}