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