[5566] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
[5601] | 12 | main-programmer: claudio |
---|
[5800] | 13 | co-programmer: |
---|
[5566] | 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
| 18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
| 19 | */ |
---|
| 20 | #define DEBUG_MODULE_NETWORK |
---|
| 21 | |
---|
[5747] | 22 | |
---|
[5647] | 23 | #include "base_object.h" |
---|
[5731] | 24 | #include "network_protocol.h" |
---|
[7565] | 25 | #include "udp_socket.h" |
---|
| 26 | #include "udp_server_socket.h" |
---|
[5647] | 27 | #include "connection_monitor.h" |
---|
| 28 | #include "synchronizeable.h" |
---|
[6341] | 29 | #include "network_game_manager.h" |
---|
[6959] | 30 | #include "shared_network_data.h" |
---|
[6341] | 31 | |
---|
[7565] | 32 | #include "lib/util/loading/factory.h" |
---|
| 33 | |
---|
[5649] | 34 | #include "debug.h" |
---|
[6139] | 35 | #include "class_list.h" |
---|
[6144] | 36 | #include <algorithm> |
---|
[5647] | 37 | |
---|
[5566] | 38 | /* include your own header */ |
---|
| 39 | #include "network_stream.h" |
---|
| 40 | |
---|
[5595] | 41 | /* probably unnecessary */ |
---|
[5594] | 42 | using namespace std; |
---|
| 43 | |
---|
[5595] | 44 | |
---|
[5747] | 45 | #define PACKAGE_SIZE 256 |
---|
[5647] | 46 | |
---|
[5747] | 47 | |
---|
[5800] | 48 | NetworkStream::NetworkStream() |
---|
[5996] | 49 | : DataStream() |
---|
[5647] | 50 | { |
---|
| 51 | this->init(); |
---|
[5648] | 52 | /* initialize the references */ |
---|
[5996] | 53 | this->type = NET_CLIENT; |
---|
[5741] | 54 | this->networkProtocol = new NetworkProtocol(); |
---|
[5648] | 55 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 56 | } |
---|
| 57 | |
---|
[6695] | 58 | |
---|
[7540] | 59 | NetworkStream::NetworkStream( std::string host, int port ) |
---|
[5996] | 60 | { |
---|
[6139] | 61 | this->type = NET_CLIENT; |
---|
[5996] | 62 | this->init(); |
---|
[7565] | 63 | this->peers[0].socket = new UdpSocket( host, port ); |
---|
[5996] | 64 | this->networkProtocol = new NetworkProtocol(); |
---|
| 65 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[7565] | 66 | this->maxConnections = MAX_CONNECTIONS; |
---|
[5996] | 67 | } |
---|
| 68 | |
---|
| 69 | |
---|
[7565] | 70 | NetworkStream::NetworkStream( int port ) |
---|
[5647] | 71 | { |
---|
[6139] | 72 | this->type = NET_SERVER; |
---|
[5647] | 73 | this->init(); |
---|
[7565] | 74 | this->serverSocket = new UdpServerSocket(port); |
---|
[5741] | 75 | this->networkProtocol = new NetworkProtocol(); |
---|
| 76 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5996] | 77 | this->bActive = true; |
---|
[5649] | 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
[5647] | 81 | void NetworkStream::init() |
---|
| 82 | { |
---|
| 83 | /* set the class id for the base object */ |
---|
| 84 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
[5996] | 85 | this->bActive = false; |
---|
[6139] | 86 | this->serverSocket = NULL; |
---|
[6341] | 87 | this->networkGameManager = NULL; |
---|
[6139] | 88 | myHostId = 0; |
---|
[7565] | 89 | currentState = 0; |
---|
[5594] | 90 | } |
---|
| 91 | |
---|
[5647] | 92 | |
---|
[5566] | 93 | NetworkStream::~NetworkStream() |
---|
[5598] | 94 | { |
---|
[6139] | 95 | if ( this->serverSocket ) |
---|
| 96 | { |
---|
| 97 | serverSocket->close(); |
---|
| 98 | delete serverSocket; |
---|
| 99 | } |
---|
[5723] | 100 | |
---|
[7565] | 101 | for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++) |
---|
[6139] | 102 | { |
---|
[7565] | 103 | if ( i->second.socket ) |
---|
[6139] | 104 | { |
---|
[7565] | 105 | i->second.socket->disconnectServer(); |
---|
| 106 | delete i->second.socket; |
---|
| 107 | i->second.socket = NULL; |
---|
[6139] | 108 | } |
---|
[7565] | 109 | |
---|
| 110 | if ( i->second.handshake ) |
---|
[6139] | 111 | { |
---|
[7565] | 112 | delete i->second.handshake; |
---|
| 113 | i->second.handshake = NULL; |
---|
[6139] | 114 | } |
---|
| 115 | } |
---|
[7565] | 116 | |
---|
| 117 | if ( serverSocket ) |
---|
| 118 | { |
---|
| 119 | delete serverSocket; |
---|
| 120 | serverSocket = NULL; |
---|
| 121 | } |
---|
[5805] | 122 | |
---|
[5996] | 123 | delete connectionMonitor; |
---|
| 124 | delete networkProtocol; |
---|
[5598] | 125 | } |
---|
| 126 | |
---|
[5996] | 127 | |
---|
[6695] | 128 | void NetworkStream::createNetworkGameManager() |
---|
| 129 | { |
---|
| 130 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 131 | // setUniqueID( maxCon+2 ) because we need one id for every handshake |
---|
| 132 | // and one for handshake to reject client maxCon+1 |
---|
| 133 | this->networkGameManager->setUniqueID( this->maxConnections + 2 ); |
---|
[7565] | 134 | |
---|
[6695] | 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | void NetworkStream::startHandshake() |
---|
| 139 | { |
---|
| 140 | Handshake* hs = new Handshake(false); |
---|
| 141 | hs->setUniqueID( 0 ); |
---|
[7565] | 142 | assert( peers[0].handshake == NULL ); |
---|
| 143 | peers[0].handshake = hs; |
---|
[6695] | 144 | //this->connectSynchronizeable(*hs); |
---|
| 145 | PRINTF(0)("NetworkStream: %s\n", hs->getName()); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
[5996] | 149 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
| 150 | { |
---|
[6139] | 151 | this->synchronizeables.push_back(&sync); |
---|
| 152 | sync.setNetworkStream( this ); |
---|
| 153 | |
---|
[7565] | 154 | this->bActive = true; |
---|
[5996] | 155 | } |
---|
| 156 | |
---|
[6695] | 157 | |
---|
[6139] | 158 | void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) |
---|
| 159 | { |
---|
[6144] | 160 | // removing the Synchronizeable from the List. |
---|
| 161 | std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); |
---|
| 162 | if (disconnectSynchro != this->synchronizeables.end()) |
---|
| 163 | this->synchronizeables.erase(disconnectSynchro); |
---|
[5996] | 164 | |
---|
[7565] | 165 | this->bActive = false; |
---|
[6139] | 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
[5604] | 169 | void NetworkStream::processData() |
---|
| 170 | { |
---|
[7565] | 171 | currentState++; |
---|
| 172 | |
---|
[6139] | 173 | if ( this->type == NET_SERVER ) |
---|
| 174 | this->updateConnectionList(); |
---|
| 175 | else |
---|
| 176 | { |
---|
[7565] | 177 | if ( peers[0].socket && !peers[0].socket->isOk() ) |
---|
[6139] | 178 | { |
---|
| 179 | PRINTF(1)("lost connection to server\n"); |
---|
[5741] | 180 | |
---|
[7565] | 181 | peers[0].socket->disconnectServer(); |
---|
| 182 | delete peers[0].socket; |
---|
| 183 | peers[0].socket = NULL; |
---|
[6498] | 184 | |
---|
[7565] | 185 | if ( peers[0].handshake ) |
---|
| 186 | delete peers[0].handshake; |
---|
| 187 | peers[0].handshake = NULL; |
---|
[6139] | 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
[6341] | 191 | |
---|
[6498] | 192 | |
---|
[6341] | 193 | |
---|
[5650] | 194 | /* DOWNSTREAM */ |
---|
[7565] | 195 | #if 0 |
---|
[5800] | 196 | |
---|
[6695] | 197 | |
---|
[6139] | 198 | int dataLength; |
---|
| 199 | int reciever; |
---|
| 200 | Header header; |
---|
[6959] | 201 | int counter; |
---|
| 202 | |
---|
[6139] | 203 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
[5996] | 204 | { |
---|
[6959] | 205 | counter = 0; |
---|
| 206 | |
---|
[6695] | 207 | if ( (*it)!=NULL && (*it)->beSynchronized() /*&& (*it)->getOwner() == myHostId*/ ) |
---|
[6139] | 208 | { |
---|
| 209 | do { |
---|
[6959] | 210 | counter++; |
---|
| 211 | |
---|
[6139] | 212 | reciever = 0; |
---|
[7444] | 213 | #warning fix this |
---|
| 214 | dataLength = 0; |
---|
| 215 | //TODO fix |
---|
| 216 | //dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever); |
---|
[5730] | 217 | |
---|
[6139] | 218 | if ( dataLength<=0 ){ |
---|
| 219 | reciever = 0; |
---|
| 220 | continue; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it))); |
---|
| 224 | |
---|
| 225 | Header* header = (Header*)downBuffer; |
---|
[6341] | 226 | if ( header->synchronizeableID < this->maxConnections+2 ) |
---|
| 227 | { |
---|
| 228 | //if ( !isServer() ) PRINTF(0)("RESET UNIQUEID FROM %d TO 0 maxCon=%d\n", header->synchronizeableID, this->maxConnections); |
---|
[6139] | 229 | header->synchronizeableID = 0; |
---|
[6341] | 230 | } |
---|
| 231 | else |
---|
| 232 | { |
---|
| 233 | //if ( !isServer() ) PRINTF(0)("UNIQUEID=%d\n", header->synchronizeableID); |
---|
| 234 | } |
---|
[6139] | 235 | |
---|
| 236 | if ( dataLength<=0 ) |
---|
| 237 | continue; |
---|
| 238 | |
---|
| 239 | if ( reciever!=0 ) |
---|
| 240 | { |
---|
[6868] | 241 | if ( reciever < 0) |
---|
[6139] | 242 | { |
---|
[6868] | 243 | for ( int i = 0; i<networkSockets.size(); i++) |
---|
| 244 | { |
---|
[6959] | 245 | if ( i!=abs(reciever) && networkSockets[i] != NULL ) |
---|
[6868] | 246 | { |
---|
[6959] | 247 | PRINTF(0)("write %d bytes to socket %d uniqueid %d reciever %d\n", dataLength, i, (*it)->getUniqueID(), reciever); |
---|
[6868] | 248 | networkSockets[i]->writePacket(downBuffer, dataLength); |
---|
| 249 | } |
---|
| 250 | } |
---|
[6139] | 251 | } |
---|
| 252 | else |
---|
| 253 | { |
---|
[6868] | 254 | if ( networkSockets[reciever] != NULL ) |
---|
| 255 | { |
---|
| 256 | PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever); |
---|
| 257 | networkSockets[reciever]->writePacket(downBuffer, dataLength); |
---|
| 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
| 261 | PRINTF(1)("networkSockets[reciever] == NULL\n"); |
---|
| 262 | } |
---|
[6139] | 263 | } |
---|
| 264 | } |
---|
| 265 | else |
---|
| 266 | { |
---|
| 267 | for ( int i = 0; i<networkSockets.size(); i++) |
---|
| 268 | { |
---|
| 269 | if ( networkSockets[i] != NULL ) |
---|
| 270 | { |
---|
[6634] | 271 | PRINTF(5)("write %d bytes to socket %d\n", dataLength, i); |
---|
[6139] | 272 | networkSockets[i]->writePacket(downBuffer, dataLength); |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | } while( reciever!=0 ); |
---|
| 278 | } |
---|
[5996] | 279 | } |
---|
[5741] | 280 | |
---|
| 281 | /* UPSTREAM */ |
---|
[5809] | 282 | |
---|
[6139] | 283 | for ( int i = 0; i<networkSockets.size(); i++) |
---|
[5802] | 284 | { |
---|
[6139] | 285 | if ( networkSockets[i] ) |
---|
[5810] | 286 | { |
---|
[6139] | 287 | do { |
---|
| 288 | dataLength = networkSockets[i]->readPacket(upBuffer, DATA_STREAM_BUFFER_SIZE); |
---|
[5730] | 289 | |
---|
[6139] | 290 | if ( dataLength<=0 ) |
---|
| 291 | continue; |
---|
| 292 | |
---|
| 293 | header = networkProtocol->extractHeader(upBuffer, dataLength); |
---|
| 294 | dataLength -= sizeof(header); |
---|
| 295 | |
---|
[6498] | 296 | PRINTF(5)("read %d bytes from socket uniqueID = %d\n", dataLength, header.synchronizeableID); |
---|
[6341] | 297 | |
---|
[6139] | 298 | if ( dataLength != header.length ) |
---|
| 299 | { |
---|
| 300 | PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length); |
---|
| 301 | continue; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | if ( header.synchronizeableID == 0 ) |
---|
| 305 | { |
---|
| 306 | header.synchronizeableID = i; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
| 310 | { |
---|
[7444] | 311 | #warning fix this |
---|
[7565] | 312 | |
---|
[6139] | 313 | if ( *it && (*it)->getUniqueID()==header.synchronizeableID ) |
---|
[6341] | 314 | { |
---|
| 315 | if ( (*it)->writeBytes(upBuffer+sizeof(header), dataLength, i) != header.length ) |
---|
| 316 | { |
---|
[6634] | 317 | PRINTF(1)("%s did not read all the data id = %d!\n", (*it)->getClassName(), (*it)->getUniqueID()); |
---|
[6341] | 318 | break; |
---|
| 319 | } |
---|
| 320 | continue; |
---|
| 321 | } |
---|
[7565] | 322 | |
---|
[6139] | 323 | } |
---|
| 324 | |
---|
| 325 | } while ( dataLength>0 ); |
---|
[5810] | 326 | } |
---|
[7565] | 327 | |
---|
[5810] | 328 | } |
---|
[7565] | 329 | #endif |
---|
[6139] | 330 | } |
---|
| 331 | |
---|
| 332 | void NetworkStream::updateConnectionList( ) |
---|
| 333 | { |
---|
| 334 | //check for new connections |
---|
| 335 | |
---|
| 336 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
| 337 | |
---|
| 338 | if ( tempNetworkSocket ) |
---|
[5810] | 339 | { |
---|
[6139] | 340 | int clientId; |
---|
| 341 | if ( freeSocketSlots.size() >0 ) |
---|
[5810] | 342 | { |
---|
[6139] | 343 | clientId = freeSocketSlots.back(); |
---|
| 344 | freeSocketSlots.pop_back(); |
---|
[7565] | 345 | peers[clientId].socket = tempNetworkSocket; |
---|
| 346 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); |
---|
| 347 | peers[clientId].handshake->setUniqueID(clientId); |
---|
| 348 | peers[clientId].userId = clientId; |
---|
[6139] | 349 | } else |
---|
| 350 | { |
---|
[7565] | 351 | clientId = 0; |
---|
| 352 | |
---|
| 353 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
| 354 | if ( it->first >= clientId ) |
---|
| 355 | clientId = it->first + 1; |
---|
| 356 | |
---|
| 357 | peers[clientId].socket = tempNetworkSocket; |
---|
| 358 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); |
---|
| 359 | peers[clientId].handshake->setUniqueID(clientId); |
---|
| 360 | peers[clientId].userId = clientId; |
---|
[6139] | 361 | } |
---|
[5730] | 362 | |
---|
[6139] | 363 | if ( clientId > this->maxConnections ) |
---|
| 364 | { |
---|
[7565] | 365 | peers[clientId].handshake->doReject( "too many connections" ); |
---|
[6139] | 366 | PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); |
---|
[5810] | 367 | } |
---|
[6139] | 368 | else |
---|
| 369 | |
---|
| 370 | PRINTF(0)("New Client: %d\n", clientId); |
---|
| 371 | |
---|
[6695] | 372 | //this->connectSynchronizeable(*handshakes[clientId]); |
---|
[5802] | 373 | } |
---|
[5800] | 374 | |
---|
[5809] | 375 | |
---|
[6139] | 376 | //check if connections are ok else remove them |
---|
[7565] | 377 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
[6139] | 378 | { |
---|
[7565] | 379 | if ( it->second.socket && !it->second.socket->isOk() ) |
---|
[6139] | 380 | { |
---|
[7565] | 381 | PRINTF(0)("Client is gone: %d\n", it->second.userId); |
---|
[6139] | 382 | |
---|
[7565] | 383 | it->second.socket->disconnectServer(); |
---|
| 384 | delete it->second.socket; |
---|
| 385 | it->second.socket = NULL; |
---|
[6498] | 386 | |
---|
[7565] | 387 | if ( it->second.handshake ) |
---|
| 388 | delete it->second.handshake; |
---|
| 389 | it->second.handshake = NULL; |
---|
[6139] | 390 | |
---|
[6737] | 391 | |
---|
[7565] | 392 | NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId); |
---|
[6737] | 393 | |
---|
[7565] | 394 | freeSocketSlots.push_back( it->second.userId ); |
---|
| 395 | |
---|
[6139] | 396 | } |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | |
---|
[5604] | 400 | } |
---|
[6139] | 401 | |
---|
[6695] | 402 | void NetworkStream::debug() |
---|
| 403 | { |
---|
| 404 | if( this->isServer()) |
---|
| 405 | PRINT(0)(" Host ist Server with ID: %i\n", this->myHostId); |
---|
| 406 | else |
---|
| 407 | PRINT(0)(" Host ist Client with ID: %i\n", this->myHostId); |
---|
| 408 | |
---|
| 409 | PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); |
---|
| 410 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
| 411 | { |
---|
| 412 | if( (*it)->beSynchronized() == true) |
---|
| 413 | PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(), |
---|
| 414 | (*it)->getUniqueID(), (*it)->beSynchronized()); |
---|
| 415 | } |
---|
| 416 | PRINT(0)(" Maximal Connections: %i\n", this->maxConnections); |
---|
| 417 | |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | |
---|
| 421 | int NetworkStream::getSyncCount() |
---|
| 422 | { |
---|
| 423 | int n = 0; |
---|
| 424 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
| 425 | if( (*it)->beSynchronized() == true) |
---|
| 426 | ++n; |
---|
| 427 | |
---|
| 428 | //return synchronizeables.size(); |
---|
| 429 | return n; |
---|
| 430 | } |
---|
| 431 | |
---|
[7565] | 432 | /** |
---|
| 433 | * check if handshakes completed |
---|
| 434 | */ |
---|
| 435 | void NetworkStream::handleHandshakes( ) |
---|
| 436 | { |
---|
| 437 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
| 438 | { |
---|
| 439 | if ( it->second.handshake ) |
---|
| 440 | { |
---|
| 441 | if ( it->second.handshake->completed() ) |
---|
| 442 | { |
---|
| 443 | if ( it->second.handshake->ok() ) |
---|
| 444 | { |
---|
| 445 | if ( type != NET_SERVER ) |
---|
| 446 | { |
---|
| 447 | SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); |
---|
| 448 | myHostId = SharedNetworkData::getInstance()->getHostID(); |
---|
[6695] | 449 | |
---|
[7565] | 450 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 451 | this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); |
---|
| 452 | } |
---|
[6695] | 453 | |
---|
[7565] | 454 | PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); |
---|
[6695] | 455 | |
---|
| 456 | |
---|
[7565] | 457 | delete it->second.handshake; |
---|
| 458 | it->second.handshake = NULL; |
---|
| 459 | } |
---|
| 460 | else |
---|
| 461 | { |
---|
| 462 | PRINT(1)("handshake failed!\n"); |
---|
| 463 | it->second.socket->disconnectServer(); |
---|
| 464 | } |
---|
| 465 | } |
---|
| 466 | } |
---|
| 467 | } |
---|
| 468 | } |
---|
[6695] | 469 | |
---|
[7565] | 470 | /** |
---|
| 471 | * handle upstream network traffic |
---|
| 472 | */ |
---|
| 473 | void NetworkStream::handleUpstream( ) |
---|
| 474 | { |
---|
| 475 | byte buf[UDP_PACKET_SIZE]; |
---|
| 476 | int offset; |
---|
| 477 | int n; |
---|
| 478 | |
---|
| 479 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
---|
| 480 | { |
---|
| 481 | offset = INTSIZE; //make already space for length |
---|
| 482 | |
---|
| 483 | if ( peer->second.socket ) |
---|
| 484 | continue; |
---|
| 485 | |
---|
| 486 | n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 487 | assert( n == INTSIZE ); |
---|
| 488 | offset += n; |
---|
| 489 | |
---|
| 490 | n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 491 | assert( n == INTSIZE ); |
---|
| 492 | offset += n; |
---|
| 493 | |
---|
| 494 | n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 495 | assert( n == INTSIZE ); |
---|
| 496 | offset += n; |
---|
| 497 | |
---|
| 498 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
| 499 | { |
---|
| 500 | Synchronizeable & sync = **it; |
---|
| 501 | |
---|
| 502 | assert( offset + INTSIZE <= UDP_PACKET_SIZE ); |
---|
| 503 | |
---|
| 504 | n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 505 | assert( n == INTSIZE ); |
---|
| 506 | offset += n; |
---|
| 507 | |
---|
| 508 | offset += sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, 0 ); |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE ); |
---|
| 512 | |
---|
| 513 | assert( peer->second.socket->writePacket( buf, offset ) ); |
---|
| 514 | } |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | /** |
---|
| 518 | * handle downstream network traffic |
---|
| 519 | */ |
---|
| 520 | void NetworkStream::handleDownstream( ) |
---|
| 521 | { |
---|
| 522 | byte buf[UDP_PACKET_SIZE]; |
---|
| 523 | int offset = 0; |
---|
| 524 | |
---|
| 525 | int length = 0; |
---|
| 526 | int packetLength = 0; |
---|
| 527 | int uniqueId = 0; |
---|
| 528 | int state = 0; |
---|
| 529 | int ackedState = 0; |
---|
| 530 | int fromState = 0; |
---|
| 531 | |
---|
| 532 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
---|
| 533 | { |
---|
| 534 | packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE ); |
---|
| 535 | |
---|
| 536 | assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE ); |
---|
| 537 | assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE ); |
---|
| 538 | assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); |
---|
| 539 | assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); |
---|
| 540 | |
---|
| 541 | //if this is an old state drop it |
---|
| 542 | if ( state <= peer->second.lastRecvedState ) |
---|
| 543 | continue; |
---|
| 544 | |
---|
| 545 | if ( packetLength != length ) |
---|
| 546 | { |
---|
| 547 | PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length); |
---|
| 548 | peer->second.socket->disconnectServer(); |
---|
| 549 | continue; |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | offset = 4*INTSIZE; |
---|
| 553 | |
---|
| 554 | while ( offset < length ) |
---|
| 555 | { |
---|
| 556 | assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); |
---|
| 557 | offset += INTSIZE; |
---|
| 558 | |
---|
| 559 | Synchronizeable * sync = NULL; |
---|
| 560 | |
---|
| 561 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
| 562 | { |
---|
| 563 | if ( (*it)->getUniqueID() == uniqueId ) |
---|
| 564 | { |
---|
| 565 | sync = *it; |
---|
| 566 | break; |
---|
| 567 | } |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | if ( sync == NULL ) |
---|
| 571 | { |
---|
| 572 | //TODO dont accept new object from all peers (probably only servers) |
---|
| 573 | int leafClassId; |
---|
| 574 | if ( INTSIZE > length - offset ) |
---|
| 575 | break; |
---|
| 576 | |
---|
| 577 | Converter::byteArrayToInt( buf + offset, &leafClassId ); |
---|
| 578 | |
---|
| 579 | BaseObject * b; |
---|
| 580 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
---|
| 581 | /* Exception 1: NullParent */ |
---|
| 582 | if( leafClassId == CL_NULL_PARENT) |
---|
| 583 | { |
---|
| 584 | PRINTF(1)("Can not create Class with ID %x!", (int)leafClassId); |
---|
| 585 | break; |
---|
| 586 | } |
---|
| 587 | else |
---|
| 588 | b = Factory::fabricate( (ClassID)leafClassId ); |
---|
| 589 | |
---|
| 590 | if ( !b ) |
---|
| 591 | { |
---|
| 592 | PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); |
---|
| 593 | break; |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
| 597 | { |
---|
| 598 | sync = dynamic_cast<Synchronizeable*>(b); |
---|
| 599 | sync->setUniqueID( uniqueId ); |
---|
| 600 | sync->setSynchronized(true); |
---|
| 601 | |
---|
| 602 | PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID()); |
---|
| 603 | } |
---|
| 604 | else |
---|
| 605 | { |
---|
| 606 | PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)leafClassId); |
---|
| 607 | delete b; |
---|
| 608 | break; |
---|
| 609 | } |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | offset += sync->setStateDiff( peer->second.userId, buf+offset, length-offset, state, fromState ); |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | if ( offset != length ) |
---|
| 616 | { |
---|
| 617 | peer->second.socket->disconnectServer(); |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | peer->second.lastAckedState = ackedState; |
---|
| 621 | } |
---|
| 622 | } |
---|
| 623 | |
---|
| 624 | |
---|
| 625 | |
---|
| 626 | |
---|
| 627 | |
---|
| 628 | |
---|