Changeset 7631 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- May 17, 2006, 8:28:23 AM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 2 added
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/Makefile.am
r7540 r7631 20 20 network_game_manager.cc \ 21 21 converter.cc \ 22 message_manager.cc \ 22 23 \ 23 24 synchronizeable_var/synchronizeable_var.cc \ … … 50 51 converter.h \ 51 52 netdefs.h \ 53 message_manager.h \ 52 54 \ 53 55 synchronizeable_var/synchronizeable_var.h \ -
branches/network/src/lib/network/converter.cc
r7230 r7631 493 493 } 494 494 495 s [0] = '\0';495 s = ""; 496 496 s.append( (char*)a+n, length ); 497 497 -
branches/network/src/lib/network/network_stream.cc
r7614 r7631 62 62 this->init(); 63 63 this->peers[0].socket = new UdpSocket( host, port ); 64 this->peers[0].userId = 0; 65 this->peers[0].isServer = true; 64 66 this->networkProtocol = new NetworkProtocol(); 65 67 this->connectionMonitor = new ConnectionMonitor(); … … 357 359 peers[clientId].handshake->setUniqueID(clientId); 358 360 peers[clientId].userId = clientId; 361 peers[clientId].isServer = false; 359 362 } else 360 363 { … … 369 372 peers[clientId].handshake->setUniqueID(clientId); 370 373 peers[clientId].userId = clientId; 374 peers[clientId].isServer = false; 371 375 372 376 PRINTF(0)("num sync: %d\n", synchronizeables.size()); … … 400 404 delete it->second.handshake; 401 405 it->second.handshake = NULL; 402 406 407 for ( SynchronizeableList::iterator it2 = synchronizeables.begin(); it2 != synchronizeables.end(); it2++ ) 408 { 409 (*it2)->cleanUpUser( it->second.userId ); 410 } 403 411 404 412 NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId); … … 632 640 int leafClassId; 633 641 if ( INTSIZE > length - offset ) 634 break; 642 { 643 offset += syncDataLength; 644 continue; 645 } 635 646 636 647 Converter::byteArrayToInt( buf + offset, &leafClassId ); … … 639 650 /* These are some small exeptions in creation: Not all objects can/should be created via Factory */ 640 651 /* Exception 1: NullParent */ 641 if( leafClassId == CL_NULL_PARENT) 642 { 643 PRINTF(1)("Can not create Class with ID %x!", (int)leafClassId); 644 break; 652 if( leafClassId == CL_NULL_PARENT || leafClassId == CL_SYNCHRONIZEABLE ) 653 { 654 PRINTF(1)("Can not create Class with ID %x!\n", (int)leafClassId); 655 offset += syncDataLength; 656 continue; 645 657 } 646 658 else … … 650 662 { 651 663 PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId); 652 break; 664 offset += syncDataLength; 665 continue; 653 666 } 654 667 … … 663 676 else 664 677 { 665 PRINTF(1)("Class with ID %x is not a synchronizeable! ", (int)leafClassId);678 PRINTF(1)("Class with ID %x is not a synchronizeable!\n", (int)leafClassId); 666 679 delete b; 667 break; 680 offset += syncDataLength; 681 continue; 668 682 } 669 683 } -
branches/network/src/lib/network/network_stream.h
r7602 r7631 69 69 70 70 inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } 71 inline bool isUserServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isServer; } 71 72 72 73 void debug(); -
branches/network/src/lib/network/synchronizeable.cc
r7617 r7631 92 92 93 93 94 /** 95 * get the diff to last acked state of userId 96 * 97 * each synchrinizeable defines its own stack of states received and sent over the network. The stack contains 98 * a per user entry for the last sent/received state This function returns a delta compressed state of the 99 * synchronizeable. This state will be transmitted over the network to the other participants 100 * 101 * @param userId user to create diff for 102 * @param data buffer to copy diff in 103 * @param maxLength max bytes to copy into data 104 * @param stateId id of current state 105 * @param fromStateId the reference state for the delta state 106 * @param priorityTH tells getStateDiff to not send element with priority \< priorityTH 107 * @return n bytes copied into data 108 */ 94 109 95 int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ) 110 96 { … … 119 105 neededSize += (*it)->getSize(); 120 106 121 assert( neededSize <= maxLength ); 107 if ( !( neededSize <= maxLength ) ) 108 { 109 PRINTF(0)( "%d > %d\n", neededSize, maxLength ); 110 assert(false); 111 } 122 112 123 113 //remove older states from history than fromStateId … … 170 160 int i = 0; 171 161 int n; 162 163 bool hasPermission; 172 164 173 165 // now do the actual synchronization: kick all variables to write into a common buffer 174 166 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 175 167 { 176 PRINTF(0)("SIZE: %d\n", syncVarList.size()); 177 if ( (*it)->getPriority() >= priorityTH || sizeIter == stateFrom->sizeList.end() ) 168 hasPermission = ( 169 this->isServer() && (*it)->checkPermission( PERMISSION_SERVER ) || 170 this->owner == this->hostID && (*it)->checkPermission( PERMISSION_OWNER ) || 171 (*it)->checkPermission( PERMISSION_ALL ) 172 ); 173 174 if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeIter == stateFrom->sizeList.end() ) 178 175 { 179 176 (*it)->debug(); … … 182 179 i += n; 183 180 } 184 else if ( ! ( 185 this->isServer() && (*it)->checkPremission( PERMISSION_SERVER ) || 186 this->owner == this->hostID && (*it)->checkPremission( PERMISSION_OWNER ) || 187 (*it)->checkPremission( PERMISSION_ALL ) 188 ) ) 189 { 190 for ( int j = 0; j < (*it)->getSize(); it++ ) 181 else if ( ! hasPermission ) 182 { 183 for ( int j = 0; j < (*it)->getSize(); j++ ) 191 184 { 192 185 assert(false); … … 287 280 stateFrom = (*it); 288 281 289 std::list<int> changes;290 291 282 //apply diff 292 283 for ( int i = 0; i<length; i++ ) … … 297 288 stateTo->data[i] = data[i]; 298 289 299 if ( data[i] != 0 )300 changes.push_back(i);301 290 } 302 291 … … 305 294 306 295 int i = 0; 307 296 std::list<int> changes; 297 308 298 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 309 299 { 310 i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i ); 300 if ( 301 (*it)->checkPermission( PERMISSION_SERVER ) && networkStream->isUserServer( userId ) || 302 (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == userId || 303 (*it)->checkPermission( PERMISSION_ALL ) 304 ) 305 { 306 i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i ); 307 if ( (*it)->getHasChanged() ) 308 changes.push_back( (*it)->getVarId() ); 309 } 310 else 311 { 312 PRINTF(0)("DONT SET VAR BECAUSE OF PERMISSION: %s %d %d %d %d %d %d\n", (*it)->getName().c_str(), (*it)->checkPermission( PERMISSION_SERVER ), (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_ALL ), networkStream->isUserServer( userId ), this->owner, userId ); 313 i += (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i ); 314 } 311 315 } 312 316 … … 331 335 void Synchronizeable::registerVar( SynchronizeableVar * var ) 332 336 { 337 PRINTF(0)("ADDING var: %s (%s)\n", var->getName().c_str(), this->getName() ); 333 338 syncVarList.push_back( var ); 334 339 } … … 342 347 int Synchronizeable::registerVarId( SynchronizeableVar * var ) 343 348 { 349 PRINTF(0)("ADDING var: %s (%s)\n", var->getName().c_str(), this->getName() ); 344 350 syncVarList.push_back( var ); 351 var->setWatched( true ); 352 var->setVarId( syncVarList.size()-1 ); 345 353 return syncVarList.size()-1; 346 354 } 347 355 348 349 356 /** 357 * removed user's states from memory 358 * @param userId user to clean 359 */ 360 void Synchronizeable::cleanUpUser( int userId ) 361 { 362 for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ ) 363 { 364 for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it++ ) 365 { 366 if ( (*it2)->data ) 367 delete (*it2)->data; 368 (*it2)->data = NULL; 369 370 delete *it2; 371 } 372 } 373 374 sentStates.clear(); 375 376 for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ ) 377 { 378 for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it++ ) 379 { 380 if ( (*it2)->data ) 381 delete (*it2)->data; 382 (*it2)->data = NULL; 383 384 delete *it2; 385 } 386 } 387 388 recvStates.clear(); 389 } 390 391 392 -
branches/network/src/lib/network/synchronizeable.h
r7591 r7631 40 40 typedef std::vector<StateHistory> UserStateHistory; 41 41 42 enum {43 PERMISSION_SERVER = 1,44 PERMISSION_OWNER = 2,45 PERMISSION_ALL = 446 };47 48 42 typedef std::vector<SynchronizeableVar*> SyncVarList; 49 43 … … 62 56 virtual void varChangeHandler( std::list<int> & id ); 63 57 64 int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ); 65 int setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ); 58 virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ); 59 virtual int setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ); 60 virtual void cleanUpUser( int userId ); 66 61 67 62 void registerVar( SynchronizeableVar * var ); -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_bool.cc
r7578 r7631 60 60 assert( maxLength >= 1 ); 61 61 62 bool oldVal = *vPtrOut; 63 62 64 *vPtrOut = buf[0] != 0; 65 66 setHasChanged( oldVal != *vPtrOut ); 63 67 64 68 return 1; -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_bool.h
r7578 r7631 12 12 13 13 public: 14 SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission = 0, int priority = 0 );14 SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission = PERMISSION_SERVER, int priority = 0 ); 15 15 virtual ~SynchronizeableBool(); 16 16 -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_float.cc
r7578 r7631 62 62 assert( maxLength >= FLOATSIZE ); 63 63 64 float oldVal = *vPtrOut; 65 64 66 int res = Converter::byteArrayToFloat( buf, vPtrOut ); 67 68 setHasChanged( oldVal != *vPtrOut ); 65 69 66 70 assert( res == FLOATSIZE ); -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_float.h
r7578 r7631 12 12 13 13 public: 14 SynchronizeableFloat( float * ptrIn, float * ptrOut, std::string name, int permission = 0, int priority = 0 );14 SynchronizeableFloat( float * ptrIn, float * ptrOut, std::string name, int permission = PERMISSION_SERVER, int priority = 0 ); 15 15 virtual ~SynchronizeableFloat(); 16 16 -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_int.cc
r7578 r7631 62 62 assert( maxLength >= INTSIZE ); 63 63 64 int oldVal = *vPtrOut; 65 64 66 int res = Converter::byteArrayToInt( buf, vPtrOut ); 67 68 setHasChanged( oldVal != *vPtrOut ); 65 69 66 70 assert( res == INTSIZE ); -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_int.h
r7578 r7631 12 12 13 13 public: 14 SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission = 0, int priority = 0 );14 SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission = PERMISSION_SERVER, int priority = 0 ); 15 15 virtual ~SynchronizeableInt(); 16 16 -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_quaternion.cc
r7614 r7631 98 98 assert( res > 0 ); 99 99 n += res; 100 101 Quaternion oldVal = *vPtrOut; 100 102 101 103 *vPtrOut = Quaternion( Vector(x, y, z), w ); 104 105 setHasChanged( ! ( oldVal == *vPtrOut ) ); 102 106 103 107 assert( n == 4*FLOATSIZE ); -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_quaternion.h
r7578 r7631 14 14 15 15 public: 16 SynchronizeableQuaternion( Quaternion * ptrIn, Quaternion * ptrOut, std::string name, int permission = 0, int priority = 0 );16 SynchronizeableQuaternion( Quaternion * ptrIn, Quaternion * ptrOut, std::string name, int permission = PERMISSION_SERVER, int priority = 0 ); 17 17 virtual ~SynchronizeableQuaternion(); 18 18 -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_string.cc
r7578 r7631 60 60 int SynchronizeableString::readFromBuf( byte * buf, int maxLength ) 61 61 { 62 std::string oldVal = *vPtrOut; 63 62 64 int res = Converter::byteArrayToString( buf, *vPtrOut, maxLength ); 65 66 setHasChanged( oldVal != *vPtrOut ); 63 67 64 68 assert( res > 0 ); … … 76 80 77 81 82 /** 83 * reads actual size from buffer. this is used when size is not constant 84 * @param buf pointer to data 85 * @param maxLength maximal size of data 86 * @return same as readFromBuf would return 87 */ 88 int SynchronizeableString::getSizeFromBuf( byte * buf, int maxLength ) 89 { 90 std::string t; 91 92 int res = Converter::byteArrayToString( buf, t, maxLength ); 93 94 95 assert( res > 0 ); 96 97 return res; 98 } 99 100 -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_string.h
r7578 r7631 15 15 16 16 public: 17 SynchronizeableString( std::string * ptrIn, std::string * ptrOut, std::string name, int permission = 0, int priority = 0 );17 SynchronizeableString( std::string * ptrIn, std::string * ptrOut, std::string name, int permission = PERMISSION_SERVER, int priority = 0 ); 18 18 virtual ~SynchronizeableString(); 19 19 … … 30 30 virtual int getSize(){ return vPtrIn->length()+INTSIZE; } 31 31 32 virtual int getSizeFromBuf( byte * buf, int maxLength ); 33 32 34 virtual void debug(); 33 35 -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_uint.h
r7578 r7631 12 12 13 13 public: 14 SynchronizeableUInt( unsigned int * ptrIn, unsigned int * ptrOut, std::string name, int permission = 0, int priority = 0 );14 SynchronizeableUInt( unsigned int * ptrIn, unsigned int * ptrOut, std::string name, int permission = PERMISSION_SERVER, int priority = 0 ); 15 15 virtual ~SynchronizeableUInt(); 16 16 -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.cc
r7559 r7631 35 35 this->realPriority = priority; 36 36 this->name = name; 37 this->bWatched = false; 37 38 } 38 39 -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.h
r7578 r7631 11 11 #include <assert.h> 12 12 13 enum { 14 PERMISSION_SERVER = 1, 15 PERMISSION_OWNER = 2, 16 PERMISSION_ALL = 4 17 }; 18 13 19 class SynchronizeableVar { 14 20 15 21 public: 16 SynchronizeableVar( void * ptrIn, void * ptrOut, std::string name, int length, int permission = 0, int priority = 0 );22 SynchronizeableVar( void * ptrIn, void * ptrOut, std::string name, int length, int permission = PERMISSION_SERVER, int priority = 0 ); 17 23 virtual ~SynchronizeableVar(); 18 24 … … 22 28 */ 23 29 inline bool beWatched(){ return this->bWatched; } 30 31 /** 32 * set flag if synchronizeable wants to be informed on changes 33 */ 34 inline void setWatched( bool watched ){ this->bWatched = watched; } 24 35 25 36 /** … … 55 66 * @return true if you can write 56 67 */ 57 inline bool checkP remission( int permission ){ return (permission & this->permission) != 0; }68 inline bool checkPermission( int permission ){ return (permission & this->permission) != 0; } 58 69 59 70 /** … … 87 98 88 99 /** 100 * reads actual size from buffer. this is used when size is not constant 101 * @param buf pointer to data 102 * @param maxLength maximal size of data 103 * @return same as readFromBuf would return 104 */ 105 virtual inline int getSizeFromBuf( byte * buf, int maxLength ){ return this->getSize(); } 106 107 /** 108 * set variable id 109 * @param id 110 */ 111 inline void setVarId( int id ){ this->varId = id; } 112 113 /** 114 * get variable id 115 * @return variable id 116 */ 117 inline int getVarId(){ return varId; } 118 119 /** 120 * set hasChanged 121 * @param changed 122 */ 123 inline void setHasChanged( bool changed ){ this->changed = changed; } 124 125 /** 126 * get hasChanged 127 * @return variable id 128 */ 129 inline bool getHasChanged(){ return changed; } 130 131 /** 89 132 * print out variable value 90 133 */ … … 94 137 private: 95 138 bool bWatched; //!< true if synchronizeable wants to be informed on changes 139 140 int permission; //!< who is allowed to change this var 141 int priority; //!< priority assigned to var 142 int realPriority; //!< priority assigned to var, increased every time not sent 143 int varId; //!< id to identify varables 144 145 bool changed; //!< true if last readFromBuf changed anything 96 146 97 147 protected: … … 99 149 void * ptrOut; //!< pointer to data (write) 100 150 int length; //!< data length 101 int permission; //!< who is allowed to change this var102 int priority; //!< priority assigned to var103 int realPriority; //!< priority assigned to var, increased every time not sent104 151 105 152 -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_vector.h
r7578 r7631 14 14 15 15 public: 16 SynchronizeableVector( Vector * ptrIn, Vector * ptrOut, std::string name, int permission = 0, int priority = 0 );16 SynchronizeableVector( Vector * ptrIn, Vector * ptrOut, std::string name, int permission = PERMISSION_SERVER, int priority = 0 ); 17 17 virtual ~SynchronizeableVector(); 18 18
Note: See TracChangeset
for help on using the changeset viewer.