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