[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" |
---|
[7671] | 31 | #include "message_manager.h" |
---|
[6341] | 32 | |
---|
[7565] | 33 | #include "lib/util/loading/factory.h" |
---|
| 34 | |
---|
[5649] | 35 | #include "debug.h" |
---|
[6139] | 36 | #include "class_list.h" |
---|
[6144] | 37 | #include <algorithm> |
---|
[5647] | 38 | |
---|
[5566] | 39 | /* include your own header */ |
---|
| 40 | #include "network_stream.h" |
---|
| 41 | |
---|
[5595] | 42 | /* probably unnecessary */ |
---|
[5594] | 43 | using namespace std; |
---|
| 44 | |
---|
[5595] | 45 | |
---|
[5747] | 46 | #define PACKAGE_SIZE 256 |
---|
[5647] | 47 | |
---|
[5747] | 48 | |
---|
[5800] | 49 | NetworkStream::NetworkStream() |
---|
[5996] | 50 | : DataStream() |
---|
[5647] | 51 | { |
---|
| 52 | this->init(); |
---|
[5648] | 53 | /* initialize the references */ |
---|
[5996] | 54 | this->type = NET_CLIENT; |
---|
[5741] | 55 | this->networkProtocol = new NetworkProtocol(); |
---|
[5648] | 56 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 57 | } |
---|
| 58 | |
---|
[6695] | 59 | |
---|
[7540] | 60 | NetworkStream::NetworkStream( std::string host, int port ) |
---|
[5996] | 61 | { |
---|
[6139] | 62 | this->type = NET_CLIENT; |
---|
[5996] | 63 | this->init(); |
---|
[7565] | 64 | this->peers[0].socket = new UdpSocket( host, port ); |
---|
[7631] | 65 | this->peers[0].userId = 0; |
---|
| 66 | this->peers[0].isServer = true; |
---|
[5996] | 67 | this->networkProtocol = new NetworkProtocol(); |
---|
| 68 | this->connectionMonitor = new ConnectionMonitor(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
[7565] | 72 | NetworkStream::NetworkStream( int port ) |
---|
[5647] | 73 | { |
---|
[6139] | 74 | this->type = NET_SERVER; |
---|
[5647] | 75 | this->init(); |
---|
[7565] | 76 | this->serverSocket = new UdpServerSocket(port); |
---|
[5741] | 77 | this->networkProtocol = new NetworkProtocol(); |
---|
| 78 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5996] | 79 | this->bActive = true; |
---|
[5649] | 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
[5647] | 83 | void NetworkStream::init() |
---|
| 84 | { |
---|
| 85 | /* set the class id for the base object */ |
---|
| 86 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
[5996] | 87 | this->bActive = false; |
---|
[6139] | 88 | this->serverSocket = NULL; |
---|
[6341] | 89 | this->networkGameManager = NULL; |
---|
[6139] | 90 | myHostId = 0; |
---|
[7565] | 91 | currentState = 0; |
---|
[5594] | 92 | } |
---|
| 93 | |
---|
[5647] | 94 | |
---|
[5566] | 95 | NetworkStream::~NetworkStream() |
---|
[5598] | 96 | { |
---|
[6139] | 97 | if ( this->serverSocket ) |
---|
| 98 | { |
---|
| 99 | serverSocket->close(); |
---|
| 100 | delete serverSocket; |
---|
| 101 | } |
---|
[5723] | 102 | |
---|
[7565] | 103 | for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++) |
---|
[6139] | 104 | { |
---|
[7565] | 105 | if ( i->second.socket ) |
---|
[6139] | 106 | { |
---|
[7565] | 107 | i->second.socket->disconnectServer(); |
---|
| 108 | delete i->second.socket; |
---|
| 109 | i->second.socket = NULL; |
---|
[6139] | 110 | } |
---|
[7565] | 111 | |
---|
| 112 | if ( i->second.handshake ) |
---|
[6139] | 113 | { |
---|
[7565] | 114 | delete i->second.handshake; |
---|
| 115 | i->second.handshake = NULL; |
---|
[6139] | 116 | } |
---|
| 117 | } |
---|
[7565] | 118 | |
---|
| 119 | if ( serverSocket ) |
---|
| 120 | { |
---|
| 121 | delete serverSocket; |
---|
| 122 | serverSocket = NULL; |
---|
| 123 | } |
---|
[5805] | 124 | |
---|
[5996] | 125 | delete connectionMonitor; |
---|
| 126 | delete networkProtocol; |
---|
[5598] | 127 | } |
---|
| 128 | |
---|
[5996] | 129 | |
---|
[6695] | 130 | void NetworkStream::createNetworkGameManager() |
---|
| 131 | { |
---|
| 132 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 133 | // setUniqueID( maxCon+2 ) because we need one id for every handshake |
---|
| 134 | // and one for handshake to reject client maxCon+1 |
---|
[7575] | 135 | this->networkGameManager->setUniqueID( MAX_CONNECTIONS + 2 ); |
---|
[7681] | 136 | MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
[6695] | 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | void NetworkStream::startHandshake() |
---|
| 141 | { |
---|
| 142 | Handshake* hs = new Handshake(false); |
---|
| 143 | hs->setUniqueID( 0 ); |
---|
[7565] | 144 | assert( peers[0].handshake == NULL ); |
---|
| 145 | peers[0].handshake = hs; |
---|
[7575] | 146 | // peers[0].handshake->setSynchronized( true ); |
---|
[6695] | 147 | //this->connectSynchronizeable(*hs); |
---|
[7591] | 148 | //this->connectSynchronizeable(*hs); |
---|
[7575] | 149 | PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName()); |
---|
[6695] | 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
[5996] | 153 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
| 154 | { |
---|
[6139] | 155 | this->synchronizeables.push_back(&sync); |
---|
| 156 | sync.setNetworkStream( this ); |
---|
| 157 | |
---|
[7565] | 158 | this->bActive = true; |
---|
[5996] | 159 | } |
---|
| 160 | |
---|
[6695] | 161 | |
---|
[6139] | 162 | void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) |
---|
| 163 | { |
---|
[6144] | 164 | // removing the Synchronizeable from the List. |
---|
| 165 | std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); |
---|
| 166 | if (disconnectSynchro != this->synchronizeables.end()) |
---|
| 167 | this->synchronizeables.erase(disconnectSynchro); |
---|
[7602] | 168 | |
---|
| 169 | //TODO set timestamp |
---|
| 170 | oldSynchronizeables[sync.getUniqueID()] = 0; |
---|
[6139] | 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
[5604] | 174 | void NetworkStream::processData() |
---|
| 175 | { |
---|
[7565] | 176 | currentState++; |
---|
| 177 | |
---|
[6139] | 178 | if ( this->type == NET_SERVER ) |
---|
[7571] | 179 | { |
---|
| 180 | if ( serverSocket ) |
---|
| 181 | serverSocket->update(); |
---|
| 182 | |
---|
[6139] | 183 | this->updateConnectionList(); |
---|
[7571] | 184 | } |
---|
[6139] | 185 | else |
---|
| 186 | { |
---|
[7565] | 187 | if ( peers[0].socket && !peers[0].socket->isOk() ) |
---|
[6139] | 188 | { |
---|
| 189 | PRINTF(1)("lost connection to server\n"); |
---|
[5741] | 190 | |
---|
[7565] | 191 | peers[0].socket->disconnectServer(); |
---|
| 192 | delete peers[0].socket; |
---|
| 193 | peers[0].socket = NULL; |
---|
[6498] | 194 | |
---|
[7565] | 195 | if ( peers[0].handshake ) |
---|
| 196 | delete peers[0].handshake; |
---|
| 197 | peers[0].handshake = NULL; |
---|
[6139] | 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
[7575] | 201 | handleHandshakes(); |
---|
[7671] | 202 | handleDownstream(); |
---|
[7575] | 203 | handleUpstream(); |
---|
[6341] | 204 | |
---|
[6139] | 205 | } |
---|
| 206 | |
---|
| 207 | void NetworkStream::updateConnectionList( ) |
---|
| 208 | { |
---|
| 209 | //check for new connections |
---|
| 210 | |
---|
| 211 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
| 212 | |
---|
| 213 | if ( tempNetworkSocket ) |
---|
[5810] | 214 | { |
---|
[6139] | 215 | int clientId; |
---|
| 216 | if ( freeSocketSlots.size() >0 ) |
---|
[5810] | 217 | { |
---|
[6139] | 218 | clientId = freeSocketSlots.back(); |
---|
| 219 | freeSocketSlots.pop_back(); |
---|
[7565] | 220 | peers[clientId].socket = tempNetworkSocket; |
---|
[7681] | 221 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); |
---|
[7565] | 222 | peers[clientId].handshake->setUniqueID(clientId); |
---|
| 223 | peers[clientId].userId = clientId; |
---|
[7631] | 224 | peers[clientId].isServer = false; |
---|
[6139] | 225 | } else |
---|
| 226 | { |
---|
[7575] | 227 | clientId = 1; |
---|
[7565] | 228 | |
---|
| 229 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
| 230 | if ( it->first >= clientId ) |
---|
| 231 | clientId = it->first + 1; |
---|
| 232 | |
---|
| 233 | peers[clientId].socket = tempNetworkSocket; |
---|
[7681] | 234 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); |
---|
[7565] | 235 | peers[clientId].handshake->setUniqueID(clientId); |
---|
| 236 | peers[clientId].userId = clientId; |
---|
[7631] | 237 | peers[clientId].isServer = false; |
---|
[7613] | 238 | |
---|
| 239 | PRINTF(0)("num sync: %d\n", synchronizeables.size()); |
---|
[6139] | 240 | } |
---|
[5730] | 241 | |
---|
[7575] | 242 | if ( clientId > MAX_CONNECTIONS ) |
---|
[6139] | 243 | { |
---|
[7565] | 244 | peers[clientId].handshake->doReject( "too many connections" ); |
---|
[6139] | 245 | PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); |
---|
[5810] | 246 | } |
---|
[6139] | 247 | else |
---|
| 248 | |
---|
| 249 | PRINTF(0)("New Client: %d\n", clientId); |
---|
| 250 | |
---|
[6695] | 251 | //this->connectSynchronizeable(*handshakes[clientId]); |
---|
[5802] | 252 | } |
---|
[5800] | 253 | |
---|
[5809] | 254 | |
---|
[6139] | 255 | //check if connections are ok else remove them |
---|
[7565] | 256 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
[6139] | 257 | { |
---|
[7565] | 258 | if ( it->second.socket && !it->second.socket->isOk() ) |
---|
[6139] | 259 | { |
---|
[7565] | 260 | PRINTF(0)("Client is gone: %d\n", it->second.userId); |
---|
[6139] | 261 | |
---|
[7565] | 262 | it->second.socket->disconnectServer(); |
---|
| 263 | delete it->second.socket; |
---|
| 264 | it->second.socket = NULL; |
---|
[6498] | 265 | |
---|
[7565] | 266 | if ( it->second.handshake ) |
---|
| 267 | delete it->second.handshake; |
---|
| 268 | it->second.handshake = NULL; |
---|
[7631] | 269 | |
---|
| 270 | for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) |
---|
| 271 | { |
---|
| 272 | (*it2)->cleanUpUser( it->second.userId ); |
---|
| 273 | } |
---|
[6139] | 274 | |
---|
[7565] | 275 | NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId); |
---|
[6737] | 276 | |
---|
[7565] | 277 | freeSocketSlots.push_back( it->second.userId ); |
---|
| 278 | |
---|
[6139] | 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | |
---|
[5604] | 283 | } |
---|
[6139] | 284 | |
---|
[6695] | 285 | void NetworkStream::debug() |
---|
| 286 | { |
---|
| 287 | if( this->isServer()) |
---|
| 288 | PRINT(0)(" Host ist Server with ID: %i\n", this->myHostId); |
---|
| 289 | else |
---|
| 290 | PRINT(0)(" Host ist Client with ID: %i\n", this->myHostId); |
---|
| 291 | |
---|
| 292 | PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); |
---|
| 293 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
| 294 | { |
---|
| 295 | if( (*it)->beSynchronized() == true) |
---|
| 296 | PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(), |
---|
| 297 | (*it)->getUniqueID(), (*it)->beSynchronized()); |
---|
| 298 | } |
---|
[7575] | 299 | PRINT(0)(" Maximal Connections: %i\n", MAX_CONNECTIONS ); |
---|
[6695] | 300 | |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | |
---|
| 304 | int NetworkStream::getSyncCount() |
---|
| 305 | { |
---|
| 306 | int n = 0; |
---|
| 307 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
| 308 | if( (*it)->beSynchronized() == true) |
---|
| 309 | ++n; |
---|
| 310 | |
---|
| 311 | //return synchronizeables.size(); |
---|
| 312 | return n; |
---|
| 313 | } |
---|
| 314 | |
---|
[7565] | 315 | /** |
---|
| 316 | * check if handshakes completed |
---|
| 317 | */ |
---|
| 318 | void NetworkStream::handleHandshakes( ) |
---|
| 319 | { |
---|
| 320 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
| 321 | { |
---|
| 322 | if ( it->second.handshake ) |
---|
| 323 | { |
---|
| 324 | if ( it->second.handshake->completed() ) |
---|
| 325 | { |
---|
| 326 | if ( it->second.handshake->ok() ) |
---|
| 327 | { |
---|
| 328 | if ( type != NET_SERVER ) |
---|
| 329 | { |
---|
| 330 | SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); |
---|
| 331 | myHostId = SharedNetworkData::getInstance()->getHostID(); |
---|
[6695] | 332 | |
---|
[7565] | 333 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 334 | this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); |
---|
[7681] | 335 | MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); |
---|
[7565] | 336 | } |
---|
[6695] | 337 | |
---|
[7565] | 338 | PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); |
---|
[6695] | 339 | |
---|
[7565] | 340 | delete it->second.handshake; |
---|
| 341 | it->second.handshake = NULL; |
---|
[7671] | 342 | |
---|
| 343 | handleNewClient( it->second.userId ); |
---|
[7565] | 344 | } |
---|
| 345 | else |
---|
| 346 | { |
---|
| 347 | PRINT(1)("handshake failed!\n"); |
---|
| 348 | it->second.socket->disconnectServer(); |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | } |
---|
| 352 | } |
---|
| 353 | } |
---|
[6695] | 354 | |
---|
[7565] | 355 | /** |
---|
| 356 | * handle upstream network traffic |
---|
| 357 | */ |
---|
| 358 | void NetworkStream::handleUpstream( ) |
---|
| 359 | { |
---|
| 360 | byte buf[UDP_PACKET_SIZE]; |
---|
| 361 | int offset; |
---|
| 362 | int n; |
---|
| 363 | |
---|
| 364 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
---|
| 365 | { |
---|
| 366 | offset = INTSIZE; //make already space for length |
---|
| 367 | |
---|
[7575] | 368 | if ( !peer->second.socket ) |
---|
[7565] | 369 | continue; |
---|
| 370 | |
---|
| 371 | n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 372 | assert( n == INTSIZE ); |
---|
| 373 | offset += n; |
---|
| 374 | |
---|
| 375 | n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 376 | assert( n == INTSIZE ); |
---|
| 377 | offset += n; |
---|
| 378 | |
---|
| 379 | n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 380 | assert( n == INTSIZE ); |
---|
| 381 | offset += n; |
---|
| 382 | |
---|
| 383 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
| 384 | { |
---|
| 385 | Synchronizeable & sync = **it; |
---|
[7591] | 386 | |
---|
[7575] | 387 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
| 388 | continue; |
---|
| 389 | |
---|
| 390 | //if handshake not finished only sync handshake |
---|
| 391 | if ( peer->second.handshake && sync.getLeafClassID() != CL_HANDSHAKE ) |
---|
| 392 | continue; |
---|
[7591] | 393 | |
---|
| 394 | if ( isServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) |
---|
| 395 | continue; |
---|
[7614] | 396 | |
---|
| 397 | //do not sync null parent |
---|
| 398 | if ( sync.getLeafClassID() == CL_NULL_PARENT ) |
---|
| 399 | continue; |
---|
[7575] | 400 | |
---|
[7565] | 401 | assert( offset + INTSIZE <= UDP_PACKET_SIZE ); |
---|
| 402 | |
---|
[7575] | 403 | //server fakes uniqueid=0 for handshake |
---|
| 404 | if ( this->isServer() && sync.getUniqueID() < MAX_CONNECTIONS - 1 ) |
---|
| 405 | n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 406 | else |
---|
| 407 | n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); |
---|
[7565] | 408 | assert( n == INTSIZE ); |
---|
| 409 | offset += n; |
---|
| 410 | |
---|
[7602] | 411 | //make space for size |
---|
| 412 | offset += INTSIZE; |
---|
[7614] | 413 | |
---|
[7602] | 414 | n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, 0 ); |
---|
| 415 | offset += n; |
---|
| 416 | |
---|
[7614] | 417 | assert( Converter::intToByteArray( n, buf + offset - n - INTSIZE, INTSIZE ) == INTSIZE ); |
---|
[7565] | 418 | } |
---|
| 419 | |
---|
| 420 | assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE ); |
---|
| 421 | |
---|
| 422 | assert( peer->second.socket->writePacket( buf, offset ) ); |
---|
[7614] | 423 | PRINTF(0)("send packet: %d userId = %d\n", offset, peer->second.userId); |
---|
[7565] | 424 | } |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | /** |
---|
| 428 | * handle downstream network traffic |
---|
| 429 | */ |
---|
| 430 | void NetworkStream::handleDownstream( ) |
---|
| 431 | { |
---|
| 432 | byte buf[UDP_PACKET_SIZE]; |
---|
| 433 | int offset = 0; |
---|
| 434 | |
---|
| 435 | int length = 0; |
---|
| 436 | int packetLength = 0; |
---|
| 437 | int uniqueId = 0; |
---|
| 438 | int state = 0; |
---|
| 439 | int ackedState = 0; |
---|
| 440 | int fromState = 0; |
---|
[7602] | 441 | int syncDataLength = 0; |
---|
[7565] | 442 | |
---|
| 443 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
---|
| 444 | { |
---|
[7614] | 445 | |
---|
[7575] | 446 | if ( !peer->second.socket ) |
---|
| 447 | continue; |
---|
| 448 | |
---|
[7681] | 449 | while ( 0 < (packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE )) ) |
---|
[7659] | 450 | { |
---|
[7575] | 451 | |
---|
[7659] | 452 | if ( packetLength < 4*INTSIZE ) |
---|
| 453 | { |
---|
| 454 | if ( packetLength != 0 ) |
---|
| 455 | PRINTF(1)("got too small packet: %d\n", packetLength); |
---|
| 456 | continue; |
---|
| 457 | } |
---|
[7565] | 458 | |
---|
[7659] | 459 | assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE ); |
---|
| 460 | assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE ); |
---|
| 461 | assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); |
---|
| 462 | assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); |
---|
| 463 | offset = 4*INTSIZE; |
---|
[7575] | 464 | |
---|
[7659] | 465 | PRINTF(0)("got packet: %d, %d\n", length, packetLength); |
---|
[7565] | 466 | |
---|
| 467 | //if this is an old state drop it |
---|
[7659] | 468 | if ( state <= peer->second.lastRecvedState ) |
---|
| 469 | continue; |
---|
[7565] | 470 | |
---|
[7659] | 471 | if ( packetLength != length ) |
---|
| 472 | { |
---|
| 473 | PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length); |
---|
| 474 | peer->second.socket->disconnectServer(); |
---|
| 475 | continue; |
---|
| 476 | } |
---|
[7614] | 477 | |
---|
[7659] | 478 | while ( offset < length ) |
---|
| 479 | { |
---|
| 480 | assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); |
---|
| 481 | offset += INTSIZE; |
---|
[7565] | 482 | |
---|
[7659] | 483 | assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE ); |
---|
| 484 | offset += INTSIZE; |
---|
[7602] | 485 | |
---|
[7659] | 486 | Synchronizeable * sync = NULL; |
---|
[7565] | 487 | |
---|
[7659] | 488 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
| 489 | { |
---|
[7575] | 490 | // client thinks his handshake has id 0!!!!! |
---|
[7659] | 491 | if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) |
---|
| 492 | { |
---|
| 493 | sync = *it; |
---|
| 494 | break; |
---|
| 495 | } |
---|
[7565] | 496 | } |
---|
| 497 | |
---|
[7659] | 498 | if ( sync == NULL ) |
---|
[7602] | 499 | { |
---|
[7659] | 500 | if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() ) |
---|
| 501 | { |
---|
| 502 | offset += syncDataLength; |
---|
| 503 | continue; |
---|
| 504 | } |
---|
[7602] | 505 | |
---|
[7565] | 506 | //TODO dont accept new object from all peers (probably only servers) |
---|
[7659] | 507 | int leafClassId; |
---|
| 508 | if ( INTSIZE > length - offset ) |
---|
| 509 | { |
---|
| 510 | offset += syncDataLength; |
---|
| 511 | continue; |
---|
| 512 | } |
---|
[7565] | 513 | |
---|
[7659] | 514 | Converter::byteArrayToInt( buf + offset, &leafClassId ); |
---|
[7565] | 515 | |
---|
[7659] | 516 | BaseObject * b; |
---|
| 517 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
---|
| 518 | /* Exception 1: NullParent */ |
---|
| 519 | if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE ) |
---|
| 520 | { |
---|
| 521 | PRINTF(1)("Can not create Class with ID %x!\n", (int)leafClassId); |
---|
| 522 | offset += syncDataLength; |
---|
| 523 | continue; |
---|
| 524 | } |
---|
| 525 | else |
---|
| 526 | b = Factory::fabricate( (ClassID)leafClassId ); |
---|
[7565] | 527 | |
---|
[7659] | 528 | if ( !b ) |
---|
| 529 | { |
---|
| 530 | PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); |
---|
| 531 | offset += syncDataLength; |
---|
| 532 | continue; |
---|
| 533 | } |
---|
[7565] | 534 | |
---|
[7659] | 535 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
| 536 | { |
---|
| 537 | sync = dynamic_cast<Synchronizeable*>(b); |
---|
| 538 | sync->setUniqueID( uniqueId ); |
---|
| 539 | sync->setSynchronized(true); |
---|
[7565] | 540 | |
---|
[7659] | 541 | PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID()); |
---|
| 542 | } |
---|
| 543 | else |
---|
| 544 | { |
---|
| 545 | PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId); |
---|
| 546 | delete b; |
---|
| 547 | offset += syncDataLength; |
---|
| 548 | continue; |
---|
| 549 | } |
---|
[7565] | 550 | } |
---|
[7659] | 551 | |
---|
| 552 | offset += sync->setStateDiff( peer->second.userId, buf+offset, length-offset, state, fromState ); |
---|
[7565] | 553 | } |
---|
| 554 | |
---|
[7659] | 555 | if ( offset != length ) |
---|
| 556 | { |
---|
| 557 | peer->second.socket->disconnectServer(); |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | peer->second.lastAckedState = ackedState; |
---|
[7565] | 561 | } |
---|
[7659] | 562 | |
---|
[7565] | 563 | } |
---|
[7614] | 564 | |
---|
[7565] | 565 | } |
---|
| 566 | |
---|
[7671] | 567 | /** |
---|
| 568 | * is executed when a handshake has finished |
---|
| 569 | * @todo create playable for new user |
---|
| 570 | */ |
---|
| 571 | void NetworkStream::handleNewClient( int userId ) |
---|
| 572 | { |
---|
| 573 | MessageManager::getInstance()->initUser( userId ); |
---|
| 574 | } |
---|
[7565] | 575 | |
---|
| 576 | |
---|
| 577 | |
---|
| 578 | |
---|
| 579 | |
---|
[7671] | 580 | |
---|