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