| 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: |
|---|
| 12 | main-programmer: Christoph Renner rennerc@ee.ethz.ch |
|---|
| 13 | co-programmer: Patrick Boenzli boenzlip@orxonox.ethz.ch |
|---|
| 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) |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #define DEBUG_MODULE_NETWORK |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #include "base_object.h" |
|---|
| 24 | #include "network_protocol.h" |
|---|
| 25 | #include "udp_socket.h" |
|---|
| 26 | #include "udp_server_socket.h" |
|---|
| 27 | #include "connection_monitor.h" |
|---|
| 28 | #include "synchronizeable.h" |
|---|
| 29 | #include "network_game_manager.h" |
|---|
| 30 | #include "shared_network_data.h" |
|---|
| 31 | #include "message_manager.h" |
|---|
| 32 | #include "preferences.h" |
|---|
| 33 | #include "zip.h" |
|---|
| 34 | |
|---|
| 35 | #include "src/lib/util/loading/resource_manager.h" |
|---|
| 36 | |
|---|
| 37 | #include "network_log.h" |
|---|
| 38 | |
|---|
| 39 | #include "player_stats.h" |
|---|
| 40 | |
|---|
| 41 | #include "lib/util/loading/factory.h" |
|---|
| 42 | |
|---|
| 43 | #include "debug.h" |
|---|
| 44 | #include "class_list.h" |
|---|
| 45 | #include <algorithm> |
|---|
| 46 | |
|---|
| 47 | /* include your own header */ |
|---|
| 48 | #include "network_stream.h" |
|---|
| 49 | |
|---|
| 50 | /* probably unnecessary */ |
|---|
| 51 | using namespace std; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | #define PACKAGE_SIZE 256 |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * empty constructor |
|---|
| 59 | */ |
|---|
| 60 | NetworkStream::NetworkStream() |
|---|
| 61 | : DataStream() |
|---|
| 62 | { |
|---|
| 63 | this->init(); |
|---|
| 64 | /* initialize the references */ |
|---|
| 65 | this->type = NET_CLIENT; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * start as a client, connect to a server |
|---|
| 71 | * @param host: host name (address) |
|---|
| 72 | * @param port: port number |
|---|
| 73 | */ |
|---|
| 74 | NetworkStream::NetworkStream( std::string host, int port ) |
|---|
| 75 | { |
|---|
| 76 | this->type = NET_CLIENT; |
|---|
| 77 | this->init(); |
|---|
| 78 | this->peers[0].socket = new UdpSocket( host, port ); |
|---|
| 79 | this->peers[0].userId = 0; |
|---|
| 80 | this->peers[0].isMasterServer = true; |
|---|
| 81 | this->peers[0].connectionMonitor = new ConnectionMonitor( 0 ); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * start as a server |
|---|
| 87 | * @param port: at this port |
|---|
| 88 | */ |
|---|
| 89 | NetworkStream::NetworkStream( int port ) |
|---|
| 90 | { |
|---|
| 91 | this->type = NET_MASTER_SERVER; |
|---|
| 92 | this->init(); |
|---|
| 93 | this->serverSocket = new UdpServerSocket(port); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * generic init functions |
|---|
| 99 | */ |
|---|
| 100 | void NetworkStream::init() |
|---|
| 101 | { |
|---|
| 102 | /* set the class id for the base object */ |
|---|
| 103 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
|---|
| 104 | this->serverSocket = NULL; |
|---|
| 105 | this->networkGameManager = NULL; |
|---|
| 106 | this->myHostId = 0; |
|---|
| 107 | this->currentState = 0; |
|---|
| 108 | |
|---|
| 109 | remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 ); |
|---|
| 110 | |
|---|
| 111 | assert( Zip::getInstance()->loadDictionary( "testdict" ) >= 0 ); |
|---|
| 112 | this->dictClient = Zip::getInstance()->loadDictionary( "dict2pl_client" ); |
|---|
| 113 | assert( this->dictClient >= 0 ); |
|---|
| 114 | this->dictServer = Zip::getInstance()->loadDictionary( "dict2p_server" ); |
|---|
| 115 | assert( this->dictServer >= 0 ); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * deconstructor |
|---|
| 121 | */ |
|---|
| 122 | NetworkStream::~NetworkStream() |
|---|
| 123 | { |
|---|
| 124 | if ( this->serverSocket ) |
|---|
| 125 | { |
|---|
| 126 | serverSocket->close(); |
|---|
| 127 | delete serverSocket; |
|---|
| 128 | serverSocket = NULL; |
|---|
| 129 | } |
|---|
| 130 | for ( PeerList::iterator i = peers.begin(); i!=peers.end(); i++) |
|---|
| 131 | { |
|---|
| 132 | if ( i->second.socket ) |
|---|
| 133 | { |
|---|
| 134 | i->second.socket->disconnectServer(); |
|---|
| 135 | delete i->second.socket; |
|---|
| 136 | i->second.socket = NULL; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | if ( i->second.handshake ) |
|---|
| 140 | { |
|---|
| 141 | delete i->second.handshake; |
|---|
| 142 | i->second.handshake = NULL; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | if ( i->second.connectionMonitor ) |
|---|
| 146 | { |
|---|
| 147 | delete i->second.connectionMonitor; |
|---|
| 148 | i->second.connectionMonitor = NULL; |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | for ( SynchronizeableList::const_iterator it = getSyncBegin(); it != getSyncEnd(); it ++ ) |
|---|
| 152 | (*it)->setNetworkStream( NULL ); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | * creates a new instance of the network game manager |
|---|
| 158 | */ |
|---|
| 159 | void NetworkStream::createNetworkGameManager() |
|---|
| 160 | { |
|---|
| 161 | this->networkGameManager = NetworkGameManager::getInstance(); |
|---|
| 162 | // setUniqueID( maxCon+2 ) because we need one id for every handshake |
|---|
| 163 | // and one for handshake to reject client maxCon+1 |
|---|
| 164 | this->networkGameManager->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
|---|
| 165 | MessageManager::getInstance()->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * starts the network handshake |
|---|
| 171 | */ |
|---|
| 172 | void NetworkStream::startHandshake() |
|---|
| 173 | { |
|---|
| 174 | Handshake* hs = new Handshake(false); |
|---|
| 175 | hs->setUniqueID( 0 ); |
|---|
| 176 | assert( peers[0].handshake == NULL ); |
|---|
| 177 | peers[0].handshake = hs; |
|---|
| 178 | |
|---|
| 179 | hs->setPreferedNickName( Preferences::getInstance()->getString( "multiplayer", "nickname", "Player" ) ); |
|---|
| 180 | |
|---|
| 181 | // peers[0].handshake->setSynchronized( true ); |
|---|
| 182 | //this->connectSynchronizeable(*hs); |
|---|
| 183 | //this->connectSynchronizeable(*hs); |
|---|
| 184 | PRINTF(0)("NetworkStream: Handshake created: %s\n", hs->getName()); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * this functions connects a synchronizeable to the networkstream, therefore synchronizeing |
|---|
| 190 | * it all over the network and creating it on the other platforms (if and only if it is a |
|---|
| 191 | * server |
|---|
| 192 | */ |
|---|
| 193 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
|---|
| 194 | { |
|---|
| 195 | this->synchronizeables.push_back(&sync); |
|---|
| 196 | sync.setNetworkStream( this ); |
|---|
| 197 | |
|---|
| 198 | // this->bActive = true; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | /** |
|---|
| 203 | * removes the synchronizeable from the list of synchronized entities |
|---|
| 204 | */ |
|---|
| 205 | void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) |
|---|
| 206 | { |
|---|
| 207 | // removing the Synchronizeable from the List. |
|---|
| 208 | std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); |
|---|
| 209 | if (disconnectSynchro != this->synchronizeables.end()) |
|---|
| 210 | this->synchronizeables.erase(disconnectSynchro); |
|---|
| 211 | |
|---|
| 212 | oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks(); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * this is called to process data from the network socket to the synchronizeable and vice versa |
|---|
| 218 | */ |
|---|
| 219 | void NetworkStream::processData() |
|---|
| 220 | { |
|---|
| 221 | int tick = SDL_GetTicks(); |
|---|
| 222 | |
|---|
| 223 | this->currentState++; |
|---|
| 224 | // there was a wrap around |
|---|
| 225 | if( this->currentState < 0) |
|---|
| 226 | { |
|---|
| 227 | 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"); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | if ( this->type == NET_MASTER_SERVER ) |
|---|
| 231 | { |
|---|
| 232 | // execute everytthing the master server shoudl do |
|---|
| 233 | if ( serverSocket ) |
|---|
| 234 | serverSocket->update(); |
|---|
| 235 | |
|---|
| 236 | this->updateConnectionList(); |
|---|
| 237 | } |
|---|
| 238 | else if( this->type == NET_PROXY_SERVER) |
|---|
| 239 | { |
|---|
| 240 | // execute everything the proxy server should do |
|---|
| 241 | } |
|---|
| 242 | else |
|---|
| 243 | { |
|---|
| 244 | // check if the connection is ok else terminate and remove |
|---|
| 245 | if ( peers[0].socket && ( !peers[0].socket->isOk() || peers[0].connectionMonitor->hasTimedOut() ) ) |
|---|
| 246 | { |
|---|
| 247 | PRINTF(1)("lost connection to server\n"); |
|---|
| 248 | |
|---|
| 249 | peers[0].socket->disconnectServer(); |
|---|
| 250 | delete peers[0].socket; |
|---|
| 251 | peers[0].socket = NULL; |
|---|
| 252 | |
|---|
| 253 | if ( peers[0].handshake ) |
|---|
| 254 | delete peers[0].handshake; |
|---|
| 255 | peers[0].handshake = NULL; |
|---|
| 256 | |
|---|
| 257 | if ( peers[0].connectionMonitor ) |
|---|
| 258 | delete peers[0].connectionMonitor; |
|---|
| 259 | peers[0].connectionMonitor = NULL; |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | cleanUpOldSyncList(); |
|---|
| 264 | handleHandshakes(); |
|---|
| 265 | |
|---|
| 266 | // order of up/downstream is important!!!! |
|---|
| 267 | // don't change it |
|---|
| 268 | handleDownstream( tick ); |
|---|
| 269 | handleUpstream( tick ); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | /** |
|---|
| 274 | * if we are a server update the connection list to accept new connections (clients) |
|---|
| 275 | * also start the handsake for the new clients |
|---|
| 276 | */ |
|---|
| 277 | void NetworkStream::updateConnectionList( ) |
|---|
| 278 | { |
|---|
| 279 | //check for new connections |
|---|
| 280 | |
|---|
| 281 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
|---|
| 282 | |
|---|
| 283 | // we got new network node |
|---|
| 284 | if ( tempNetworkSocket ) |
|---|
| 285 | { |
|---|
| 286 | int clientId; |
|---|
| 287 | // if there is a list of free client id slots, take these |
|---|
| 288 | if ( freeSocketSlots.size() > 0 ) |
|---|
| 289 | { |
|---|
| 290 | clientId = freeSocketSlots.back(); |
|---|
| 291 | freeSocketSlots.pop_back(); |
|---|
| 292 | peers[clientId].socket = tempNetworkSocket; |
|---|
| 293 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() ); |
|---|
| 294 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
|---|
| 295 | peers[clientId].handshake->setUniqueID(clientId); |
|---|
| 296 | peers[clientId].userId = clientId; |
|---|
| 297 | peers[clientId].isMasterServer = false; |
|---|
| 298 | } |
|---|
| 299 | else |
|---|
| 300 | { |
|---|
| 301 | clientId = 1; |
|---|
| 302 | |
|---|
| 303 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
|---|
| 304 | if ( it->first >= clientId ) |
|---|
| 305 | clientId = it->first + 1; |
|---|
| 306 | |
|---|
| 307 | peers[clientId].socket = tempNetworkSocket; |
|---|
| 308 | peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID()); |
|---|
| 309 | peers[clientId].handshake->setUniqueID(clientId); |
|---|
| 310 | peers[clientId].connectionMonitor = new ConnectionMonitor( clientId ); |
|---|
| 311 | peers[clientId].userId = clientId; |
|---|
| 312 | peers[clientId].isMasterServer = false; |
|---|
| 313 | |
|---|
| 314 | PRINTF(0)("num sync: %d\n", synchronizeables.size()); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | // check if there are too many clients connected |
|---|
| 318 | if ( clientId > MAX_CONNECTIONS ) |
|---|
| 319 | { |
|---|
| 320 | peers[clientId].handshake->doReject( "too many connections" ); |
|---|
| 321 | PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); |
|---|
| 322 | } |
|---|
| 323 | else |
|---|
| 324 | { |
|---|
| 325 | PRINTF(0)("New Client: %d\n", clientId); |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | //this->connectSynchronizeable(*handshakes[clientId]); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | //check if connections are ok else remove them |
|---|
| 334 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); ) |
|---|
| 335 | { |
|---|
| 336 | if ( |
|---|
| 337 | it->second.socket && |
|---|
| 338 | ( |
|---|
| 339 | !it->second.socket->isOk() || |
|---|
| 340 | it->second.connectionMonitor->hasTimedOut() |
|---|
| 341 | ) |
|---|
| 342 | ) |
|---|
| 343 | { |
|---|
| 344 | std::string reason = "disconnected"; |
|---|
| 345 | if ( it->second.connectionMonitor->hasTimedOut() ) |
|---|
| 346 | reason = "timeout"; |
|---|
| 347 | PRINTF(0)("Client is gone: %d (%s)\n", it->second.userId, reason.c_str()); |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | // clean up the network data |
|---|
| 351 | it->second.socket->disconnectServer(); |
|---|
| 352 | delete it->second.socket; |
|---|
| 353 | it->second.socket = NULL; |
|---|
| 354 | |
|---|
| 355 | if ( it->second.connectionMonitor ) |
|---|
| 356 | delete it->second.connectionMonitor; |
|---|
| 357 | it->second.connectionMonitor = NULL; |
|---|
| 358 | |
|---|
| 359 | if ( it->second.handshake ) |
|---|
| 360 | delete it->second.handshake; |
|---|
| 361 | it->second.handshake = NULL; |
|---|
| 362 | |
|---|
| 363 | for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) |
|---|
| 364 | { |
|---|
| 365 | (*it2)->cleanUpUser( it->second.userId ); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId); |
|---|
| 369 | |
|---|
| 370 | freeSocketSlots.push_back( it->second.userId ); |
|---|
| 371 | |
|---|
| 372 | PeerList::iterator delit = it; |
|---|
| 373 | it++; |
|---|
| 374 | |
|---|
| 375 | peers.erase( delit ); |
|---|
| 376 | |
|---|
| 377 | continue; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | it++; |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | void NetworkStream::debug() |
|---|
| 388 | { |
|---|
| 389 | if( this->isMasterServer()) |
|---|
| 390 | PRINT(0)(" Host ist Server with ID: %i\n", this->myHostId); |
|---|
| 391 | else |
|---|
| 392 | PRINT(0)(" Host ist Client with ID: %i\n", this->myHostId); |
|---|
| 393 | |
|---|
| 394 | PRINT(0)(" Got %i connected Synchronizeables, showing active Syncs:\n", this->synchronizeables.size()); |
|---|
| 395 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
|---|
| 396 | { |
|---|
| 397 | if( (*it)->beSynchronized() == true) |
|---|
| 398 | PRINT(0)(" Synchronizeable of class: %s::%s, with unique ID: %i, Synchronize: %i\n", (*it)->getClassName(), (*it)->getName(), |
|---|
| 399 | (*it)->getUniqueID(), (*it)->beSynchronized()); |
|---|
| 400 | } |
|---|
| 401 | PRINT(0)(" Maximal Connections: %i\n", MAX_CONNECTIONS ); |
|---|
| 402 | |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | /** |
|---|
| 407 | * @returns the number of synchronizeables registered to this stream |
|---|
| 408 | */ |
|---|
| 409 | int NetworkStream::getSyncCount() |
|---|
| 410 | { |
|---|
| 411 | int n = 0; |
|---|
| 412 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
|---|
| 413 | if( (*it)->beSynchronized() == true) |
|---|
| 414 | ++n; |
|---|
| 415 | |
|---|
| 416 | //return synchronizeables.size(); |
|---|
| 417 | return n; |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | /** |
|---|
| 422 | * check if handshakes completed. if so create the network game manager else remove it again |
|---|
| 423 | */ |
|---|
| 424 | void NetworkStream::handleHandshakes( ) |
|---|
| 425 | { |
|---|
| 426 | for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ ) |
|---|
| 427 | { |
|---|
| 428 | if ( it->second.handshake ) |
|---|
| 429 | { |
|---|
| 430 | if ( it->second.handshake->completed() ) |
|---|
| 431 | { |
|---|
| 432 | if ( it->second.handshake->ok() ) |
|---|
| 433 | { |
|---|
| 434 | if ( !it->second.handshake->allowDel() ) |
|---|
| 435 | { |
|---|
| 436 | //if ( type != NET_MASTER_SERVER ) |
|---|
| 437 | if ( type == NET_CLIENT ) |
|---|
| 438 | { |
|---|
| 439 | SharedNetworkData::getInstance()->setHostID( it->second.handshake->getHostId() ); |
|---|
| 440 | myHostId = SharedNetworkData::getInstance()->getHostID(); |
|---|
| 441 | |
|---|
| 442 | this->networkGameManager = NetworkGameManager::getInstance(); |
|---|
| 443 | this->networkGameManager->setUniqueID( it->second.handshake->getNetworkGameManagerId() ); |
|---|
| 444 | MessageManager::getInstance()->setUniqueID( it->second.handshake->getMessageManagerId() ); |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | PRINT(0)("handshake finished id=%d\n", it->second.handshake->getNetworkGameManagerId()); |
|---|
| 449 | |
|---|
| 450 | it->second.handshake->del(); |
|---|
| 451 | } |
|---|
| 452 | else |
|---|
| 453 | { |
|---|
| 454 | if ( it->second.handshake->canDel() ) |
|---|
| 455 | { |
|---|
| 456 | if ( type == NET_MASTER_SERVER ) |
|---|
| 457 | { |
|---|
| 458 | handleNewClient( it->second.userId ); |
|---|
| 459 | |
|---|
| 460 | if ( PlayerStats::getStats( it->second.userId ) && it->second.handshake->getPreferedNickName() != "" ) |
|---|
| 461 | { |
|---|
| 462 | PlayerStats::getStats( it->second.userId )->setNickName( it->second.handshake->getPreferedNickName() ); |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | PRINT(0)("handshake finished delete it\n"); |
|---|
| 467 | delete it->second.handshake; |
|---|
| 468 | it->second.handshake = NULL; |
|---|
| 469 | } |
|---|
| 470 | } |
|---|
| 471 | |
|---|
| 472 | } |
|---|
| 473 | else |
|---|
| 474 | { |
|---|
| 475 | PRINT(1)("handshake failed!\n"); |
|---|
| 476 | it->second.socket->disconnectServer(); |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | } |
|---|
| 480 | } |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | /** |
|---|
| 485 | * handle upstream network traffic |
|---|
| 486 | */ |
|---|
| 487 | void NetworkStream::handleUpstream( int tick ) |
|---|
| 488 | { |
|---|
| 489 | int offset; |
|---|
| 490 | int n; |
|---|
| 491 | |
|---|
| 492 | for ( PeerList::reverse_iterator peer = peers.rbegin(); peer != peers.rend(); peer++ ) |
|---|
| 493 | { |
|---|
| 494 | offset = INTSIZE; // reserve enough space for the packet length |
|---|
| 495 | |
|---|
| 496 | // continue with the next peer if this peer has no socket assigned (therefore no network) |
|---|
| 497 | if ( !peer->second.socket ) |
|---|
| 498 | continue; |
|---|
| 499 | |
|---|
| 500 | // header informations: current state |
|---|
| 501 | n = Converter::intToByteArray( currentState, buf + offset, UDP_PACKET_SIZE - offset ); |
|---|
| 502 | assert( n == INTSIZE ); |
|---|
| 503 | offset += n; |
|---|
| 504 | |
|---|
| 505 | // header informations: last acked state |
|---|
| 506 | n = Converter::intToByteArray( peer->second.lastAckedState, buf + offset, UDP_PACKET_SIZE - offset ); |
|---|
| 507 | assert( n == INTSIZE ); |
|---|
| 508 | offset += n; |
|---|
| 509 | |
|---|
| 510 | // header informations: last recved state |
|---|
| 511 | n = Converter::intToByteArray( peer->second.lastRecvedState, buf + offset, UDP_PACKET_SIZE - offset ); |
|---|
| 512 | assert( n == INTSIZE ); |
|---|
| 513 | offset += n; |
|---|
| 514 | |
|---|
| 515 | // now write all synchronizeables in the packet |
|---|
| 516 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
|---|
| 517 | { |
|---|
| 518 | int oldOffset = offset; |
|---|
| 519 | Synchronizeable & sync = **it; |
|---|
| 520 | |
|---|
| 521 | // do not include synchronizeables with uninit id and syncs that don't want to be synchronized |
|---|
| 522 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
|---|
| 523 | continue; |
|---|
| 524 | |
|---|
| 525 | // if handshake not finished only sync handshake |
|---|
| 526 | if ( peer->second.handshake && sync.getLeafClassID() != CL_HANDSHAKE ) |
|---|
| 527 | continue; |
|---|
| 528 | |
|---|
| 529 | // if we are a server and this is not our handshake |
|---|
| 530 | if ( isMasterServer() && sync.getLeafClassID() == CL_HANDSHAKE && sync.getUniqueID() != peer->second.userId ) |
|---|
| 531 | continue; |
|---|
| 532 | |
|---|
| 533 | /* list of synchronizeables that will never be synchronized over the network: */ |
|---|
| 534 | // do not sync null parent |
|---|
| 535 | if ( sync.getLeafClassID() == CL_NULL_PARENT ) |
|---|
| 536 | continue; |
|---|
| 537 | |
|---|
| 538 | assert( offset + INTSIZE <= UDP_PACKET_SIZE ); |
|---|
| 539 | |
|---|
| 540 | // server fakes uniqueid == 0 for handshake |
|---|
| 541 | if ( this->isMasterServer() && sync.getUniqueID() < MAX_CONNECTIONS - 1 ) |
|---|
| 542 | n = Converter::intToByteArray( 0, buf + offset, UDP_PACKET_SIZE - offset ); |
|---|
| 543 | else |
|---|
| 544 | n = Converter::intToByteArray( sync.getUniqueID(), buf + offset, UDP_PACKET_SIZE - offset ); |
|---|
| 545 | |
|---|
| 546 | assert( n == INTSIZE ); |
|---|
| 547 | offset += n; |
|---|
| 548 | |
|---|
| 549 | // make space for size |
|---|
| 550 | offset += INTSIZE; |
|---|
| 551 | |
|---|
| 552 | n = sync.getStateDiff( peer->second.userId, buf + offset, UDP_PACKET_SIZE-offset, currentState, peer->second.lastAckedState, -1000 ); |
|---|
| 553 | offset += n; |
|---|
| 554 | |
|---|
| 555 | assert( Converter::intToByteArray( n, buf + offset - n - INTSIZE, INTSIZE ) == INTSIZE ); |
|---|
| 556 | |
|---|
| 557 | // check if all data bytes == 0 -> remove data and the synchronizeable from the sync process since there is no update |
|---|
| 558 | // TODO not all synchronizeables like this maybe add Synchronizeable::canRemoveZeroDiff() |
|---|
| 559 | bool allZero = true; |
|---|
| 560 | for ( int i = 0; i < n; i++ ) |
|---|
| 561 | { |
|---|
| 562 | if ( buf[i+oldOffset+2*INTSIZE] != 0 ) |
|---|
| 563 | allZero = false; |
|---|
| 564 | } |
|---|
| 565 | // if there is no new data in this synchronizeable reset the data offset to the last state -> dont synchronizes |
|---|
| 566 | // data that hast not changed |
|---|
| 567 | if ( allZero ) |
|---|
| 568 | { |
|---|
| 569 | offset = oldOffset; |
|---|
| 570 | } |
|---|
| 571 | } // all synchronizeables written |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | |
|---|
| 575 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
|---|
| 576 | { |
|---|
| 577 | Synchronizeable & sync = **it; |
|---|
| 578 | |
|---|
| 579 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
|---|
| 580 | continue; |
|---|
| 581 | |
|---|
| 582 | sync.handleSentState( peer->second.userId, currentState, peer->second.lastAckedState ); |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE ); |
|---|
| 587 | |
|---|
| 588 | // now compress the data with the zip library |
|---|
| 589 | int compLength = 0; |
|---|
| 590 | if ( this->isMasterServer() ) |
|---|
| 591 | compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictServer ); |
|---|
| 592 | else |
|---|
| 593 | compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE, dictClient ); |
|---|
| 594 | |
|---|
| 595 | if ( compLength <= 0 ) |
|---|
| 596 | { |
|---|
| 597 | PRINTF(1)("compression failed!\n"); |
|---|
| 598 | continue; |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | assert( peer->second.socket->writePacket( compBuf, compLength ) ); |
|---|
| 602 | |
|---|
| 603 | if ( this->remainingBytesToWriteToDict > 0 ) |
|---|
| 604 | writeToNewDict( buf, offset, true ); |
|---|
| 605 | |
|---|
| 606 | peer->second.connectionMonitor->processUnzippedOutgoingPacket( tick, buf, offset, currentState ); |
|---|
| 607 | peer->second.connectionMonitor->processZippedOutgoingPacket( tick, compBuf, compLength, currentState ); |
|---|
| 608 | |
|---|
| 609 | } |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | /** |
|---|
| 613 | * handle downstream network traffic |
|---|
| 614 | */ |
|---|
| 615 | void NetworkStream::handleDownstream( int tick ) |
|---|
| 616 | { |
|---|
| 617 | int offset = 0; |
|---|
| 618 | |
|---|
| 619 | int length = 0; |
|---|
| 620 | int packetLength = 0; |
|---|
| 621 | int compLength = 0; |
|---|
| 622 | int uniqueId = 0; |
|---|
| 623 | int state = 0; |
|---|
| 624 | int ackedState = 0; |
|---|
| 625 | int fromState = 0; |
|---|
| 626 | int syncDataLength = 0; |
|---|
| 627 | |
|---|
| 628 | for ( PeerList::iterator peer = peers.begin(); peer != peers.end(); peer++ ) |
|---|
| 629 | { |
|---|
| 630 | |
|---|
| 631 | if ( !peer->second.socket ) |
|---|
| 632 | continue; |
|---|
| 633 | |
|---|
| 634 | while ( 0 < (compLength = peer->second.socket->readPacket( compBuf, UDP_PACKET_SIZE )) ) |
|---|
| 635 | { |
|---|
| 636 | peer->second.connectionMonitor->processZippedIncomingPacket( tick, compBuf, compLength ); |
|---|
| 637 | |
|---|
| 638 | packetLength = Zip::getInstance()->unZip( compBuf, compLength, buf, UDP_PACKET_SIZE ); |
|---|
| 639 | |
|---|
| 640 | if ( packetLength < 4*INTSIZE ) |
|---|
| 641 | { |
|---|
| 642 | if ( packetLength != 0 ) |
|---|
| 643 | PRINTF(1)("got too small packet: %d\n", packetLength); |
|---|
| 644 | continue; |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | if ( this->remainingBytesToWriteToDict > 0 ) |
|---|
| 648 | writeToNewDict( buf, packetLength, false ); |
|---|
| 649 | |
|---|
| 650 | assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE ); |
|---|
| 651 | assert( Converter::byteArrayToInt( buf + INTSIZE, &state ) == INTSIZE ); |
|---|
| 652 | assert( Converter::byteArrayToInt( buf + 2*INTSIZE, &fromState ) == INTSIZE ); |
|---|
| 653 | assert( Converter::byteArrayToInt( buf + 3*INTSIZE, &ackedState ) == INTSIZE ); |
|---|
| 654 | offset = 4*INTSIZE; |
|---|
| 655 | |
|---|
| 656 | peer->second.connectionMonitor->processUnzippedIncomingPacket( tick, buf, packetLength, state, ackedState ); |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | //if this is an old state drop it |
|---|
| 660 | if ( state <= peer->second.lastRecvedState ) |
|---|
| 661 | continue; |
|---|
| 662 | |
|---|
| 663 | if ( packetLength != length ) |
|---|
| 664 | { |
|---|
| 665 | PRINTF(1)("real packet length (%d) and transmitted packet length (%d) do not match!\n", packetLength, length); |
|---|
| 666 | peer->second.socket->disconnectServer(); |
|---|
| 667 | continue; |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | while ( offset + 2*INTSIZE < length ) |
|---|
| 671 | { |
|---|
| 672 | assert( offset > 0 ); |
|---|
| 673 | assert( Converter::byteArrayToInt( buf + offset, &uniqueId ) == INTSIZE ); |
|---|
| 674 | offset += INTSIZE; |
|---|
| 675 | |
|---|
| 676 | assert( Converter::byteArrayToInt( buf + offset, &syncDataLength ) == INTSIZE ); |
|---|
| 677 | offset += INTSIZE; |
|---|
| 678 | |
|---|
| 679 | assert( syncDataLength > 0 ); |
|---|
| 680 | assert( syncDataLength < 10000 ); |
|---|
| 681 | |
|---|
| 682 | Synchronizeable * sync = NULL; |
|---|
| 683 | |
|---|
| 684 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
|---|
| 685 | { |
|---|
| 686 | // client thinks his handshake has id 0!!!!! |
|---|
| 687 | if ( (*it)->getUniqueID() == uniqueId || ( uniqueId == 0 && (*it)->getUniqueID() == peer->second.userId ) ) |
|---|
| 688 | { |
|---|
| 689 | sync = *it; |
|---|
| 690 | break; |
|---|
| 691 | } |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | if ( sync == NULL ) |
|---|
| 695 | { |
|---|
| 696 | PRINTF(0)("could not find sync with id %d. try to create it\n", uniqueId); |
|---|
| 697 | if ( oldSynchronizeables.find( uniqueId ) != oldSynchronizeables.end() ) |
|---|
| 698 | { |
|---|
| 699 | offset += syncDataLength; |
|---|
| 700 | continue; |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | if ( !peers[peer->second.userId].isMasterServer ) |
|---|
| 704 | { |
|---|
| 705 | offset += syncDataLength; |
|---|
| 706 | continue; |
|---|
| 707 | } |
|---|
| 708 | |
|---|
| 709 | int leafClassId; |
|---|
| 710 | if ( INTSIZE > length - offset ) |
|---|
| 711 | { |
|---|
| 712 | offset += syncDataLength; |
|---|
| 713 | continue; |
|---|
| 714 | } |
|---|
| 715 | |
|---|
| 716 | Converter::byteArrayToInt( buf + offset, &leafClassId ); |
|---|
| 717 | |
|---|
| 718 | assert( leafClassId != 0 ); |
|---|
| 719 | |
|---|
| 720 | BaseObject * b = NULL; |
|---|
| 721 | /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ |
|---|
| 722 | /* Exception 1: NullParent */ |
|---|
| 723 | if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE || leafClassId == CL_NETWORK_GAME_MANAGER ) |
|---|
| 724 | { |
|---|
| 725 | PRINTF(1)("Can not create Class with ID %x!\n", (int)leafClassId); |
|---|
| 726 | offset += syncDataLength; |
|---|
| 727 | continue; |
|---|
| 728 | } |
|---|
| 729 | else |
|---|
| 730 | b = Factory::fabricate( (ClassID)leafClassId ); |
|---|
| 731 | |
|---|
| 732 | if ( !b ) |
|---|
| 733 | { |
|---|
| 734 | PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); |
|---|
| 735 | offset += syncDataLength; |
|---|
| 736 | continue; |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
|---|
| 740 | { |
|---|
| 741 | sync = dynamic_cast<Synchronizeable*>(b); |
|---|
| 742 | sync->setUniqueID( uniqueId ); |
|---|
| 743 | sync->setSynchronized(true); |
|---|
| 744 | |
|---|
| 745 | PRINTF(0)("Fabricated %s with id %d\n", sync->getClassName(), sync->getUniqueID()); |
|---|
| 746 | } |
|---|
| 747 | else |
|---|
| 748 | { |
|---|
| 749 | PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId); |
|---|
| 750 | delete b; |
|---|
| 751 | offset += syncDataLength; |
|---|
| 752 | continue; |
|---|
| 753 | } |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | |
|---|
| 757 | int n = sync->setStateDiff( peer->second.userId, buf+offset, syncDataLength, state, fromState ); |
|---|
| 758 | offset += n; |
|---|
| 759 | //NETPRINTF(0)("SSSSSEEEEETTTTT: %s %d\n",sync->getClassName(), n); |
|---|
| 760 | |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | if ( offset != length ) |
|---|
| 764 | { |
|---|
| 765 | PRINTF(0)("offset (%d) != length (%d)\n", offset, length); |
|---|
| 766 | peer->second.socket->disconnectServer(); |
|---|
| 767 | } |
|---|
| 768 | |
|---|
| 769 | |
|---|
| 770 | for ( SynchronizeableList::iterator it = synchronizeables.begin(); it != synchronizeables.end(); it++ ) |
|---|
| 771 | { |
|---|
| 772 | Synchronizeable & sync = **it; |
|---|
| 773 | |
|---|
| 774 | if ( !sync.beSynchronized() || sync.getUniqueID() < 0 ) |
|---|
| 775 | continue; |
|---|
| 776 | |
|---|
| 777 | sync.handleRecvState( peer->second.userId, state, fromState ); |
|---|
| 778 | } |
|---|
| 779 | |
|---|
| 780 | assert( peer->second.lastAckedState <= ackedState ); |
|---|
| 781 | peer->second.lastAckedState = ackedState; |
|---|
| 782 | |
|---|
| 783 | assert( peer->second.lastRecvedState < state ); |
|---|
| 784 | peer->second.lastRecvedState = state; |
|---|
| 785 | |
|---|
| 786 | } |
|---|
| 787 | |
|---|
| 788 | } |
|---|
| 789 | |
|---|
| 790 | } |
|---|
| 791 | |
|---|
| 792 | /** |
|---|
| 793 | * is executed when a handshake has finished |
|---|
| 794 | * @todo create playable for new user |
|---|
| 795 | */ |
|---|
| 796 | void NetworkStream::handleNewClient( int userId ) |
|---|
| 797 | { |
|---|
| 798 | MessageManager::getInstance()->initUser( userId ); |
|---|
| 799 | |
|---|
| 800 | networkGameManager->signalNewPlayer( userId ); |
|---|
| 801 | } |
|---|
| 802 | |
|---|
| 803 | /** |
|---|
| 804 | * removes old items from oldSynchronizeables |
|---|
| 805 | */ |
|---|
| 806 | void NetworkStream::cleanUpOldSyncList( ) |
|---|
| 807 | { |
|---|
| 808 | int now = SDL_GetTicks(); |
|---|
| 809 | |
|---|
| 810 | for ( std::map<int,int>::iterator it = oldSynchronizeables.begin(); it != oldSynchronizeables.end(); ) |
|---|
| 811 | { |
|---|
| 812 | if ( it->second < now - 10*1000 ) |
|---|
| 813 | { |
|---|
| 814 | std::map<int,int>::iterator delIt = it; |
|---|
| 815 | it++; |
|---|
| 816 | oldSynchronizeables.erase( delIt ); |
|---|
| 817 | continue; |
|---|
| 818 | } |
|---|
| 819 | it++; |
|---|
| 820 | } |
|---|
| 821 | } |
|---|
| 822 | |
|---|
| 823 | /** |
|---|
| 824 | * writes data to DATA/dicts/newdict |
|---|
| 825 | * @param data pointer to data |
|---|
| 826 | * @param length length |
|---|
| 827 | */ |
|---|
| 828 | void NetworkStream::writeToNewDict( byte * data, int length, bool upstream ) |
|---|
| 829 | { |
|---|
| 830 | if ( remainingBytesToWriteToDict <= 0 ) |
|---|
| 831 | return; |
|---|
| 832 | |
|---|
| 833 | if ( length > remainingBytesToWriteToDict ) |
|---|
| 834 | length = remainingBytesToWriteToDict; |
|---|
| 835 | |
|---|
| 836 | std::string fileName = ResourceManager::getInstance()->getDataDir(); |
|---|
| 837 | fileName += "/dicts/newdict"; |
|---|
| 838 | |
|---|
| 839 | if ( upstream ) |
|---|
| 840 | fileName += "_upstream"; |
|---|
| 841 | else |
|---|
| 842 | fileName += "_downstream"; |
|---|
| 843 | |
|---|
| 844 | FILE * f = fopen( fileName.c_str(), "a" ); |
|---|
| 845 | |
|---|
| 846 | if ( !f ) |
|---|
| 847 | { |
|---|
| 848 | PRINTF(2)("could not open %s\n", fileName.c_str()); |
|---|
| 849 | remainingBytesToWriteToDict = 0; |
|---|
| 850 | return; |
|---|
| 851 | } |
|---|
| 852 | |
|---|
| 853 | if ( fwrite( data, 1, length, f ) != length ) |
|---|
| 854 | { |
|---|
| 855 | PRINTF(2)("could not write to file\n"); |
|---|
| 856 | fclose( f ); |
|---|
| 857 | return; |
|---|
| 858 | } |
|---|
| 859 | |
|---|
| 860 | fclose( f ); |
|---|
| 861 | |
|---|
| 862 | remainingBytesToWriteToDict -= length; |
|---|
| 863 | } |
|---|
| 864 | |
|---|
| 865 | |
|---|
| 866 | |
|---|
| 867 | |
|---|
| 868 | |
|---|
| 869 | |
|---|