[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: |
---|
[9246] | 12 | main-programmer: Christoph Renner rennerc@ee.ethz.ch |
---|
| 13 | co-programmer: Patrick Boenzli boenzlip@orxonox.ethz.ch |
---|
[9252] | 14 | |
---|
| 15 | June 2006: finishing work on the network stream for pps presentation (rennerc@ee.ethz.ch) |
---|
| 16 | July 2006: some code rearangement and integration of the proxy server mechanism (boenzlip@ee.ethz.ch) |
---|
[5566] | 17 | */ |
---|
| 18 | |
---|
| 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" |
---|
[9275] | 27 | #include "monitor/connection_monitor.h" |
---|
| 28 | #include "monitor/network_monitor.h" |
---|
[5647] | 29 | #include "synchronizeable.h" |
---|
[9334] | 30 | #include "ip.h" |
---|
[6341] | 31 | #include "network_game_manager.h" |
---|
[6959] | 32 | #include "shared_network_data.h" |
---|
[7954] | 33 | #include "message_manager.h" |
---|
| 34 | #include "preferences.h" |
---|
| 35 | #include "zip.h" |
---|
[6341] | 36 | |
---|
[7954] | 37 | #include "src/lib/util/loading/resource_manager.h" |
---|
| 38 | |
---|
| 39 | #include "network_log.h" |
---|
| 40 | |
---|
[9235] | 41 | #include "player_stats.h" |
---|
[7954] | 42 | |
---|
| 43 | #include "lib/util/loading/factory.h" |
---|
| 44 | |
---|
[5649] | 45 | #include "debug.h" |
---|
[6139] | 46 | #include "class_list.h" |
---|
[6144] | 47 | #include <algorithm> |
---|
[5647] | 48 | |
---|
[9268] | 49 | |
---|
[5566] | 50 | #include "network_stream.h" |
---|
| 51 | |
---|
[9268] | 52 | |
---|
[5594] | 53 | using namespace std; |
---|
| 54 | |
---|
[5595] | 55 | |
---|
[5747] | 56 | #define PACKAGE_SIZE 256 |
---|
[5647] | 57 | |
---|
[5747] | 58 | |
---|
[9248] | 59 | /** |
---|
| 60 | * empty constructor |
---|
| 61 | */ |
---|
[5800] | 62 | NetworkStream::NetworkStream() |
---|
[5996] | 63 | : DataStream() |
---|
[5647] | 64 | { |
---|
| 65 | this->init(); |
---|
[5648] | 66 | /* initialize the references */ |
---|
[9285] | 67 | this->pInfo->nodeType = NET_CLIENT; |
---|
[5647] | 68 | } |
---|
| 69 | |
---|
[6695] | 70 | |
---|
[9246] | 71 | /** |
---|
[9248] | 72 | * start as a client, connect to a server |
---|
[9246] | 73 | * @param host: host name (address) |
---|
| 74 | * @param port: port number |
---|
| 75 | */ |
---|
[7954] | 76 | NetworkStream::NetworkStream( std::string host, int port ) |
---|
[5996] | 77 | { |
---|
| 78 | this->init(); |
---|
[9285] | 79 | // init the peers[0] the server to ceonnect to |
---|
[7954] | 80 | this->peers[0].socket = new UdpSocket( host, port ); |
---|
| 81 | this->peers[0].userId = 0; |
---|
[9282] | 82 | this->peers[0].nodeType = NET_MASTER_SERVER; |
---|
[7954] | 83 | this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); |
---|
[9308] | 84 | this->peers[0].ip = this->peers[0].socket->getRemoteAddress(); |
---|
[9285] | 85 | // and set also the localhost |
---|
| 86 | this->pInfo->nodeType = NET_CLIENT; |
---|
[9300] | 87 | // get the local ip address |
---|
[9334] | 88 | IPaddress ip; |
---|
| 89 | SDLNet_ResolveHost( &ip, NULL, port ); |
---|
| 90 | this->pInfo->ip = ip; |
---|
[5996] | 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
[9246] | 94 | /** |
---|
| 95 | * start as a server |
---|
| 96 | * @param port: at this port |
---|
| 97 | */ |
---|
[7954] | 98 | NetworkStream::NetworkStream( int port ) |
---|
[5647] | 99 | { |
---|
| 100 | this->init(); |
---|
[7954] | 101 | this->serverSocket = new UdpServerSocket(port); |
---|
[9285] | 102 | this->pInfo->nodeType = NET_MASTER_SERVER; |
---|
[9300] | 103 | // get the local ip address |
---|
[9334] | 104 | IPaddress ip; |
---|
| 105 | SDLNet_ResolveHost( &ip, NULL, port ); |
---|
| 106 | this->pInfo->ip = ip; |
---|
[5649] | 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
[9246] | 110 | /** |
---|
| 111 | * generic init functions |
---|
| 112 | */ |
---|
[5647] | 113 | void NetworkStream::init() |
---|
| 114 | { |
---|
| 115 | /* set the class id for the base object */ |
---|
| 116 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
[6139] | 117 | this->serverSocket = NULL; |
---|
[6341] | 118 | this->networkGameManager = NULL; |
---|
[9281] | 119 | this->networkMonitor = NULL; |
---|
[9279] | 120 | |
---|
[9284] | 121 | this->pInfo = new PeerInfo(); |
---|
| 122 | this->pInfo->userId = 0; |
---|
| 123 | this->pInfo->lastAckedState = 0; |
---|
| 124 | this->pInfo->lastRecvedState = 0; |
---|
| 125 | |
---|
[9285] | 126 | |
---|
[9248] | 127 | this->currentState = 0; |
---|
[9246] | 128 | |
---|
[7954] | 129 | remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 ); |
---|
[9246] | 130 | |
---|
[8623] | 131 | assert( Zip::getInstance()->loadDictionary( "testdict" ) >= 0 ); |
---|
| 132 | this->dictClient = Zip::getInstance()->loadDictionary( "dict2pl_client" ); |
---|
| 133 | assert( this->dictClient >= 0 ); |
---|
| 134 | this->dictServer = Zip::getInstance()->loadDictionary( "dict2p_server" ); |
---|
| 135 | assert( this->dictServer >= 0 ); |
---|
[5594] | 136 | } |
---|
| 137 | |
---|
[5647] | 138 | |
---|
[9246] | 139 | /** |
---|
| 140 | * deconstructor |
---|
| 141 | */ |
---|
[5566] | 142 | NetworkStream::~NetworkStream() |
---|
[5598] | 143 | { |
---|
[6139] | 144 | if ( this->serverSocket ) |
---|
| 145 | { |
---|
| 146 | serverSocket->close(); |
---|
| 147 | delete serverSocket; |
---|
[8228] | 148 | serverSocket = NULL; |
---|
[6139] | 149 | } |
---|
[7954] | 150 | for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++) |
---|
[6139] | 151 | { |
---|
[7954] | 152 | if ( i->second.socket ) |
---|
[6139] | 153 | { |
---|
[7954] | 154 | i->second.socket->disconnectServer(); |
---|
| 155 | delete i->second.socket; |
---|
| 156 | i->second.socket = NULL; |
---|
[6139] | 157 | } |
---|
[9246] | 158 | |
---|
[7954] | 159 | if ( i->second.handshake ) |
---|
[6139] | 160 | { |
---|
[7954] | 161 | delete i->second.handshake; |
---|
| 162 | i->second.handshake = NULL; |
---|
[6139] | 163 | } |
---|
[9246] | 164 | |
---|
[8623] | 165 | if ( i->second.connectionMonitor ) |
---|
| 166 | { |
---|
| 167 | delete i->second.connectionMonitor; |
---|
| 168 | i->second.connectionMonitor = NULL; |
---|
| 169 | } |
---|
[6139] | 170 | } |
---|
[8228] | 171 | for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ ) |
---|
| 172 | (*it)->setNetworkStream( NULL ); |
---|
[9284] | 173 | |
---|
| 174 | if( this->pInfo) |
---|
| 175 | delete this->pInfo; |
---|
[9300] | 176 | |
---|
| 177 | if( this->networkMonitor) |
---|
| 178 | delete this->networkMonitor; |
---|
[5598] | 179 | } |
---|
| 180 | |
---|
[5996] | 181 | |
---|
[9246] | 182 | /** |
---|
| 183 | * creates a new instance of the network game manager |
---|
| 184 | */ |
---|
[6695] | 185 | void NetworkStream::createNetworkGameManager() |
---|
| 186 | { |
---|
| 187 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 188 | // setUniqueID( maxCon+2 ) because we need one id for every handshake |
---|
| 189 | // and one for handshake to reject client maxCon+1 |
---|
[7954] | 190 | this->networkGameManager->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
| 191 | MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
[6695] | 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
[9246] | 195 | /** |
---|
| 196 | * starts the network handshake |
---|
[9300] | 197 | * handsakes are always initialized from the client side first. this starts the handshake and therefore is only |
---|
| 198 | * executed as client |
---|
[9246] | 199 | */ |
---|
[6695] | 200 | void NetworkStream::startHandshake() |
---|
| 201 | { |
---|
[9285] | 202 | Handshake* hs = new Handshake(this->pInfo->nodeType); |
---|
[6695] | 203 | hs->setUniqueID( 0 ); |
---|
[7954] | 204 | assert( peers[0].handshake == NULL ); |
---|
| 205 | peers[0].handshake = hs; |
---|
[9246] | 206 | |
---|
[9300] | 207 | // set the preferred nick name |
---|
[9235] | 208 | hs->setPreferedNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Player" ) ); |
---|
[9246] | 209 | |
---|
[7954] | 210 | PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName()); |
---|
[6695] | 211 | } |
---|
| 212 | |
---|
| 213 | |
---|
[9246] | 214 | /** |
---|
| 215 | * this functions connects a synchronizeable to the networkstream, therefore synchronizeing |
---|
| 216 | * it all over the network and creating it on the other platforms (if and only if it is a |
---|
| 217 | * server |
---|
| 218 | */ |
---|
[5996] | 219 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
| 220 | { |
---|
[6139] | 221 | this->synchronizeables.push_back(&sync); |
---|
| 222 | sync.setNetworkStream( this ); |
---|
| 223 | |
---|
[9254] | 224 | // this->bActive = true; |
---|
[5996] | 225 | } |
---|
| 226 | |
---|
[6695] | 227 | |
---|
[9246] | 228 | /** |
---|
| 229 | * removes the synchronizeable from the list of synchronized entities |
---|
| 230 | */ |
---|
[6139] | 231 | void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) |
---|
| 232 | { |
---|
[6144] | 233 | // removing the Synchronizeable from the List. |
---|
| 234 | std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); |
---|
| 235 | if (disconnectSynchro != this->synchronizeables.end()) |
---|
| 236 | this->synchronizeables.erase(disconnectSynchro); |
---|
[9246] | 237 | |
---|
[7954] | 238 | oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks(); |
---|
[6139] | 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
[9246] | 242 | /** |
---|
| 243 | * this is called to process data from the network socket to the synchronizeable and vice versa |
---|
| 244 | */ |
---|
[5604] | 245 | void NetworkStream::processData() |
---|
| 246 | { |
---|
[9281] | 247 | // create the network monitor after all the init work and before there is any connection handlings |
---|
| 248 | if( this->networkMonitor == NULL) |
---|
| 249 | this->networkMonitor = new NetworkMonitor(this); |
---|
| 250 | |
---|
| 251 | |
---|
[8068] | 252 | int tick = SDL_GetTicks(); |
---|
[9246] | 253 | |
---|
[9255] | 254 | this->currentState++; |
---|
| 255 | // there was a wrap around |
---|
| 256 | if( this->currentState < 0) |
---|
| 257 | { |
---|
| 258 | PRINTF(1)("A wrap around in the state variable as occured. The server was running so long? Pls restart server or write a mail to the supporters!\n"); |
---|
| 259 | } |
---|
[9246] | 260 | |
---|
[9285] | 261 | if ( this->pInfo->nodeType == NET_MASTER_SERVER ) |
---|
[7954] | 262 | { |
---|
[9255] | 263 | // execute everytthing the master server shoudl do |
---|
[7954] | 264 | if ( serverSocket ) |
---|
| 265 | serverSocket->update(); |
---|
[9246] | 266 | |
---|
[6139] | 267 | this->updateConnectionList(); |
---|
[7954] | 268 | } |
---|
[9303] | 269 | else if( this->pInfo->nodeType == NET_PROXY_SERVER_ACTIVE) |
---|
[9255] | 270 | { |
---|
| 271 | // execute everything the proxy server should do |
---|
| 272 | } |
---|
[6139] | 273 | else |
---|
| 274 | { |
---|
[9246] | 275 | // check if the connection is ok else terminate and remove |
---|
[7954] | 276 | if ( peers[0].socket && ( !peers[0].socket->isOk() || peers[0].connectionMonitor->hasTimedOut() ) ) |
---|
[6139] | 277 | { |
---|
| 278 | PRINTF(1)("lost connection to server\n"); |
---|
[5741] | 279 | |
---|
[7954] | 280 | peers[0].socket->disconnectServer(); |
---|
| 281 | delete peers[0].socket; |
---|
| 282 | peers[0].socket = NULL; |
---|
[6498] | 283 | |
---|
[7954] | 284 | if ( peers[0].handshake ) |
---|
| 285 | delete peers[0].handshake; |
---|
| 286 | peers[0].handshake = NULL; |
---|
[9246] | 287 | |
---|
[8623] | 288 | if ( peers[0].connectionMonitor ) |
---|
| 289 | delete peers[0].connectionMonitor; |
---|
| 290 | peers[0].connectionMonitor = NULL; |
---|
[6139] | 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
[7954] | 294 | cleanUpOldSyncList(); |
---|
| 295 | handleHandshakes(); |
---|
[9246] | 296 | |
---|
[9280] | 297 | // update the network monitor |
---|
| 298 | this->networkMonitor->process(); |
---|
| 299 | |
---|
[7954] | 300 | // order of up/downstream is important!!!! |
---|
| 301 | // don't change it |
---|
[8068] | 302 | handleDownstream( tick ); |
---|
| 303 | handleUpstream( tick ); |
---|
[7954] | 304 | } |
---|
| 305 | |
---|
[9246] | 306 | |
---|
| 307 | /** |
---|
[9308] | 308 | * if we are a NET_MASTER_SERVER or NET_PROXY_SERVER_ACTIVE update the connection list to accept new |
---|
| 309 | * connections (clients) also start the handsake for the new clients |
---|
[9246] | 310 | */ |
---|
[7954] | 311 | void NetworkStream::updateConnectionList( ) |
---|
| 312 | { |
---|
| 313 | //check for new connections |
---|
| 314 | |
---|
| 315 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
| 316 | |
---|
[9246] | 317 | // we got new network node |
---|
[7954] | 318 | if ( tempNetworkSocket ) |
---|
[6139] | 319 | { |
---|
[7954] | 320 | int clientId; |
---|
[9246] | 321 | // if there is a list of free client id slots, take these |
---|
| 322 | if ( freeSocketSlots.size() > 0 ) |
---|
[6139] | 323 | { |
---|
[7954] | 324 | clientId = freeSocketSlots.back(); |
---|
| 325 | freeSocketSlots.pop_back(); |
---|
| 326 | peers[clientId].socket = tempNetworkSocket; |
---|
[9285] | 327 | peers[clientId].handshake = new Handshake(this->pInfo->nodeType, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); |
---|
[9300] | 328 | peers[clientId].handshake->setUniqueID(clientId); |
---|
| 329 | |
---|
[7954] | 330 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
---|
| 331 | peers[clientId].userId = clientId; |
---|
[9246] | 332 | } |
---|
| 333 | else |
---|
[7954] | 334 | { |
---|
| 335 | clientId = 1; |
---|
[9246] | 336 | |
---|
[7954] | 337 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
| 338 | if ( it->first >= clientId ) |
---|
| 339 | clientId = it->first + 1; |
---|
[9246] | 340 | |
---|
[7954] | 341 | peers[clientId].socket = tempNetworkSocket; |
---|
[9300] | 342 | // handshake handling |
---|
[9285] | 343 | peers[clientId].handshake = new Handshake(this->pInfo->nodeType, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); |
---|
[7954] | 344 | peers[clientId].handshake->setUniqueID(clientId); |
---|
[9300] | 345 | |
---|
[7954] | 346 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
---|
| 347 | peers[clientId].userId = clientId; |
---|
[9246] | 348 | |
---|
[7954] | 349 | PRINTF(0)("num sync: %d\n", synchronizeables.size()); |
---|
| 350 | } |
---|
[6341] | 351 | |
---|
[9309] | 352 | |
---|
| 353 | // get the proxy server informations and write them to the handshake, if any (proxy) |
---|
| 354 | PeerInfo* pi = this->networkMonitor->getFirstChoiceProxy(); |
---|
| 355 | if( pi != NULL) |
---|
| 356 | peers[clientId].handshake->setProxy1Address( pi->ip); |
---|
| 357 | |
---|
| 358 | pi = this->networkMonitor->getSecondChoiceProxy(); |
---|
| 359 | if( pi != NULL) |
---|
| 360 | peers[clientId].handshake->setProxy2Address( pi->ip); |
---|
| 361 | |
---|
| 362 | // check if the connecting client should reconnect to a proxy server |
---|
[9334] | 363 | peers[clientId].handshake->setRedirect(this->networkMonitor->isReconnectNextClient()); |
---|
[9309] | 364 | |
---|
| 365 | |
---|
| 366 | peers[clientId].nodeType = NET_CLIENT; |
---|
| 367 | peers[clientId].ip = peers[clientId].socket->getRemoteAddress(); |
---|
| 368 | |
---|
| 369 | |
---|
[9300] | 370 | // check if there are too many clients connected (DEPRECATED: new: the masterserver sends a list of proxy servers) |
---|
[9334] | 371 | // if ( clientId > SharedNetworkData::getInstance()->getMaxPlayer() ) |
---|
| 372 | // { |
---|
| 373 | // // peers[clientId].handshake->setRedirect(true); |
---|
| 374 | // // |
---|
| 375 | // // peers[clientId].handshake->doReject( "too many connections" ); |
---|
| 376 | // PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); |
---|
| 377 | // } |
---|
| 378 | // else |
---|
| 379 | // { |
---|
| 380 | // PRINTF(0)("New Client: %d\n", clientId); |
---|
| 381 | // } |
---|
| 382 | PRINTF(0)("New Client: %d\n", clientId); |
---|
[6498] | 383 | |
---|
[9334] | 384 | |
---|
[7954] | 385 | } |
---|
[6341] | 386 | |
---|
[9246] | 387 | |
---|
| 388 | |
---|
[7954] | 389 | //check if connections are ok else remove them |
---|
[8228] | 390 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); ) |
---|
[7954] | 391 | { |
---|
[9246] | 392 | if ( |
---|
[7954] | 393 | it->second.socket && |
---|
[9246] | 394 | ( |
---|
[7954] | 395 | !it->second.socket->isOk() || |
---|
| 396 | it->second.connectionMonitor->hasTimedOut() |
---|
| 397 | ) |
---|
| 398 | ) |
---|
| 399 | { |
---|
| 400 | std::string reason = "disconnected"; |
---|
| 401 | if ( it->second.connectionMonitor->hasTimedOut() ) |
---|
| 402 | reason = "timeout"; |
---|
| 403 | PRINTF(0)("Client is gone: %d (%s)\n", it->second.userId, reason.c_str()); |
---|
| 404 | |
---|
[9246] | 405 | |
---|
| 406 | // clean up the network data |
---|
[7954] | 407 | it->second.socket->disconnectServer(); |
---|
| 408 | delete it->second.socket; |
---|
| 409 | it->second.socket = NULL; |
---|
| 410 | |
---|
[8623] | 411 | if ( it->second.connectionMonitor ) |
---|
| 412 | delete it->second.connectionMonitor; |
---|
| 413 | it->second.connectionMonitor = NULL; |
---|
[9246] | 414 | |
---|
[7954] | 415 | if ( it->second.handshake ) |
---|
| 416 | delete it->second.handshake; |
---|
| 417 | it->second.handshake = NULL; |
---|
[9246] | 418 | |
---|
[7954] | 419 | for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) |
---|
| 420 | { |
---|
| 421 | (*it2)->cleanUpUser( it->second.userId ); |
---|
[6139] | 422 | } |
---|
[7954] | 423 | |
---|
| 424 | NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId); |
---|
| 425 | |
---|
| 426 | freeSocketSlots.push_back( it->second.userId ); |
---|
[9246] | 427 | |
---|
[8228] | 428 | PeerList::iterator delit = it; |
---|
| 429 | it++; |
---|
[9246] | 430 | |
---|
[8228] | 431 | peers.erase( delit ); |
---|
[9246] | 432 | |
---|
[8228] | 433 | continue; |
---|
[6139] | 434 | } |
---|
[9246] | 435 | |
---|
[8228] | 436 | it++; |
---|
[6139] | 437 | } |
---|
| 438 | |
---|
| 439 | |
---|
[7954] | 440 | } |
---|
[5800] | 441 | |
---|
[9246] | 442 | |
---|
[7954] | 443 | void NetworkStream::debug() |
---|
| 444 | { |
---|
[9248] | 445 | if( this->isMasterServer()) |
---|
[9285] | 446 | PRINT(0)(" Host ist Server with ID: %i\n", this->pInfo->userId); |
---|
[7954] | 447 | else |
---|
[9285] | 448 | PRINT(0)(" Host ist Client with ID: %i\n", this->pInfo->userId); |
---|
[6695] | 449 | |
---|
[7954] | 450 | PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); |
---|
[6139] | 451 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
[5996] | 452 | { |
---|
[7954] | 453 | if( (*it)->beSynchronized() == true) |
---|
| 454 | PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(), |
---|
| 455 | (*it)->getUniqueID(), (*it)->beSynchronized()); |
---|
| 456 | } |
---|
[9327] | 457 | PRINT(0)(" Maximal Connections: %i\n", SharedNetworkData::getInstance()->getMaxPlayer() ); |
---|
[6959] | 458 | |
---|
[7954] | 459 | } |
---|
[6959] | 460 | |
---|
| 461 | |
---|
[9246] | 462 | /** |
---|
| 463 | * @returns the number of synchronizeables registered to this stream |
---|
| 464 | */ |
---|
[7954] | 465 | int NetworkStream::getSyncCount() |
---|
| 466 | { |
---|
| 467 | int n = 0; |
---|
| 468 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
| 469 | if( (*it)->beSynchronized() == true) |
---|
| 470 | ++n; |
---|
[5730] | 471 | |
---|
[7954] | 472 | //return synchronizeables.size(); |
---|
| 473 | return n; |
---|
| 474 | } |
---|
[6139] | 475 | |
---|
[9246] | 476 | |
---|
[7954] | 477 | /** |
---|
[9246] | 478 | * check if handshakes completed. if so create the network game manager else remove it again |
---|
[7954] | 479 | */ |
---|
| 480 | void NetworkStream::handleHandshakes( ) |
---|
| 481 | { |
---|
| 482 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
---|
| 483 | { |
---|
| 484 | if ( it->second.handshake ) |
---|
| 485 | { |
---|
[9269] | 486 | // handshake finished |
---|
[7954] | 487 | if ( it->second.handshake->completed() ) |
---|
| 488 | { |
---|
[9269] | 489 | //handshake is correct |
---|
[7954] | 490 | if ( it->second.handshake->ok() ) |
---|
[6341] | 491 | { |
---|
[7954] | 492 | if ( !it->second.handshake->allowDel() ) |
---|
[6139] | 493 | { |
---|
[9285] | 494 | if ( this->pInfo->nodeType == NET_CLIENT ) |
---|
[6868] | 495 | { |
---|
[7954] | 496 | SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); |
---|
[9285] | 497 | this->pInfo->userId = SharedNetworkData::getInstance()->getHostID(); |
---|
[7954] | 498 | |
---|
[9271] | 499 | it->second.nodeType = it->second.handshake->getRemoteNodeType(); |
---|
[9308] | 500 | it->second.ip = it->second.socket->getRemoteAddress(); |
---|
[9334] | 501 | // add the new server to the nodes list (it can be a NET_MASTER_SERVER or NET_PROXY_SERVER) |
---|
[9282] | 502 | this->networkMonitor->addNode(&it->second); |
---|
[9300] | 503 | // get proxy 1 address and add it |
---|
[9308] | 504 | this->networkMonitor->addNode(it->second.handshake->getProxy1Address(), NET_PROXY_SERVER_ACTIVE); |
---|
[9300] | 505 | // get proxy 2 address and add it |
---|
[9308] | 506 | this->networkMonitor->addNode(it->second.handshake->getProxy2Address(), NET_PROXY_SERVER_ACTIVE); |
---|
[9300] | 507 | |
---|
[9334] | 508 | // now check if the server accepted the connection |
---|
[9300] | 509 | if( it->second.handshake->redirect()) |
---|
[9334] | 510 | this->handleReconnect( it->second.userId); |
---|
[9300] | 511 | |
---|
| 512 | // create the new network game manager and init it |
---|
[7954] | 513 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 514 | this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); |
---|
[9300] | 515 | // init the new message manager |
---|
[7954] | 516 | MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); |
---|
[6868] | 517 | } |
---|
[7954] | 518 | |
---|
[9246] | 519 | |
---|
[7954] | 520 | PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); |
---|
| 521 | |
---|
| 522 | it->second.handshake->del(); |
---|
[6139] | 523 | } |
---|
| 524 | else |
---|
| 525 | { |
---|
[9308] | 526 | // handshake finished registring new player |
---|
[7954] | 527 | if ( it->second.handshake->canDel() ) |
---|
[6868] | 528 | { |
---|
[9285] | 529 | if ( this->pInfo->nodeType == NET_MASTER_SERVER ) |
---|
[7954] | 530 | { |
---|
[9271] | 531 | it->second.nodeType = it->second.handshake->getRemoteNodeType(); |
---|
[9308] | 532 | it->second.ip = it->second.socket->getRemoteAddress(); |
---|
| 533 | |
---|
[9287] | 534 | this->networkMonitor->addNode(&it->second); |
---|
[9271] | 535 | |
---|
[9334] | 536 | this->handleNewClient( it->second.userId ); |
---|
[9246] | 537 | |
---|
[9235] | 538 | if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) |
---|
| 539 | { |
---|
| 540 | PlayerStats::getStats( it->second.userId )->setNickName( it->second.handshake->getPreferedNickName() ); |
---|
| 541 | } |
---|
[7954] | 542 | } |
---|
[9246] | 543 | |
---|
[7954] | 544 | PRINT(0)("handshake finished delete it\n"); |
---|
| 545 | delete it->second.handshake; |
---|
| 546 | it->second.handshake = NULL; |
---|
[6868] | 547 | } |
---|
[6139] | 548 | } |
---|
[7954] | 549 | |
---|
[6139] | 550 | } |
---|
| 551 | else |
---|
| 552 | { |
---|
[7954] | 553 | PRINT(1)("handshake failed!\n"); |
---|
| 554 | it->second.socket->disconnectServer(); |
---|
[6139] | 555 | } |
---|
[7954] | 556 | } |
---|
[6139] | 557 | } |
---|
[5996] | 558 | } |
---|
[7954] | 559 | } |
---|
[5741] | 560 | |
---|
[9246] | 561 | |
---|
[7954] | 562 | /** |
---|
[9334] | 563 | * this functions handles a reconnect event received from the a NET_MASTER_SERVER or NET_PROXY_SERVER |
---|
| 564 | */ |
---|
| 565 | void NetworkStream::handleReconnect(int userId) |
---|
| 566 | { |
---|
| 567 | PRINTF(0)("===============================================\n"); |
---|
| 568 | PRINTF(0)("Client is redirected to the other proxy servers\n"); |
---|
| 569 | PRINTF(0)("===============================================\n"); |
---|
| 570 | |
---|
| 571 | PeerInfo* pInfo = &this->peers[userId]; |
---|
| 572 | |
---|
| 573 | // reject the server |
---|
| 574 | pInfo->handshake->doReject( "redirected to different server"); |
---|
| 575 | |
---|
| 576 | // flush the old synchronization states, since the numbering could be completely different |
---|
| 577 | pInfo->lastAckedState = 0; |
---|
| 578 | pInfo->lastRecvedState = 0; |
---|
| 579 | // not sure if this works as expected |
---|
| 580 | if( pInfo->handshake) |
---|
| 581 | delete pInfo->handshake; |
---|
| 582 | |
---|
| 583 | // disconnect from the current server and reconnect to proxy server |
---|
| 584 | pInfo->socket->reconnectToServer( pInfo->handshake->getProxy1Address().ipString(), pInfo->handshake->getProxy1Address().port()); |
---|
| 585 | |
---|
| 586 | // and restart the handshake |
---|
| 587 | this->startHandshake(); |
---|
| 588 | } |
---|
| 589 | |
---|
| 590 | |
---|
| 591 | /** |
---|
[7954] | 592 | * handle upstream network traffic |
---|
| 593 | */ |
---|
[8068] | 594 | void NetworkStream::handleUpstream( int tick ) |
---|
[7954] | 595 | { |
---|
| 596 | int offset; |
---|
| 597 | int n; |
---|
[9246] | 598 | |
---|
[8068] | 599 | for ( PeerList::reverse_iterator peer = peers.rbegin(); peer != peers.rend(); peer++ ) |
---|
[5802] | 600 | { |
---|
[9246] | 601 | offset = INTSIZE; // reserve enough space for the packet length |
---|
| 602 | |
---|
| 603 | // continue with the next peer if this peer has no socket assigned (therefore no network) |
---|
[7954] | 604 | if ( !peer->second.socket ) |
---|
| 605 | continue; |
---|
[9246] | 606 | |
---|
| 607 | // header informations: current state |
---|
[7954] | 608 | n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 609 | assert( n == INTSIZE ); |
---|
| 610 | offset += n; |
---|
[9246] | 611 | |
---|
| 612 | // header informations: last acked state |
---|
[7954] | 613 | n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 614 | assert( n == INTSIZE ); |
---|
| 615 | offset += n; |
---|
[9246] | 616 | |
---|
| 617 | // header informations: last recved state |
---|
[7954] | 618 | n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 619 | assert( n == INTSIZE ); |
---|
| 620 | offset += n; |
---|
[9246] | 621 | |
---|
| 622 | // now write all synchronizeables in the packet |
---|
[7954] | 623 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
[5810] | 624 | { |
---|
[7954] | 625 | int oldOffset = offset; |
---|
| 626 | Synchronizeable & sync = **it; |
---|
[9246] | 627 | |
---|
| 628 | // do not include synchronizeables with uninit id and syncs that don't want to be synchronized |
---|
[7954] | 629 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
| 630 | continue; |
---|
[5730] | 631 | |
---|
[9246] | 632 | // if handshake not finished only sync handshake |
---|
[7954] | 633 | if ( peer->second.handshake && sync.getLeafClassID() != CL_HANDSHAKE ) |
---|
| 634 | continue; |
---|
[9246] | 635 | |
---|
| 636 | // if we are a server and this is not our handshake |
---|
[9249] | 637 | if ( isMasterServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) |
---|
[7954] | 638 | continue; |
---|
[9246] | 639 | |
---|
| 640 | /* list of synchronizeables that will never be synchronized over the network: */ |
---|
| 641 | // do not sync null parent |
---|
[7954] | 642 | if ( sync.getLeafClassID() == CL_NULL_PARENT ) |
---|
| 643 | continue; |
---|
[6139] | 644 | |
---|
[7954] | 645 | assert( offset + INTSIZE <= UDP_PACKET_SIZE ); |
---|
[9246] | 646 | |
---|
| 647 | // server fakes uniqueid == 0 for handshake |
---|
[9327] | 648 | if ( this->isMasterServer() && sync.getUniqueID() < SharedNetworkData::getInstance()->getMaxPlayer() - 1 ) |
---|
[7954] | 649 | n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); |
---|
| 650 | else |
---|
| 651 | n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); |
---|
[9246] | 652 | |
---|
[7954] | 653 | assert( n == INTSIZE ); |
---|
| 654 | offset += n; |
---|
[9246] | 655 | |
---|
| 656 | // make space for size |
---|
[7954] | 657 | offset += INTSIZE; |
---|
[6139] | 658 | |
---|
[7954] | 659 | n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, -1000 ); |
---|
| 660 | offset += n; |
---|
[9246] | 661 | |
---|
[7954] | 662 | assert( Converter::intToByteArray( n, buf + offset - n - INTSIZE, INTSIZE ) == INTSIZE ); |
---|
[6341] | 663 | |
---|
[9246] | 664 | // check if all data bytes == 0 -> remove data and the synchronizeable from the sync process since there is no update |
---|
| 665 | // TODO not all synchronizeables like this maybe add Synchronizeable::canRemoveZeroDiff() |
---|
| 666 | bool allZero = true; |
---|
| 667 | for ( int i = 0; i < n; i++ ) |
---|
| 668 | { |
---|
| 669 | if ( buf[i+oldOffset+2*INTSIZE] != 0 ) |
---|
| 670 | allZero = false; |
---|
| 671 | } |
---|
| 672 | // if there is no new data in this synchronizeable reset the data offset to the last state -> dont synchronizes |
---|
| 673 | // data that hast not changed |
---|
| 674 | if ( allZero ) |
---|
| 675 | { |
---|
| 676 | offset = oldOffset; |
---|
| 677 | } |
---|
| 678 | } // all synchronizeables written |
---|
[6139] | 679 | |
---|
[9246] | 680 | |
---|
| 681 | |
---|
[7954] | 682 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
| 683 | { |
---|
| 684 | Synchronizeable & sync = **it; |
---|
[9246] | 685 | |
---|
[7954] | 686 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
| 687 | continue; |
---|
[9246] | 688 | |
---|
[7954] | 689 | sync.handleSentState( peer->second.userId, currentState, peer->second.lastAckedState ); |
---|
| 690 | } |
---|
[9246] | 691 | |
---|
| 692 | |
---|
[7954] | 693 | assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE ); |
---|
[9246] | 694 | |
---|
| 695 | // now compress the data with the zip library |
---|
[8623] | 696 | int compLength = 0; |
---|
[9249] | 697 | if ( this->isMasterServer() ) |
---|
[8623] | 698 | compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer ); |
---|
| 699 | else |
---|
| 700 | compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictClient ); |
---|
[9246] | 701 | |
---|
[8623] | 702 | if ( compLength <= 0 ) |
---|
[7954] | 703 | { |
---|
| 704 | PRINTF(1)("compression failed!\n"); |
---|
| 705 | continue; |
---|
| 706 | } |
---|
[9246] | 707 | |
---|
[7954] | 708 | assert( peer->second.socket->writePacket( compBuf, compLength ) ); |
---|
[9246] | 709 | |
---|
[7954] | 710 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
[8623] | 711 | writeToNewDict( buf, offset, true ); |
---|
[9246] | 712 | |
---|
[8068] | 713 | peer->second.connectionMonitor->processUnzippedOutgoingPacket( tick, buf, offset, currentState ); |
---|
| 714 | peer->second.connectionMonitor->processZippedOutgoingPacket( tick, compBuf, compLength, currentState ); |
---|
[9246] | 715 | |
---|
[5810] | 716 | } |
---|
[6139] | 717 | } |
---|
| 718 | |
---|
[7954] | 719 | /** |
---|
| 720 | * handle downstream network traffic |
---|
| 721 | */ |
---|
[8068] | 722 | void NetworkStream::handleDownstream( int tick ) |
---|
[6139] | 723 | { |
---|
[7954] | 724 | int offset = 0; |
---|
[9246] | 725 | |
---|
[7954] | 726 | int length = 0; |
---|
| 727 | int packetLength = 0; |
---|
| 728 | int compLength = 0; |
---|
| 729 | int uniqueId = 0; |
---|
| 730 | int state = 0; |
---|
| 731 | int ackedState = 0; |
---|
| 732 | int fromState = 0; |
---|
| 733 | int syncDataLength = 0; |
---|
[9246] | 734 | |
---|
[7954] | 735 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
---|
[5810] | 736 | { |
---|
[9246] | 737 | |
---|
[7954] | 738 | if ( !peer->second.socket ) |
---|
| 739 | continue; |
---|
[5730] | 740 | |
---|
[7954] | 741 | while ( 0 < (compLength = peer->second.socket->readPacket( compBuf, UDP_PACKET_SIZE )) ) |
---|
[6139] | 742 | { |
---|
[8068] | 743 | peer->second.connectionMonitor->processZippedIncomingPacket( tick, compBuf, compLength ); |
---|
[9246] | 744 | |
---|
[7954] | 745 | packetLength = Zip::getInstance()->unZip( compBuf, compLength, buf, UDP_PACKET_SIZE ); |
---|
[8623] | 746 | |
---|
[7954] | 747 | if ( packetLength < 4*INTSIZE ) |
---|
| 748 | { |
---|
| 749 | if ( packetLength != 0 ) |
---|
| 750 | PRINTF(1)("got too small packet: %d\n", packetLength); |
---|
| 751 | continue; |
---|
| 752 | } |
---|
[9246] | 753 | |
---|
[7954] | 754 | if ( this->remainingBytesToWriteToDict > 0 ) |
---|
[8623] | 755 | writeToNewDict( buf, packetLength, false ); |
---|
[9246] | 756 | |
---|
[7954] | 757 | assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE ); |
---|
| 758 | assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE ); |
---|
| 759 | assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); |
---|
| 760 | assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); |
---|
| 761 | offset = 4*INTSIZE; |
---|
[9246] | 762 | |
---|
[8623] | 763 | peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, packetLength, state, ackedState ); |
---|
[6139] | 764 | |
---|
[9246] | 765 | |
---|
[9255] | 766 | //if this is an old state drop it |
---|
[7954] | 767 | if ( state <= peer->second.lastRecvedState ) |
---|
| 768 | continue; |
---|
[9246] | 769 | |
---|
[7954] | 770 | if ( packetLength != length ) |
---|
| 771 | { |
---|
| 772 | PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length); |
---|
| 773 | peer->second.socket->disconnectServer(); |
---|
| 774 | continue; |
---|
| 775 | } |
---|
[9246] | 776 | |
---|
[7954] | 777 | while ( offset + 2*INTSIZE < length ) |
---|
| 778 | { |
---|
| 779 | assert( offset > 0 ); |
---|
| 780 | assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); |
---|
| 781 | offset += INTSIZE; |
---|
[9246] | 782 | |
---|
[7954] | 783 | assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE ); |
---|
| 784 | offset += INTSIZE; |
---|
[9246] | 785 | |
---|
[7954] | 786 | assert( syncDataLength > 0 ); |
---|
| 787 | assert( syncDataLength < 10000 ); |
---|
[9246] | 788 | |
---|
[7954] | 789 | Synchronizeable * sync = NULL; |
---|
[9246] | 790 | |
---|
[7954] | 791 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
[9246] | 792 | { |
---|
[7954] | 793 | // client thinks his handshake has id 0!!!!! |
---|
| 794 | if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) |
---|
| 795 | { |
---|
| 796 | sync = *it; |
---|
| 797 | break; |
---|
| 798 | } |
---|
| 799 | } |
---|
[9246] | 800 | |
---|
[7954] | 801 | if ( sync == NULL ) |
---|
| 802 | { |
---|
| 803 | PRINTF(0)("could not find sync with id %d. try to create it\n", uniqueId); |
---|
| 804 | if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() ) |
---|
| 805 | { |
---|
| 806 | offset += syncDataLength; |
---|
| 807 | continue; |
---|
| 808 | } |
---|
[9246] | 809 | |
---|
[9282] | 810 | if ( !peers[peer->second.userId].isMasterServer() ) |
---|
[7954] | 811 | { |
---|
| 812 | offset += syncDataLength; |
---|
| 813 | continue; |
---|
| 814 | } |
---|
[9246] | 815 | |
---|
[7954] | 816 | int leafClassId; |
---|
| 817 | if ( INTSIZE > length - offset ) |
---|
| 818 | { |
---|
| 819 | offset += syncDataLength; |
---|
| 820 | continue; |
---|
| 821 | } |
---|
[6139] | 822 | |
---|
[7954] | 823 | Converter::byteArrayToInt( buf + offset, &leafClassId ); |
---|
[9246] | 824 | |
---|
[7954] | 825 | assert( leafClassId != 0 ); |
---|
[9246] | 826 | |
---|
[7954] | 827 | BaseObject * b = NULL; |
---|
| 828 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
---|
| 829 | /* Exception 1: NullParent */ |
---|
| 830 | if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE || leafClassId == CL_NETWORK_GAME_MANAGER ) |
---|
| 831 | { |
---|
| 832 | PRINTF(1)("Can not create Class with ID %x!\n", (int)leafClassId); |
---|
| 833 | offset += syncDataLength; |
---|
| 834 | continue; |
---|
| 835 | } |
---|
| 836 | else |
---|
| 837 | b = Factory::fabricate( (ClassID)leafClassId ); |
---|
[5800] | 838 | |
---|
[7954] | 839 | if ( !b ) |
---|
| 840 | { |
---|
| 841 | PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); |
---|
| 842 | offset += syncDataLength; |
---|
| 843 | continue; |
---|
| 844 | } |
---|
[5809] | 845 | |
---|
[7954] | 846 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
| 847 | { |
---|
| 848 | sync = dynamic_cast<Synchronizeable*>(b); |
---|
| 849 | sync->setUniqueID( uniqueId ); |
---|
| 850 | sync->setSynchronized(true); |
---|
[9246] | 851 | |
---|
[7954] | 852 | PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID()); |
---|
| 853 | } |
---|
| 854 | else |
---|
| 855 | { |
---|
| 856 | PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId); |
---|
| 857 | delete b; |
---|
| 858 | offset += syncDataLength; |
---|
| 859 | continue; |
---|
| 860 | } |
---|
| 861 | } |
---|
[6139] | 862 | |
---|
[9246] | 863 | |
---|
| 864 | int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); |
---|
[7954] | 865 | offset += n; |
---|
| 866 | //NETPRINTF(0)("SSSSSEEEEETTTTT: %s %d\n",sync->getClassName(), n); |
---|
[6498] | 867 | |
---|
[7954] | 868 | } |
---|
[9246] | 869 | |
---|
[7954] | 870 | if ( offset != length ) |
---|
[6139] | 871 | { |
---|
[7954] | 872 | PRINTF(0)("offset (%d) != length (%d)\n", offset, length); |
---|
| 873 | peer->second.socket->disconnectServer(); |
---|
[6139] | 874 | } |
---|
[9246] | 875 | |
---|
| 876 | |
---|
[7954] | 877 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
---|
[6139] | 878 | { |
---|
[7954] | 879 | Synchronizeable & sync = **it; |
---|
[9246] | 880 | |
---|
[7954] | 881 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
---|
| 882 | continue; |
---|
[9246] | 883 | |
---|
[7954] | 884 | sync.handleRecvState( peer->second.userId, state, fromState ); |
---|
[6139] | 885 | } |
---|
[9246] | 886 | |
---|
[7954] | 887 | assert( peer->second.lastAckedState <= ackedState ); |
---|
| 888 | peer->second.lastAckedState = ackedState; |
---|
[9246] | 889 | |
---|
[7954] | 890 | assert( peer->second.lastRecvedState < state ); |
---|
| 891 | peer->second.lastRecvedState = state; |
---|
[8228] | 892 | |
---|
[6139] | 893 | } |
---|
[9246] | 894 | |
---|
[6139] | 895 | } |
---|
[9246] | 896 | |
---|
[7954] | 897 | } |
---|
[6139] | 898 | |
---|
[7954] | 899 | /** |
---|
| 900 | * is executed when a handshake has finished |
---|
| 901 | */ |
---|
| 902 | void NetworkStream::handleNewClient( int userId ) |
---|
| 903 | { |
---|
[9268] | 904 | // init and assign the message manager |
---|
[7954] | 905 | MessageManager::getInstance()->initUser( userId ); |
---|
[9268] | 906 | // do all game relevant stuff here |
---|
| 907 | networkGameManager->signalNewPlayer( userId ); |
---|
[9246] | 908 | |
---|
[9268] | 909 | // register the new client at the network monitor |
---|
| 910 | // this->networkMonitor->addClient(); |
---|
[5604] | 911 | } |
---|
[6139] | 912 | |
---|
[9268] | 913 | |
---|
[7954] | 914 | /** |
---|
| 915 | * removes old items from oldSynchronizeables |
---|
| 916 | */ |
---|
| 917 | void NetworkStream::cleanUpOldSyncList( ) |
---|
[6139] | 918 | { |
---|
[7954] | 919 | int now = SDL_GetTicks(); |
---|
[9246] | 920 | |
---|
[7954] | 921 | for ( std::map<int,int>::iterator it = oldSynchronizeables.begin(); it != oldSynchronizeables.end(); ) |
---|
[6139] | 922 | { |
---|
[7954] | 923 | if ( it->second < now - 10*1000 ) |
---|
| 924 | { |
---|
| 925 | std::map<int,int>::iterator delIt = it; |
---|
| 926 | it++; |
---|
| 927 | oldSynchronizeables.erase( delIt ); |
---|
| 928 | continue; |
---|
| 929 | } |
---|
| 930 | it++; |
---|
[6139] | 931 | } |
---|
[7954] | 932 | } |
---|
| 933 | |
---|
| 934 | /** |
---|
| 935 | * writes data to DATA/dicts/newdict |
---|
| 936 | * @param data pointer to data |
---|
| 937 | * @param length length |
---|
| 938 | */ |
---|
[8623] | 939 | void NetworkStream::writeToNewDict( byte * data, int length, bool upstream ) |
---|
[7954] | 940 | { |
---|
| 941 | if ( remainingBytesToWriteToDict <= 0 ) |
---|
| 942 | return; |
---|
[9246] | 943 | |
---|
[7954] | 944 | if ( length > remainingBytesToWriteToDict ) |
---|
| 945 | length = remainingBytesToWriteToDict; |
---|
[9246] | 946 | |
---|
[7954] | 947 | std::string fileName = ResourceManager::getInstance()->getDataDir(); |
---|
| 948 | fileName += "/dicts/newdict"; |
---|
[9246] | 949 | |
---|
[8623] | 950 | if ( upstream ) |
---|
| 951 | fileName += "_upstream"; |
---|
| 952 | else |
---|
| 953 | fileName += "_downstream"; |
---|
[9246] | 954 | |
---|
[7954] | 955 | FILE * f = fopen( fileName.c_str(), "a" ); |
---|
[9246] | 956 | |
---|
[7954] | 957 | if ( !f ) |
---|
[6139] | 958 | { |
---|
[7954] | 959 | PRINTF(2)("could not open %s\n", fileName.c_str()); |
---|
| 960 | remainingBytesToWriteToDict = 0; |
---|
[6139] | 961 | return; |
---|
| 962 | } |
---|
[9246] | 963 | |
---|
[7954] | 964 | if ( fwrite( data, 1, length, f ) != length ) |
---|
[6341] | 965 | { |
---|
[7954] | 966 | PRINTF(2)("could not write to file\n"); |
---|
| 967 | fclose( f ); |
---|
[6341] | 968 | return; |
---|
| 969 | } |
---|
[9246] | 970 | |
---|
[7954] | 971 | fclose( f ); |
---|
[9246] | 972 | |
---|
| 973 | remainingBytesToWriteToDict -= length; |
---|
[6139] | 974 | } |
---|
| 975 | |
---|
| 976 | |
---|
[6695] | 977 | |
---|
| 978 | |
---|
| 979 | |
---|
| 980 | |
---|