//////////////////////////////////////////////////////////////////////////////
//
// Asura_PlatformNetwork.cpp
//
// Copyright (c) 2003 Rebellion. All rights reserved.
//
// Description:
//
//     interface to SCE-RT
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Core/Asura.h"

// independent headers
#include "Debugging/Asura_Assert.h"
#include "Debugging/Asura_Logfile.h"
#include "Network/Asura_Message.h"
#include "Network/Asura_Network.h"

// PS2 headers
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
#include "Network/Asura_PS2_SCE_RT.h"
#endif
 

void Asura_Network::PlatformInitialise(void)
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	bool bNetworkInitialisedOK = Asura_PS2_SCE_RT::Initialise();
	if (!bNetworkInitialisedOK)
	{
		Asura_Assert(false, "failed to init network");
		Asura_OutputToDebugger_Retail(("failed to init PS2 network.\n"));
	}
#endif
}


void Asura_Network::PlatformShutdown(void)
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	Asura_PS2_SCE_RT::Shutdown();
#endif
}


void Asura_Network::PlatformUpdate(void)
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	Asura_PS2_SCE_RT::Update();
#endif
}


void Asura_Network::PlatformStartClient(const char* /*szIPAddressToConnectTo*/)
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	bool bOK = Asura_PS2_SCE_RT::StartClientAndFindSessions();
	Asura_Assert(bOK, "StartClientAndFindSessions failed.");
#endif
}


void Asura_Network::PlatformStopClient(void)
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	Asura_PS2_SCE_RT::StopClient();
#endif
}


//void Asura_Network::Platform_ConnectToSession(unsigned int uSessionID)
//{
//#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
//	Asura_PS2_SCE_RT::ConnectToSession(uSessionID);
//#endif
//}


bool Asura_Network::PlatformStartServer(void)
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	return Asura_PS2_SCE_RT::StartServerAndHostSession();
#else
	return false;
#endif
}


void Asura_Network::PlatformStopServer(void)
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	Asura_PS2_SCE_RT::StopServer();
#endif
}


void Asura_Network::Platform_SendMessageToServer( const Asura_Message* pxMessage )
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	Asura_PS2_SCE_RT::SendMessageToServer(pxMessage);
#endif
}


void Asura_Network::Platform_SendMessageToAllClients( const Asura_Message* pxMessage )
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	Asura_PS2_SCE_RT::SendMessageToAllClients(pxMessage);
#endif
}


void Asura_Network::Platform_SendMessageToClientMachine( const Asura_Message* pxMessage, int iMachine )
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	Asura_PS2_SCE_RT::SendMessageToClientMachine(iMachine, pxMessage);
#endif
}


void Asura_Network::Platform_LockMessageQueue(void)
{
	// not required?
}


void Asura_Network::Platform_UnlockMessageQueue(void)
{
	// not required?
}


float Asura_Network::Platform_GetLatency(void)
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	if (s_bServer) return 0.0f;
	
	float fLatencyMilliseconds = static_cast<float>(Asura_PS2_SCE_RT::GetLatency());
	return fLatencyMilliseconds / 1000.0f;
#else
    return 0.0f;
#endif
}


void Asura_Network::Platform_EndOfFrame(void)
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	Asura_PS2_SCE_RT::EndOfFrame();
#endif
}

void Asura_Network::Platform_StartGame( void )
{
}

//void Asura_Network::Platform_UpdateProjectSessionData( class Asura_Network_SessionData*, u_int )
//{
//}
//
//
//void Asura_Network::Platform_UpdateProjectSessionData(const class Asura_Network_SessionData* pxSessionData, const u_int uSessionDataSize)
//{
//}


void Asura_Network::Platform_GetNetworkBandwidthUsage(unsigned int &, unsigned int &)
{
}

void Asura_Network::Platform_GetPeakNetworkBandwidthUsage(unsigned int &, unsigned int &)
{
}

void Asura_Network::Platform_SetServerLANReporting(bool)
{    
}

u_int Asura_Network::Platform_GetLatencyForPlayer(unsigned int)
{
    return 0;
}

//void Asura_Network::Platform_GetCurrentSessionInfo(void* pxSessionData, const u_int uSessionDataSize)
//{
//}

bool Asura_Network::Platform_ClientMessageHandler(const Asura_Message* pxMessage)
{
	return false;
}

bool Asura_Network::Platform_ServerMessageHandler(const Asura_Message* pxMessage)
{
	return false;
}

void Asura_Network::Platform_RemoveMachineFromGame(int, float)
{}
