2004-08-16 17:19:17 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2006-03-21 21:55:45 +01:00
|
|
|
#include "scripting/DOMEvent.h"
|
|
|
|
#include "scripting/JSConversions.h"
|
|
|
|
#include "scripting/ScriptableObject.h"
|
2006-06-02 05:56:24 +02:00
|
|
|
#include "Client.h"
|
|
|
|
#include "JSEvents.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/CStr.h"
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/CConsole.h"
|
|
|
|
#include "ps/Game.h"
|
2008-01-03 23:07:18 +01:00
|
|
|
#include "ps/Globals.h" // g_frequencyFilter
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/GameAttributes.h"
|
|
|
|
#include "simulation/Simulation.h"
|
2004-08-16 17:19:17 +02:00
|
|
|
|
|
|
|
#define LOG_CAT_NET "net"
|
|
|
|
|
|
|
|
CNetClient *g_NetClient=NULL;
|
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
CNetClient::CServerSession::CServerSession(int sessionID, const CStrW& name):
|
2005-02-21 18:13:31 +01:00
|
|
|
m_SessionID(sessionID),
|
|
|
|
m_Name(name)
|
|
|
|
{
|
|
|
|
ONCE( ScriptingInit(); );
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNetClient::CServerSession::ScriptingInit()
|
|
|
|
{
|
2005-04-22 09:12:55 +02:00
|
|
|
AddProperty(L"id", &CNetClient::CServerSession::m_SessionID, true);
|
|
|
|
AddProperty(L"name", &CNetClient::CServerSession::m_Name, true);
|
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
CJSObject<CServerSession>::ScriptingInit("NetClient_ServerSession");
|
|
|
|
}
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
CNetClient::CNetClient(CGame *pGame, CGameAttributes *pGameAttribs):
|
|
|
|
CNetSession(ConnectHandler),
|
2005-02-21 18:13:31 +01:00
|
|
|
m_JSI_ServerSessions(&m_ServerSessions),
|
|
|
|
m_pLocalPlayerSlot(NULL),
|
2004-08-16 17:19:17 +02:00
|
|
|
m_pGame(pGame),
|
2007-11-18 10:09:06 +01:00
|
|
|
m_pGameAttributes(pGameAttribs),
|
|
|
|
m_TurnPending(false)
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
2005-02-21 18:13:31 +01:00
|
|
|
ONCE( ScriptingInit(); );
|
2005-01-19 23:32:25 +01:00
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
m_pGame->GetSimulation()->SetTurnManager(this);
|
2005-01-17 05:52:02 +01:00
|
|
|
|
|
|
|
g_ScriptingHost.SetGlobal("g_NetClient", OBJECT_TO_JSVAL(GetScript()));
|
|
|
|
}
|
|
|
|
|
|
|
|
CNetClient::~CNetClient()
|
|
|
|
{
|
|
|
|
g_ScriptingHost.SetGlobal("g_NetClient", JSVAL_NULL);
|
2005-06-20 17:14:36 +02:00
|
|
|
|
|
|
|
SessionMap::iterator it=m_ServerSessions.begin();
|
2007-11-18 10:09:06 +01:00
|
|
|
for (; it != m_ServerSessions.end(); ++it)
|
2005-06-20 17:14:36 +02:00
|
|
|
{
|
|
|
|
delete it->second;
|
|
|
|
}
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
void CNetClient::ScriptingInit()
|
|
|
|
{
|
|
|
|
AddMethod<bool, &CNetClient::JSI_BeginConnect>("beginConnect", 1);
|
|
|
|
|
2005-04-22 09:12:55 +02:00
|
|
|
AddProperty(L"onStartGame", &CNetClient::m_OnStartGame);
|
|
|
|
AddProperty(L"onChat", &CNetClient::m_OnChat);
|
|
|
|
AddProperty(L"onConnectComplete", &CNetClient::m_OnConnectComplete);
|
|
|
|
AddProperty(L"onDisconnect", &CNetClient::m_OnDisconnect);
|
|
|
|
AddProperty(L"onClientConnect", &CNetClient::m_OnClientConnect);
|
|
|
|
AddProperty(L"onClientDisconnect", &CNetClient::m_OnClientDisconnect);
|
2005-03-30 18:14:19 +02:00
|
|
|
|
2005-04-22 09:12:55 +02:00
|
|
|
AddProperty(L"password", &CNetClient::m_Password);
|
|
|
|
AddProperty<CStrW>(L"playerName", &CNetClient::m_Name);
|
2005-06-20 17:14:36 +02:00
|
|
|
AddProperty(L"sessionId", &CNetClient::m_SessionID);
|
2005-03-30 18:14:19 +02:00
|
|
|
|
2005-04-22 09:12:55 +02:00
|
|
|
AddProperty(L"sessions", &CNetClient::m_JSI_ServerSessions);
|
2005-02-21 18:13:31 +01:00
|
|
|
CJSMap<SessionMap>::ScriptingInit("NetClient_SessionMap");
|
|
|
|
CJSObject<CNetClient>::ScriptingInit("NetClient");
|
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
bool CNetClient::JSI_BeginConnect(JSContext* UNUSED(cx), uintN argc, jsval *argv)
|
2005-01-17 05:52:02 +01:00
|
|
|
{
|
|
|
|
CStr connectHostName;
|
|
|
|
uint connectPort=PS_DEFAULT_PORT;
|
|
|
|
if (argc >= 1)
|
|
|
|
{
|
|
|
|
connectHostName=g_ScriptingHost.ValueToString(argv[0]);
|
|
|
|
}
|
|
|
|
if (argc >= 2)
|
|
|
|
{
|
2005-08-09 17:55:44 +02:00
|
|
|
connectPort=ToPrimitive<int>(argv[1]);
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PS_RESULT res=BeginConnect(connectHostName.c_str(), connectPort);
|
|
|
|
if (res != PS_OK)
|
|
|
|
{
|
2007-12-29 17:22:23 +01:00
|
|
|
LOG(CLogger::Error, LOG_CAT_NET, "CNetClient::JSI_Connect(): BeginConnect error: %s", res);
|
2005-01-17 05:52:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return true;
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TEMPLATE FOR MESSAGE HANDLERS:
|
|
|
|
|
|
|
|
bool CNetClient::<X>Handler(CNetMessage *pMsg, CNetSession *pSession)
|
|
|
|
{
|
|
|
|
CNetClient *pClient=(CNetClient *)pSession;
|
|
|
|
switch (pMsg->GetType())
|
|
|
|
{
|
|
|
|
case XXX:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNHANDLED(pMsg);
|
|
|
|
}
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define UNHANDLED(_pMsg) return false;
|
|
|
|
#define HANDLED(_pMsg) delete _pMsg; return true;
|
|
|
|
#define TAKEN(_pMsg) return true;
|
2005-01-23 02:36:47 +01:00
|
|
|
// Uglily assumes the arguments are called pMsg and pSession (which they are
|
|
|
|
// all through this file)
|
|
|
|
#define CHAIN(_chainHandler) STMT(if (_chainHandler(pMsg, pSession)) return true;)
|
2004-08-16 17:19:17 +02:00
|
|
|
|
|
|
|
bool CNetClient::ConnectHandler(CNetMessage *pMsg, CNetSession *pSession)
|
|
|
|
{
|
|
|
|
CNetClient *pClient=(CNetClient *)pSession;
|
2005-01-17 05:52:02 +01:00
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
switch (pMsg->GetType())
|
|
|
|
{
|
|
|
|
case NMT_CONNECT_COMPLETE:
|
|
|
|
pClient->m_pMessageHandler=HandshakeHandler;
|
2005-01-19 23:32:25 +01:00
|
|
|
if (pClient->m_OnConnectComplete.Defined())
|
2005-01-17 05:52:02 +01:00
|
|
|
{
|
2005-01-23 02:36:47 +01:00
|
|
|
CConnectCompleteEvent evt(CStr((char *)PS_OK), true);
|
2005-01-19 23:32:25 +01:00
|
|
|
pClient->m_OnConnectComplete.DispatchEvent(pClient->GetScript(), &evt);
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|
2004-08-16 17:19:17 +02:00
|
|
|
break;
|
|
|
|
case NMT_ERROR:
|
|
|
|
{
|
|
|
|
CNetErrorMessage *msg=(CNetErrorMessage *)pMsg;
|
2007-12-29 17:22:23 +01:00
|
|
|
LOG(CLogger::Error, LOG_CAT_NET, "CNetClient::ConnectHandler(): Connect Failed: %s", msg->m_Error);
|
2005-01-19 23:32:25 +01:00
|
|
|
if (pClient->m_OnConnectComplete.Defined())
|
2005-01-17 05:52:02 +01:00
|
|
|
{
|
2005-01-23 02:36:47 +01:00
|
|
|
CConnectCompleteEvent evt(CStr(msg->m_Error), false);
|
2005-01-19 23:32:25 +01:00
|
|
|
pClient->m_OnConnectComplete.DispatchEvent(pClient->GetScript(), &evt);
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|
2004-08-16 17:19:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNHANDLED(pMsg);
|
|
|
|
}
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
|
|
|
|
2005-01-23 02:36:47 +01:00
|
|
|
bool CNetClient::BaseHandler(CNetMessage *pMsg, CNetSession *pSession)
|
|
|
|
{
|
|
|
|
CNetClient *pClient=(CNetClient *)pSession;
|
|
|
|
switch (pMsg->GetType())
|
|
|
|
{
|
|
|
|
case NMT_ERROR:
|
|
|
|
{
|
|
|
|
CNetErrorMessage *msg=(CNetErrorMessage *)pMsg;
|
|
|
|
if (msg->m_State == SS_UNCONNECTED)
|
|
|
|
{
|
|
|
|
CStr message=msg->m_Error;
|
|
|
|
CDisconnectEvent evt(message);
|
|
|
|
if (pClient->m_OnDisconnect.Defined())
|
|
|
|
pClient->m_OnDisconnect.DispatchEvent(pClient->GetScript(), &evt);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNHANDLED(pMsg);
|
|
|
|
}
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
bool CNetClient::HandshakeHandler(CNetMessage *pMsg, CNetSession *pSession)
|
|
|
|
{
|
|
|
|
CNetClient *pClient=(CNetClient *)pSession;
|
2005-01-23 02:36:47 +01:00
|
|
|
|
|
|
|
CHAIN(BaseHandler);
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
switch (pMsg->GetType())
|
|
|
|
{
|
|
|
|
case NMT_ServerHandshake:
|
|
|
|
{
|
|
|
|
CClientHandshake *msg=new CClientHandshake();
|
|
|
|
msg->m_MagicResponse=PS_PROTOCOL_MAGIC_RESPONSE;
|
|
|
|
msg->m_ProtocolVersion=PS_PROTOCOL_VERSION;
|
|
|
|
msg->m_SoftwareVersion=PS_PROTOCOL_VERSION;
|
|
|
|
pClient->Push(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NMT_ServerHandshakeResponse:
|
|
|
|
{
|
|
|
|
CAuthenticate *msg=new CAuthenticate();
|
|
|
|
msg->m_Name=pClient->m_Name;
|
|
|
|
msg->m_Password=pClient->m_Password;
|
|
|
|
|
|
|
|
pClient->m_pMessageHandler=AuthenticateHandler;
|
|
|
|
pClient->Push(msg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNHANDLED(pMsg);
|
|
|
|
}
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetClient::AuthenticateHandler(CNetMessage *pMsg, CNetSession *pSession)
|
|
|
|
{
|
|
|
|
CNetClient *pClient=(CNetClient *)pSession;
|
2005-01-23 02:36:47 +01:00
|
|
|
|
|
|
|
CHAIN(BaseHandler);
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
switch (pMsg->GetType())
|
|
|
|
{
|
2005-02-21 18:13:31 +01:00
|
|
|
case NMT_AuthenticationResult:
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
2005-02-21 18:13:31 +01:00
|
|
|
CAuthenticationResult *msg=(CAuthenticationResult *)pMsg;
|
2004-08-16 17:19:17 +02:00
|
|
|
if (msg->m_Code != NRC_OK)
|
|
|
|
{
|
2007-12-29 17:22:23 +01:00
|
|
|
LOG(CLogger::Error, LOG_CAT_NET, "CNetClient::AuthenticateHandler(): Authentication failed: %ls", msg->m_Message.c_str());
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
else
|
2004-09-21 16:40:43 +02:00
|
|
|
{
|
2007-12-29 17:22:23 +01:00
|
|
|
LOG(CLogger::Normal, LOG_CAT_NET, "CNetClient::AuthenticateHandler(): Authenticated!");
|
2005-02-21 18:13:31 +01:00
|
|
|
pClient->m_SessionID=msg->m_SessionID;
|
2004-09-21 16:40:43 +02:00
|
|
|
pClient->m_pMessageHandler=PreGameHandler;
|
|
|
|
}
|
2004-08-16 17:19:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNHANDLED(pMsg);
|
|
|
|
}
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNetClient::PreGameHandler(CNetMessage *pMsg, CNetSession *pSession)
|
|
|
|
{
|
|
|
|
CNetClient *pClient=(CNetClient *)pSession;
|
2005-01-23 02:36:47 +01:00
|
|
|
|
|
|
|
CHAIN(BaseHandler);
|
|
|
|
CHAIN(ChatHandler);
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
switch (pMsg->GetType())
|
|
|
|
{
|
|
|
|
case NMT_StartGame:
|
|
|
|
{
|
2007-06-04 09:41:05 +02:00
|
|
|
pClient->OnStartGameMessage();
|
2004-08-16 17:19:17 +02:00
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
2005-02-21 18:13:31 +01:00
|
|
|
case NMT_ClientConnect:
|
|
|
|
{
|
|
|
|
CClientConnect *msg=(CClientConnect *)pMsg;
|
|
|
|
for (uint i=0;i<msg->m_Clients.size();i++)
|
|
|
|
{
|
|
|
|
pClient->OnClientConnect(msg->m_Clients[i].m_SessionID,
|
|
|
|
msg->m_Clients[i].m_Name);
|
|
|
|
}
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
|
|
|
case NMT_ClientDisconnect:
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
2005-02-21 18:13:31 +01:00
|
|
|
CClientDisconnect *msg=(CClientDisconnect *)pMsg;
|
|
|
|
pClient->OnClientDisconnect(msg->m_SessionID);
|
2004-08-16 17:19:17 +02:00
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
|
|
|
case NMT_SetGameConfig:
|
|
|
|
{
|
|
|
|
CSetGameConfig *msg=(CSetGameConfig *)pMsg;
|
|
|
|
for (uint i=0;i<msg->m_Values.size();i++)
|
|
|
|
{
|
|
|
|
pClient->m_pGameAttributes->SetValue(msg->m_Values[i].m_Name, msg->m_Values[i].m_Value);
|
|
|
|
}
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
2005-02-21 18:13:31 +01:00
|
|
|
case NMT_AssignPlayerSlot:
|
|
|
|
{
|
|
|
|
CAssignPlayerSlot *msg=(CAssignPlayerSlot *)pMsg;
|
|
|
|
// FIXME Validate slot id to prevent us from going boom
|
|
|
|
CPlayerSlot *pSlot=pClient->m_pGameAttributes->GetSlot(msg->m_SlotID);
|
|
|
|
if (pSlot == pClient->m_pLocalPlayerSlot)
|
|
|
|
pClient->m_pLocalPlayerSlot=NULL;
|
|
|
|
switch (msg->m_Assignment)
|
|
|
|
{
|
|
|
|
case PS_ASSIGN_SESSION:
|
2005-08-09 17:55:44 +02:00
|
|
|
if ((int)msg->m_SessionID == (int)pClient->m_SessionID) // squelch bogus sign mismatch warning
|
2005-02-21 18:13:31 +01:00
|
|
|
pClient->m_pLocalPlayerSlot=pSlot;
|
|
|
|
pSlot->AssignToSessionID(msg->m_SessionID);
|
|
|
|
break;
|
|
|
|
case PS_ASSIGN_CLOSED:
|
|
|
|
pSlot->AssignClosed();
|
|
|
|
break;
|
|
|
|
case PS_ASSIGN_OPEN:
|
|
|
|
pSlot->AssignOpen();
|
|
|
|
break;
|
|
|
|
default:
|
2007-12-29 17:22:23 +01:00
|
|
|
LOG(CLogger::Warning, LOG_CAT_NET, "CNetClient::PreGameHandler(): Received an invalid slot assignment %s", msg->GetString().c_str());
|
2005-02-21 18:13:31 +01:00
|
|
|
}
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
2005-01-29 17:51:13 +01:00
|
|
|
case NMT_SetPlayerConfig:
|
|
|
|
{
|
|
|
|
CSetPlayerConfig *msg=(CSetPlayerConfig *)pMsg;
|
|
|
|
// FIXME Check player ID
|
2005-02-21 18:13:31 +01:00
|
|
|
CPlayer *pPlayer=pClient->m_pGameAttributes->GetPlayer(msg->m_PlayerID);
|
2005-01-29 17:51:13 +01:00
|
|
|
for (uint i=0;i<msg->m_Values.size();i++)
|
|
|
|
{
|
|
|
|
pPlayer->SetValue(msg->m_Values[i].m_Name, msg->m_Values[i].m_Value);
|
|
|
|
}
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
2005-10-07 17:24:29 +02:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
UNHANDLED(pMsg);
|
|
|
|
}
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-21 16:40:43 +02:00
|
|
|
bool CNetClient::InGameHandler(CNetMessage *pMsg, CNetSession *pSession)
|
|
|
|
{
|
|
|
|
CNetClient *pClient=(CNetClient *)pSession;
|
|
|
|
ENetMessageType msgType=pMsg->GetType();
|
|
|
|
|
2005-01-23 02:36:47 +01:00
|
|
|
CHAIN(BaseHandler);
|
|
|
|
CHAIN(ChatHandler);
|
|
|
|
|
2005-05-18 07:32:09 +02:00
|
|
|
if (msgType >= NMT_COMMAND_FIRST && msgType < NMT_COMMAND_LAST)
|
2004-09-21 16:40:43 +02:00
|
|
|
{
|
2007-11-18 10:09:06 +01:00
|
|
|
pClient->QueueIncomingMessage(pMsg);
|
2004-09-21 16:40:43 +02:00
|
|
|
TAKEN(pMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msgType)
|
|
|
|
{
|
|
|
|
case NMT_EndCommandBatch:
|
2005-10-07 17:24:29 +02:00
|
|
|
{
|
2005-01-17 05:52:02 +01:00
|
|
|
CEndCommandBatch *msg=(CEndCommandBatch *)pMsg;
|
|
|
|
pClient->SetTurnLength(1, msg->m_TurnLength);
|
2007-11-18 10:09:06 +01:00
|
|
|
|
|
|
|
//debug_printf("Got end of batch, setting NewTurnPending\n");
|
|
|
|
pClient->m_TurnPending = true;
|
|
|
|
// We will ack the turn when our simulation calls NewTurn.
|
2004-09-21 16:40:43 +02:00
|
|
|
|
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
2005-10-07 17:24:29 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
UNHANDLED(pMsg);
|
|
|
|
}
|
2004-09-21 16:40:43 +02:00
|
|
|
}
|
|
|
|
|
2007-11-18 10:09:06 +01:00
|
|
|
void CNetClient::QueueIncomingMessage(CNetMessage *pMsg)
|
|
|
|
{
|
|
|
|
CScopeLock lock(m_Mutex);
|
|
|
|
debug_printf("Got a command! queueing it to 2 turns from now\n");
|
|
|
|
QueueMessage(2, pMsg);
|
|
|
|
}
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
bool CNetClient::ChatHandler(CNetMessage *pMsg, CNetSession *pSession)
|
|
|
|
{
|
|
|
|
CNetClient *pClient=(CNetClient *)pSession;
|
|
|
|
switch (pMsg->GetType())
|
|
|
|
{
|
|
|
|
case NMT_ChatMessage:
|
|
|
|
{
|
|
|
|
CChatMessage *msg=(CChatMessage *)pMsg;
|
|
|
|
g_Console->ReceivedChatMessage(msg->m_Sender, msg->m_Message);
|
2005-01-17 05:52:02 +01:00
|
|
|
if (pClient->m_OnChat.Defined())
|
|
|
|
{
|
|
|
|
CChatEvent evt(msg->m_Sender, msg->m_Message);
|
|
|
|
pClient->m_OnChat.DispatchEvent(pClient->GetScript(), &evt);
|
|
|
|
}
|
2004-08-16 17:19:17 +02:00
|
|
|
HANDLED(pMsg);
|
|
|
|
}
|
2006-01-29 19:23:47 +01:00
|
|
|
default:
|
|
|
|
UNHANDLED(pMsg);
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
void CNetClient::OnClientConnect(int sessionID, const CStrW& name)
|
2005-02-21 18:13:31 +01:00
|
|
|
{
|
|
|
|
// Find existing server session, if any, and delete it
|
|
|
|
SessionMap::iterator it=m_ServerSessions.find(sessionID);
|
|
|
|
if (it != m_ServerSessions.end())
|
|
|
|
{
|
|
|
|
delete it->second;
|
|
|
|
m_ServerSessions.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert new serversession
|
|
|
|
m_ServerSessions[sessionID]=new CServerSession(sessionID, name);
|
|
|
|
|
|
|
|
// Call JS Callback
|
|
|
|
if (m_OnClientConnect.Defined())
|
|
|
|
{
|
|
|
|
CClientConnectEvent evt(sessionID, name);
|
|
|
|
m_OnClientConnect.DispatchEvent(GetScript(), &evt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNetClient::OnClientDisconnect(int sessionID)
|
|
|
|
{
|
|
|
|
SessionMap::iterator it=m_ServerSessions.find(sessionID);
|
|
|
|
if (it == m_ServerSessions.end())
|
|
|
|
{
|
2007-12-29 17:22:23 +01:00
|
|
|
LOG(CLogger::Warning, LOG_CAT_NET, "Server said session %d disconnected. I don't know of such a session.");
|
2005-02-21 18:13:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call JS Callback
|
|
|
|
if (m_OnClientConnect.Defined())
|
|
|
|
{
|
|
|
|
CClientDisconnectEvent evt(it->second->m_SessionID, it->second->m_Name);
|
|
|
|
m_OnClientConnect.DispatchEvent(GetScript(), &evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete server session and remove from map
|
|
|
|
delete it->second;
|
|
|
|
m_ServerSessions.erase(it);
|
|
|
|
}
|
|
|
|
|
2007-06-04 09:41:05 +02:00
|
|
|
void CNetClient::OnStartGameMessage()
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
2004-09-21 16:40:43 +02:00
|
|
|
m_pMessageHandler=InGameHandler;
|
2007-06-04 09:41:05 +02:00
|
|
|
debug_assert( m_OnStartGame.Defined() );
|
|
|
|
CStartGameEvent evt;
|
|
|
|
m_OnStartGame.DispatchEvent(GetScript(), &evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CNetClient::StartGame()
|
|
|
|
{
|
|
|
|
if (m_pGame->StartGame(m_pGameAttributes) != PSRETURN_OK)
|
|
|
|
{
|
2007-11-18 10:09:06 +01:00
|
|
|
// TODO: Send a failed-to-launch-game message and drop out.
|
2007-06-04 09:41:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
2005-01-17 05:52:02 +01:00
|
|
|
{
|
2007-11-18 10:09:06 +01:00
|
|
|
debug_printf("Client StartGame - sending end-of-batch ack\n");
|
|
|
|
// Send an end-of-batch message for turn 0 to signal that we're ready.
|
|
|
|
CEndCommandBatch *pMsg=new CEndCommandBatch();
|
2008-01-03 23:07:18 +01:00
|
|
|
pMsg->m_TurnLength=1000/g_frequencyFilter->StableFrequency();
|
2007-11-18 10:09:06 +01:00
|
|
|
Push(pMsg);
|
2007-06-04 09:41:05 +02:00
|
|
|
return 0;
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
|
2007-06-04 09:41:05 +02:00
|
|
|
CPlayer* CNetClient::GetLocalPlayer()
|
|
|
|
{
|
|
|
|
return m_pLocalPlayerSlot->GetPlayer();
|
|
|
|
}
|
|
|
|
|
2007-11-18 10:09:06 +01:00
|
|
|
bool CNetClient::NewTurnReady()
|
|
|
|
{
|
|
|
|
return m_TurnPending;
|
|
|
|
}
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
void CNetClient::NewTurn()
|
|
|
|
{
|
2007-11-18 10:09:06 +01:00
|
|
|
CScopeLock lock(m_Mutex);
|
|
|
|
|
2004-08-16 17:19:17 +02:00
|
|
|
RotateBatches();
|
|
|
|
ClearBatch(2);
|
2007-11-18 10:09:06 +01:00
|
|
|
m_TurnPending = false;
|
2004-08-16 17:19:17 +02:00
|
|
|
|
2007-11-18 10:09:06 +01:00
|
|
|
//debug_printf("In NewTurn - sending ack\n");
|
2005-01-17 05:52:02 +01:00
|
|
|
CEndCommandBatch *pMsg=new CEndCommandBatch();
|
2008-01-03 23:07:18 +01:00
|
|
|
pMsg->m_TurnLength=1000/g_frequencyFilter->StableFrequency(); // JW: it'd probably be nicer to get the FPS as a parameter
|
2005-01-17 05:52:02 +01:00
|
|
|
Push(pMsg);
|
2004-08-16 17:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNetClient::QueueLocalCommand(CNetMessage *pMsg)
|
|
|
|
{
|
2004-09-21 16:40:43 +02:00
|
|
|
// Don't save these locally, since they'll be bounced by the server anyway
|
2007-11-18 10:09:06 +01:00
|
|
|
//debug_printf("Sending command from client\n");
|
2004-08-16 17:19:17 +02:00
|
|
|
Push(pMsg);
|
|
|
|
}
|