18 #include "precompiled.h"
60 CGameStartMessage start;
70 m_UserName(L
"anonymous"),
71 m_GUID(GenerateGUID()), m_HostID((
u32)-1), m_ClientTurnManager(NULL), m_Game(game)
246 chat.m_Message = text;
262 CFileTransferRequestMessage* reqMessage = (CFileTransferRequestMessage*)message;
267 std::stringstream stream;
271 stream.write((
char*)&turn,
sizeof(turn));
278 std::string compressed;
303 std::stringstream stream(state);
306 stream.read((
char*)&turn,
sizeof(turn));
309 LOGMESSAGE(L
"Rejoining client deserializing state at turn %u\n", turn);
328 CLoadedGameMessage loaded;
352 CCliHandshakeMessage handshake;
367 CAuthenticateMessage authenticate;
370 authenticate.m_Password = L
"";
382 CAuthenticateResultMessage* message = (CAuthenticateResultMessage*)event->
GetParamRef();
384 LOGMESSAGE(L
"Net: Authentication result: host=%u, %ls", message->m_HostID, message->m_Message.c_str());
388 client->
m_HostID = message->m_HostID;
404 CChatMessage* message = (CChatMessage*)event->
GetParamRef();
439 CPlayerAssignmentMessage* message = (CPlayerAssignmentMessage*)event->
GetParamRef();
443 for (
size_t i = 0; i < message->m_Hosts.size(); ++i)
447 assignment.
m_Name = message->m_Hosts[i].m_Name;
448 assignment.
m_PlayerID = message->m_Hosts[i].m_PlayerID;
449 newPlayerAssignments[message->m_Hosts[i].m_GUID] = assignment;
503 CEndCommandBatchMessage* endMessage = (CEndCommandBatchMessage*)event->
GetParamRef();
546 CSyncErrorMessage* syncMessage =
static_cast<CSyncErrorMessage*
> (message);
551 CEndCommandBatchMessage* endMessage =
static_cast<CEndCommandBatchMessage*
> (message);
569 for (
size_t i = 0; i < 2; ++i)
#define PS_PROTOCOL_VERSION
The container that holds the rules, resources and attributes of the game.
ScriptInterface & GetScriptInterface()
Get the script interface associated with this network client, which is equivalent to the one used by ...
void DestroyConnection()
Destroy the connection to the server.
std::map< CStr, PlayerAssignment > PlayerAssignmentMap
Simple (non-streaming) compression functions.
virtual void OnComplete()
Called when m_Buffer contains the full received data.
CNetClient * g_NetClient
Global network client for the standard game.
CStr GenerateGUID()
Initialise m_GUID with a random value.
CNetClientTurnManager * m_ClientTurnManager
Turn manager associated with the current game (or NULL if we haven't started the game yet) ...
Represents a signal in the state machine that a change has occurred.
CNetClientSession * m_Session
Current network session (or NULL if not connected)
bool DeserializeState(std::istream &stream)
void Poll()
Process queued incoming messages.
void LoadFinished()
Call when the game has started and all data files have been loaded, to signal to the server that we a...
static bool OnChat(void *context, CFsmEvent *event)
i32 m_PlayerID
The player that the given host controls, or -1 if none (observer)
CScriptValRooted GuiPoll()
Retrieves the next queued GUI message, and removes it from the queue.
CNetFileReceiveTask_ClientRejoin(CNetClient &client)
void PushGuiMessage(const CScriptValRooted &message)
Add a message to the queue, to be read by GuiPoll.
static bool OnHandshake(void *context, CFsmEvent *event)
static bool OnLoadedGame(void *context, CFsmEvent *event)
static bool OnInGame(void *context, CFsmEvent *event)
NONCOPYABLE(CNetFileReceiveTask_ClientRejoin)
int sprintf_s(char *buf, size_t max_chars, const char *fmt,...) PRINTF_ARGS(3)
void HandleConnect()
Call when the network connection has been successfully initiated.
void StartTask(const shared_ptr< CNetFileReceiveTask > &task)
Registers a file-receiving task.
static bool OnAuthenticate(void *context, CFsmEvent *event)
void HandleDisconnect(u32 reason)
Call when the network connection has been lost.
void Flush()
Flush any queued outgoing network messages.
void PostPlayerAssignmentsToScript()
Push a message onto the GUI queue listing the current player assignments.
bool SendMessage(const CNetMessage *message)
Send a message to the server.
void StartResponse(u32 requestID, const std::string &data)
Registers data to be sent in response to a request.
std::deque< CScriptValRooted > m_GuiMessageQueue
Queue of messages for GuiPoll.
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Status HandleMessageReceive(const CNetMessage *message)
Should be called when a message is received from the network.
void SetPlayerID(int playerID)
CNetClient(CGame *game)
Construct a client associated with the given game object.
static bool OnConnect(void *context, CFsmEvent *event)
void SetTurnManager(CNetTurnManager *turnManager)
Replace the current turn manager.
PlayerAssignmentMap m_PlayerAssignments
Latest copy of player assignments heard from the server.
void SetAndOwnSession(CNetClientSession *session)
Take ownership of a session object, and use it for all network communication.
Asynchronous file-receiving task.
static bool OnPlayerAssignment(void *context, CFsmEvent *event)
virtual bool SendMessage(const CNetMessage *message)
Send a message to the server.
u32 m_HostID
Unique-per-game identifier of this client, used to identify the sender of simulation commands...
virtual void OnSyncError(u32 turn, const std::string &expectedHash)
Called when there has been an out-of-sync error.
void ResetState(u32 newCurrentTurn, u32 newReadyTurn)
void Flush()
Flush queued outgoing network messages.
Special message type for updated to game startup settings.
void StartGame(const CScriptValRooted &attribs, const std::string &savedState)
void SetUserName(const CStrW &username)
Set the user's name that will be displayed to all players.
ScriptInterface & GetScriptInterface() const
#define SAFE_DELETE(p)
delete memory ensuing from new and set the pointer to zero (thus making double-frees safe / a no-op) ...
bool undefined() const
Returns whether the value is uninitialised or is JSVAL_VOID.
bool Eval(const char *code)
static bool OnJoinSyncEndCommandBatch(void *context, CFsmEvent *event)
static bool OnGameSetup(void *context, CFsmEvent *event)
i64 Status
Error handling system.
CStr m_GUID
Globally unique identifier to distinguish users beyond the lifetime of a single network session...
bool Update(unsigned int eventType, void *pEventData)
void Poll()
Poll the connection for messages from the server and process them, and send any queued messages...
std::string m_JoinSyncBuffer
Serialized game state received when joining an in-progress game.
void SetCurrState(unsigned int state)
Status sys_generate_random_bytes(u8 *buf, size_t count)
generate high-quality random bytes.
NetMessageType GetType() const
Retrieves the message type.
bool SerializeState(std::ostream &stream)
CScriptValRooted m_GameAttributes
Latest copy of game setup attributes heard from the server.
The base class for all network messages exchanged within the game.
CNetFileTransferer & GetFileTransferer()
void FinishedAllCommands(u32 turn, u32 turnLength)
Called when all commands for a given turn have been received.
CSimulation2 * GetSimulation2()
Get the pointer to the simulation2 object.
bool GetProperty(jsval obj, const char *name, T &out)
Get the named property on the given object.
void CompressZLib(const std::string &data, std::string &out, bool includeLengthHeader)
bool HandleMessage(CNetMessage *message)
Call when a message has been received from the network.
static bool OnJoinSyncStart(void *context, CFsmEvent *event)
bool SetupConnection(const CStr &server)
Set up a connection to the remote networked server.
bool m_Enabled
Whether the player is currently connected and active.
unsigned int GetCurrState(void) const
#define PS_PROTOCOL_MAGIC_RESPONSE
Async task for receiving the initial game state when rejoining an in-progress network game...
CFsmTransition * AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState)
void SendChatMessage(const std::wstring &text)
bool UpdateFastForward()
Advance the simulation by as much as possible.
Implementation of CNetTurnManager for network clients.
static bool OnGameStart(void *context, CFsmEvent *event)
void DecompressZLib(const std::string &data, std::string &out, bool includeLengthHeader)
Network client/server sessions.
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
virtual void OnSimulationMessage(CSimulationMessage *msg)
Called by networking code when a simulation message is received.
Abstraction around a SpiderMonkey JSContext.
std::wstring ToString(jsval obj, bool pretty=false)
bool SetProperty(jsval obj, const char *name, const T &value, bool constant=false, bool enumerate=true)
Set the named property on the given object.
unsigned int GetType(void) const
void SetFirstState(unsigned int firstState)
The client end of a network session.
friend class CNetFileReceiveTask_ClientRejoin
bool Connect(u16 port, const CStr &server)
IReplayLogger & GetReplayLogger() const
std::wstring TestReadGuiMessages()
Return a concatenation of all messages in the GUI queue, for test cases to easily verify the queue co...
Special message type for simulation commands.
static bool OnHandshakeResponse(void *context, CFsmEvent *event)