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