[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: |
---|
[5601] | 12 | main-programmer: claudio |
---|
[5800] | 13 | co-programmer: |
---|
[5566] | 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
| 18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
| 19 | */ |
---|
| 20 | #define DEBUG_MODULE_NETWORK |
---|
| 21 | |
---|
[5747] | 22 | |
---|
[5647] | 23 | #include "base_object.h" |
---|
[5731] | 24 | #include "network_protocol.h" |
---|
[5647] | 25 | #include "network_socket.h" |
---|
| 26 | #include "connection_monitor.h" |
---|
| 27 | #include "synchronizeable.h" |
---|
[6139] | 28 | #include "network_manager.h" |
---|
[6341] | 29 | #include "network_game_manager.h" |
---|
| 30 | |
---|
[5649] | 31 | #include "debug.h" |
---|
[6139] | 32 | #include "class_list.h" |
---|
[6144] | 33 | #include <algorithm> |
---|
[5647] | 34 | |
---|
[5566] | 35 | /* include your own header */ |
---|
| 36 | #include "network_stream.h" |
---|
| 37 | |
---|
[5595] | 38 | /* probably unnecessary */ |
---|
[5594] | 39 | using namespace std; |
---|
| 40 | |
---|
[5595] | 41 | |
---|
[5747] | 42 | #define PACKAGE_SIZE 256 |
---|
[5647] | 43 | |
---|
[5747] | 44 | |
---|
[5800] | 45 | NetworkStream::NetworkStream() |
---|
[5996] | 46 | : DataStream() |
---|
[5647] | 47 | { |
---|
| 48 | this->init(); |
---|
[5648] | 49 | /* initialize the references */ |
---|
[5996] | 50 | this->type = NET_CLIENT; |
---|
[5741] | 51 | this->networkProtocol = new NetworkProtocol(); |
---|
[5648] | 52 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 53 | } |
---|
| 54 | |
---|
[6139] | 55 | NetworkStream::NetworkStream(IPaddress& address) |
---|
[5996] | 56 | { |
---|
[6139] | 57 | this->type = NET_CLIENT; |
---|
[5996] | 58 | this->init(); |
---|
[6139] | 59 | this->networkSockets.push_back(new NetworkSocket(address)); |
---|
[5996] | 60 | this->networkProtocol = new NetworkProtocol(); |
---|
| 61 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[5647] | 62 | |
---|
[6139] | 63 | Handshake* hs = new Handshake(false); |
---|
| 64 | hs->setUniqueID( 0 ); |
---|
| 65 | this->handshakes.push_back(hs); |
---|
| 66 | this->connectSynchronizeable(*hs); |
---|
| 67 | PRINTF(0)("NetworkStream: %s\n", hs->getName()); |
---|
[6341] | 68 | this->maxConnections = 1; |
---|
[5996] | 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
[6139] | 72 | NetworkStream::NetworkStream(unsigned int port) |
---|
[5647] | 73 | { |
---|
[6139] | 74 | this->type = NET_SERVER; |
---|
[5647] | 75 | this->init(); |
---|
[6139] | 76 | this->serverSocket = new ServerSocket(port); |
---|
[5741] | 77 | this->networkProtocol = new NetworkProtocol(); |
---|
| 78 | this->connectionMonitor = new ConnectionMonitor(); |
---|
[6139] | 79 | this->networkSockets.push_back( NULL ); |
---|
[6503] | 80 | this->networkSockets[0] = NULL; //TODO: remove this |
---|
[6139] | 81 | this->handshakes.push_back( NULL ); |
---|
[5996] | 82 | this->bActive = true; |
---|
[6341] | 83 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 84 | // setUniqueID( maxCon+2 ) because we need one id for every handshake |
---|
| 85 | // and one for handshake to reject client maxCon+1 |
---|
| 86 | this->networkGameManager->setUniqueID( this->maxConnections+2 ); |
---|
| 87 | this->connectSynchronizeable( *(this->networkGameManager) ); |
---|
[5607] | 88 | |
---|
[6139] | 89 | this->setMaxConnections( 10 ); |
---|
[5649] | 90 | } |
---|
| 91 | |
---|
| 92 | |
---|
[5647] | 93 | void NetworkStream::init() |
---|
| 94 | { |
---|
| 95 | /* set the class id for the base object */ |
---|
| 96 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
[5996] | 97 | this->bActive = false; |
---|
[6139] | 98 | this->serverSocket = NULL; |
---|
[6341] | 99 | this->networkGameManager = NULL; |
---|
[6139] | 100 | myHostId = 0; |
---|
[5594] | 101 | } |
---|
| 102 | |
---|
[5647] | 103 | |
---|
[5566] | 104 | NetworkStream::~NetworkStream() |
---|
[5598] | 105 | { |
---|
[6139] | 106 | if ( this->serverSocket ) |
---|
| 107 | { |
---|
| 108 | serverSocket->close(); |
---|
| 109 | delete serverSocket; |
---|
| 110 | } |
---|
[5723] | 111 | |
---|
[6139] | 112 | for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++) |
---|
| 113 | { |
---|
| 114 | if ( *i ) |
---|
| 115 | { |
---|
| 116 | (*i)->disconnectServer(); |
---|
| 117 | (*i)->destroy(); |
---|
| 118 | } |
---|
| 119 | } |
---|
[5723] | 120 | |
---|
[6139] | 121 | for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++) |
---|
| 122 | { |
---|
| 123 | if ( *i ) |
---|
| 124 | { |
---|
| 125 | delete (*i); |
---|
| 126 | } |
---|
| 127 | } |
---|
[5805] | 128 | |
---|
[5996] | 129 | delete connectionMonitor; |
---|
| 130 | delete networkProtocol; |
---|
[5598] | 131 | } |
---|
| 132 | |
---|
[5996] | 133 | |
---|
| 134 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
| 135 | { |
---|
[6139] | 136 | this->synchronizeables.push_back(&sync); |
---|
| 137 | sync.setNetworkStream( this ); |
---|
| 138 | |
---|
| 139 | if( this->networkSockets.size()>0 ) |
---|
[5996] | 140 | this->bActive = true; |
---|
| 141 | } |
---|
| 142 | |
---|
[6139] | 143 | void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) |
---|
| 144 | { |
---|
[6144] | 145 | // removing the Synchronizeable from the List. |
---|
| 146 | std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync); |
---|
| 147 | if (disconnectSynchro != this->synchronizeables.end()) |
---|
| 148 | this->synchronizeables.erase(disconnectSynchro); |
---|
[5996] | 149 | |
---|
[6139] | 150 | if( this->networkSockets.size()<=0 ) |
---|
| 151 | this->bActive = false; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
[5604] | 155 | void NetworkStream::processData() |
---|
| 156 | { |
---|
[6139] | 157 | if ( this->type == NET_SERVER ) |
---|
| 158 | this->updateConnectionList(); |
---|
| 159 | else |
---|
| 160 | { |
---|
| 161 | if ( networkSockets[0] && !networkSockets[0]->isOk() ) |
---|
| 162 | { |
---|
| 163 | PRINTF(1)("lost connection to server\n"); |
---|
[5741] | 164 | |
---|
[6139] | 165 | //delete networkSockets[i]; |
---|
| 166 | networkSockets[0]->disconnectServer(); |
---|
| 167 | networkSockets[0]->destroy(); |
---|
| 168 | networkSockets[0] = NULL; |
---|
[6498] | 169 | |
---|
[6139] | 170 | if ( handshakes[0] ) |
---|
| 171 | delete handshakes[0]; |
---|
| 172 | handshakes[0] = NULL; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | for (int i = 0; i<handshakes.size(); i++) |
---|
| 177 | { |
---|
| 178 | if ( handshakes[i] ) |
---|
| 179 | { |
---|
| 180 | if ( handshakes[i]->completed() ) |
---|
| 181 | { |
---|
| 182 | if ( handshakes[i]->ok() ) |
---|
| 183 | { |
---|
| 184 | if ( type != NET_SERVER ) |
---|
| 185 | { |
---|
| 186 | NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() ); |
---|
| 187 | myHostId = NetworkManager::getInstance()->getHostID(); |
---|
[6341] | 188 | |
---|
| 189 | this->networkGameManager = NetworkGameManager::getInstance(); |
---|
| 190 | this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() ); |
---|
| 191 | this->connectSynchronizeable( *(this->networkGameManager) ); |
---|
[6139] | 192 | } |
---|
[6341] | 193 | else |
---|
| 194 | { |
---|
[6498] | 195 | |
---|
[6341] | 196 | } |
---|
| 197 | PRINT(0)("handshake finished id=%d\n", handshakes[i]->getNetworkGameManagerId()); |
---|
| 198 | |
---|
| 199 | |
---|
[6139] | 200 | delete handshakes[i]; |
---|
| 201 | handshakes[i] = NULL; |
---|
| 202 | } |
---|
| 203 | else |
---|
| 204 | { |
---|
| 205 | PRINT(1)("handshake failed!\n"); |
---|
| 206 | networkSockets[i]->disconnectServer(); |
---|
| 207 | delete handshakes[i]; |
---|
| 208 | handshakes[i] = NULL; |
---|
| 209 | //TODO: handle error |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | |
---|
[5650] | 216 | /* DOWNSTREAM */ |
---|
[5800] | 217 | |
---|
[6139] | 218 | int dataLength; |
---|
| 219 | int reciever; |
---|
| 220 | Header header; |
---|
| 221 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
[5996] | 222 | { |
---|
[6341] | 223 | if ( (*it)!=NULL /*&& (*it)->getOwner() == myHostId*/ ) |
---|
[6139] | 224 | { |
---|
| 225 | do { |
---|
| 226 | reciever = 0; |
---|
| 227 | dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever); |
---|
[5730] | 228 | |
---|
[5800] | 229 | |
---|
[6139] | 230 | if ( dataLength<=0 ){ |
---|
| 231 | reciever = 0; |
---|
| 232 | continue; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it))); |
---|
| 236 | |
---|
| 237 | Header* header = (Header*)downBuffer; |
---|
[6341] | 238 | if ( header->synchronizeableID < this->maxConnections+2 ) |
---|
| 239 | { |
---|
| 240 | //if ( !isServer() ) PRINTF(0)("RESET UNIQUEID FROM %d TO 0 maxCon=%d\n", header->synchronizeableID, this->maxConnections); |
---|
[6139] | 241 | header->synchronizeableID = 0; |
---|
[6341] | 242 | } |
---|
| 243 | else |
---|
| 244 | { |
---|
| 245 | //if ( !isServer() ) PRINTF(0)("UNIQUEID=%d\n", header->synchronizeableID); |
---|
| 246 | } |
---|
[6139] | 247 | |
---|
| 248 | if ( dataLength<=0 ) |
---|
| 249 | continue; |
---|
| 250 | |
---|
| 251 | if ( reciever!=0 ) |
---|
| 252 | { |
---|
| 253 | if ( networkSockets[reciever] != NULL ) |
---|
| 254 | { |
---|
[6498] | 255 | PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever); |
---|
[6139] | 256 | networkSockets[reciever]->writePacket(downBuffer, dataLength); |
---|
| 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
| 260 | PRINTF(1)("networkSockets[reciever] == NULL\n"); |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | for ( int i = 0; i<networkSockets.size(); i++) |
---|
| 266 | { |
---|
| 267 | if ( networkSockets[i] != NULL ) |
---|
| 268 | { |
---|
[6503] | 269 | PRINTF(5)("write %d bytes to socket %d\n", dataLength, i); |
---|
[6139] | 270 | networkSockets[i]->writePacket(downBuffer, dataLength); |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | } while( reciever!=0 ); |
---|
| 276 | } |
---|
[5996] | 277 | } |
---|
[5741] | 278 | |
---|
| 279 | /* UPSTREAM */ |
---|
[5809] | 280 | |
---|
[6139] | 281 | for ( int i = 0; i<networkSockets.size(); i++) |
---|
[5802] | 282 | { |
---|
[6139] | 283 | if ( networkSockets[i] ) |
---|
[5810] | 284 | { |
---|
[6139] | 285 | do { |
---|
| 286 | dataLength = networkSockets[i]->readPacket(upBuffer, DATA_STREAM_BUFFER_SIZE); |
---|
[5730] | 287 | |
---|
[6139] | 288 | if ( dataLength<=0 ) |
---|
| 289 | continue; |
---|
| 290 | |
---|
| 291 | header = networkProtocol->extractHeader(upBuffer, dataLength); |
---|
| 292 | dataLength -= sizeof(header); |
---|
| 293 | |
---|
[6498] | 294 | PRINTF(5)("read %d bytes from socket uniqueID = %d\n", dataLength, header.synchronizeableID); |
---|
[6341] | 295 | |
---|
[6139] | 296 | if ( dataLength != header.length ) |
---|
| 297 | { |
---|
| 298 | PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length); |
---|
| 299 | continue; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | if ( header.synchronizeableID == 0 ) |
---|
| 303 | { |
---|
| 304 | header.synchronizeableID = i; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
| 308 | { |
---|
| 309 | if ( *it && (*it)->getUniqueID()==header.synchronizeableID ) |
---|
[6341] | 310 | { |
---|
| 311 | if ( (*it)->writeBytes(upBuffer+sizeof(header), dataLength, i) != header.length ) |
---|
| 312 | { |
---|
[6526] | 313 | PRINTF(1)("%s did not read all the data id = %d!\n", (*it)->getClassName(), (*it)->getUniqueID()); |
---|
[6341] | 314 | break; |
---|
| 315 | } |
---|
| 316 | continue; |
---|
| 317 | } |
---|
[6139] | 318 | } |
---|
| 319 | |
---|
| 320 | } while ( dataLength>0 ); |
---|
[5810] | 321 | } |
---|
| 322 | } |
---|
[6139] | 323 | } |
---|
| 324 | |
---|
| 325 | void NetworkStream::updateConnectionList( ) |
---|
| 326 | { |
---|
| 327 | //check for new connections |
---|
| 328 | |
---|
| 329 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
| 330 | |
---|
| 331 | if ( tempNetworkSocket ) |
---|
[5810] | 332 | { |
---|
[6139] | 333 | int clientId; |
---|
| 334 | if ( freeSocketSlots.size() >0 ) |
---|
[5810] | 335 | { |
---|
[6139] | 336 | clientId = freeSocketSlots.back(); |
---|
| 337 | freeSocketSlots.pop_back(); |
---|
| 338 | networkSockets[clientId] = tempNetworkSocket; |
---|
[6341] | 339 | handshakes[clientId] = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); |
---|
[6139] | 340 | handshakes[clientId]->setUniqueID(clientId); |
---|
| 341 | } else |
---|
| 342 | { |
---|
| 343 | clientId = networkSockets.size(); |
---|
| 344 | networkSockets.push_back(tempNetworkSocket); |
---|
[6341] | 345 | Handshake* tHs = new Handshake(true, clientId, this->networkGameManager->getUniqueID()); |
---|
[6139] | 346 | tHs->setUniqueID(clientId); |
---|
| 347 | handshakes.push_back(tHs); |
---|
| 348 | } |
---|
[5730] | 349 | |
---|
[6139] | 350 | if ( clientId > this->maxConnections ) |
---|
| 351 | { |
---|
| 352 | handshakes[clientId]->doReject(); |
---|
| 353 | PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); |
---|
[5810] | 354 | } |
---|
[6139] | 355 | else |
---|
| 356 | |
---|
| 357 | PRINTF(0)("New Client: %d\n", clientId); |
---|
| 358 | |
---|
| 359 | this->connectSynchronizeable(*handshakes[clientId]); |
---|
[5802] | 360 | } |
---|
[5800] | 361 | |
---|
[5809] | 362 | |
---|
[6139] | 363 | //check if connections are ok else remove them |
---|
| 364 | for ( int i = 1; i<networkSockets.size(); i++) |
---|
| 365 | { |
---|
| 366 | if ( networkSockets[i] && !networkSockets[i]->isOk() ) |
---|
| 367 | { |
---|
| 368 | //TODO: tell EntityManager that this player left the game |
---|
| 369 | PRINTF(0)("Client is gone: %d\n", i); |
---|
| 370 | |
---|
| 371 | //delete networkSockets[i]; |
---|
| 372 | networkSockets[i]->disconnectServer(); |
---|
| 373 | networkSockets[i]->destroy(); |
---|
| 374 | networkSockets[i] = NULL; |
---|
[6498] | 375 | |
---|
[6139] | 376 | if ( handshakes[i] ) |
---|
| 377 | delete handshakes[i]; |
---|
| 378 | handshakes[i] = NULL; |
---|
| 379 | |
---|
| 380 | if ( i == networkSockets.size()-1 ) |
---|
| 381 | { |
---|
| 382 | networkSockets.pop_back(); |
---|
| 383 | handshakes.pop_back(); |
---|
| 384 | } |
---|
| 385 | else |
---|
| 386 | { |
---|
| 387 | freeSocketSlots.push_back(i); |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | |
---|
[5604] | 393 | } |
---|
[6139] | 394 | |
---|
| 395 | void NetworkStream::setMaxConnections( int n ) |
---|
| 396 | { |
---|
| 397 | if ( !this->isServer() ) |
---|
| 398 | { |
---|
| 399 | PRINTF(1)("Cannot set maxConnections because I am no server.\n"); |
---|
| 400 | } |
---|
| 401 | if ( this->networkSockets.size() > 1 ) |
---|
| 402 | { |
---|
| 403 | PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size()); |
---|
| 404 | return; |
---|
| 405 | } |
---|
[6341] | 406 | |
---|
| 407 | if ( n > MAX_CONNECTIONS ) |
---|
| 408 | { |
---|
| 409 | PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS); |
---|
| 410 | return; |
---|
| 411 | } |
---|
| 412 | |
---|
[6139] | 413 | this->maxConnections = n; |
---|
[6341] | 414 | this->networkGameManager->setUniqueID( n+2 ); |
---|
[6139] | 415 | } |
---|
| 416 | |
---|
| 417 | |
---|