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