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