- Timestamp:
- May 30, 2006, 5:36:03 PM (18 years ago)
- Location:
- branches/network/src
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/defs/class_id.h
r7974 r7984 134 134 CL_GAME_WORLD_DATA = 0x00102000, 135 135 CL_GAME_RULES = 0x00104000, 136 CL_MISSION_GOAL = 0x00105000, 136 CL_NETWORK_GAME_RULES = 0x00108000, 137 CL_MISSION_GOAL = 0x00110000, 137 138 138 139 CL_CAMPAIGN = 0x00000101, … … 146 147 CL_MOVIE_LOADER = 0x00000109, 147 148 148 CL_MULTIPLAYER_TEAM_DEATHMATCH= 0x00 104001,149 CL_SINGLEPLAYER_SHOOTEMUP = 0x00 104002,149 CL_MULTIPLAYER_TEAM_DEATHMATCH= 0x00000121, 150 CL_SINGLEPLAYER_SHOOTEMUP = 0x00000122, 150 151 151 152 CL_KILL_TARGET = 0x00105001, -
branches/network/src/lib/network/message_manager.h
r7954 r7984 28 28 enum MessageId 29 29 { 30 TESTMESSAGEID = 1, 31 MSGID_YOU_ARE 30 TESTMESSAGEID = 1 32 31 }; 33 32 -
branches/network/src/lib/network/network_game_manager.cc
r7954 r7984 34 34 #include "game_world.h" 35 35 36 #include "game_rules.h" 37 #include "network_game_rules.h" 38 36 39 #include "network_game_manager.h" 37 40 … … 54 57 55 58 this->setSynchronized(true); 56 57 MessageManager::getInstance()->registerMessageHandler( MSGID_YOU_ARE, youAreHandler, NULL );58 59 } 59 60 … … 63 64 NetworkGameManager::~NetworkGameManager() 64 65 { 65 #if 066 for ( int i = 0; i<outBuffer.size(); i++)67 {68 if ( outBuffer[i].buffer )69 delete outBuffer[i].buffer;70 }71 #endif72 73 }74 75 #if 076 int NetworkGameManager::writeBytes(const byte* data, int length, int sender)77 {78 int i = 0;79 byte b;80 81 while ( i<length )82 {83 b = data[i++];84 85 /**************** Commands only processed by servers ****************/86 if ( isServer() )87 {88 if ( b == NET_REQUEST_CREATE )89 {90 if ( !handleRequestCreate( i, data, length, sender ) )91 return i;92 continue;93 }94 else if ( b == NET_REQUEST_REMOVE )95 {96 if ( !handleRequestRemove( i, data, length, sender ) )97 return i;98 continue;99 }100 else if ( b == NET_REQUEST_PNODE_PATH )101 {102 if ( !handleRequestPNodePath( i, data, length, sender ) )103 return i;104 continue;105 }106 }107 else108 {109 /**************** Commands only processed by clients ****************/110 if ( b == NET_CREATE_ENTITY )111 {112 PRINTF(0)("CREATE_ENTITY\n");113 if ( !handleCreateEntity( i, data, length, sender ) )114 return i;115 continue;116 }117 else if ( b == NET_REMOVE_ENTITY )118 {119 if ( !handleRemoveEntity( i, data, length, sender ) )120 return i;121 continue;122 }123 else if ( b == NET_CREATE_ENTITY_LIST )124 {125 if ( !handleCreateEntityList( i, data, length, sender ) )126 return i;127 continue;128 }129 else if ( b == NET_REMOVE_ENTITY_LIST )130 {131 if ( !handleRemoveEntityList( i, data, length, sender ) )132 return i;133 continue;134 }135 else if ( b == NET_YOU_ARE_ENTITY )136 {137 if ( !handleYouAreEntity( i, data, length, sender ) )138 return i;139 continue;140 }141 }142 143 /**************** Commands processed by servers and clients ****************/144 if ( b == NET_REQUEST_ENTITY_LIST )145 {146 sendEntityList( sender );147 continue;148 }149 150 151 PRINTF(1)("Network is asynchronous: couldn't decode the command sent by %i\n", sender);152 PRINTF(1)("Probably this is because the network protocol has different \n");153 PRINTF(1)("versions or there occured an error in the sending algorithm\n");154 PRINTF(1)("Data is not in the right format! i=%d\n", i);155 return i;156 }157 158 return i;159 }160 #endif161 162 #if 0163 int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever)164 {165 if ( !isServer() && !hasRequestedWorld )166 {167 assert( maxLength >= 1 );168 data[0] = NET_REQUEST_ENTITY_LIST;169 hasRequestedWorld = true;170 return 1;171 }172 173 for ( int i = 0; i<outBuffer.size(); i++ )174 {175 *reciever = i;176 if ( outBuffer[i].length>0 )177 {178 int nbytes = outBuffer[i].length;179 outBuffer[i].length = 0;180 181 if ( nbytes > maxLength )182 {183 PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength);184 return 0;185 }186 187 memcpy(data, outBuffer[i].buffer, nbytes);188 return nbytes;189 }190 }191 192 return 0;193 }194 #endif195 196 #if 0197 void NetworkGameManager::writeDebug() const198 {199 }200 201 void NetworkGameManager::readDebug() const202 {203 }204 #endif205 206 207 /*!208 * Checks whether this is connected to a server or a client209 * and afterwards creates the needed entity210 * @param classID: The ID of the class of which an entity should be created211 */212 int NetworkGameManager::createEntity( ClassID classID, int owner )213 {214 if ( this->isServer())215 {216 int res = this->executeCreateEntity( classID, SharedNetworkData::getInstance()->getNewUniqueID(), owner );217 218 if ( res < 0 )219 {220 PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");221 return -1;222 }223 224 return res;225 }226 else227 {228 #if 0229 this->requestCreateEntity( classID );230 #endif231 return -1;232 }233 66 } 234 67 235 68 236 /* !237 * Checks whether this is connected to a server or a client238 * and afterwards creates the needed entity239 * @ param classID: The ID of the class of which an entity should be created69 /** 70 * insert new player into game 71 * @param userId 72 * @return 240 73 */ 241 BaseObject* NetworkGameManager::createEntity(const TiXmlElement* element)74 bool NetworkGameManager::signalNewPlayer( int userId ) 242 75 { 243 if ( this->isServer() ) 244 { 245 if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 ) 246 { 247 PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n"); 248 return NULL; 249 } 250 251 BaseObject * b = Factory::fabricate( element ); 252 253 if ( !b ) 254 { 255 PRINTF(1)("Could not fabricate Object with className %s\n", element->Value() ); 256 return NULL; 257 } 258 259 260 if ( b->isA(CL_SYNCHRONIZEABLE) ) 261 { 262 Synchronizeable * s = dynamic_cast<Synchronizeable*>(b); 263 s->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); 264 s->setOwner( 0 ); 265 // all entities created via this function are added automaticaly to the synchronizeable list 266 s->setSynchronized(true); 267 return b; 268 } 269 else 270 { 271 PRINTF(1)("Class %s is not a synchronizeable!\n", b->getClassName() ); 272 delete b; 273 } 274 275 } 276 else 277 278 { 279 PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value()); 280 } 281 return NULL; 76 assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) ); 77 78 NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules())); 79 80 int team = rules.getTeamForNewUser(); 81 ClassID playableClassId = rules.getPlayableClassId( team ); 82 std::string playableModel = rules.getPlayableModelFileName( team, playableClassId ); 83 84 BaseObject * bo = Factory::fabricate( playableClassId ); 85 86 assert( bo->isA( CL_PLAYABLE ) ); 87 88 Playable & playable = *(dynamic_cast<Playable*>(bo)); 89 90 playable.loadModel( playableModel ); 91 92 PlayerStats * stats = rules.getNewPlayerStats( userId ); 93 94 stats->setTeamId( team ); 95 stats->setPlayableClassId( playableClassId ); 96 stats->setPlayableUniqueId( playable.getUniqueID() ); 97 stats->setModelFileName( playableModel ); 282 98 } 283 99 284 100 285 /* !286 * Checks whether this is connected to a server or a client287 * and afterwards removes the specified entity288 * @ param uniqueID: The ID of the entity object which should be removed101 /** 102 * remove player from game 103 * @param userID 104 * @return 289 105 */ 290 void NetworkGameManager::removeEntity(int uniqueID)106 bool NetworkGameManager::signalLeftPlayer(int userID) 291 107 { 292 if ( this->isServer() )293 {294 this->executeRemoveEntity( uniqueID );295 }296 else297 {298 #if 0299 this->requestRemoveEntity( uniqueID );300 #endif301 }302 }303 304 305 #if 0306 /*!307 * Creates the needed entity on the server if possible308 * @param classID: The ID of the class of which an entity should be created309 */310 void NetworkGameManager::requestCreateEntity(ClassID classID)311 {312 for ( int i = 0; i<outBuffer.size(); i++)313 {314 if ( !this->networkStream->isUserIdActive( i ) )315 continue;316 317 if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_CREATE ) )318 return;319 if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )320 return;321 }322 }323 #endif324 325 #if 0326 /*!327 * Removes the specified entity on the server328 * @param uniqueID: The ID of the entity object which should be removed329 */330 void NetworkGameManager::requestRemoveEntity(int uniqueID)331 {332 for ( int i = 0; i<outBuffer.size(); i++)333 {334 if ( !this->networkStream->isUserIdActive( i ) )335 continue;336 337 if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_REMOVE ) )338 return;339 if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )340 return;341 }342 }343 #endif344 345 /*!346 * Creates the needed entity if possible347 * This function is called if this is a server348 * @param classID: The ID of the class of which an entity should be created349 */350 int NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner)351 {352 #if 0353 for ( int i = 0; i<outBuffer.size(); i++)354 {355 if ( !this->networkStream->isUserIdActive( i ) )356 continue;357 358 if ( !writeToClientBuffer( outBuffer[i], (byte)NET_CREATE_ENTITY ) )359 return -1;360 if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )361 return -1;362 if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )363 return -1;364 if ( !writeToClientBuffer( outBuffer[i], owner ) )365 return -1;366 }367 #endif368 PRINTF(0)("ExecuteCreateEntity: server side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);369 doCreateEntity( classID, uniqueID, owner );370 371 return uniqueID;372 }373 374 /*!375 * Removes the specified entity376 * This function is called if this is a server377 * @param uniqueID: The ID of the entity object which should be removed378 */379 void NetworkGameManager::executeRemoveEntity(int uniqueID)380 {381 #if 0382 for ( int i = 0; i<outBuffer.size(); i++)383 {384 if ( !this->networkStream->isUserIdActive( i ) )385 continue;386 387 if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REMOVE_ENTITY ) )388 return;389 if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )390 return;391 }392 #endif393 394 doRemoveEntity(uniqueID);395 }396 397 /*!398 * Checks whether it is possible to create an entity of a given class399 * @return: true if the entity can be created, false otherwise400 */401 bool NetworkGameManager::canCreateEntity(ClassID classID)402 {403 return true;404 }405 406 #if 0407 /*!408 * Sends the Entities to the new connected client409 * @param userID: The ID of the user410 */411 void NetworkGameManager::sendEntityList( int userID )412 {413 if ( !isServer() )414 return;415 416 if ( userID >= outBuffer.size() )417 resizeBufferVector( userID );418 419 SynchronizeableList::const_iterator it, e;420 421 it = this->networkStream->getSyncBegin();422 e = this->networkStream->getSyncEnd();423 424 // send the packet header425 if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_CREATE_ENTITY_LIST ) )426 return;427 428 // send the number of entities: -2 because you must not send network_game_manager and handshake429 if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) )430 return;431 432 //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );433 434 // first send the NullParent435 if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getLeafClassID()) )436 return;437 if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getUniqueID()) )438 return;439 if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getOwner()) )440 return;441 442 // now send the rest of the entities443 while ( it != e )444 {445 if( (*it)->beSynchronized() && (*it) != PNode::getNullParent())446 {447 PRINTF(0)("SENDING ENTITY %s classid: %x, uniqueid %d\n", (*it)->getClassName(), (*it)->getLeafClassID(), (*it)->getUniqueID() );448 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) )449 return;450 451 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) )452 return;453 454 if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) )455 return;456 }457 it++;458 }459 460 signalNewPlayer( userID );461 }462 #endif463 464 465 466 bool NetworkGameManager::signalNewPlayer(int userId)467 {468 /* create new playable for Player*/469 PRINTF(0)("Request for creation: %i\n", userId);470 int uniqueId = this->createEntity(CL_SPACE_SHIP, userId);471 PRINTF(0)("Request for creation: userid: %i, uniqueid: %i\n", userId, uniqueId);472 this->sendYouAre(uniqueId, userId);473 108 } 474 109 475 110 476 111 477 bool NetworkGameManager::signalLeftPlayer(int userID)478 {479 const std::list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);480 481 if ( !playableList )482 return false;483 484 std::list<BaseObject*>::const_iterator it = playableList->begin();485 486 for(; it != playableList->end(); it++)487 {488 if( dynamic_cast<Synchronizeable*>(*it)->getOwner() == userID )489 {490 PRINTF(0)("remove playable from %i\n", userID);491 this->removeEntity(dynamic_cast<Synchronizeable*>(*it)->getUniqueID());492 return true;493 }494 }495 return false;496 }497 498 #if 0499 /**500 * Creates a buffer for user n501 * @param n The ID of the user502 */503 void NetworkGameManager::resizeBufferVector( int n )504 {505 for ( int i = outBuffer.size(); i<=n; i++)506 {507 clientBuffer outBuf;508 509 outBuf.length = 0;510 511 outBuf.maxLength = 5*1024;512 513 outBuf.buffer = new byte[5*1014];514 515 outBuffer.push_back(outBuf);516 }517 }518 #endif519 520 /**521 * Creates the entity on this host522 * @param classID: ClassID of the entity to create523 * @param uniqueID: Unique ID to assign to the synchronizeable524 * @param owner: owner of this synchronizealbe525 */526 BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )527 {528 PRINTF(0)("Creating Entity via Factory: classid: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);529 530 BaseObject * b;531 /* These are some small exeptions in creation: Not all objects can/should be created via Factory */532 /* Exception 1: NullParent */533 if( classID == CL_NULL_PARENT)534 {535 b = (BaseObject*)PNode::getNullParent();536 }537 else538 b = Factory::fabricate( classID );539 540 if ( !b )541 {542 PRINTF(1)("Could not fabricate Object with classID %x\n", classID);543 return NULL;544 }545 546 if ( b->isA(CL_SYNCHRONIZEABLE) )547 {548 Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);549 s->setUniqueID( uniqueID );550 s->setOwner( owner );551 s->setSynchronized(true);552 //this->networkStream->connectSynchronizeable( *s );553 554 PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID());555 556 //TODO HACK: hack to prevent collision557 if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) )558 {559 if ( SharedNetworkData::getInstance()->getHostID()!=0 )560 {561 static Vector pos = Vector(1000.0, 1000.0, 1000.0);562 PNode *p = dynamic_cast<PNode*>(b);563 p->setAbsCoor(pos);564 p->updateNode(0);565 pos += Vector(1000.0, 1000.0, 1000.0);566 }567 }568 ///TODO HACK this is only for network multiplayer games.569 if( b->isA(CL_PLAYABLE))570 {571 Playable* ss = dynamic_cast<Playable*>(b);572 if( owner%2 == 0)573 {574 575 ss->loadModel("models/ships/reap_0.obj");576 ss->toList(OM_GROUP_00);577 ss->setAbsCoor(213.37, 57.71, -47.98);578 ss->setAbsDir(Quaternion(0.16, 0.98, -0.10));579 }580 else581 {582 ss->loadModel( "models/ships/fighter.obj" );583 ss->toList(OM_GROUP_01);584 ss->setAbsCoor(-314.450, 40.701, 83.554);585 }586 }587 588 return b;589 }590 else591 {592 PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID);593 delete b;594 }595 return NULL;596 }597 598 /**599 * Removes a entity on this host600 * @param uniqueID: unique ID assigned with the entity to remove601 */602 void NetworkGameManager::doRemoveEntity( int uniqueID )603 {604 SynchronizeableList::const_iterator it,e;605 it = this->networkStream->getSyncBegin();606 e = this->networkStream->getSyncEnd();607 608 while ( it != e )609 {610 if ( (*it)->getUniqueID() == uniqueID )611 {612 assert((*it)->isA(CL_WORLD_ENTITY));613 dynamic_cast<WorldEntity*>(*it)->leaveWorld();614 dynamic_cast<WorldEntity*>(*it)->toList(OM_DEAD);615 break;616 }617 it++;618 }619 }620 621 #if 0622 /**623 * Copies length bytes to the clientBuffer with error checking624 * @param clientBuffer: the clientBuffer to write to625 * @param data: buffer to the data626 * @param length: length of data627 * @return false on error true else628 */629 bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length )630 {631 if ( length > cb.maxLength-cb.length )632 {633 PRINTF(1)("No space left in clientBuffer\n");634 return false;635 }636 637 memcpy( cb.buffer+cb.length, data, length );638 return true;639 }640 #endif641 642 #if 0643 /**644 * Reads data from clientBuffer with error checking645 * @param clientBuffer: the clientBuffer to read from646 * @param data: pointer to the buffer647 * @param length:648 * @return649 */650 bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length )651 {652 if ( cb.length < length )653 {654 PRINTF(0)("There is not enough data in clientBuffer\n");655 return 0;656 }657 658 memcpy( data, cb.buffer+cb.length-length, length );659 return true;660 }661 #endif662 663 /**664 * Tells this client that he has to control this entity665 * @param uniqueID: the entity's uniqeID666 */667 void NetworkGameManager::doYouAre( int uniqueID )668 {669 670 SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin();671 672 Playable *p = NULL;673 Synchronizeable *s = NULL;674 675 for ( ; it !=networkStream->getSyncEnd(); it++ )676 {677 if ( (*it)->getUniqueID()==uniqueID )678 {679 if ( (*it)->isA( CL_SYNCHRONIZEABLE ) )680 {681 s = dynamic_cast<Synchronizeable*>(*it);682 }683 if ( (*it)->isA( CL_PLAYABLE ) )684 {685 p = dynamic_cast<Playable*>(*it);686 break;687 } else688 {689 PRINTF(1)("UniqueID %d is not a Playable\n", uniqueID);690 }691 }692 }693 694 Player* player = State::getPlayer();695 assert(p != NULL);696 assert(s != NULL);697 assert(player != NULL);698 699 PRINTF(0)("uniqueID = %d\n", s->getUniqueID());700 701 player->setPlayable(p);702 112 703 113 704 }705 706 /**707 * Tells a remote client that he has to control this entity708 * @param uniqueID: the entity's uniqeID709 * @param userID: the users ID710 */711 void NetworkGameManager::sendYouAre( int uniqueID, int userID )712 {713 if ( !isServer() )714 return;715 716 byte buf[INTSIZE];717 718 Converter::intToByteArray( uniqueID, buf, INTSIZE );719 720 MessageManager::getInstance()->sendMessage( MSGID_YOU_ARE, buf, INTSIZE, RT_USER, userID, MP_HIGHBANDWIDTH);721 }722 723 bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender )724 {725 if ( INTSIZE > length-i )726 {727 PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");728 return false;729 }730 int classID;731 i += Converter::byteArrayToInt( &data[i], &classID );732 733 createEntity( (ClassID)classID );734 735 return true;736 }737 738 bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender )739 {740 if ( INTSIZE > length-i )741 {742 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");743 return false;744 }745 int uniqueID;746 i += Converter::byteArrayToInt( &data[i], &uniqueID );747 748 removeEntity( uniqueID );749 750 return true;751 }752 753 bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender )754 {755 if ( INTSIZE > length-i )756 {757 PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");758 return false;759 }760 int classID;761 i += Converter::byteArrayToInt( &data[i], &classID );762 763 if ( INTSIZE > length-i )764 {765 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");766 return false;767 }768 int uniqueID;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 int owner;777 i += Converter::byteArrayToInt( &data[i], &owner );778 779 PRINTF(0)("handleCreateEntity: client side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);780 doCreateEntity( (ClassID)classID, uniqueID, owner );781 782 return true;783 }784 785 bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender )786 {787 if ( INTSIZE > length-i )788 {789 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");790 return false;791 }792 int uniqueID;793 i += Converter::byteArrayToInt( &data[i], &uniqueID );794 795 doRemoveEntity( uniqueID );796 797 return true;798 }799 800 bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender )801 {802 if ( INTSIZE > length-i )803 {804 PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");805 return false;806 }807 808 PRINTF(0)("HandleCreateEntityList: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);809 810 int n;811 i += Converter::byteArrayToInt( &data[i], &n );812 114 813 115 814 PRINTF(0)("HandleCreateEntityList: n = %d\n", n);815 816 int classID, uniqueID, owner;817 818 for ( int j = 0; j<n; j++ )819 {820 821 if ( INTSIZE > length-i )822 {823 PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");824 return false;825 }826 i += Converter::byteArrayToInt( &data[i], &classID );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 i += Converter::byteArrayToInt( &data[i], &uniqueID );834 835 if ( INTSIZE > length-i )836 {837 PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");838 return false;839 }840 i += Converter::byteArrayToInt( &data[i], &owner );841 842 if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE )843 {844 BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner );845 }846 847 }848 849 return true;850 }851 852 bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender )853 {854 if ( INTSIZE > length-i )855 {856 PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");857 return false;858 }859 int n;860 i += Converter::byteArrayToInt( &data[i], &n );861 862 int uniqueID;863 864 for ( int j = 0; j<n; j++ )865 {866 867 if ( INTSIZE > length-i )868 {869 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");870 return false;871 }872 i += Converter::byteArrayToInt( &data[i], &uniqueID );873 874 doRemoveEntity( uniqueID );875 }876 877 return true;878 }879 880 bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender )881 {882 if ( INTSIZE > length-i )883 {884 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");885 return false;886 }887 888 int uniqueID;889 i += Converter::byteArrayToInt( &data[i], &uniqueID );890 891 doYouAre( uniqueID );892 893 return true;894 }895 896 /**897 * handles the network signal NET_REQUEST_PNODE_PATH898 * @param i byte offset in the buffer899 * @param data data array900 * @param length length of the data arary901 * @param sender the sender id902 * @return true if process terminated sucessfully903 */904 bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender)905 {906 if( INTSIZE > length-i )907 {908 PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");909 return false;910 }911 PRINTF(0)("HandleRequestPNodePath: data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);912 913 int uniqueID1, uniqueID2;914 if( INTSIZE > length-i )915 {916 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");917 return false;918 }919 i += Converter::byteArrayToInt( &data[i], &uniqueID1 );920 921 if( INTSIZE > length-i )922 {923 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");924 return false;925 }926 i += Converter::byteArrayToInt( &data[i], &uniqueID2 );927 928 929 PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2);930 931 return true;932 }933 934 bool NetworkGameManager::youAreHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )935 {936 assert( dataLength == INTSIZE );937 int uniqueId;938 939 Converter::byteArrayToInt( data, &uniqueId );940 941 SynchronizeableList::const_iterator it = NetworkGameManager::getInstance()->networkStream->getSyncBegin();942 943 Playable *p = NULL;944 Synchronizeable *s = NULL;945 946 for ( ; it !=NetworkGameManager::getInstance()->networkStream->getSyncEnd(); it++ )947 {948 if ( (*it)->getUniqueID()==uniqueId )949 {950 break;951 }952 }953 954 if ( it == NetworkGameManager::getInstance()->networkStream->getSyncEnd() )955 return false;956 957 NetworkGameManager::getInstance()->doYouAre( uniqueId );958 959 return true;960 }961 962 #if 0963 bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )964 {965 if ( cb.maxLength-cb.length < 1 )966 {967 PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n");968 return false;969 }970 971 cb.buffer[cb.length++] = b;972 973 return true;974 }975 976 977 bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i )978 {979 int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length );980 cb.length += n;981 982 if ( n <= 0 )983 {984 PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n");985 return false;986 }987 988 return true;989 }990 #endif991 992 -
branches/network/src/lib/network/network_game_manager.h
r7954 r7984 51 51 { if (!NetworkGameManager::singletonRef) NetworkGameManager::singletonRef = new NetworkGameManager(); return NetworkGameManager::singletonRef; } 52 52 53 #if 054 virtual int writeBytes(const byte* data, int length, int sender);55 virtual int readBytes(byte* data, int maxLength, int * reciever);56 virtual void writeDebug() const;57 virtual void readDebug() const;58 #endif59 60 int createEntity( ClassID classID, int owner = 0 );61 BaseObject* createEntity(const TiXmlElement* element);62 void removeEntity( int uniqueID );63 void sendYouAre( int uniqueID, int userID );64 65 #if 066 void sendEntityList(int userID);67 #endif68 53 69 54 bool signalNewPlayer(int userId); … … 76 61 static bool youAreHandler(MessageId messageId, byte * data, int dataLength, void * someData, int userId ); 77 62 78 79 /* some network signal handlers */80 bool handleRequestCreate( int& i, const byte* data, int length, int sender );81 bool handleRequestRemove( int& i, const byte* data, int length, int sender );82 bool handleCreateEntity( int& i, const byte* data, int length, int sender );83 bool handleRemoveEntity( int& i, const byte* data, int length, int sender );84 bool handleCreateEntityList( int& i, const byte* data, int length, int sender );85 bool handleRemoveEntityList( int& i, const byte* data, int length, int sender );86 bool handleYouAreEntity( int& i, const byte* data, int length, int sender );87 bool handleRequestPNodePath(int& i, const byte* data, int length, int sender);88 bool handleSendPNodePath(int& i, const byte* data, int length, int sender);89 90 91 /* some network handlers helper functions */92 // void requestCreateEntity(ClassID classID);93 int executeCreateEntity(ClassID classID, int uniqueID = 0, int owner = 0);94 BaseObject* doCreateEntity(ClassID classID, int uniqueID, int owner);95 96 // void requestRemoveEntity(int uniqueID);97 void executeRemoveEntity(int uniqueID);98 void doRemoveEntity(int uniqueID);99 100 void doYouAre( int uniqueID );101 102 // void requestPNodePath(const PNode* node1, const PNode* node2);103 void executeRequestPNodePath(const PNode* node2, const PNode* node2);104 void doRequestPNodePath(const PNode* node1, const PNode* node2);105 106 bool canCreateEntity(ClassID classID);107 #if 0108 void resizeBufferVector(int n);109 110 111 bool writeToClientBuffer( clientBuffer &cb, byte*data, int length );112 bool writeToClientBuffer( clientBuffer &cb, byte b );113 bool writeToClientBuffer( clientBuffer &cb, int i );114 bool readFromClientBuffer( clientBuffer &cb, byte*data, int length );115 #endif116 117 private:118 #if 0119 std::vector<clientBuffer> outBuffer;120 //clientBuffer allOutBuffer;121 #endif122 63 static NetworkGameManager* singletonRef; 123 64 }; -
branches/network/src/lib/network/network_stream.cc
r7974 r7984 279 279 PRINTF(0)("Client is gone: %d (%s)\n", it->second.userId, reason.c_str()); 280 280 281 assert(false);281 //assert(false); 282 282 283 283 it->second.socket->disconnectServer(); -
branches/network/src/lib/network/player_stats.cc
r7974 r7984 18 18 #include "class_list.h" 19 19 #include "src/lib/util/loading/factory.h" 20 21 #include "player.h" 22 #include "state.h" 20 23 21 24 … … 45 48 46 49 this->userId = 0; 47 this-> groupId = 0;50 this->teamId = 0; 48 51 this->score = 0; 49 52 this->playableClassId = 0; … … 51 54 52 55 userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) ); 53 groupId_handle = registerVarId( new SynchronizeableInt( & groupId, &groupId, "groupId" ) );56 groupId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) ); 54 57 score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score" ) ); 55 58 playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId") ); … … 77 80 { 78 81 this->setPlayableUniqueId( this->playableUniqueId ); 82 83 if ( userId == getHostID() ) 84 State::getPlayer()->setPlayable( getPlayable() ); 79 85 } 80 86 } -
branches/network/src/lib/network/player_stats.h
r7979 r7984 28 28 inline int getUserId(){ return userId; } 29 29 30 inline int get GroupId(){ return groupId; }31 inline void set GroupId( int groupId ){ this->groupId = groupId; }30 inline int getTeamId(){ return teamId; } 31 inline void setTeamId( int teamId ){ this->teamId = teamId; } 32 32 33 33 inline int getScore(){ return score; } … … 47 47 private: 48 48 int userId; //!< userId 49 int groupId; //!< groupId49 int teamId; //!< teamId 50 50 51 51 int score; //!< users score points -
branches/network/src/story_entities/multi_player_world_data.cc
r7954 r7984 146 146 { 147 147 /* pass the entity to the NetworkGameManager to be created */ 148 BaseObject* created = NetworkGameManager::getInstance()->createEntity(element);148 BaseObject* created = Factory::fabricate(element); 149 149 if( created != NULL ) 150 150 PRINTF(1)("Created a %s: %s (0x%8x) from %s\n", created->getClassName(), created->getName(), created->getLeafClassID(), element->Value()); -
branches/network/src/util/Makefile.am
r7482 r7984 10 10 \ 11 11 game_rules.cc \ 12 network_game_rules.cc \ 12 13 multiplayer_team_deathmatch.cc \ 13 14 singleplayer_shootemup.cc \ … … 34 35 \ 35 36 game_rules.h \ 37 network_game_rules.h \ 36 38 multiplayer_team_deathmatch.h \ 37 39 singleplayer_shootemup.h \ -
branches/network/src/util/multiplayer_team_deathmatch.cc
r7954 r7984 45 45 */ 46 46 MultiplayerTeamDeathmatch::MultiplayerTeamDeathmatch(const TiXmlElement* root) 47 : GameRules(root)47 : NetworkGameRules(root) 48 48 { 49 49 this->setClassID(CL_MULTIPLAYER_TEAM_DEATHMATCH, "MultiplayerTeamDeathmatch"); -
branches/network/src/util/multiplayer_team_deathmatch.h
r7810 r7984 10 10 #define _MULTIPLAYER_TEAM_DEATHMATCH_H 11 11 12 #include " game_rules.h"12 #include "network_game_rules.h" 13 13 14 14 … … 19 19 20 20 21 class MultiplayerTeamDeathmatch : public GameRules21 class MultiplayerTeamDeathmatch : public NetworkGameRules 22 22 { 23 23
Note: See TracChangeset
for help on using the changeset viewer.