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