Ejemplo n.º 1
0
eNetworkResponse CNetworkModule::Connect( String strHost, unsigned short usPort, String strPass )
{
	// Are we already connected?
	if( IsConnected() )
	{
		// Disconnect
		Disconnect();
	}

	// Store the connection info
	SetLastConnection( strHost, usPort, strPass );

#ifdef DEBUG
	CLogFile::Printf( "Connection to %s:%d, password: %s", strHost.Get(), usPort, strPass.Get() );
#endif

	// Attempt to connect
	int iConnectionResult = m_pRakPeer->Connect( strHost.Get(), usPort, strPass.Get(), strPass.GetLength() );

	// Set the network state
	SetNetworkState( NETSTATE_CONNECTING );

	// Did we fail to connect?
	if( iConnectionResult != RakNet::INVALID_PARAMETER )
	{
		// Set the network state
		SetNetworkState( NETSTATE_NONE );

		// Set the last connection try
		m_uiLastConnectionTry = (unsigned int)SharedUtility::GetTime();
	}

	return (eNetworkResponse)iConnectionResult;
}
Ejemplo n.º 2
0
void CNetworkManager::Connect(CString strHost, unsigned short usPort, CString strPass)
{
	// Are we already connected?
	if(IsConnected())
	{
		// Disconnect
		Disconnect();
	}

	// Store the connection info
	SetLastConnection(strHost, usPort, strPass);

	// Attempt to connect
	int iConnectionResult = m_pRakPeer->Connect(strHost.Get(), usPort, strPass.Get(), strPass.GetLength());

	// Set the network state
	SetNetworkState(NETSTATE_CONNECTING);

	// Create the string for the connection message
	CString strMessage("Failed to connected!");

	// Get the result from the connection
	switch(iConnectionResult)
	{
		case 0: strMessage.Format("Connecting to %s:%d...", strHost.Get(), usPort); break;
		case 1: strMessage.Set("Failed to connect! (Invalid parameter)"); break;
		case 2: strMessage.Set("Failed to connect! (Cannot resolve domain name)"); break;
		case 3: strMessage.Set("Failed to connect! (Already connected)"); break;
		case 4: strMessage.Set("Failed to connect! (Connection attempt already in progress)"); break;
		case 5: strMessage.Set("Failed to connect! (Security initialization failed)"); break;
		case 6: strMessage.Set("Failed to connect! (No host set)"); break;
	}

	// Did we fail to connect?
	if(iConnectionResult != 0)
	{
		// Set the network state
		SetNetworkState(NETSTATE_DISCONNECTED);

		// Set the last connection try
		m_uiLastConnectionTry = (unsigned int)SharedUtility::GetTime();
	}

	CGUI* pGUI = g_pCore->GetGUI();
	if (pGUI)
	{
		//pGUI->ClearView(CGUI::GUI_SERVER);
		//pGUI->SetView(CGUI::GUI_SERVER);
	}

	// Output the connection message
	g_pCore->GetChat()->Outputf(true, "#16C5F2%s", strMessage.Get());
}
Ejemplo n.º 3
0
void CNetworkModule::Disconnect( bool bRestart )
{
	// Are we not connected?
	if( !IsConnected() )
		return;

	// Close the connection
	m_pRakPeer->CloseConnection( RakNet::UNASSIGNED_SYSTEM_ADDRESS, true );

	// Set the network state
	SetNetworkState( NETSTATE_DISCONNECTED );

	// Shutdown raknet
	Shutdown();

	// Should we restart raknet?
	if( bRestart )
	{
		// Start up raknet again
		Startup();

		// Reset default server info
		pCore->SetServerName( "M2-MP Server" );
		pCore->SetServerMaxPlayers( 0 );

		// Clear the chat
		pCore->GetChat()->Clear ();
		pCore->GetChat()->ClearHistory ();

		// Reset player model
		//pCore->GetPlayerManager()->GetLocalPlayer()->SetModel ( 10 );
	}
}
Ejemplo n.º 4
0
void CNetworkModule::UpdateNetwork( void )
{
	// Create a packet
	RakNet::Packet * pPacket = NULL;

	//
	bool bDisconnect = false;

	// Process RakNet
	while( pPacket = m_pRakPeer->Receive() )
	{
		// Is the packet invalid?
		if ( !pPacket )
			continue;

		// Have we connected?
		if( pPacket->data[0] == ID_CONNECTION_REQUEST_ACCEPTED )
		{
			// Call the connection accepted handler
			ConnectionAccepted( pPacket );

			// Process this packet with the server browser
			pCore->GetGUI()->GetServerBrowser()->ProcessNetworkPacket( (DefaultMessageIDTypes)pPacket->data[0] );
		}
		else if( pPacket->data[0] == ID_DISCONNECTION_NOTIFICATION || pPacket->data[0] == ID_CONNECTION_LOST ||
			 pPacket->data[0] == ID_NO_FREE_INCOMING_CONNECTIONS || pPacket->data[0] == ID_INVALID_PASSWORD ||
			 pPacket->data[0] == ID_CONNECTION_BANNED || pPacket->data[0] == ID_CONNECTION_ATTEMPT_FAILED || pPacket->data[0] == ID_ALREADY_CONNECTED )
		{
			// Did we timeout?
			if( pPacket->data[0] == ID_DISCONNECTION_NOTIFICATION || pPacket->data[0] == ID_CONNECTION_LOST )
			{
				// Stop multiplayer
				pCore->StopMultiplayer ();

				// Start multiplayer
				pCore->StartMultiplayer ();
			}

			// Set the network state
			SetNetworkState( NETSTATE_NONE );

			// Process this packet with the server browser
			pCore->GetGUI()->GetServerBrowser()->ProcessNetworkPacket( (DefaultMessageIDTypes)pPacket->data[0] );
		}

		// Deallocate the memory used by the packet
		m_pRakPeer->DeallocatePacket( pPacket );

		// Should we disconnect?
		if ( bDisconnect )
		{
			// Restart raknet
			Disconnect ();

			//
			bDisconnect = false;
		}
	}
}
Ejemplo n.º 5
0
CNetworkModule::CNetworkModule(void)
{
	// Get the RakPeerInterface instance
	m_pRakPeer = RakNet::RakPeerInterface::GetInstance();

	// Get the RPC4 instance
	m_pRPC = RakNet::RPC4::GetInstance();

	// Attact RPC4 to RakPeerInterface
	m_pRakPeer->AttachPlugin(m_pRPC);

	// Register the RPC's
	CNetworkRPC::Register(m_pRPC);

	// Set the network state
	SetNetworkState(NETSTATE_NONE);
}
void CNetworkManager::Disconnect(bool bShowMessage)
{
	// Are we not connected?
	if(!IsConnected())
		return;

	// Unregister the RPC's
	CNetworkRPC::Unregister(m_pRPC);

	// Close the connection
	m_pRakPeer->CloseConnection(RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);

	// Set the network state
	SetNetworkState(NETSTATE_DISCONNECTED);

	// Should we output a message?
	if(bShowMessage)
		g_pCore->GetChat()->Outputf(true, "#16C5F2The server connection closed.");
}
void CNetworkManager::ConnectionAccepted(RakNet::Packet * pPacket)
{
	// Set the network state
	SetNetworkState(NETSTATE_CONNECTED);

	// Construct a new bitstream
	RakNet::BitStream pBitStream;

	// Write the network version
	pBitStream.Write((DWORD)NETWORK_VERSION);

	// Write the player nickname
	pBitStream.Write(RakNet::RakString(g_pCore->GetNick().Get()));

	// Write the player serial
	pBitStream.Write(RakNet::RakString(SharedUtility::GetSerialHash().Get()));

	// Send to the server
	Call(GET_RPC_CODEX(RPC_INITIAL_DATA), &pBitStream, HIGH_PRIORITY, RELIABLE_ORDERED, true);
}
CNetworkManager::CNetworkManager()
{
	// Get the RakPeerInterface instance
	m_pRakPeer = RakNet::RakPeerInterface::GetInstance();

	// Get the RPC4 instance
	m_pRPC = RakNet::RPC4::GetInstance();

	// Attact RPC4 to RakPeerInterface
	m_pRakPeer->AttachPlugin(m_pRPC);

	// Register the RPC's
	CNetworkRPC::Register(m_pRPC);

	// Set the network state
	SetNetworkState(NETSTATE_NONE);

	// Reset the last connection try
	m_uiLastConnectionTry = (unsigned int)SharedUtility::GetTime();
}
void CNetworkManager::Pulse()
{
	// Is the game not loaded?
	if(!g_pCore->IsGameLoaded())
		return;

	// Are we disconnected from the network?
	if(GetNetworkState() == NETSTATE_DISCONNECTED)
		return;

	// Should we try connecting again?
	if(GetNetworkState() == NETSTATE_TIMEOUT && ((unsigned short)SharedUtility::GetTime() - m_uiLastConnectionTry) >= NETWORK_TIMEOUT)
	{
		// Attempt to reconnect
		Connect(m_strIp, m_usPort, m_strPass);

		// Set the network state
		SetNetworkState(NETSTATE_CONNECTING);

		// Set the last connection try
		m_uiLastConnectionTry = (unsigned int)SharedUtility::GetTime();
	}

	// Process the network
	UpdateNetwork();

	// Are we connected?
	if(IsConnected())
	{
		// Pulse the player manager
		g_pCore->GetGame()->GetPlayerManager()->Pulse();

		// Pulse the vehicle manager
		g_pCore->GetGame()->GetVehicleManager()->Pulse();
	}
}
void CNetworkManager::UpdateNetwork()
{
	// Create a packet
	RakNet::Packet * pPacket = NULL;

	// Process RakNet
	while(pPacket = m_pRakPeer->Receive())
	{
		switch(pPacket->data[0])
		{
			case ID_NO_FREE_INCOMING_CONNECTIONS:
			{
				g_pCore->GetChat()->Outputf(true, "#16C5F2The server is full. Rerying...");

				// Set the network state
				SetNetworkState(NETSTATE_TIMEOUT);

				// Set the last connection try
				m_uiLastConnectionTry = GetTickCount();
				break;
			}

			case ID_DISCONNECTION_NOTIFICATION:
			{
				g_pCore->GetChat()->Outputf(true, "#16C5F2The server connection closed.");
				
				// Set the network state
				SetNetworkState(NETSTATE_DISCONNECTED);

				// Shutdown the core
				//pCore->HandleNetworkShutdown();
				break;
			}

			case ID_INVALID_PASSWORD:
			{
				g_pCore->GetChat()->Outputf(true, "#16C5F2Incorrect server password.");

				// Set the network state
				SetNetworkState(NETSTATE_DISCONNECTED);
				break;
			}

			case ID_CONNECTION_BANNED:
			{
				g_pCore->GetChat()->Outputf(true, "#16C5F2You're banned from this server.");

				// Set the network state
				SetNetworkState(NETSTATE_DISCONNECTED);
				break;
			}

			case ID_CONNECTION_LOST:
			{
				g_pCore->GetChat()->Outputf(true, "#16C5F2Lost connection to the server. Reconnecting...");

				// Set the network state
				SetNetworkState(NETSTATE_TIMEOUT);

				// Handle the connection timeout
				g_pCore->OnNetworkTimeout();

				// Set the last connection try
				m_uiLastConnectionTry = GetTickCount();
				break;
			}

			case ID_CONNECTION_REQUEST_ACCEPTED:
			{
				ConnectionAccepted(pPacket);
				break;
			}

			case ID_CONNECTION_ATTEMPT_FAILED:
			{
				g_pCore->GetChat()->Outputf(true, "#16C5F2Failed to connect to the server. Retrying...");

				// Set the network state
				SetNetworkState(NETSTATE_TIMEOUT);

				// Set the last connection try
				m_uiLastConnectionTry = GetTickCount();
				break;
			}
		}

		// Deallocate the memory used by the packet
		m_pRakPeer->DeallocatePacket(pPacket);
	}
}