| 1 | /*************************************************************************** |
| 2 | networking.cpp - 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.cpp 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 | #include "netnode.h" |
| 21 | #include "networking.h" |
| 22 | |
| 23 | |
| 24 | /*=====================================================================*/ |
| 25 | Networking::Networking(): |
| 26 | _networkingCode( OC_NET_UNDEFINED ), |
| 27 | _pSocketSet( NULL ), |
| 28 | _pServerSocket( NULL ) |
| 29 | { |
| 30 | OPENCITY_DEBUG("ctor"); |
| 31 | |
| 32 | _boolNetworkInitialized = (SDLNet_Init() != -1); |
| 33 | if (_boolNetworkInitialized == false) { |
| 34 | cerr << "WARNING: I couldn't initialize networking. " |
| 35 | << "The error was: " << SDLNet_GetError() << endl; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /*=====================================================================*/ |
| 41 | Networking::~Networking() |
| 42 | { |
| 43 | OPENCITY_DEBUG("dtor"); |
| 44 | |
| 45 | //FIXME: free server ressources |
| 46 | |
| 47 | if (_boolNetworkInitialized == true) { |
| 48 | SDLNet_Quit(); |
| 49 | _boolNetworkInitialized = false; |
| 50 | _networkingCode = OC_NET_UNDEFINED; |
| 51 | _pServerSocket = NULL; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /*=====================================================================*/ |
| 57 | const string |
| 58 | Networking::GetClientHost( |
| 59 | const uint index ) |
| 60 | { |
| 61 | assert( index < _vClient.size() ); |
| 62 | return (string)(SDLNet_ResolveIP(&_vClient[index].ip)); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /*=====================================================================*/ |
| 67 | const uint |
| 68 | Networking::GetClientNum() |
| 69 | { |
| 70 | return _vClient.size(); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /*=====================================================================*/ |
| 75 | const uint |
| 76 | Networking::GetClientMax() |
| 77 | { |
| 78 | return OC_NET_CLIENT_MAX; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /*=====================================================================*/ |
| 83 | const OPENCITY_NET_CODE |
| 84 | Networking::GetMachineRole() |
| 85 | { |
| 86 | return this->_networkingCode; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /*=====================================================================*/ |
| 91 | const uint |
| 92 | Networking::GetPingTimeout() |
| 93 | { |
| 94 | return OC_NET_PING_TIMEOUT; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /*=====================================================================*/ |
| 99 | const uint |
| 100 | Networking::GetServerNum() |
| 101 | { |
| 102 | //TODO: STUB |
| 103 | OPENCITY_DEBUG("TODO"); |
| 104 | assert(0); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /*=====================================================================*/ |
| 110 | const string |
| 111 | Networking::GetServerHost( const uint index ) |
| 112 | { |
| 113 | //TODO: STUB |
| 114 | OPENCITY_DEBUG("TODO"); |
| 115 | assert(0); |
| 116 | return ""; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /*=====================================================================*/ |
| 121 | const string |
| 122 | Networking::GetVersion() |
| 123 | { |
| 124 | return VERSION; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /*=====================================================================*/ |
| 129 | const string |
| 130 | Networking::GetProtocol() |
| 131 | { |
| 132 | return OC_NET_PROTOCOL; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /*=====================================================================*/ |
| 137 | const OPENCITY_NET_CODE |
| 138 | Networking::Accept( uint & rid ) |
| 139 | { |
| 140 | // IF the networking is not init THEN error |
| 141 | if (this->_boolNetworkInitialized == false) |
| 142 | return OC_NET_ERROR; |
| 143 | |
| 144 | // IF the server is not running THEN error |
| 145 | if (this->_pServerSocket == NULL) |
| 146 | return OC_NET_SERVER_STOPED; |
| 147 | |
| 148 | // IF the server is full THEN error |
| 149 | // if (_vClient.size() == OC_NET_CLIENT_MAX) |
| 150 | // return OC_NET_SERVER_FULL; |
| 151 | |
| 152 | // Create the new socket to handle the client |
| 153 | Netnode nodeClient; |
| 154 | nodeClient.socket = SDLNet_TCP_Accept(this->_pServerSocket); |
| 155 | if (nodeClient.socket != NULL) { |
| 156 | NetMessage msg; |
| 157 | // Put the client socket into the server's socket set for future use |
| 158 | if (SDLNet_TCP_AddSocket( this->_pSocketSet, nodeClient.socket ) == -1) { |
| 159 | // Send NCK |
| 160 | msg.cmd = OC_NET_NCK; |
| 161 | (void)SDLNet_TCP_Send(nodeClient.socket, &msg, sizeof(NetMessage)); |
| 162 | SDLNet_TCP_Close(nodeClient.socket); |
| 163 | rid = 0; |
| 164 | |
| 165 | return OC_NET_SERVER_FULL; |
| 166 | } |
| 167 | else { |
| 168 | nodeClient.ip = *(SDLNet_TCP_GetPeerAddress(nodeClient.socket)); |
| 169 | _vClient.push_back(nodeClient); |
| 170 | rid = _vClient.size()-1; |
| 171 | |
| 172 | // Send ACK |
| 173 | msg.cmd = OC_NET_ACK; |
| 174 | this->SendMessage( msg, rid ); |
| 175 | } |
| 176 | } |
| 177 | else { |
| 178 | rid = 0; |
| 179 | return OC_NET_NOCLIENT; |
| 180 | } |
| 181 | |
| 182 | return OC_NET_OK; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /*=====================================================================*/ |
| 187 | const OPENCITY_NET_CODE |
| 188 | Networking::Reject( const uint id ) |
| 189 | { |
| 190 | OPENCITY_DEBUG( "TODO" ); |
| 191 | assert( 0 ); |
| 192 | return OC_NET_ERROR; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /*=====================================================================*/ |
| 197 | const OPENCITY_NET_CODE |
| 198 | Networking::StartServer( const OC_USHORT port ) |
| 199 | { |
| 200 | // IF the server is already running THEN error |
| 201 | if (_pServerSocket != NULL) { |
| 202 | return OC_NET_SERVER_STARTED; |
| 203 | } |
| 204 | |
| 205 | IPaddress ip; |
| 206 | if (SDLNet_ResolveHost(&ip, NULL, port) == -1) { |
| 207 | cerr << "ERROR: I couldn't start the server: " |
| 208 | << SDLNet_GetError() << endl; |
| 209 | return OC_NET_ERROR; |
| 210 | } |
| 211 | |
| 212 | // Open the server socket |
| 213 | this->_pServerSocket = SDLNet_TCP_Open(&ip); |
| 214 | if (_pServerSocket == NULL) { |
| 215 | cerr << "ERROR: I couldn't open the server's socket: " |
| 216 | << SDLNet_GetError() << endl; |
| 217 | return OC_NET_ERROR; |
| 218 | } |
| 219 | |
| 220 | // Allocate the socket set for the server |
| 221 | this->_pSocketSet = SDLNet_AllocSocketSet(OC_NET_CLIENT_MAX); |
| 222 | if (this->_pSocketSet == NULL) { |
| 223 | cerr << "ERROR: I couldn't allocate a socket set: " |
| 224 | << SDLNet_GetError() << endl; |
| 225 | return OC_NET_ERROR; |
| 226 | } |
| 227 | |
| 228 | return OC_NET_OK; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | /*=====================================================================*/ |
| 233 | const OPENCITY_NET_CODE |
| 234 | Networking::StopServer() |
| 235 | { |
| 236 | // IF the server is not running THEN error |
| 237 | if (_pServerSocket == NULL) { |
| 238 | return OC_NET_SERVER_STOPED; |
| 239 | } |
| 240 | |
| 241 | SDLNet_TCP_Close(this->_pServerSocket); |
| 242 | _pServerSocket = NULL; |
| 243 | |
| 244 | SDLNet_FreeSocketSet(this->_pSocketSet); |
| 245 | this->_pSocketSet = NULL; |
| 246 | |
| 247 | return OC_NET_OK; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /*=====================================================================*/ |
| 252 | const OPENCITY_NET_CODE |
| 253 | Networking::ProcessServerData() |
| 254 | { |
| 255 | uint id = 0; |
| 256 | OPENCITY_NET_CODE netCode = OC_NET_UNDEFINED; |
| 257 | NetMessage msg; |
| 258 | ClientIterator i; |
| 259 | int recv; |
| 260 | std::vector<ClientIterator> vToClose; |
| 261 | |
| 262 | // Accept an incoming connection and automatically ACK |
| 263 | netCode = this->Accept( id ); |
| 264 | if (netCode == OC_NET_OK) { |
| 265 | OPENCITY_DEBUG( "The new number of clients is: " << this->GetClientNum() ); |
| 266 | } |
| 267 | else if (netCode == OC_NET_SERVER_FULL) { |
| 268 | OPENCITY_DEBUG( "The server is full, the new connection demand has been rejected" ); |
| 269 | } |
| 270 | |
| 271 | // Check socket set for activity |
| 272 | // Otherwise return |
| 273 | if (SDLNet_CheckSockets( this->_pSocketSet, OC_NET_CHECK_TIMEOUT ) < 1) |
| 274 | return OC_NET_OK; |
| 275 | |
| 276 | // Process waiting messages sent by client |
| 277 | OPENCITY_DEBUG( "Begin reading socket" ); |
| 278 | for (i = _vClient.begin(); i != _vClient.end(); i++) { |
| 279 | if (SDLNet_SocketReady((*i).socket) != 0) { |
| 280 | recv = SDLNet_TCP_Recv((*i).socket, &msg, sizeof(NetMessage) ); |
| 281 | |
| 282 | if (recv > 0) { |
| 283 | // Disconnect request ? |
| 284 | if (msg.cmd == OC_NET_DNT) { |
| 285 | OPENCITY_DEBUG( "One client disconnected." ); |
| 286 | // FIXME: send ACK |
| 287 | vToClose.push_back( i ); |
| 288 | } |
| 289 | } |
| 290 | else { |
| 291 | OPENCITY_DEBUG( "No data received or error. Closing client socket." ); |
| 292 | vToClose.push_back( i ); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | OPENCITY_DEBUG( "End reading socket" ); |
| 297 | |
| 298 | uint c; |
| 299 | for ( c = 0; c < vToClose.size(); c++ ) { |
| 300 | this->Close(vToClose[c]); |
| 301 | } |
| 302 | |
| 303 | return OC_NET_OK; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /*=====================================================================*/ |
| 308 | const OPENCITY_NET_CODE |
| 309 | Networking::Open( |
| 310 | const string serverHost, |
| 311 | const OC_USHORT port ) |
| 312 | { |
| 313 | // IF we are already connected THEN |
| 314 | if (this->_pServerSocket != NULL) |
| 315 | return OC_NET_CLIENT_CONNECTED; |
| 316 | |
| 317 | // IF we can not resolve the host THEN error |
| 318 | IPaddress ip; |
| 319 | if (SDLNet_ResolveHost(&ip, serverHost.c_str(), port) == -1) { |
| 320 | cerr << "ERROR: I couldn't resolve the server name: " |
| 321 | << SDLNet_GetError() << endl; |
| 322 | return OC_NET_ERROR; |
| 323 | } |
| 324 | |
| 325 | // Open the server socket |
| 326 | this->_pServerSocket = SDLNet_TCP_Open(&ip); |
| 327 | if (_pServerSocket == NULL) { |
| 328 | cerr << "ERROR: I couldn't connect to the server: " |
| 329 | << SDLNet_GetError() << endl; |
| 330 | return OC_NET_ERROR; |
| 331 | } |
| 332 | |
| 333 | // Reading ACK/NCK |
| 334 | NetMessage msg; |
| 335 | OPENCITY_NET_CODE nc = OC_NET_OK; |
| 336 | if (this->Receive( &msg, sizeof(NetMessage) ) == OC_NET_OK) { |
| 337 | switch (msg.cmd) { |
| 338 | case OC_NET_ACK: |
| 339 | nc = OC_NET_CLIENT_ACCEPTED; |
| 340 | break; |
| 341 | |
| 342 | case OC_NET_NCK: |
| 343 | // Close the socket |
| 344 | SDLNet_TCP_Close(this->_pServerSocket); |
| 345 | this->_pServerSocket = NULL; |
| 346 | nc = OC_NET_CLIENT_REJECTED; |
| 347 | break; |
| 348 | |
| 349 | default: |
| 350 | nc = OC_NET_ERROR; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return nc; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /*=====================================================================*/ |
| 359 | const OPENCITY_NET_CODE |
| 360 | Networking::Close() |
| 361 | { |
| 362 | NetMessage msg; |
| 363 | OPENCITY_NET_CODE netCode; |
| 364 | |
| 365 | // Send disconnect command |
| 366 | msg.cmd = OC_NET_DNT; |
| 367 | netCode = SendMessage( msg ); |
| 368 | |
| 369 | // Close the socket |
| 370 | SDLNet_TCP_Close(this->_pServerSocket); |
| 371 | this->_pServerSocket = NULL; |
| 372 | |
| 373 | return netCode; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /*=====================================================================*/ |
| 378 | const OPENCITY_NET_CODE |
| 379 | Networking::Close( ClientIterator i ) |
| 380 | { |
| 381 | // assert( i != NULL ); |
| 382 | |
| 383 | //TODO: send the "disconnect" command to the client |
| 384 | |
| 385 | // Remove the socket from the set |
| 386 | (void)SDLNet_TCP_DelSocket( this->_pSocketSet, (*i).socket ); |
| 387 | |
| 388 | // Close the client socket |
| 389 | SDLNet_TCP_Close((*i).socket); |
| 390 | (*i).socket = NULL; |
| 391 | |
| 392 | // Erase the client from the vector |
| 393 | _vClient.erase(i); |
| 394 | // std::remove( i, i, (*i) ); |
| 395 | |
| 396 | return OC_NET_OK; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /*=====================================================================*/ |
| 401 | const OPENCITY_NET_CODE |
| 402 | Networking::Send( |
| 403 | const void* const data, |
| 404 | const uint len ) |
| 405 | { |
| 406 | static uint sent; |
| 407 | |
| 408 | // IF we are not connected THEN error |
| 409 | if (this->_pServerSocket == NULL) |
| 410 | return OC_NET_CLIENT_NOTCONNECTED; |
| 411 | |
| 412 | // IF there is no data to send THEN error |
| 413 | if (len == 0) |
| 414 | return OC_NET_NODATA; |
| 415 | |
| 416 | sent = SDLNet_TCP_Send(this->_pServerSocket, const_cast<void*>(data), len); |
| 417 | if (sent != len) { |
| 418 | OPENCITY_DEBUG("ERROR: Net send"); |
| 419 | return OC_NET_SENDERROR; |
| 420 | } |
| 421 | |
| 422 | return OC_NET_OK; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /*=====================================================================*/ |
| 427 | const OPENCITY_NET_CODE |
| 428 | Networking::Send( |
| 429 | const void* const data, |
| 430 | const uint len, |
| 431 | const uint cid ) |
| 432 | { |
| 433 | static uint sent; |
| 434 | |
| 435 | // WARNING: the code is similar to the Send() method |
| 436 | assert( cid < _vClient.size() ); |
| 437 | |
| 438 | // IF there is no data to send THEN error |
| 439 | if (len == 0) |
| 440 | return OC_NET_NODATA; |
| 441 | |
| 442 | sent = SDLNet_TCP_Send(_vClient[cid].socket, const_cast<void*>(data), len); |
| 443 | if (sent != len) { |
| 444 | OPENCITY_DEBUG("ERROR: Net send"); |
| 445 | return OC_NET_SENDERROR; |
| 446 | } |
| 447 | |
| 448 | return OC_NET_OK; |
| 449 | } |
| 450 | |
| 451 | |
| 452 | /*=====================================================================*/ |
| 453 | const OPENCITY_NET_CODE |
| 454 | Networking::SendMessage( NetMessage & rMsg ) |
| 455 | { |
| 456 | // IF we are not connected THEN |
| 457 | if (this->_pServerSocket == NULL) |
| 458 | return OC_NET_CLIENT_NOTCONNECTED; |
| 459 | |
| 460 | // Send the message to the node |
| 461 | rMsg.dataLength = OC_NET_DATA_LENGTH; // hack |
| 462 | return this->Send( &rMsg, sizeof(NetMessage) ); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | /*=====================================================================*/ |
| 467 | const OPENCITY_NET_CODE |
| 468 | Networking::SendMessage( |
| 469 | NetMessage & rMsg, |
| 470 | const uint cid ) |
| 471 | { |
| 472 | // Send the message to the node |
| 473 | rMsg.dataLength = OC_NET_DATA_LENGTH; // hack |
| 474 | return this->Send( &rMsg, sizeof(NetMessage), cid ); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /*=====================================================================*/ |
| 479 | const OPENCITY_NET_CODE |
| 480 | Networking::Receive( |
| 481 | void* const data, |
| 482 | const uint maxlen ) |
| 483 | { |
| 484 | static int recv; |
| 485 | |
| 486 | // IF we are not connected THEN error |
| 487 | if (this->_pServerSocket == NULL) |
| 488 | return OC_NET_CLIENT_NOTCONNECTED; |
| 489 | |
| 490 | // IF the buffer is not big enough THEN |
| 491 | if (maxlen == 0) |
| 492 | return OC_NET_BUFFERROR; |
| 493 | |
| 494 | recv = SDLNet_TCP_Recv( this->_pServerSocket, data, maxlen ); |
| 495 | if (recv <= 0) { |
| 496 | OPENCITY_DEBUG("WARNING: no data received or error"); |
| 497 | return OC_NET_RECVERROR; |
| 498 | } |
| 499 | |
| 500 | return OC_NET_OK; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /*=====================================================================*/ |
| 505 | const OPENCITY_NET_CODE |
| 506 | Networking::ReceiveMessage( |
| 507 | NetMessage & rMsg ) |
| 508 | { |
| 509 | // IF we are not connected THEN |
| 510 | if (this->_pServerSocket == NULL) |
| 511 | return OC_NET_CLIENT_NOTCONNECTED; |
| 512 | |
| 513 | // Wait for the commands sent by the server |
| 514 | return this->Receive( &rMsg, sizeof(NetMessage) ); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | /*=====================================================================*/ |
| 519 | const OPENCITY_NET_CODE |
| 520 | Networking::Receive( |
| 521 | void* const data, |
| 522 | const uint maxlen, |
| 523 | const uint cid ) |
| 524 | { |
| 525 | static int recv; |
| 526 | |
| 527 | // WARNING: the code is similar to the client's Receive() method |
| 528 | assert( cid < _vClient.size() ); |
| 529 | |
| 530 | // IF the buffer is not big enough THEN |
| 531 | if (maxlen == 0) |
| 532 | return OC_NET_BUFFERROR; |
| 533 | |
| 534 | recv = SDLNet_TCP_Recv( _vClient[cid].socket, data, maxlen ); |
| 535 | if (recv <= 0) { |
| 536 | return OC_NET_NODATA; |
| 537 | } |
| 538 | |
| 539 | return OC_NET_OK; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 |