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