[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" |
---|
[7954] | 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" |
---|
[7954] | 31 | #include "message_manager.h" |
---|
| 32 | #include "preferences.h" |
---|
| 33 | #include "zip.h" |
---|
[6341] | 34 | |
---|
[7954] | 35 | #include "src/lib/util/loading/resource_manager.h" |
---|
| 36 | |
---|
| 37 | #include "network_log.h" |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | #include "lib/util/loading/factory.h" |
---|
| 41 | |
---|
[5649] | 42 | #include "debug.h" |
---|
[6139] | 43 | #include "class_list.h" |
---|
[6144] | 44 | #include <algorithm> |
---|
[5647] | 45 | |
---|
[5566] | 46 | /* include your own header */ |
---|
| 47 | #include "network_stream.h" |
---|
| 48 | |
---|
[5595] | 49 | /* probably unnecessary */ |
---|
[5594] | 50 | using namespace std; |
---|
| 51 | |
---|
[5595] | 52 | |
---|
[5747] | 53 | #define PACKAGE_SIZE 256 |
---|
[5647] | 54 | |
---|
[5747] | 55 | |
---|
[5800] | 56 | NetworkStream::NetworkStream() |
---|
[5996] | 57 | : DataStream() |
---|
[5647] | 58 | { |
---|
| 59 | this->init(); |
---|
[5648] | 60 | /* initialize the references */ |
---|
[5996] | 61 | this->type = NET_CLIENT; |
---|
[5647] | 62 | } |
---|
| 63 | |
---|
[6695] | 64 | |
---|
[7954] | 65 | NetworkStream::NetworkStream( std::string host, int port ) |
---|
[5996] | 66 | { |
---|
[6139] | 67 | this->type = NET_CLIENT; |
---|
[5996] | 68 | this->init(); |
---|
[7954] | 69 | this->peers[0].socket = new UdpSocket( host, port ); |
---|
| 70 | this->peers[0].userId = 0; |
---|
| 71 | this->peers[0].isServer = true; |
---|
| 72 | this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); |
---|
[5996] | 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
[7954] | 76 | NetworkStream::NetworkStream( int port ) |
---|
[5647] | 77 | { |
---|
[6139] | 78 | this->type = NET_SERVER; |
---|
[5647] | 79 | this->init(); |
---|
[7954] | 80 | this->serverSocket = new UdpServerSocket(port); |
---|
[5996] | 81 | this->bActive = true; |
---|
[5649] | 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
[5647] | 85 | void NetworkStream::init() |
---|
| 86 | { |
---|
| 87 | /* set the class id for the base object */ |
---|
| 88 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
[5996] | 89 | this->bActive = false; |
---|
[6139] | 90 | this->serverSocket = NULL; |
---|
[6341] | 91 | this->networkGameManager = NULL; |
---|
[6139] | 92 | myHostId = 0; |
---|
[7954] | 93 | currentState = 0; |
---|
| 94 | |
---|
| 95 | remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 ); |
---|
| 96 | |
---|
| 97 | assert( Zip::getInstance()->loadDictionary( "testdict" ) ); |
---|
[5594] | 98 | } |
---|
| 99 | |
---|
[5647] | 100 | |
---|
[5566] | 101 | NetworkStream::~NetworkStream() |
---|
[5598] | 102 | { |
---|
[6139] | 103 | if ( this->serverSocket ) |
---|
| 104 | { |
---|
| 105 | serverSocket->close(); |
---|
| 106 | delete serverSocket; |
---|
[8228] | 107 | serverSocket = NULL; |
---|
[6139] | 108 | } |
---|
[5723] | 109 | |
---|
[7954] | 110 | for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++) |
---|
[6139] | 111 | { |
---|
[7954] | 112 | if ( i->second.socket ) |
---|
[6139] | 113 | { |
---|
[7954] | 114 | i->second.socket->disconnectServer(); |
---|
| 115 | delete i->second.socket; |
---|
| 116 | i->second.socket = NULL; |
---|
[6139] | 117 | } |
---|
[7954] | 118 | |
---|
| 119 | if ( i->second.handshake ) |
---|
[6139] | 120 | { |
---|
[7954] | 121 | delete i->second.handshake; |
---|
| 122 | i->second.handshake = NULL; |
---|
[6139] | 123 | } |
---|
| 124 | } |
---|
[7954] | 125 | |
---|
[8228] | 126 | for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ ) |
---|
| 127 | (*it)->setNetworkStream( NULL ); |
---|
[5598] | 128 | } |
---|
| 129 | |
---|
[5996] | 130 | |
---|
[6695] | 131 | void NetworkStream::createNetworkGameManager() |
---|
| 132 | { |
---|
| 133 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 134 | // setUniqueID( maxCon+2 ) because we need one id for every handshake |
---|
| 135 | // and one for handshake to reject client maxCon+1 |
---|
[7954] | 136 | this->networkGameManager->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
| 137 | MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
[6695] | 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | void NetworkStream::startHandshake() |
---|
| 142 | { |
---|
| 143 | Handshake* hs = new Handshake(false); |
---|
| 144 | hs->setUniqueID( 0 ); |
---|
[7954] | 145 | assert( peers[0].handshake == NULL ); |
---|
| 146 | peers[0].handshake = hs; |
---|
| 147 | // peers[0].handshake->setSynchronized( true ); |
---|
[6695] | 148 | //this->connectSynchronizeable(*hs); |
---|
[7954] | 149 | //this->connectSynchronizeable(*hs); |
---|
| 150 | PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName()); |
---|
[6695] | 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
[5996] | 154 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
| 155 | { |
---|
[6139] | 156 | this->synchronizeables.push_back(&sync); |
---|
| 157 | sync.setNetworkStream( this ); |
---|
| 158 | |
---|
[7954] | 159 | this->bActive = true; |
---|
[5996] | 160 | } |
---|
| 161 | |
---|
[6695] | 162 | |
---|
[6139] | 163 | void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) |
---|
| 164 | { |
---|
[6144] | 165 | // removing the Synchronizeable from the List. |
---|
| 166 | std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); |
---|
| 167 | if (disconnectSynchro != this->synchronizeables.end()) |
---|
| 168 | this->synchronizeables.erase(disconnectSynchro); |
---|
[7954] | 169 | |
---|
| 170 | oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks(); |
---|
[6139] | 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
[5604] | 174 | void NetworkStream::processData() |
---|
| 175 | { |
---|
[8068] | 176 | int tick = SDL_GetTicks(); |
---|
| 177 | |
---|
[7954] | 178 | currentState++; |
---|
| 179 | |
---|
[6139] | 180 | if ( this->type == NET_SERVER ) |
---|
[7954] | 181 | { |
---|
| 182 | if ( serverSocket ) |
---|
| 183 | serverSocket->update(); |
---|
| 184 | |
---|
[6139] | 185 | this->updateConnectionList(); |
---|
[7954] | 186 | } |
---|
[6139] | 187 | else |
---|
| 188 | { |
---|
[7954] | 189 | if ( peers[0].socket && ( !peers[0].socket->isOk() || peers[0].connectionMonitor->hasTimedOut() ) ) |
---|
[6139] | 190 | { |
---|
| 191 | PRINTF(1)("lost connection to server\n"); |
---|
[5741] | 192 | |
---|
[7954] | 193 | peers[0].socket->disconnectServer(); |
---|
| 194 | delete peers[0].socket; |
---|
| 195 | peers[0].socket = NULL; |
---|
[6498] | 196 | |
---|
[7954] | 197 | if ( peers[0].handshake ) |
---|
| 198 | delete peers[0].handshake; |
---|
| 199 | peers[0].handshake = NULL; |
---|
[6139] | 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
[7954] | 203 | cleanUpOldSyncList(); |
---|
| 204 | handleHandshakes(); |
---|
| 205 | |
---|
| 206 | // order of up/downstream is important!!!! |
---|
| 207 | // don't change it |
---|
[8068] | 208 | handleDownstream( tick ); |
---|
| 209 | handleUpstream( tick ); |
---|
[7954] | 210 | |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | void NetworkStream::updateConnectionList( ) |
---|
| 214 | { |
---|
| 215 | //check for new connections |
---|
| 216 | |
---|
| 217 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
| 218 | |
---|
| 219 | if ( tempNetworkSocket ) |
---|
[6139] | 220 | { |
---|
[7954] | 221 | int clientId; |
---|
| 222 | if ( freeSocketSlots.size() >0 ) |
---|
[6139] | 223 | { |
---|
[7954] | 224 | clientId = freeSocketSlots.back(); |
---|
| 225 | freeSocketSlots.pop_back(); |
---|
| 226 | peers[clientId].socket = tempNetworkSocket; |
---|
| 227 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); |
---|
| 228 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
---|
| 229 | peers[clientId].handshake->setUniqueID(clientId); |
---|
| 230 | peers[clientId].userId = clientId; |
---|
| 231 | peers[clientId].isServer = false; |
---|
| 232 | } else |
---|
| 233 | { |
---|
| 234 | clientId = 1; |
---|
| 235 | |
---|
| 236 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
| 237 | if ( it->first >= clientId ) |
---|
| 238 | clientId = it->first + 1; |
---|
| 239 | |
---|
| 240 | peers[clientId].socket = tempNetworkSocket; |
---|
| 241 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); |
---|
| 242 | peers[clientId].handshake->setUniqueID(clientId); |
---|
| 243 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
---|
| 244 | peers[clientId].userId = clientId; |
---|
| 245 | peers[clientId].isServer = false; |
---|
| 246 | |
---|
| 247 | PRINTF(0)("num sync: %d\n", synchronizeables.size()); |
---|
| 248 | } |
---|
[6341] | 249 | |
---|
[7954] | 250 | if ( clientId > MAX_CONNECTIONS ) |
---|
| 251 | { |
---|
| 252 | peers[clientId].handshake->doReject( "too many connections" ); |
---|
| 253 | PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); |
---|
| 254 | } |
---|
| 255 | else |
---|
[6498] | 256 | |
---|
[7954] | 257 | PRINTF(0)("New Client: %d\n", clientId); |
---|
[6341] | 258 | |
---|
[7954] | 259 | //this->connectSynchronizeable(*handshakes[clientId]); |
---|
| 260 | } |
---|
[6341] | 261 | |
---|
[7954] | 262 | //check if connections are ok else remove them |
---|
[8228] | 263 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); ) |
---|
[7954] | 264 | { |
---|
| 265 | if ( |
---|
| 266 | it->second.socket && |
---|
| 267 | ( |
---|
| 268 | !it->second.socket->isOk() || |
---|
| 269 | it->second.connectionMonitor->hasTimedOut() |
---|
| 270 | ) |
---|
| 271 | ) |
---|
| 272 | { |
---|
| 273 | std::string reason = "disconnected"; |
---|
| 274 | if ( it->second.connectionMonitor->hasTimedOut() ) |
---|
| 275 | reason = "timeout"; |
---|
| 276 | PRINTF(0)("Client is gone: %d (%s)\n", it->second.userId, reason.c_str()); |
---|
| 277 | |
---|
[8068] | 278 | //assert(false); |
---|
[7954] | 279 | |
---|
| 280 | it->second.socket->disconnectServer(); |
---|
| 281 | delete it->second.socket; |
---|
| 282 | it->second.socket = NULL; |
---|
| 283 | |
---|
| 284 | if ( it->second.handshake ) |
---|
| 285 | delete it->second.handshake; |
---|
| 286 | it->second.handshake = NULL; |
---|
| 287 | |
---|
| 288 | for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) |
---|
| 289 | { |
---|
| 290 | (*it2)->cleanUpUser( it->second.userId ); |
---|
[6139] | 291 | } |
---|
[7954] | 292 | |
---|
| 293 | NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId); |
---|
| 294 | |
---|
| 295 | freeSocketSlots.push_back( it->second.userId ); |
---|
[8228] | 296 | |
---|
| 297 | PeerList::iterator delit = it; |
---|
| 298 | it++; |
---|
| 299 | |
---|
| 300 | peers.erase( delit ); |
---|
| 301 | |
---|
| 302 | continue; |
---|
[6139] | 303 | } |
---|
[8228] | 304 | |
---|
| 305 | it++; |
---|
[6139] | 306 | } |
---|
| 307 | |
---|
| 308 | |
---|
[7954] | 309 | } |
---|
[5800] | 310 | |
---|
[7954] | 311 | void NetworkStream::debug() |
---|
| 312 | { |
---|
| 313 | if( this->isServer()) |
---|
| 314 | PRINT(0)(" Host ist Server with ID: %i\n", this->myHostId); |
---|
| 315 | else |
---|
| 316 | PRINT(0)(" Host ist Client with ID: %i\n", this->myHostId); |
---|
[6695] | 317 | |
---|
[7954] | 318 | PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); |
---|
[6139] | 319 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
[5996] | 320 | { |
---|
[7954] | 321 | if( (*it)->beSynchronized() == true) |
---|
| 322 | PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(), |
---|
| 323 | (*it)->getUniqueID(), (*it)->beSynchronized()); |
---|
| 324 | } |
---|
| 325 | PRINT(0)(" Maximal Connections: %i\n", MAX_CONNECTIONS ); |
---|
[6959] | 326 | |
---|
[7954] | 327 | } |
---|
[6959] | 328 | |
---|
| 329 | |
---|
[7954] | 330 | int NetworkStream::getSyncCount() |
---|
| 331 | { |
---|
| 332 | int n = 0; |
---|
| 333 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
| 334 | if( (*it)->beSynchronized() == true) |
---|
| 335 | ++n; |
---|
[5730] | 336 | |
---|
[7954] | 337 | //return synchronizeables.size(); |
---|
| 338 | return n; |
---|
| 339 | } |
---|
[6139] | 340 | |
---|
[7954] | 341 | /** |
---|
| 342 | * check if handshakes completed |
---|
| 343 | */ |
---|
| 344 | void NetworkStream::handleHandshakes( ) |
---|
| 345 | { |
---|
| 346 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
| 347 | { |
---|
| 348 | if ( it->second.handshake ) |
---|
| 349 | { |
---|
| 350 | if ( it->second.handshake->completed() ) |
---|
| 351 | { |
---|
| 352 | if ( it->second.handshake->ok() ) |
---|
[6341] | 353 | { |
---|
[7954] | 354 | if ( !it->second.handshake->allowDel() ) |
---|
[6139] | 355 | { |
---|
[7954] | 356 | if ( type != NET_SERVER ) |
---|
[6868] | 357 | { |
---|
[7954] | 358 | SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); |
---|
| 359 | myHostId = SharedNetworkData::getInstance()->getHostID(); |
---|
| 360 | |
---|
| 361 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 362 | this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); |
---|
| 363 | MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); |
---|
[6868] | 364 | } |
---|
[7954] | 365 | |
---|
| 366 | |
---|
| 367 | PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); |
---|
| 368 | |
---|
| 369 | it->second.handshake->del(); |
---|
[6139] | 370 | } |
---|
| 371 | else |
---|
| 372 | { |
---|
[7954] | 373 | if ( it->second.handshake->canDel() ) |
---|
[6868] | 374 | { |
---|
[7954] | 375 | if ( type == NET_SERVER ) |
---|
| 376 | { |
---|
| 377 | handleNewClient( it->second.userId ); |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | PRINT(0)("handshake finished delete it\n"); |
---|
| 381 | delete it->second.handshake; |
---|
| 382 | it->second.handshake = NULL; |
---|
[6868] | 383 | } |
---|
[6139] | 384 | } |
---|
[7954] | 385 | |
---|
[6139] | 386 | } |
---|
| 387 | else |
---|
| 388 | { |
---|
[7954] | 389 | PRINT(1)("handshake failed!\n"); |
---|
| 390 | it->second.socket->disconnectServer(); |
---|
[6139] | 391 | } |
---|
[7954] | 392 | } |
---|
[6139] | 393 | } |
---|
[5996] | 394 | } |
---|
[7954] | 395 | } |
---|
[5741] | 396 | |
---|
[7954] | 397 | /** |
---|
| 398 | * handle upstream network traffic |
---|
| 399 | */ |
---|
[8068] | 400 | void NetworkStream::handleUpstream( int tick ) |
---|
[7954] | 401 | { |
---|
| 402 | int offset; |
---|
| 403 | int n; |
---|
| 404 | |
---|
[8068] | 405 | for ( PeerList::reverse_iterator peer = peers.rbegin(); peer != peers.rend(); peer++ ) |
---|
[5802] | 406 | { |
---|
[7954] | 407 | offset = INTSIZE; //make already space for length |
---|
| 408 | |
---|
| 409 | if ( !peer->second.socket ) |
---|
| 410 | continue; |
---|
| 411 | |
---|
| 412 | n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 413 | assert( n == INTSIZE ); |
---|
| 414 | offset += n; |
---|
| 415 | |
---|
| 416 | n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 417 | assert( n == INTSIZE ); |
---|
| 418 | offset += n; |
---|
| 419 | |
---|
| 420 | n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 421 | assert( n == INTSIZE ); |
---|
| 422 | offset += n; |
---|
| 423 | |
---|
| 424 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
[5810] | 425 | { |
---|
[7954] | 426 | int oldOffset = offset; |
---|
| 427 | Synchronizeable & sync = **it; |
---|
| 428 | |
---|
| 429 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
| 430 | continue; |
---|
[5730] | 431 | |
---|
[7954] | 432 | //if handshake not finished only sync handshake |
---|
| 433 | if ( peer->second.handshake && sync.getLeafClassID() != CL_HANDSHAKE ) |
---|
| 434 | continue; |
---|
| 435 | |
---|
| 436 | if ( isServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) |
---|
| 437 | continue; |
---|
| 438 | |
---|
| 439 | //do not sync null parent |
---|
| 440 | if ( sync.getLeafClassID() == CL_NULL_PARENT ) |
---|
| 441 | continue; |
---|
[6139] | 442 | |
---|
[7954] | 443 | assert( offset + INTSIZE <= UDP_PACKET_SIZE ); |
---|
| 444 | |
---|
| 445 | //server fakes uniqueid=0 for handshake |
---|
| 446 | if ( this->isServer() && sync.getUniqueID() < MAX_CONNECTIONS - 1 ) |
---|
| 447 | n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 448 | else |
---|
| 449 | n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 450 | assert( n == INTSIZE ); |
---|
| 451 | offset += n; |
---|
| 452 | |
---|
| 453 | //make space for size |
---|
| 454 | offset += INTSIZE; |
---|
[6139] | 455 | |
---|
[7954] | 456 | n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, -1000 ); |
---|
| 457 | offset += n; |
---|
| 458 | //NETPRINTF(0)("GGGGGEEEEETTTTT: %s (%d) %d\n",sync.getClassName(), sync.getUniqueID(), n); |
---|
| 459 | |
---|
| 460 | assert( Converter::intToByteArray( n, buf + offset - n - INTSIZE, INTSIZE ) == INTSIZE ); |
---|
| 461 | |
---|
| 462 | //check if all bytes == 0 -> remove data |
---|
| 463 | //TODO not all synchronizeables like this maybe add Synchronizeable::canRemoveZeroDiff() |
---|
| 464 | bool allZero = true; |
---|
| 465 | for ( int i = 0; i < n; i++ ) |
---|
| 466 | { |
---|
| 467 | if ( buf[i+oldOffset+2*INTSIZE] != 0 ) |
---|
| 468 | allZero = false; |
---|
| 469 | } |
---|
[6341] | 470 | |
---|
[7954] | 471 | if ( allZero ) |
---|
| 472 | { |
---|
| 473 | //NETPRINTF(n)("REMOVE ZERO DIFF: %s (%d)\n", sync.getClassName(), sync.getUniqueID()); |
---|
| 474 | offset = oldOffset; |
---|
| 475 | } |
---|
[6139] | 476 | |
---|
[7954] | 477 | |
---|
[5810] | 478 | } |
---|
[7954] | 479 | |
---|
| 480 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
| 481 | { |
---|
| 482 | Synchronizeable & sync = **it; |
---|
| 483 | |
---|
| 484 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
| 485 | continue; |
---|
| 486 | |
---|
| 487 | sync.handleSentState( peer->second.userId, currentState, peer->second.lastAckedState ); |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE ); |
---|
| 491 | |
---|
| 492 | int compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE ); |
---|
| 493 | |
---|
| 494 | if ( compLength < 0 ) |
---|
| 495 | { |
---|
| 496 | PRINTF(1)("compression failed!\n"); |
---|
| 497 | continue; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | assert( peer->second.socket->writePacket( compBuf, compLength ) ); |
---|
| 501 | |
---|
| 502 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
| 503 | writeToNewDict( buf, offset ); |
---|
| 504 | |
---|
[8068] | 505 | peer->second.connectionMonitor->processUnzippedOutgoingPacket( tick, buf, offset, currentState ); |
---|
| 506 | peer->second.connectionMonitor->processZippedOutgoingPacket( tick, compBuf, compLength, currentState ); |
---|
[7954] | 507 | |
---|
| 508 | //NETPRINTF(n)("send packet: %d userId = %d\n", offset, peer->second.userId); |
---|
[5810] | 509 | } |
---|
[6139] | 510 | } |
---|
| 511 | |
---|
[7954] | 512 | /** |
---|
| 513 | * handle downstream network traffic |
---|
| 514 | */ |
---|
[8068] | 515 | void NetworkStream::handleDownstream( int tick ) |
---|
[6139] | 516 | { |
---|
[7954] | 517 | int offset = 0; |
---|
| 518 | |
---|
| 519 | int length = 0; |
---|
| 520 | int packetLength = 0; |
---|
| 521 | int compLength = 0; |
---|
| 522 | int uniqueId = 0; |
---|
| 523 | int state = 0; |
---|
| 524 | int ackedState = 0; |
---|
| 525 | int fromState = 0; |
---|
| 526 | int syncDataLength = 0; |
---|
| 527 | |
---|
| 528 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
---|
[5810] | 529 | { |
---|
[7954] | 530 | |
---|
| 531 | if ( !peer->second.socket ) |
---|
| 532 | continue; |
---|
[5730] | 533 | |
---|
[7954] | 534 | while ( 0 < (compLength = peer->second.socket->readPacket( compBuf, UDP_PACKET_SIZE )) ) |
---|
[6139] | 535 | { |
---|
[8068] | 536 | peer->second.connectionMonitor->processZippedIncomingPacket( tick, compBuf, compLength ); |
---|
| 537 | |
---|
[7954] | 538 | //PRINTF(0)("GGGGGOOOOOOOOOOTTTTTTTT: %d\n", compLength); |
---|
| 539 | packetLength = Zip::getInstance()->unZip( compBuf, compLength, buf, UDP_PACKET_SIZE ); |
---|
| 540 | |
---|
| 541 | if ( packetLength < 4*INTSIZE ) |
---|
| 542 | { |
---|
| 543 | if ( packetLength != 0 ) |
---|
| 544 | PRINTF(1)("got too small packet: %d\n", packetLength); |
---|
| 545 | continue; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
| 549 | writeToNewDict( buf, packetLength ); |
---|
| 550 | |
---|
| 551 | assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE ); |
---|
| 552 | assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE ); |
---|
| 553 | assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); |
---|
| 554 | assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); |
---|
| 555 | //NETPRINTF(n)("ackedstate: %d\n", ackedState); |
---|
| 556 | offset = 4*INTSIZE; |
---|
[8068] | 557 | |
---|
| 558 | peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, offset, state, ackedState ); |
---|
[6139] | 559 | |
---|
[7954] | 560 | //NETPRINTF(n)("got packet: %d, %d\n", length, packetLength); |
---|
| 561 | |
---|
| 562 | //if this is an old state drop it |
---|
| 563 | if ( state <= peer->second.lastRecvedState ) |
---|
| 564 | continue; |
---|
| 565 | |
---|
| 566 | if ( packetLength != length ) |
---|
| 567 | { |
---|
| 568 | PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length); |
---|
| 569 | peer->second.socket->disconnectServer(); |
---|
| 570 | continue; |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | while ( offset + 2*INTSIZE < length ) |
---|
| 574 | { |
---|
| 575 | assert( offset > 0 ); |
---|
| 576 | assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); |
---|
| 577 | offset += INTSIZE; |
---|
| 578 | |
---|
| 579 | assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE ); |
---|
| 580 | offset += INTSIZE; |
---|
| 581 | |
---|
| 582 | assert( syncDataLength > 0 ); |
---|
| 583 | assert( syncDataLength < 10000 ); |
---|
| 584 | |
---|
| 585 | Synchronizeable * sync = NULL; |
---|
| 586 | |
---|
| 587 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
| 588 | { |
---|
| 589 | // client thinks his handshake has id 0!!!!! |
---|
| 590 | if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) |
---|
| 591 | { |
---|
| 592 | sync = *it; |
---|
| 593 | break; |
---|
| 594 | } |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | if ( sync == NULL ) |
---|
| 598 | { |
---|
| 599 | PRINTF(0)("could not find sync with id %d. try to create it\n", uniqueId); |
---|
| 600 | if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() ) |
---|
| 601 | { |
---|
| 602 | offset += syncDataLength; |
---|
| 603 | continue; |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | if ( !peers[peer->second.userId].isServer ) |
---|
| 607 | { |
---|
| 608 | offset += syncDataLength; |
---|
| 609 | continue; |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | int leafClassId; |
---|
| 613 | if ( INTSIZE > length - offset ) |
---|
| 614 | { |
---|
| 615 | offset += syncDataLength; |
---|
| 616 | continue; |
---|
| 617 | } |
---|
[6139] | 618 | |
---|
[7954] | 619 | Converter::byteArrayToInt( buf + offset, &leafClassId ); |
---|
| 620 | |
---|
| 621 | assert( leafClassId != 0 ); |
---|
| 622 | |
---|
| 623 | BaseObject * b = NULL; |
---|
| 624 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
---|
| 625 | /* Exception 1: NullParent */ |
---|
| 626 | if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE || leafClassId == CL_NETWORK_GAME_MANAGER ) |
---|
| 627 | { |
---|
| 628 | PRINTF(1)("Can not create Class with ID %x!\n", (int)leafClassId); |
---|
| 629 | offset += syncDataLength; |
---|
| 630 | continue; |
---|
| 631 | } |
---|
| 632 | else |
---|
| 633 | b = Factory::fabricate( (ClassID)leafClassId ); |
---|
[5800] | 634 | |
---|
[7954] | 635 | if ( !b ) |
---|
| 636 | { |
---|
| 637 | PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); |
---|
| 638 | offset += syncDataLength; |
---|
| 639 | continue; |
---|
| 640 | } |
---|
[5809] | 641 | |
---|
[7954] | 642 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
| 643 | { |
---|
| 644 | sync = dynamic_cast<Synchronizeable*>(b); |
---|
| 645 | sync->setUniqueID( uniqueId ); |
---|
| 646 | sync->setSynchronized(true); |
---|
| 647 | |
---|
| 648 | PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID()); |
---|
| 649 | } |
---|
| 650 | else |
---|
| 651 | { |
---|
| 652 | PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId); |
---|
| 653 | delete b; |
---|
| 654 | offset += syncDataLength; |
---|
| 655 | continue; |
---|
| 656 | } |
---|
| 657 | } |
---|
[8228] | 658 | |
---|
[6139] | 659 | |
---|
[7954] | 660 | int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); |
---|
| 661 | offset += n; |
---|
| 662 | //NETPRINTF(0)("SSSSSEEEEETTTTT: %s %d\n",sync->getClassName(), n); |
---|
[6498] | 663 | |
---|
[7954] | 664 | } |
---|
| 665 | |
---|
| 666 | if ( offset != length ) |
---|
[6139] | 667 | { |
---|
[7954] | 668 | PRINTF(0)("offset (%d) != length (%d)\n", offset, length); |
---|
| 669 | peer->second.socket->disconnectServer(); |
---|
[6139] | 670 | } |
---|
[7954] | 671 | |
---|
| 672 | |
---|
| 673 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
[6139] | 674 | { |
---|
[7954] | 675 | Synchronizeable & sync = **it; |
---|
| 676 | |
---|
| 677 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
| 678 | continue; |
---|
| 679 | |
---|
| 680 | sync.handleRecvState( peer->second.userId, state, fromState ); |
---|
[6139] | 681 | } |
---|
[7954] | 682 | |
---|
| 683 | assert( peer->second.lastAckedState <= ackedState ); |
---|
| 684 | peer->second.lastAckedState = ackedState; |
---|
| 685 | |
---|
| 686 | assert( peer->second.lastRecvedState < state ); |
---|
| 687 | peer->second.lastRecvedState = state; |
---|
[8228] | 688 | |
---|
[6139] | 689 | } |
---|
[7954] | 690 | |
---|
[6139] | 691 | } |
---|
[7954] | 692 | |
---|
| 693 | } |
---|
[6139] | 694 | |
---|
[7954] | 695 | /** |
---|
| 696 | * is executed when a handshake has finished |
---|
| 697 | * @todo create playable for new user |
---|
| 698 | */ |
---|
| 699 | void NetworkStream::handleNewClient( int userId ) |
---|
| 700 | { |
---|
| 701 | MessageManager::getInstance()->initUser( userId ); |
---|
| 702 | |
---|
| 703 | networkGameManager->signalNewPlayer( userId ); |
---|
[5604] | 704 | } |
---|
[6139] | 705 | |
---|
[7954] | 706 | /** |
---|
| 707 | * removes old items from oldSynchronizeables |
---|
| 708 | */ |
---|
| 709 | void NetworkStream::cleanUpOldSyncList( ) |
---|
[6139] | 710 | { |
---|
[7954] | 711 | int now = SDL_GetTicks(); |
---|
| 712 | |
---|
| 713 | for ( std::map<int,int>::iterator it = oldSynchronizeables.begin(); it != oldSynchronizeables.end(); ) |
---|
[6139] | 714 | { |
---|
[7954] | 715 | if ( it->second < now - 10*1000 ) |
---|
| 716 | { |
---|
| 717 | std::map<int,int>::iterator delIt = it; |
---|
| 718 | it++; |
---|
| 719 | oldSynchronizeables.erase( delIt ); |
---|
| 720 | continue; |
---|
| 721 | } |
---|
| 722 | it++; |
---|
[6139] | 723 | } |
---|
[7954] | 724 | } |
---|
| 725 | |
---|
| 726 | /** |
---|
| 727 | * writes data to DATA/dicts/newdict |
---|
| 728 | * @param data pointer to data |
---|
| 729 | * @param length length |
---|
| 730 | */ |
---|
| 731 | void NetworkStream::writeToNewDict( byte * data, int length ) |
---|
| 732 | { |
---|
| 733 | if ( remainingBytesToWriteToDict <= 0 ) |
---|
| 734 | return; |
---|
| 735 | |
---|
| 736 | if ( length > remainingBytesToWriteToDict ) |
---|
| 737 | length = remainingBytesToWriteToDict; |
---|
| 738 | |
---|
| 739 | std::string fileName = ResourceManager::getInstance()->getDataDir(); |
---|
| 740 | fileName += "/dicts/newdict"; |
---|
| 741 | |
---|
| 742 | FILE * f = fopen( fileName.c_str(), "a" ); |
---|
| 743 | |
---|
| 744 | if ( !f ) |
---|
[6139] | 745 | { |
---|
[7954] | 746 | PRINTF(2)("could not open %s\n", fileName.c_str()); |
---|
| 747 | remainingBytesToWriteToDict = 0; |
---|
[6139] | 748 | return; |
---|
| 749 | } |
---|
[7954] | 750 | |
---|
| 751 | if ( fwrite( data, 1, length, f ) != length ) |
---|
[6341] | 752 | { |
---|
[7954] | 753 | PRINTF(2)("could not write to file\n"); |
---|
| 754 | fclose( f ); |
---|
[6341] | 755 | return; |
---|
| 756 | } |
---|
[7954] | 757 | |
---|
| 758 | fclose( f ); |
---|
| 759 | |
---|
| 760 | remainingBytesToWriteToDict -= length; |
---|
[6139] | 761 | } |
---|
| 762 | |
---|
| 763 | |
---|
[6695] | 764 | |
---|
| 765 | |
---|
| 766 | |
---|
| 767 | |
---|