[6067] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Wuest |
---|
| 13 | co-programmer: ... |
---|
| 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 | |
---|
[6341] | 22 | #include "factory.h" |
---|
| 23 | #include "network_stream.h" |
---|
| 24 | #include "converter.h" |
---|
| 25 | |
---|
[6424] | 26 | #include "p_node.h" |
---|
[6498] | 27 | #include "state.h" |
---|
| 28 | #include "game_world.h" |
---|
| 29 | #include "world_entity.h" |
---|
| 30 | #include "playable.h" |
---|
| 31 | #include "player.h" |
---|
| 32 | #include "network_manager.h" |
---|
[6424] | 33 | |
---|
| 34 | |
---|
[6067] | 35 | /* include your own header */ |
---|
[6116] | 36 | #include "network_game_manager.h" |
---|
[6067] | 37 | |
---|
| 38 | |
---|
| 39 | /* using namespace std is default, this needs to be here */ |
---|
| 40 | using namespace std; |
---|
| 41 | |
---|
[6341] | 42 | NetworkGameManager* NetworkGameManager::singletonRef = NULL; |
---|
| 43 | |
---|
[6067] | 44 | /*! |
---|
| 45 | * Standard constructor |
---|
| 46 | */ |
---|
[6116] | 47 | NetworkGameManager::NetworkGameManager() |
---|
[6067] | 48 | { |
---|
[6341] | 49 | PRINTF(0)("START\n"); |
---|
| 50 | |
---|
[6067] | 51 | /* set the class id for the base object */ |
---|
[6341] | 52 | this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager"); |
---|
| 53 | |
---|
| 54 | newUniqueID = MAX_CONNECTIONS + 2; |
---|
| 55 | |
---|
| 56 | hasRequestedWorld = false; |
---|
[6067] | 57 | } |
---|
| 58 | |
---|
| 59 | /*! |
---|
| 60 | * Standard destructor |
---|
| 61 | */ |
---|
[6116] | 62 | NetworkGameManager::~NetworkGameManager() |
---|
[6067] | 63 | { |
---|
[6341] | 64 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
| 65 | { |
---|
| 66 | if ( outBuffer[i].buffer ) |
---|
| 67 | delete outBuffer[i].buffer; |
---|
| 68 | } |
---|
| 69 | |
---|
[6067] | 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
[6341] | 73 | int NetworkGameManager::writeBytes(const byte* data, int length, int sender) |
---|
[6067] | 74 | { |
---|
[6341] | 75 | int i = 0; |
---|
| 76 | byte b; |
---|
| 77 | |
---|
| 78 | while ( i<length ) |
---|
| 79 | { |
---|
| 80 | b = data[i++]; |
---|
| 81 | |
---|
| 82 | if ( isServer() ) |
---|
| 83 | { |
---|
| 84 | if ( b == REQUEST_CREATE ) |
---|
| 85 | { |
---|
| 86 | if ( !handleRequestCreate( i, data, length, sender ) ) |
---|
| 87 | return i; |
---|
| 88 | continue; |
---|
| 89 | } |
---|
| 90 | if ( b == REQUEST_REMOVE ) |
---|
| 91 | { |
---|
| 92 | if ( !handleRequestRemove( i, data, length, sender ) ) |
---|
| 93 | return i; |
---|
| 94 | continue; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | else |
---|
| 98 | { |
---|
| 99 | if ( b == CREATE_ENTITY ) |
---|
| 100 | { |
---|
[6634] | 101 | PRINTF(0)("CREATE_ENTITY\n"); |
---|
[6341] | 102 | if ( !handleCreateEntity( i, data, length, sender ) ) |
---|
| 103 | return i; |
---|
| 104 | continue; |
---|
| 105 | } |
---|
| 106 | if ( b == REMOVE_ENTITY ) |
---|
| 107 | { |
---|
| 108 | if ( !handleRemoveEntity( i, data, length, sender ) ) |
---|
| 109 | return i; |
---|
| 110 | continue; |
---|
| 111 | } |
---|
| 112 | if ( b == CREATE_ENTITY_LIST ) |
---|
| 113 | { |
---|
| 114 | if ( !handleCreateEntityList( i, data, length, sender ) ) |
---|
| 115 | return i; |
---|
| 116 | continue; |
---|
| 117 | } |
---|
| 118 | if ( b == REMOVE_ENTITY_LIST ) |
---|
| 119 | { |
---|
| 120 | if ( !handleRemoveEntityList( i, data, length, sender ) ) |
---|
| 121 | return i; |
---|
| 122 | continue; |
---|
| 123 | } |
---|
| 124 | if ( b == YOU_ARE_ENTITY ) |
---|
| 125 | { |
---|
| 126 | if ( !handleYouAreEntity( i, data, length, sender ) ) |
---|
| 127 | return i; |
---|
| 128 | continue; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
[6634] | 132 | if ( b == REQUEST_ENTITY_LIST ) |
---|
[6341] | 133 | { |
---|
[6634] | 134 | sendEntityList( sender ); |
---|
[6341] | 135 | continue; |
---|
| 136 | } |
---|
| 137 | |
---|
[6634] | 138 | if ( b == REQUEST_SYNC ) |
---|
[6341] | 139 | { |
---|
[6634] | 140 | if ( !handleRequestSync( i, data, length, sender ) ) |
---|
| 141 | return i; |
---|
[6341] | 142 | continue; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | //if we get her something with data is wrong |
---|
| 146 | PRINTF(1)("Data is not in the right format! i=%d\n", i); |
---|
| 147 | return i; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | return i; |
---|
[6067] | 151 | } |
---|
| 152 | |
---|
[6116] | 153 | int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever) |
---|
[6067] | 154 | { |
---|
[6341] | 155 | if ( !isServer() && !hasRequestedWorld ) |
---|
| 156 | { |
---|
| 157 | SYNCHELP_WRITE_BEGIN(); |
---|
| 158 | byte b = REQUEST_ENTITY_LIST; |
---|
| 159 | SYNCHELP_WRITE_BYTE( b ); |
---|
| 160 | hasRequestedWorld = true; |
---|
| 161 | return SYNCHELP_WRITE_N; |
---|
| 162 | } |
---|
[6498] | 163 | |
---|
[6341] | 164 | for ( int i = 0; i<outBuffer.size(); i++ ) |
---|
| 165 | { |
---|
| 166 | *reciever = i; |
---|
| 167 | if ( outBuffer[i].length>0 ) |
---|
| 168 | { |
---|
| 169 | int nbytes = outBuffer[i].length; |
---|
| 170 | outBuffer[i].length = 0; |
---|
| 171 | |
---|
| 172 | if ( nbytes > maxLength ) |
---|
| 173 | { |
---|
| 174 | PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength); |
---|
| 175 | return 0; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | memcpy(data, outBuffer[i].buffer, nbytes); |
---|
| 179 | return nbytes; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
[6634] | 183 | return 0; |
---|
[6067] | 184 | } |
---|
| 185 | |
---|
[6116] | 186 | void NetworkGameManager::writeDebug() const |
---|
[6067] | 187 | { |
---|
| 188 | } |
---|
| 189 | |
---|
[6116] | 190 | void NetworkGameManager::readDebug() const |
---|
[6067] | 191 | { |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | /*! |
---|
| 196 | * Checks whether this is connected to a server or a client |
---|
[6341] | 197 | * and afterwards creates the needed entity |
---|
[6067] | 198 | * @param classID: The ID of the class of which an entity should be created |
---|
| 199 | */ |
---|
[6498] | 200 | int NetworkGameManager::createEntity( ClassID classID, int owner ) |
---|
[6067] | 201 | { |
---|
[6341] | 202 | if ( this->isServer() ) |
---|
| 203 | { |
---|
| 204 | if ( newUniqueID < 0 ) |
---|
| 205 | { |
---|
| 206 | PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); |
---|
[6498] | 207 | return -1; |
---|
[6341] | 208 | } |
---|
| 209 | |
---|
[6498] | 210 | return this->executeCreateEntity( classID, newUniqueID++, owner ); |
---|
[6341] | 211 | } |
---|
| 212 | else |
---|
| 213 | { |
---|
| 214 | this->requestCreateEntity( classID ); |
---|
[6498] | 215 | return -1; |
---|
[6341] | 216 | } |
---|
[6067] | 217 | } |
---|
| 218 | |
---|
[6341] | 219 | |
---|
[6067] | 220 | /*! |
---|
| 221 | * Checks whether this is connected to a server or a client |
---|
[6341] | 222 | * and afterwards creates the needed entity |
---|
| 223 | * @param classID: The ID of the class of which an entity should be created |
---|
| 224 | */ |
---|
[6424] | 225 | BaseObject* NetworkGameManager::createEntity(const TiXmlElement* element) |
---|
[6341] | 226 | { |
---|
| 227 | if ( this->isServer() ) |
---|
| 228 | { |
---|
| 229 | if ( newUniqueID < 0 ) |
---|
| 230 | { |
---|
| 231 | PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); |
---|
| 232 | return NULL; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | BaseObject * b = Factory::fabricate( element ); |
---|
| 236 | |
---|
| 237 | if ( !b ) |
---|
| 238 | { |
---|
[6424] | 239 | PRINTF(1)("Could not fabricate Object with className %s\n", element->Value() ); |
---|
[6341] | 240 | return NULL; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
| 245 | { |
---|
| 246 | Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); |
---|
[6634] | 247 | s->setUniqueID( newUniqueID++ ); |
---|
[6341] | 248 | s->setOwner( 0 ); |
---|
| 249 | this->networkStream->connectSynchronizeable( *s ); |
---|
| 250 | return b; |
---|
| 251 | } |
---|
| 252 | else |
---|
| 253 | { |
---|
| 254 | PRINTF(1)("Class %s is not a synchronizeable!\n", b->getClassName() ); |
---|
| 255 | delete b; |
---|
| 256 | } |
---|
[6498] | 257 | |
---|
[6341] | 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
| 261 | PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value()); |
---|
| 262 | } |
---|
| 263 | return NULL; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | /*! |
---|
| 268 | * Checks whether this is connected to a server or a client |
---|
[6067] | 269 | * and afterwards removes the specified entity |
---|
| 270 | * @param uniqueID: The ID of the entity object which should be removed |
---|
| 271 | */ |
---|
[6116] | 272 | void NetworkGameManager::removeEntity(int uniqueID) |
---|
[6067] | 273 | { |
---|
[6341] | 274 | if ( this->isServer() ) |
---|
| 275 | { |
---|
| 276 | this->executeRemoveEntity( uniqueID ); |
---|
| 277 | } |
---|
| 278 | else |
---|
| 279 | { |
---|
| 280 | this->requestRemoveEntity( uniqueID ); |
---|
| 281 | } |
---|
[6067] | 282 | } |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | /*! |
---|
| 287 | * Creates the needed entity on the server if possible |
---|
| 288 | * @param classID: The ID of the class of which an entity should be created |
---|
| 289 | */ |
---|
[6341] | 290 | void NetworkGameManager::requestCreateEntity(ClassID classID) |
---|
[6067] | 291 | { |
---|
[6634] | 292 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
| 293 | { |
---|
| 294 | if ( !this->networkStream->isUserIdActive( i ) ) |
---|
| 295 | continue; |
---|
| 296 | |
---|
| 297 | if ( !writeToClientBuffer( outBuffer[i], (byte)REQUEST_CREATE ) ) |
---|
| 298 | return; |
---|
| 299 | if ( !writeToClientBuffer( outBuffer[i], (int)classID ) ) |
---|
| 300 | return; |
---|
| 301 | } |
---|
[6067] | 302 | } |
---|
| 303 | |
---|
| 304 | /*! |
---|
| 305 | * Removes the specified entity on the server |
---|
| 306 | * @param uniqueID: The ID of the entity object which should be removed |
---|
| 307 | */ |
---|
[6116] | 308 | void NetworkGameManager::requestRemoveEntity(int uniqueID) |
---|
[6067] | 309 | { |
---|
[6634] | 310 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
| 311 | { |
---|
| 312 | if ( !this->networkStream->isUserIdActive( i ) ) |
---|
| 313 | continue; |
---|
| 314 | |
---|
| 315 | if ( !writeToClientBuffer( outBuffer[i], (byte)REQUEST_REMOVE ) ) |
---|
| 316 | return; |
---|
| 317 | if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) |
---|
| 318 | return; |
---|
| 319 | } |
---|
[6067] | 320 | } |
---|
| 321 | |
---|
| 322 | /*! |
---|
| 323 | * Creates the needed entity if possible |
---|
| 324 | * This function is called if this is a server |
---|
| 325 | * @param classID: The ID of the class of which an entity should be created |
---|
| 326 | */ |
---|
[6498] | 327 | int NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner) |
---|
[6067] | 328 | { |
---|
[6634] | 329 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
| 330 | { |
---|
| 331 | if ( !this->networkStream->isUserIdActive( i ) ) |
---|
| 332 | continue; |
---|
[6341] | 333 | |
---|
[6634] | 334 | if ( !writeToClientBuffer( outBuffer[i], (byte)CREATE_ENTITY ) ) |
---|
| 335 | return -1; |
---|
| 336 | if ( !writeToClientBuffer( outBuffer[i], (int)classID ) ) |
---|
| 337 | return -1; |
---|
| 338 | if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) |
---|
| 339 | return -1; |
---|
| 340 | if ( !writeToClientBuffer( outBuffer[i], owner ) ) |
---|
| 341 | return -1; |
---|
| 342 | } |
---|
| 343 | |
---|
[6341] | 344 | doCreateEntity( classID, uniqueID, owner ); |
---|
[6498] | 345 | |
---|
| 346 | return uniqueID; |
---|
[6067] | 347 | } |
---|
| 348 | |
---|
| 349 | /*! |
---|
| 350 | * Removes the specified entity |
---|
| 351 | * This function is called if this is a server |
---|
| 352 | * @param uniqueID: The ID of the entity object which should be removed |
---|
| 353 | */ |
---|
[6116] | 354 | void NetworkGameManager::executeRemoveEntity(int uniqueID) |
---|
[6067] | 355 | { |
---|
[6634] | 356 | for ( int i = 0; i<outBuffer.size(); i++) |
---|
| 357 | { |
---|
| 358 | if ( !this->networkStream->isUserIdActive( i ) ) |
---|
| 359 | continue; |
---|
[6341] | 360 | |
---|
[6634] | 361 | if ( !writeToClientBuffer( outBuffer[i], (byte)REMOVE_ENTITY ) ) |
---|
| 362 | return; |
---|
| 363 | if ( !writeToClientBuffer( outBuffer[i], uniqueID ) ) |
---|
| 364 | return; |
---|
| 365 | } |
---|
| 366 | |
---|
[6341] | 367 | doRemoveEntity(uniqueID); |
---|
[6067] | 368 | } |
---|
| 369 | |
---|
| 370 | /*! |
---|
| 371 | * Checks whether it is possible to create an entity of a given class |
---|
| 372 | * @return: true if the entity can be created, false otherwise |
---|
| 373 | */ |
---|
[6341] | 374 | bool NetworkGameManager::canCreateEntity(ClassID classID) |
---|
[6067] | 375 | { |
---|
[6341] | 376 | return true; |
---|
[6067] | 377 | } |
---|
[6341] | 378 | |
---|
| 379 | /*! |
---|
| 380 | * Sends the Entities to the new connected client |
---|
| 381 | * @param userID: The ID of the user |
---|
| 382 | */ |
---|
| 383 | void NetworkGameManager::sendEntityList( int userID ) |
---|
| 384 | { |
---|
| 385 | if ( !isServer() ) |
---|
| 386 | return; |
---|
| 387 | |
---|
| 388 | if ( userID >= outBuffer.size() ) |
---|
| 389 | resizeBufferVector( userID ); |
---|
| 390 | |
---|
| 391 | SynchronizeableList::const_iterator it, e; |
---|
| 392 | |
---|
| 393 | it = this->networkStream->getSyncBegin(); |
---|
| 394 | e = this->networkStream->getSyncEnd(); |
---|
| 395 | |
---|
| 396 | if ( !writeToClientBuffer( outBuffer[userID], (byte)CREATE_ENTITY_LIST ) ) |
---|
| 397 | return; |
---|
| 398 | |
---|
| 399 | // -2 because you must not send network_game_manager and handshake |
---|
| 400 | if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) ) |
---|
| 401 | return; |
---|
| 402 | |
---|
| 403 | //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 ); |
---|
| 404 | |
---|
[6424] | 405 | int n = 0; |
---|
| 406 | |
---|
[6341] | 407 | while ( it != e ) |
---|
| 408 | { |
---|
[6634] | 409 | PRINTF(5)("SENDING ENTITY %s id %d\n", (*it)->getClassName(), (*it)->getUniqueID() ); |
---|
[6341] | 410 | if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) ) |
---|
| 411 | return; |
---|
| 412 | |
---|
| 413 | if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) ) |
---|
| 414 | return; |
---|
| 415 | |
---|
| 416 | if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) ) |
---|
| 417 | return; |
---|
| 418 | |
---|
[6498] | 419 | it++; |
---|
| 420 | } |
---|
[6424] | 421 | |
---|
[6498] | 422 | signalNewPlayer( userID ); |
---|
| 423 | } |
---|
[6424] | 424 | |
---|
| 425 | |
---|
| 426 | |
---|
[6498] | 427 | bool NetworkGameManager::signalNewPlayer(int userId) |
---|
| 428 | { |
---|
[6634] | 429 | if ( userId >= outBuffer.size() ) |
---|
| 430 | resizeBufferVector( userId ); |
---|
[6341] | 431 | |
---|
[6498] | 432 | /* create new playable for Player*/ |
---|
| 433 | PRINTF(0)("Request for creation: %i\n", userId); |
---|
| 434 | int uniqueId = this->createEntity(CL_SPACE_SHIP, userId); |
---|
| 435 | PRINTF(0)("Request for creation: userid: %i, uniqueid: %i\n", userId, uniqueId); |
---|
| 436 | this->sendYouAre(uniqueId, userId); |
---|
[6341] | 437 | |
---|
| 438 | } |
---|
| 439 | |
---|
[6498] | 440 | |
---|
[6341] | 441 | /** |
---|
| 442 | * Creates a buffer for user n |
---|
| 443 | * @param n The ID of the user |
---|
| 444 | */ |
---|
| 445 | void NetworkGameManager::resizeBufferVector( int n ) |
---|
| 446 | { |
---|
| 447 | for ( int i = outBuffer.size(); i<=n; i++) |
---|
| 448 | { |
---|
| 449 | clientBuffer outBuf; |
---|
| 450 | |
---|
| 451 | outBuf.length = 0; |
---|
| 452 | |
---|
| 453 | outBuf.maxLength = 5*1024; |
---|
| 454 | |
---|
| 455 | outBuf.buffer = new byte[5*1014]; |
---|
| 456 | |
---|
| 457 | outBuffer.push_back(outBuf); |
---|
| 458 | } |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | /** |
---|
| 462 | * Creates the entity on this host |
---|
| 463 | * @param classID: ClassID of the entity to create |
---|
| 464 | * @param uniqueID: Unique ID to assign to the synchronizeable |
---|
| 465 | * @param owner: owner of this synchronizealbe |
---|
| 466 | */ |
---|
[6424] | 467 | BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner ) |
---|
[6341] | 468 | { |
---|
| 469 | BaseObject * b = Factory::fabricate( classID ); |
---|
| 470 | |
---|
| 471 | if ( !b ) |
---|
| 472 | { |
---|
| 473 | PRINTF(1)("Could not fabricate Object with classID %x\n", classID); |
---|
[6424] | 474 | return NULL; |
---|
[6341] | 475 | } |
---|
| 476 | |
---|
| 477 | if ( b->isA(CL_SYNCHRONIZEABLE) ) |
---|
| 478 | { |
---|
| 479 | Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); |
---|
| 480 | s->setUniqueID( uniqueID ); |
---|
| 481 | s->setOwner( owner ); |
---|
| 482 | this->networkStream->connectSynchronizeable( *s ); |
---|
| 483 | if ( !isServer() ) |
---|
| 484 | s->setIsOutOfSync( true ); |
---|
| 485 | PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID()); |
---|
[6498] | 486 | |
---|
| 487 | //HACK: hack to prevent collision |
---|
[6634] | 488 | if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) ) |
---|
[6498] | 489 | { |
---|
| 490 | if ( NetworkManager::getInstance()->getHostID()!=0 ) |
---|
| 491 | { |
---|
| 492 | static Vector pos = Vector(1000.0, 1000.0, 1000.0); |
---|
| 493 | PNode *p = dynamic_cast<PNode*>(b); |
---|
[6634] | 494 | p->setAbsCoor(pos); |
---|
| 495 | //p->updateNode(0); |
---|
[6498] | 496 | pos += Vector(1000.0, 1000.0, 1000.0); |
---|
| 497 | } |
---|
| 498 | } |
---|
| 499 | |
---|
[6424] | 500 | return b; |
---|
[6341] | 501 | } |
---|
| 502 | else |
---|
| 503 | { |
---|
| 504 | PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID); |
---|
| 505 | delete b; |
---|
| 506 | } |
---|
[6424] | 507 | return NULL; |
---|
[6341] | 508 | } |
---|
| 509 | |
---|
| 510 | /** |
---|
| 511 | * Removes a entity on this host |
---|
| 512 | * @param uniqueID: unique ID assigned with the entity to remove |
---|
| 513 | */ |
---|
| 514 | void NetworkGameManager::doRemoveEntity( int uniqueID ) |
---|
| 515 | { |
---|
| 516 | SynchronizeableList::const_iterator it,e; |
---|
| 517 | it = this->networkStream->getSyncBegin(); |
---|
| 518 | e = this->networkStream->getSyncEnd(); |
---|
| 519 | |
---|
| 520 | while ( it != e ) |
---|
| 521 | { |
---|
| 522 | if ( (*it)->getUniqueID() == uniqueID ) |
---|
| 523 | { |
---|
| 524 | delete *it; |
---|
| 525 | break; |
---|
| 526 | } |
---|
| 527 | it++; |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | /** |
---|
| 532 | * Tell the synchronizeable that a user's synchronizeable is out of sync |
---|
| 533 | * @param uniqueID: unique ID assigned with the entity which is out of sync |
---|
| 534 | * @param userID: user ID who's synchronizeable is out of sync |
---|
| 535 | */ |
---|
| 536 | void NetworkGameManager::doRequestSync( int uniqueID, int userID ) |
---|
| 537 | { |
---|
| 538 | SynchronizeableList::const_iterator it,e; |
---|
| 539 | it = this->networkStream->getSyncBegin(); |
---|
| 540 | e = this->networkStream->getSyncEnd(); |
---|
| 541 | |
---|
| 542 | while ( it != e ) |
---|
| 543 | { |
---|
| 544 | if ( (*it)->getUniqueID() == uniqueID ) |
---|
| 545 | { |
---|
| 546 | (*it)->requestSync( userID ); |
---|
| 547 | break; |
---|
| 548 | } |
---|
| 549 | it++; |
---|
| 550 | } |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | /** |
---|
| 554 | * Copies length bytes to the clientBuffer with error checking |
---|
| 555 | * @param clientBuffer: the clientBuffer to write to |
---|
| 556 | * @param data: buffer to the data |
---|
| 557 | * @param length: length of data |
---|
| 558 | * @return false on error true else |
---|
| 559 | */ |
---|
| 560 | bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length ) |
---|
| 561 | { |
---|
| 562 | if ( length > cb.maxLength-cb.length ) |
---|
| 563 | { |
---|
| 564 | PRINTF(1)("No space left in clientBuffer\n"); |
---|
| 565 | return false; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | memcpy( cb.buffer+cb.length, data, length ); |
---|
| 569 | return true; |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | /** |
---|
| 573 | * Reads data from clientBuffer with error checking |
---|
| 574 | * @param clientBuffer: the clientBuffer to read from |
---|
| 575 | * @param data: pointer to the buffer |
---|
| 576 | * @param length: |
---|
| 577 | * @return |
---|
| 578 | */ |
---|
| 579 | bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length ) |
---|
| 580 | { |
---|
| 581 | if ( cb.length < length ) |
---|
| 582 | { |
---|
| 583 | PRINTF(0)("There is not enough data in clientBuffer\n"); |
---|
| 584 | return 0; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | memcpy( data, cb.buffer+cb.length-length, length ); |
---|
| 588 | return true; |
---|
| 589 | } |
---|
| 590 | |
---|
| 591 | /** |
---|
| 592 | * Tells this client that he has to control this entity |
---|
| 593 | * @param uniqueID: the entity's uniqeID |
---|
| 594 | */ |
---|
| 595 | void NetworkGameManager::doYouAre( int uniqueID ) |
---|
| 596 | { |
---|
[6498] | 597 | |
---|
| 598 | SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin(); |
---|
| 599 | |
---|
| 600 | Playable *p = NULL; |
---|
[6634] | 601 | Synchronizeable *s = NULL; |
---|
[6498] | 602 | |
---|
| 603 | for ( ; it !=networkStream->getSyncEnd(); it++ ) |
---|
| 604 | { |
---|
| 605 | if ( (*it)->getUniqueID()==uniqueID ) |
---|
| 606 | { |
---|
[6634] | 607 | if ( (*it)->isA( CL_SYNCHRONIZEABLE ) ) |
---|
| 608 | { |
---|
| 609 | s = dynamic_cast<Synchronizeable*>(*it); |
---|
| 610 | } |
---|
[6498] | 611 | if ( (*it)->isA( CL_PLAYABLE ) ) |
---|
| 612 | { |
---|
| 613 | p = dynamic_cast<Playable*>(*it); |
---|
| 614 | break; |
---|
| 615 | } else |
---|
| 616 | { |
---|
| 617 | PRINTF(1)("UniqueID is not a Playable\n"); |
---|
| 618 | } |
---|
| 619 | } |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | Player* player = State::getPlayer(); |
---|
| 623 | assert(p != NULL); |
---|
[6634] | 624 | assert(s != NULL); |
---|
[6498] | 625 | assert(player != NULL); |
---|
| 626 | |
---|
[6634] | 627 | s->setIsOutOfSync( true ); |
---|
| 628 | |
---|
| 629 | PRINTF(0)("uniqueID = %d\n", s->getUniqueID()); |
---|
| 630 | |
---|
[6498] | 631 | player->setControllable(p); |
---|
| 632 | |
---|
| 633 | |
---|
[6341] | 634 | } |
---|
| 635 | |
---|
| 636 | /** |
---|
| 637 | * Tells a remote client that he has to control this entity |
---|
| 638 | * @param uniqueID: the entity's uniqeID |
---|
| 639 | * @param userID: the users ID |
---|
| 640 | */ |
---|
| 641 | void NetworkGameManager::sendYouAre( int uniqueID, int userID ) |
---|
| 642 | { |
---|
| 643 | if ( !isServer() ) |
---|
| 644 | return; |
---|
| 645 | |
---|
| 646 | if ( userID != 0 ) |
---|
| 647 | { |
---|
| 648 | if ( !writeToClientBuffer( outBuffer[userID], (byte)YOU_ARE_ENTITY ) ) |
---|
| 649 | return; |
---|
| 650 | |
---|
| 651 | if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) ) |
---|
| 652 | return; |
---|
| 653 | } |
---|
| 654 | else |
---|
| 655 | { |
---|
| 656 | doYouAre(uniqueID); |
---|
| 657 | } |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender ) |
---|
| 661 | { |
---|
| 662 | if ( INTSIZE > length-i ) |
---|
| 663 | { |
---|
| 664 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
| 665 | return false; |
---|
| 666 | } |
---|
| 667 | int classID; |
---|
| 668 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
| 669 | |
---|
| 670 | createEntity( (ClassID)classID ); |
---|
| 671 | |
---|
| 672 | return true; |
---|
| 673 | } |
---|
| 674 | |
---|
| 675 | bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender ) |
---|
| 676 | { |
---|
| 677 | if ( INTSIZE > length-i ) |
---|
| 678 | { |
---|
| 679 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
| 680 | return false; |
---|
| 681 | } |
---|
| 682 | int uniqueID; |
---|
| 683 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
| 684 | |
---|
| 685 | removeEntity( uniqueID ); |
---|
| 686 | |
---|
| 687 | return true; |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender ) |
---|
| 691 | { |
---|
| 692 | if ( INTSIZE > length-i ) |
---|
| 693 | { |
---|
| 694 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
| 695 | return false; |
---|
| 696 | } |
---|
| 697 | int classID; |
---|
| 698 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
| 699 | |
---|
| 700 | if ( INTSIZE > length-i ) |
---|
| 701 | { |
---|
| 702 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
| 703 | return false; |
---|
| 704 | } |
---|
| 705 | int uniqueID; |
---|
| 706 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
| 707 | |
---|
| 708 | if ( INTSIZE > length-i ) |
---|
| 709 | { |
---|
| 710 | PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); |
---|
| 711 | return false; |
---|
| 712 | } |
---|
| 713 | int owner; |
---|
| 714 | i += Converter::byteArrayToInt( &data[i], &owner ); |
---|
| 715 | |
---|
| 716 | doCreateEntity( (ClassID)classID, uniqueID, owner ); |
---|
| 717 | |
---|
| 718 | return true; |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender ) |
---|
| 722 | { |
---|
| 723 | if ( INTSIZE > length-i ) |
---|
| 724 | { |
---|
| 725 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
| 726 | return false; |
---|
| 727 | } |
---|
| 728 | int uniqueID; |
---|
| 729 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
| 730 | |
---|
| 731 | doRemoveEntity( uniqueID ); |
---|
| 732 | |
---|
| 733 | return true; |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender ) |
---|
| 737 | { |
---|
| 738 | if ( INTSIZE > length-i ) |
---|
| 739 | { |
---|
| 740 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
| 741 | return false; |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | PRINTF(0)("HandleCreateEntityList: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]); |
---|
| 745 | |
---|
| 746 | int n; |
---|
| 747 | i += Converter::byteArrayToInt( &data[i], &n ); |
---|
| 748 | |
---|
| 749 | |
---|
| 750 | PRINTF(0)("HandleCreateEntityList: n = %d\n", n); |
---|
| 751 | |
---|
| 752 | int classID, uniqueID, owner; |
---|
| 753 | |
---|
| 754 | for ( int j = 0; j<n; j++ ) |
---|
| 755 | { |
---|
| 756 | |
---|
| 757 | if ( INTSIZE > length-i ) |
---|
| 758 | { |
---|
| 759 | PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); |
---|
| 760 | return false; |
---|
| 761 | } |
---|
| 762 | i += Converter::byteArrayToInt( &data[i], &classID ); |
---|
| 763 | |
---|
| 764 | if ( INTSIZE > length-i ) |
---|
| 765 | { |
---|
| 766 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
| 767 | return false; |
---|
| 768 | } |
---|
| 769 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
| 770 | |
---|
| 771 | if ( INTSIZE > length-i ) |
---|
| 772 | { |
---|
| 773 | PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); |
---|
| 774 | return false; |
---|
| 775 | } |
---|
| 776 | i += Converter::byteArrayToInt( &data[i], &owner ); |
---|
| 777 | |
---|
| 778 | if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE ) |
---|
[6424] | 779 | { |
---|
| 780 | BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner ); |
---|
[6341] | 781 | |
---|
[6424] | 782 | /*if ( b != NULL ) |
---|
| 783 | { |
---|
| 784 | if ( b->isA(CL_WORLD_ENTITY) ) |
---|
| 785 | { |
---|
| 786 | int n = dynamic_cast<WorldEntity*>(b)->writeState( data, length, sender ); |
---|
| 787 | |
---|
| 788 | i += n; |
---|
| 789 | } |
---|
| 790 | }*/ |
---|
| 791 | } |
---|
| 792 | |
---|
[6341] | 793 | } |
---|
[6424] | 794 | |
---|
[6341] | 795 | return true; |
---|
| 796 | } |
---|
| 797 | |
---|
| 798 | bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender ) |
---|
| 799 | { |
---|
| 800 | if ( INTSIZE > length-i ) |
---|
| 801 | { |
---|
| 802 | PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); |
---|
| 803 | return false; |
---|
| 804 | } |
---|
| 805 | int n; |
---|
| 806 | i += Converter::byteArrayToInt( &data[i], &n ); |
---|
| 807 | |
---|
| 808 | int uniqueID; |
---|
| 809 | |
---|
| 810 | for ( int j = 0; j<n; j++ ) |
---|
| 811 | { |
---|
| 812 | |
---|
| 813 | if ( INTSIZE > length-i ) |
---|
| 814 | { |
---|
| 815 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
| 816 | return false; |
---|
| 817 | } |
---|
| 818 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
| 819 | |
---|
| 820 | doRemoveEntity( uniqueID ); |
---|
| 821 | } |
---|
| 822 | |
---|
| 823 | return true; |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender ) |
---|
| 827 | { |
---|
| 828 | if ( INTSIZE > length-i ) |
---|
| 829 | { |
---|
| 830 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
| 831 | return false; |
---|
| 832 | } |
---|
| 833 | |
---|
| 834 | int uniqueID; |
---|
| 835 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
| 836 | |
---|
| 837 | doYouAre( uniqueID ); |
---|
| 838 | |
---|
| 839 | return true; |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender ) |
---|
| 843 | { |
---|
| 844 | if ( INTSIZE > length-i ) |
---|
| 845 | { |
---|
| 846 | PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); |
---|
| 847 | return false; |
---|
| 848 | } |
---|
| 849 | int uniqueID; |
---|
| 850 | i += Converter::byteArrayToInt( &data[i], &uniqueID ); |
---|
| 851 | |
---|
| 852 | doRequestSync( uniqueID, sender ); |
---|
| 853 | |
---|
| 854 | return true; |
---|
| 855 | } |
---|
| 856 | |
---|
| 857 | bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b ) |
---|
| 858 | { |
---|
| 859 | if ( cb.maxLength-cb.length < 1 ) |
---|
| 860 | { |
---|
| 861 | PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n"); |
---|
| 862 | return false; |
---|
| 863 | } |
---|
| 864 | |
---|
| 865 | cb.buffer[cb.length++] = b; |
---|
| 866 | |
---|
| 867 | return true; |
---|
| 868 | } |
---|
| 869 | |
---|
| 870 | bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i ) |
---|
| 871 | { |
---|
| 872 | int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length ); |
---|
| 873 | cb.length += n; |
---|
| 874 | |
---|
| 875 | if ( n <= 0 ) |
---|
| 876 | { |
---|
| 877 | PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n"); |
---|
| 878 | return false; |
---|
| 879 | } |
---|
| 880 | |
---|
| 881 | return true; |
---|
| 882 | } |
---|
| 883 | |
---|
| 884 | void NetworkGameManager::sync( int uniqueID, int owner ) |
---|
| 885 | { |
---|
[6634] | 886 | /*if ( owner==this->getHostID() ) |
---|
| 887 | return;*/ |
---|
[6341] | 888 | |
---|
| 889 | if ( !isServer() ) |
---|
| 890 | executeRequestSync( uniqueID, 0 ); |
---|
| 891 | else |
---|
| 892 | executeRequestSync( uniqueID, owner ); |
---|
| 893 | } |
---|
| 894 | |
---|
| 895 | void NetworkGameManager::executeRequestSync( int uniqueID, int user ) |
---|
| 896 | { |
---|
| 897 | if ( user >= outBuffer.size() ) |
---|
| 898 | resizeBufferVector( user ); |
---|
| 899 | |
---|
| 900 | if ( !writeToClientBuffer( outBuffer[user], (byte)REQUEST_SYNC ) ) |
---|
| 901 | return; |
---|
| 902 | if ( !writeToClientBuffer( outBuffer[user], uniqueID ) ) |
---|
| 903 | return; |
---|
| 904 | } |
---|
| 905 | |
---|