00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _OPENCITY_NETWORKING_H_
00021 #define _OPENCITY_NETWORKING_H_ 1
00022
00023 #include "main.h"
00024 #include "SDL_net.h"
00025
00026 #include <vector>
00027
00028 enum OPENCITY_NET_CODE {
00029 OC_NET_OK = 0,
00030 OC_NET_ERROR,
00031 OC_NET_NODATA,
00032 OC_NET_NOCLIENT,
00033 OC_NET_UNDEFINED,
00034 OC_NET_SENDERROR,
00035 OC_NET_RECVERROR,
00036 OC_NET_BUFFERROR,
00037 OC_NET_COMDERROR,
00038 OC_NET_PINGTMOUT,
00039
00040 OC_NET_CLIENT_ALONE,
00041 OC_NET_CLIENT_SERVER,
00042
00043 OC_NET_CLIENT_CONNECTED,
00044 OC_NET_CLIENT_NOTCONNECTED,
00045 OC_NET_CLIENT_ACCEPTED,
00046 OC_NET_CLIENT_REJECTED,
00047 OC_NET_CLIENT_TIMEOUT,
00048
00049 OC_NET_SERVER_ALONE,
00050 OC_NET_SERVER_DISTRIBUTED,
00051
00052 OC_NET_SERVER_READY,
00053 OC_NET_SERVER_FULL,
00054 OC_NET_SERVER_STARTED,
00055 OC_NET_SERVER_STOPED,
00056
00057 OC_NET_PING,
00058 OC_NET_PONG,
00059 OC_NET_DISCONNECT,
00060 OC_NET_GETVER,
00061 OC_NET_GETPRO
00062 };
00063
00064
00065
00070 enum OPENCITY_NET_CMD {
00071 OC_NET_CNT = 0,
00072 OC_NET_DNT,
00073 OC_NET_ACK,
00074 OC_NET_NCK,
00075 OC_NET_PIN,
00076 OC_NET_PON,
00077 OC_NET_MSG,
00078 OC_NET_CMD_NUMBER
00079 };
00080
00081 const OC_CHAR OPENCITY_NET_CMD_ARRAY [OC_NET_CMD_NUMBER][4] = {
00082 "CNT", "DNT",
00083 "ACK", "NCK",
00084 "PIN", "PON",
00085 "MSG"
00086 };
00087
00088
00089 #define OC_NET_CHECK_TIMEOUT 50 // Socket set check timeout
00090 #define OC_NET_CLIENT_MAX 1 // Maximum number of clients
00091 #define OC_NET_PING_TIMEOUT 2000 // Used for PIN & PON
00092 #define OC_NET_PROTOCOL "0.0.4" // Current protocol
00093 #define OC_NET_SERVER_PORT 0xBEEF // La vache !
00094
00095 #define OC_NET_COMMAND_LENGTH 16
00096 #define OC_NET_DATA_LENGTH 1024
00097
00098
00099 class Netnode;
00100 typedef std::vector<Netnode>::iterator ClientIter;
00101
00102
00103
00107 struct NetMessage {
00108
00109
00110
00111 OPENCITY_NET_CMD cmd;
00112 uint dataLength;
00113
00114
00115
00116
00117 OC_CHAR data [OC_NET_DATA_LENGTH];
00118 };
00119
00120
00121
00125 class Networking {
00126 public:
00127 Networking();
00128 ~Networking();
00129
00130
00131
00135 const string
00136 GetClientHost( const uint index );
00137
00138
00139
00143 const uint
00144 GetClientNum();
00145
00146
00147
00153 const uint
00154 GetClientMax();
00155
00156
00157
00158 const OPENCITY_NET_CODE
00159 GetMachineRole();
00160
00161
00162
00166 const uint
00167 GetPingTimeout();
00168
00169
00170
00174 const uint
00175 GetServerNum();
00176
00177
00178
00182 const string
00183 GetServerHost( const uint index );
00184
00185
00186
00192 const string
00193 GetVersion();
00194
00195
00196
00202 const string
00203 GetProtocol();
00204
00205
00206
00211 const OPENCITY_NET_CODE
00212 Accept( uint & rid );
00213
00214
00215
00219 const OPENCITY_NET_CODE
00220 Reject( const uint id );
00221
00222
00223
00230 const OPENCITY_NET_CODE
00231 StartServer( const OC_SUINT port = OC_NET_SERVER_PORT );
00232
00233
00234
00240 const OPENCITY_NET_CODE
00241 StopServer();
00242
00243
00244
00250 const OPENCITY_NET_CODE
00251 ProcessServerData();
00252
00253
00254
00260 const OPENCITY_NET_CODE
00261 Open(
00262 const string serverHost,
00263 const OC_SUINT port = OC_NET_SERVER_PORT );
00264
00265
00266
00272 const OPENCITY_NET_CODE
00273 Close();
00274
00275
00276
00282 const OPENCITY_NET_CODE
00283 Close( ClientIter i );
00284
00285
00286
00292 const OPENCITY_NET_CODE
00293 Send(
00294 const void* const data,
00295 const uint len );
00296
00297
00298
00304 const OPENCITY_NET_CODE
00305 Send(
00306 const void* const data,
00307 const uint len,
00308 const uint cid );
00309
00310
00311
00316 const OPENCITY_NET_CODE
00317 SendMessage( NetMessage & rMsg );
00318
00319
00320
00325 const OPENCITY_NET_CODE
00326 SendMessage(
00327 NetMessage & rMsg,
00328 const uint cid
00329 );
00330
00331
00332
00338 const OPENCITY_NET_CODE
00339 Receive(
00340 void* const data,
00341 const uint maxlen );
00342
00343
00344
00350 const OPENCITY_NET_CODE
00351 ReceiveMessage( NetMessage & rMsg );
00352
00353
00354
00360 const OPENCITY_NET_CODE
00361 Receive(
00362 void* const data,
00363 const uint maxlen,
00364 const uint cid );
00365
00366
00367 private:
00368 OPENCITY_NET_CODE networkingCode;
00369 bool boolNetworkInitialized;
00370
00371 std::vector<Netnode> vClient;
00372
00373 SDLNet_SocketSet pSocketSet;
00374 TCPsocket pServerSocket;
00375
00376
00377
00378 };
00379 #endif
00380
00381
00382
00383
00384
00385
00386
00387
00388