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