Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NetClient.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef NETCLIENT_H
19 #define NETCLIENT_H
20 
21 #include "network/fsm.h"
23 #include "network/NetHost.h"
25 
26 #include "ps/CStr.h"
27 
28 #include <deque>
29 
30 class CGame;
31 class CNetClientSession;
33 class CNetServer;
34 class ScriptInterface;
35 
36 // NetClient session FSM states
37 enum
38 {
48 };
49 
50 /**
51  * Network client.
52  * This code is run by every player (including the host, if they are not
53  * a dedicated server).
54  * It provides an interface between the GUI, the network (via CNetClientSession),
55  * and the game (via CGame and CNetClientTurnManager).
56  */
57 class CNetClient : public CFsm
58 {
60 
62 
63 public:
64  /**
65  * Construct a client associated with the given game object.
66  * The game must exist for the lifetime of this object.
67  */
68  CNetClient(CGame* game);
69 
70  virtual ~CNetClient();
71 
72  /**
73  * Set the user's name that will be displayed to all players.
74  * This must not be called after the connection setup.
75  */
76  void SetUserName(const CStrW& username);
77 
78  /**
79  * Set up a connection to the remote networked server.
80  * @param server IP address or host name to connect to
81  * @return true on success, false on connection failure
82  */
83  bool SetupConnection(const CStr& server);
84 
85  /**
86  * Destroy the connection to the server.
87  * This client probably cannot be used again.
88  */
89  void DestroyConnection();
90 
91  /**
92  * Poll the connection for messages from the server and process them, and send
93  * any queued messages.
94  * This must be called frequently (i.e. once per frame).
95  */
96  void Poll();
97 
98  /**
99  * Flush any queued outgoing network messages.
100  * This should be called soon after sending a group of messages that may be batched together.
101  */
102  void Flush();
103 
104  /**
105  * Retrieves the next queued GUI message, and removes it from the queue.
106  * The returned value is in the GetScriptInterface() JS context.
107  *
108  * This is the only mechanism for the networking code to send messages to
109  * the GUI - it is pull-based (instead of push) so the engine code does not
110  * need to know anything about the code structure of the GUI scripts.
111  *
112  * The structure of the messages is <code>{ "type": "...", ... }</code>.
113  * The exact types and associated data are not specified anywhere - the
114  * implementation and GUI scripts must make the same assumptions.
115  *
116  * @return next message, or the value 'undefined' if the queue is empty
117  */
119 
120  /**
121  * Add a message to the queue, to be read by GuiPoll.
122  * The script value must be in the GetScriptInterface() JS context.
123  */
124  void PushGuiMessage(const CScriptValRooted& message);
125 
126  /**
127  * Return a concatenation of all messages in the GUI queue,
128  * for test cases to easily verify the queue contents.
129  */
130  std::wstring TestReadGuiMessages();
131 
132  /**
133  * Get the script interface associated with this network client,
134  * which is equivalent to the one used by the CGame in the constructor.
135  */
137 
138  /**
139  * Send a message to the server.
140  * @param message message to send
141  * @return true on success
142  */
143  bool SendMessage(const CNetMessage* message);
144 
145  /**
146  * Call when the network connection has been successfully initiated.
147  */
148  void HandleConnect();
149 
150  /**
151  * Call when the network connection has been lost.
152  */
153  void HandleDisconnect(u32 reason);
154 
155  /**
156  * Call when a message has been received from the network.
157  */
158  bool HandleMessage(CNetMessage* message);
159 
160  /**
161  * Call when the game has started and all data files have been loaded,
162  * to signal to the server that we are ready to begin the game.
163  */
164  void LoadFinished();
165 
166  void SendChatMessage(const std::wstring& text);
167 
168 private:
169  // Net message / FSM transition handlers
170  static bool OnConnect(void* context, CFsmEvent* event);
171  static bool OnHandshake(void* context, CFsmEvent* event);
172  static bool OnHandshakeResponse(void* context, CFsmEvent* event);
173  static bool OnAuthenticate(void* context, CFsmEvent* event);
174  static bool OnChat(void* context, CFsmEvent* event);
175  static bool OnGameSetup(void* context, CFsmEvent* event);
176  static bool OnPlayerAssignment(void* context, CFsmEvent* event);
177  static bool OnInGame(void* context, CFsmEvent* event);
178  static bool OnGameStart(void* context, CFsmEvent* event);
179  static bool OnJoinSyncStart(void* context, CFsmEvent* event);
180  static bool OnJoinSyncEndCommandBatch(void* context, CFsmEvent* event);
181  static bool OnLoadedGame(void* context, CFsmEvent* event);
182 
183  /**
184  * Take ownership of a session object, and use it for all network communication.
185  */
186  void SetAndOwnSession(CNetClientSession* session);
187 
188  /**
189  * Push a message onto the GUI queue listing the current player assignments.
190  */
192 
194  CStrW m_UserName;
195 
196  /// Current network session (or NULL if not connected)
198 
199  /// Turn manager associated with the current game (or NULL if we haven't started the game yet)
201 
202  /// Unique-per-game identifier of this client, used to identify the sender of simulation commands
204 
205  /// Latest copy of game setup attributes heard from the server
207 
208  /// Latest copy of player assignments heard from the server
210 
211  /// Globally unique identifier to distinguish users beyond the lifetime of a single network session
212  CStr m_GUID;
213  /// Initialise m_GUID with a random value
214  CStr GenerateGUID();
215 
216  /// Queue of messages for GuiPoll
217  std::deque<CScriptValRooted> m_GuiMessageQueue;
218 
219  /// Serialized game state received when joining an in-progress game
220  std::string m_JoinSyncBuffer;
221 };
222 
223 /// Global network client for the standard game
224 extern CNetClient *g_NetClient;
225 
226 #endif // NETCLIENT_H
The container that holds the rules, resources and attributes of the game.
Definition: Game.h:39
ScriptInterface & GetScriptInterface()
Get the script interface associated with this network client, which is equivalent to the one used by ...
Definition: NetClient.cpp:191
void DestroyConnection()
Destroy the connection to the server.
Definition: NetClient.cpp:144
Manages states, events, actions and transitions between states.
Definition: fsm.h:117
std::map< CStr, PlayerAssignment > PlayerAssignmentMap
Definition: NetHost.h:51
CNetClient * g_NetClient
Global network client for the standard game.
Definition: NetClient.cpp:37
CStr GenerateGUID()
Initialise m_GUID with a random value.
Definition: NetClient.cpp:559
CNetClientTurnManager * m_ClientTurnManager
Turn manager associated with the current game (or NULL if we haven&#39;t started the game yet) ...
Definition: NetClient.h:200
Represents a signal in the state machine that a change has occurred.
Definition: fsm.h:53
virtual ~CNetClient()
Definition: NetClient.cpp:118
CNetClientSession * m_Session
Current network session (or NULL if not connected)
Definition: NetClient.h:197
void LoadFinished()
Call when the game has started and all data files have been loaded, to signal to the server that we a...
Definition: NetClient.cpp:293
static bool OnChat(void *context, CFsmEvent *event)
Definition: NetClient.cpp:398
CScriptValRooted GuiPoll()
Retrieves the next queued GUI message, and removes it from the queue.
Definition: NetClient.cpp:161
void PushGuiMessage(const CScriptValRooted &message)
Add a message to the queue, to be read by GuiPoll.
Definition: NetClient.cpp:171
static bool OnHandshake(void *context, CFsmEvent *event)
Definition: NetClient.cpp:346
static bool OnLoadedGame(void *context, CFsmEvent *event)
Definition: NetClient.cpp:513
static bool OnInGame(void *context, CFsmEvent *event)
Definition: NetClient.cpp:530
void HandleConnect()
Call when the network connection has been successfully initiated.
Definition: NetClient.cpp:224
static bool OnAuthenticate(void *context, CFsmEvent *event)
Definition: NetClient.cpp:376
void HandleDisconnect(u32 reason)
Call when the network connection has been lost.
Definition: NetClient.cpp:229
void Flush()
Flush any queued outgoing network messages.
Definition: NetClient.cpp:155
void PostPlayerAssignmentsToScript()
Push a message onto the GUI queue listing the current player assignments.
Definition: NetClient.cpp:196
bool SendMessage(const CNetMessage *message)
Send a message to the server.
Definition: NetClient.cpp:216
std::deque< CScriptValRooted > m_GuiMessageQueue
Queue of messages for GuiPoll.
Definition: NetClient.h:217
Various declarations shared by networking code.
CNetClient(CGame *game)
Construct a client associated with the given game object.
Definition: NetClient.cpp:68
static bool OnConnect(void *context, CFsmEvent *event)
Definition: NetClient.cpp:333
PlayerAssignmentMap m_PlayerAssignments
Latest copy of player assignments heard from the server.
Definition: NetClient.h:209
void SetAndOwnSession(CNetClientSession *session)
Take ownership of a session object, and use it for all network communication.
Definition: NetClient.cpp:138
CGame * m_Game
Definition: NetClient.h:193
static bool OnPlayerAssignment(void *context, CFsmEvent *event)
Definition: NetClient.cpp:433
u32 m_HostID
Unique-per-game identifier of this client, used to identify the sender of simulation commands...
Definition: NetClient.h:203
void SetUserName(const CStrW &username)
Set the user&#39;s name that will be displayed to all players.
Definition: NetClient.cpp:123
CStrW m_UserName
Definition: NetClient.h:194
static bool OnJoinSyncEndCommandBatch(void *context, CFsmEvent *event)
Definition: NetClient.cpp:497
Network server interface.
Definition: NetServer.h:97
static bool OnGameSetup(void *context, CFsmEvent *event)
Definition: NetClient.cpp:415
CStr m_GUID
Globally unique identifier to distinguish users beyond the lifetime of a single network session...
Definition: NetClient.h:212
void Poll()
Poll the connection for messages from the server and process them, and send any queued messages...
Definition: NetClient.cpp:149
std::string m_JoinSyncBuffer
Serialized game state received when joining an in-progress game.
Definition: NetClient.h:220
CScriptValRooted m_GameAttributes
Latest copy of game setup attributes heard from the server.
Definition: NetClient.h:206
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:32
bool HandleMessage(CNetMessage *message)
Call when a message has been received from the network.
Definition: NetClient.cpp:250
static bool OnJoinSyncStart(void *context, CFsmEvent *event)
Definition: NetClient.cpp:483
bool SetupConnection(const CStr &server)
Set up a connection to the remote networked server.
Definition: NetClient.cpp:130
Async task for receiving the initial game state when rejoining an in-progress network game...
Definition: NetClient.cpp:43
#define u32
Definition: types.h:41
Network client.
Definition: NetClient.h:57
void SendChatMessage(const std::wstring &text)
Definition: NetClient.cpp:243
Implementation of CNetTurnManager for network clients.
static bool OnGameStart(void *context, CFsmEvent *event)
Definition: NetClient.cpp:459
Abstraction around a SpiderMonkey JSContext.
The client end of a network session.
Definition: NetSession.h:55
std::wstring TestReadGuiMessages()
Return a concatenation of all messages in the GUI queue, for test cases to easily verify the queue co...
Definition: NetClient.cpp:178
NONCOPYABLE(CNetClient)
static bool OnHandshakeResponse(void *context, CFsmEvent *event)
Definition: NetClient.cpp:361