/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/drivers/wintel32/network/sdlnetmanager.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/network/network.hpp"
#include "drivers/wintel32/network/network.hpp"
#include "SDL_net.h"


CSDLNetManager::CSDLNetManager() : CSGXNetworkManager()
{
	SDLNet_Init();
}

CSDLNetManager::~CSDLNetManager()
{
	SDLNet_Quit();
}

CSGXNetworkManager *CSDLNetManager::create()
{
    if (ms_pSingleton == NULL) {
        ms_pSingleton = SGX_STATIC_CAST(CSGXNetworkManager *, new CSDLNetManager());
    }
    return ms_pSingleton;            
}

CNetworkConnection *CSDLNetManager::CreateServer(tUINT16 port, int flags) 
{
	return new CSDLNetConnectionServer(port, flags);
}

CNetworkConnection *CSDLNetManager::CreateClient(CNetMachine *pServer, int flags) 
{
	return new CSDLNetConnectionClient(pServer, flags);
}

tBOOL 
CSDLNetManager::transmitPacket(tBYTE *ptr, tUINT32 size)
{
    CPacketHeader *pHead = (CPacketHeader *)ptr;
// TODO
	/*
			ePlayer1 = 1,
		ePlayer2,
		ePlayer3,
		ePlayer4,
		// Groups or teams 
		eClan1 = 1001,
		eClan2,
		/ Meta-users 
		eServer = 2001,
		eAll,
*/
	//pHead->m_Who
//	SDLNet_TCP_Send(serverSocket, (void *)pBuffer, bufferLength);

    dispatchPacket(pHead->m_UsersHeader.m_Type, ptr+pHead->m_HeaderSize, pHead->m_UsersHeader.m_DataSize);

    releaseLastPacket();
    return TRUE;
}
