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