Debian Codegraph @

src/networking/networking.h

raw symbols SCIP
1 /***************************************************************************
2 networking.h - description
3 -------------------
4 begin : dec 23th, 2004
5 copyright : (C) 2004-2006 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: networking.h 450 2010-11-21 19:11:43Z neoneurone $
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * any later version. *
17 * *
18 ***************************************************************************/
19
20 #ifndef _OPENCITY_NETWORKING_H_
21 #define _OPENCITY_NETWORKING_H_ 1
22
23 #include "main.h"
24 #include "SDL_net.h"
25
26 #include <vector>
27
28
29 //========================================================================
30 /** The following codes are used to describe the various networking
31 operations status.
32 */
33 enum OPENCITY_NET_CODE {
34 OC_NET_OK = 0,
35 OC_NET_ERROR,
36 OC_NET_NODATA,
37 OC_NET_NOCLIENT, ///< No waiting client
38 OC_NET_UNDEFINED,
39 OC_NET_SENDERROR, ///< Send error
40 OC_NET_RECVERROR, ///< Receive error
41 OC_NET_BUFFERROR, ///< Buffer error
42 OC_NET_COMDERROR, ///< Command error
43 OC_NET_PINGTMOUT, ///< Ping timeout
44
45 OC_NET_CLIENT_ALONE, ///< Stand-alone client the simulator and data are on a server
46 OC_NET_CLIENT_SERVER, ///< Client running a server
47
48 OC_NET_CLIENT_CONNECTED, ///< Already connected
49 OC_NET_CLIENT_NOTCONNECTED, ///< Not connected
50 OC_NET_CLIENT_ACCEPTED, ///< The client has been accepted
51 OC_NET_CLIENT_REJECTED, ///< The client has been rejected: server full/black list
52 OC_NET_CLIENT_TIMEOUT, ///< The client has timeouted
53
54 OC_NET_SERVER_ALONE, ///< Stand-alone server
55 OC_NET_SERVER_DISTRIBUTED, ///< Distributed server
56
57 OC_NET_SERVER_READY, ///< The server is up and running
58 OC_NET_SERVER_FULL, ///< The server is full
59 OC_NET_SERVER_STARTED, ///< The server has been started already
60 OC_NET_SERVER_STOPED, ///< The server is not running
61
62 OC_NET_PING, ///< Only server sends this to its clients
63 OC_NET_PONG, ///< Only client sends this to the server
64 OC_NET_DISCONNECT,
65 OC_NET_GETVER, ///< Get version command
66 OC_NET_GETPRO
67 };
68
69
70 //========================================================================
71 /** The following commands are describe in the design document. Please
72 read it for more details. Any OpenCity host should use those commands
73 instead of hard coding them.
74 */
75 enum OPENCITY_NET_CMD {
76 OC_NET_CNT = 0,
77 OC_NET_DNT,
78 OC_NET_ACK,
79 OC_NET_NCK,
80 OC_NET_PIN,
81 OC_NET_PON,
82 OC_NET_MSG,
83 OC_NET_CMD_NUMBER
84 };
85
86 const char OPENCITY_NET_CMD_ARRAY [OC_NET_CMD_NUMBER][4] = {
87 "CNT", "DNT",
88 "ACK", "NCK",
89 "PIN", "PON",
90 "MSG"
91 };
92
93
94 #define OC_NET_CHECK_TIMEOUT 50 // Socket set check timeout
95 #define OC_NET_CLIENT_MAX 1 // Maximum number of clients
96 #define OC_NET_PING_TIMEOUT 2000 // Used for PIN & PON
97 #define OC_NET_PROTOCOL "0.0.4" // Current protocol
98 #define OC_NET_SERVER_PORT 0xBEEF // La vache !
99
100 #define OC_NET_COMMAND_LENGTH 16
101 #define OC_NET_DATA_LENGTH 1024
102
103
104 class Netnode; // forward declaration
105 typedef std::vector<Netnode>::iterator ClientIterator;
106
107
108 //========================================================================
109 /** Define the network message's structure for easy use. See OpenCity's
110 design documentation for more details about each member.
111 */
112 struct NetMessage {
113 OPENCITY_NET_CMD cmd; ///< Command opcode
114 uint dataLength; ///< Data length
115 char data [OC_NET_DATA_LENGTH];
116 };
117
118
119 //========================================================================
120 /** Offer the OpenCity gamers a possibility to share their experience
121 with the others through the networks.
122 */
123 class Networking {
124 public:
125 Networking();
126 ~Networking();
127
128
129 //========================================================================
130 /**
131 \return the host (resolved IP) of the specified client
132 */
133 const string
134 GetClientHost( const uint index );
135
136
137 //========================================================================
138 /**
139 \return the current number of active connections
140 */
141 const uint
142 GetClientNum();
143
144
145 //========================================================================
146 /**
147 \return the maximum number of the connections that the server can handle.
148 If the queried machine is running OpenCity as a client application,
149 the returned value is undefined.
150 */
151 const uint
152 GetClientMax();
153
154
155 //========================================================================
156 const OPENCITY_NET_CODE
157 GetMachineRole();
158
159
160 //========================================================================
161 /**
162 \return the maximum ping time in millisecond.
163 */
164 const uint
165 GetPingTimeout();
166
167
168 //========================================================================
169 /**
170 \return the number of servers that the machine knows
171 */
172 const uint
173 GetServerNum();
174
175
176 //========================================================================
177 /**
178 \return the host (resolved IP) of the specified server
179 */
180 const string
181 GetServerHost( const uint index );
182
183
184 //========================================================================
185 /**
186 \return the string which describes the OpenCity version
187 of the remote machine.
188 Example: 0.0.2beta
189 */
190 const string
191 GetVersion();
192
193
194 //========================================================================
195 /**
196 \return the string which describes the latest OpenCity
197 networking protocol that the machine can understand.
198 The returned string is in the form: x.y.z like 0.0.1
199 */
200 const string
201 GetProtocol();
202
203
204 //========================================================================
205 /**
206 SERVER: Accept the connection request from a machine.
207 \param rid The index of the newly accepted client (>=0)
208 */
209 const OPENCITY_NET_CODE
210 Accept( uint & rid );
211
212
213 //========================================================================
214 /**
215 SERVER: Reject the connection request from a machine.
216 */
217 const OPENCITY_NET_CODE
218 Reject( const uint id );
219
220
221 //========================================================================
222 /**
223 SERVER: Create a new socket and begin to listen for incoming
224 connection requests. Return the following enumerations:
225 OC_SERVER_READY
226 OC_SERVER_STARTED
227 */
228 const OPENCITY_NET_CODE
229 StartServer( const OC_USHORT port = OC_NET_SERVER_PORT );
230
231
232 //========================================================================
233 /**
234 SERVER: close the server socket and free all server's ressources.
235 \return the following enumerations:
236 OC_SERVER_STOPED
237 */
238 const OPENCITY_NET_CODE
239 StopServer();
240
241
242 //========================================================================
243 /**
244 SERVER: check the avaibility of data sent by clients and process them.
245 This is a non-blocking call
246 \return Always return OC_NET_OK
247 */
248 const OPENCITY_NET_CODE
249 ProcessServerData();
250
251
252 //========================================================================
253 /**
254 SERVER: try to connect to a another server
255 CLIENT: try to connect to a server
256 \return the following enumerations:
257 */
258 const OPENCITY_NET_CODE
259 Open(
260 const string serverHost,
261 const OC_USHORT port = OC_NET_SERVER_PORT );
262
263
264 //========================================================================
265 /**
266 SERVER: close the connection to a super server
267 CLIENT: close the connection to a server
268 \return the following enumerations:
269 */
270 const OPENCITY_NET_CODE
271 Close();
272
273
274 //========================================================================
275 /**
276 SERVER: close the connection to a client
277 CLIENT: this command is not available
278 \return the following enumerations:
279 */
280 const OPENCITY_NET_CODE
281 Close( ClientIterator i );
282
283
284 //========================================================================
285 /**
286 SERVER: send the data to a super server
287 CLIENT: send the data to the server
288 \return the following enumerations:
289 */
290 const OPENCITY_NET_CODE
291 Send(
292 const void* const data,
293 const uint len );
294
295
296 //========================================================================
297 /**
298 SERVER: send the data to a client
299 CLIENT: this method is _not_ available to a client machine
300 \return the following enumerations:
301 */
302 const OPENCITY_NET_CODE
303 Send(
304 const void* const data,
305 const uint len,
306 const uint cid );
307
308
309 //========================================================================
310 /**
311 SERVER: send the message to a super server
312 CLIENT: send the message to a server
313 */
314 const OPENCITY_NET_CODE
315 SendMessage( NetMessage & rMsg );
316
317
318 //========================================================================
319 /**
320 SERVER: send the message to a super server
321 CLIENT: this method is _not_ available to a client host
322 */
323 const OPENCITY_NET_CODE
324 SendMessage(
325 NetMessage & rMsg,
326 const uint cid
327 );
328
329
330 //========================================================================
331 /**
332 SERVER: read the data sent by a super server
333 CLIENT: read the data sent by the server
334 \return the following enumerations:
335 */
336 const OPENCITY_NET_CODE
337 Receive(
338 void* const data,
339 const uint maxlen );
340
341
342 //========================================================================
343 /** Wait for the specified incoming command.
344 SERVER: wait for the command sent by a super server
345 CLIENT: wait for the command sent by a server
346 \note Blocking method
347 */
348 const OPENCITY_NET_CODE
349 ReceiveMessage( NetMessage & rMsg );
350
351
352 //========================================================================
353 /**
354 SERVER: read the data sent by a super server specified by its index
355 CLIENT: this method is _not_ available to a client
356 \return Error code
357 */
358 const OPENCITY_NET_CODE
359 Receive(
360 void* const data,
361 const uint maxlen,
362 const uint cid );
363
364
365 private:
366 OPENCITY_NET_CODE _networkingCode;
367 bool _boolNetworkInitialized;
368
369 std::vector<Netnode> _vClient; ///< The vector which holds the informations about the clients
370
371 SDLNet_SocketSet _pSocketSet; ///< The server puts all its client sockets into this set
372
373 /** The server listens on this socket. The client use this socket to communicate with the server */
374 TCPsocket _pServerSocket;
375
376
377 };
378 #endif
379
380
381
382
383
384
385
386
387