Changeset 6273 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- Dec 23, 2005, 5:30:22 PM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/converter.cc
r6229 r6273 92 92 return -1; 93 93 } 94 94 95 95 const int mod = 256; // = 2^8 96 96 … … 405 405 return FLOATSIZE; 406 406 } 407 408 /** 409 * copies a strint to a byte array 410 * @param s: string to copy 411 * @param a: byte array 412 * @param length: string length 413 * @return: the used number of bytes in byte array 414 */ 415 int Converter::stringToByteArray( const char * s, byte * a, int length, int maxLength ) 416 { 417 if ( length+INTSIZE > maxLength ) 418 { 419 PRINTF(1)("Byte array is too small (%d) to store %d bytes\n", maxLength, length+INTSIZE); 420 return -1; 421 } 422 423 int n = Converter::intToByteArray( length, a, maxLength ); 424 425 memcpy( a+INTSIZE, s, length ); 426 427 return length + INTSIZE; 428 } 429 430 /** 431 * reads a string out of a byte array 432 * @param a: byte array 433 * @param s: string 434 * @param maxLength: max bytes to copy 435 * @return: the number of read bytes in byte array 436 */ 437 int Converter::byteArrayToString( const byte * a, char * s, int maxLength ) 438 { 439 int length; 440 441 int n = Converter::byteArrayToInt( a, &length ); 442 443 if ( length+1 > maxLength ) 444 { 445 PRINTF(1)("There is not enough space in string (%d) to store %d bytes\n", maxLength, length+1 ); 446 return -1; 447 } 448 449 memcpy( s, a+n, length ); 450 s[length] = '\0'; 451 452 return n+length; 453 } -
branches/network/src/lib/network/converter.h
r6229 r6273 32 32 static int floatToByteArray(float x, byte* a, int length); 33 33 static int byteArrayToFloat(const byte* a, float* x); 34 34 35 static int stringToByteArray(const char* s, byte* a, int length, int maxLength); 36 static int byteArrayToString(const byte* a, char* s, int maxLength); 37 35 38 //Test 36 39 static char* floatToBinString(float x); -
branches/network/src/lib/network/network_game_manager.cc
r6257 r6273 353 353 return; 354 354 355 if ( userID > outBuffer.size() )355 if ( userID >= outBuffer.size() ) 356 356 resizeBufferVector( userID ); 357 357 … … 368 368 return; 369 369 370 PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );370 //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 ); 371 371 372 372 while ( it != e ) … … 378 378 return; 379 379 380 PRINTF(0)("SendEntityList: ClassID = %x\n", (*it)->getRealClassID());380 //PRINTF(0)("SendEntityList: ClassID = %x\n", (*it)->getRealClassID()); 381 381 382 382 if ( !writeToClientBuffer( outBuffer[userID], (*it)->getUniqueID() ) ) … … 428 428 else 429 429 { 430 PRINTF(0)("Fabricated entity: %s\n", b->getClassName());430 //PRINTF(0)("Fabricated entity: %s\n", b->getClassName()); 431 431 } 432 432 … … 437 437 s->setOwner( owner ); 438 438 this->networkStream->connectSynchronizeable( *s ); 439 if ( !isServer() ) 440 s->setIsOutOfSync( true ); 439 441 } 440 442 else … … 769 771 } 770 772 773 void NetworkGameManager::sync( int uniqueID, int owner ) 774 { 775 if ( owner==this->getHostID() ) 776 return; 777 778 if ( !isServer() ) 779 executeRequestSync( uniqueID, 0 ); 780 else 781 executeRequestSync( uniqueID, owner ); 782 } 783 784 void NetworkGameManager::executeRequestSync( int uniqueID, int user ) 785 { 786 if ( user >= outBuffer.size() ) 787 resizeBufferVector( user ); 788 789 if ( !writeToClientBuffer( outBuffer[user], (byte)REQUEST_SYNC ) ) 790 return; 791 if ( !writeToClientBuffer( outBuffer[user], uniqueID ) ) 792 return; 793 } 794 -
branches/network/src/lib/network/network_game_manager.h
r6250 r6273 77 77 void sendYouAre( int uniqueID, int userID ); 78 78 79 void sync(int uniqueID );79 void sync(int uniqueID, int owner); 80 80 81 81 void sendEntityList(int userID); … … 89 89 void requestRemoveEntity(int uniqueID); 90 90 void executeRemoveEntity(int uniqueID); 91 92 void executeRequestSync( int uniqueID, int user ); 91 93 92 94 void doCreateEntity(ClassID classID, int uniqueID, int owner); -
branches/network/src/lib/network/network_protocol.cc
r6139 r6273 30 30 #include "debug.h" 31 31 32 #include "converter.h" 33 32 34 /* using namespace std is default, this needs to be here */ 33 35 using namespace std; … … 41 43 /* set the class id for the base object */ 42 44 this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol"); 43 this->headerLength = sizeof(Header);45 this->headerLength = INTSIZE+FLOATSIZE; 44 46 } 45 47 … … 74 76 //Now create the header 75 77 /* sender ID: FIXME: there will be a better ID (for example unique:D)*/ 76 data[0] = (byte)(source.getUniqueID()); 78 //data[0] = (byte)(source.getUniqueID()); 79 int res = Converter::intToByteArray( source.getUniqueID(), data, bufferLength ); 80 77 81 /* data length*/ 78 data[1] = length; 82 //data[1] = length; 83 res = Converter::intToByteArray( length, data+res, bufferLength-res ); 79 84 80 85 … … 105 110 106 111 /* unique ID */ 107 h.synchronizeableID = data[0]; 112 //h.synchronizeableID = data[0]; 113 int res = Converter::byteArrayToInt( data, &(h.synchronizeableID) ); 108 114 /* data length*/ 109 h.length = data[1]; 115 //h.length = data[1]; 116 Converter::byteArrayToInt( data+res, &(h.length) ); 110 117 111 118 -
branches/network/src/lib/network/network_protocol.h
r6139 r6273 14 14 typedef struct Header 15 15 { 16 bytesynchronizeableID;17 bytelength;16 int synchronizeableID; 17 int length; 18 18 }; 19 19 -
branches/network/src/lib/network/network_stream.cc
r6256 r6273 215 215 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 216 216 { 217 if ( (*it)!=NULL && (*it)->getOwner() == myHostId)217 if ( (*it)!=NULL /*&& (*it)->getOwner() == myHostId*/ ) 218 218 { 219 219 do { … … 276 276 continue; 277 277 278 PRINTF(5)("read %d bytes from socket\n", dataLength);279 278 header = networkProtocol->extractHeader(upBuffer, dataLength); 280 279 dataLength -= sizeof(header); 280 281 PRINTF(0)("read %d bytes from socket uniqueID = %d\n", dataLength, header.synchronizeableID); 281 282 282 283 if ( dataLength != header.length ) -
branches/network/src/lib/network/synchronizeable.cc
r6252 r6273 30 30 this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); 31 31 owner = 0; 32 state = 0; 32 33 hostID = NetworkManager::getInstance()->getHostID(); 33 34 this->setIsServer(this->hostID == 0); 34 35 uniqueID = -1; 35 36 this->networkStream = NULL; 37 this->setRequestedSync( false ); 36 38 } 37 39 … … 94 96 else 95 97 this->state = this->state & (~STATE_OUTOFSYNC); 98 //PRINTF(0)("isoutofsync %s %d\n", this->getClassName(), state); 96 99 } 97 100 … … 102 105 bool Synchronizeable::isServer() 103 106 { 104 return this->state & STATE_SERVER == STATE_SERVER;107 return (this->state & STATE_SERVER) >0; 105 108 } 106 109 … … 111 114 bool Synchronizeable::isOutOfSync() 112 115 { 113 return this->state & STATE_OUTOFSYNC == STATE_OUTOFSYNC; 116 return (this->state & STATE_OUTOFSYNC) >0; 117 } 118 119 /** 120 * Determines if the requestedSync flag is set 121 * @return true, if the requestedSync flag is true, false else 122 */ 123 bool Synchronizeable::requestedSync() 124 { 125 return (this->state & STATE_REQUESTEDSYNC) >0; 126 } 127 128 /** 129 * Sets the requestedsync flag to a given value 130 * @param requestedSync: the boolean value which the requestedsync flag is to set to 131 */ 132 void Synchronizeable::setRequestedSync( bool requestedSync ) 133 { 134 if( requestedSync ) 135 this->state = this->state | STATE_REQUESTEDSYNC; 136 else 137 this->state = this->state & (~STATE_REQUESTEDSYNC); 114 138 } 115 139 -
branches/network/src/lib/network/synchronizeable.h
r6250 r6273 9 9 #include "base_object.h" 10 10 #include "netdefs.h" 11 #include "converter.h" 11 12 12 13 … … 18 19 #define STATE_SERVER 1 19 20 #define STATE_OUTOFSYNC 2 21 #define STATE_REQUESTEDSYNC 4 22 23 //macros to help writing data in byte buffer 24 #define SYNCHELP_WRITE_BEGIN() { int __synchelp_write_i = 0; \ 25 bool __synchelp_write_err = false; \ 26 int __synchelp_write_n; } 27 #define SYNCHELP_WRITE_RESET() { __synchelp_write_i = 0; __synchelp_write_err = false; } 28 #define SYNCHELP_WRITE_INT(i) { __synchelp_write_n = } 29 #define SYNCHELP_WIRTE_FLOAT() 30 #define SYNCHELP_WRITE_BYTE() 31 #define SYNCHELP_WRITE_STRING() 20 32 21 33 class NetworkStream; … … 35 47 void setIsServer( bool isServer ); 36 48 void setIsOutOfSync( bool outOfSync ); 49 void setRequestedSync( bool requestedSync ); 37 50 bool isServer(); 38 51 bool isOutOfSync(); 39 void setUniqueID( int id ){ uniqueID = id; } 40 int getUniqueID() const { return uniqueID; }; 41 void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } 52 bool requestedSync(); 53 inline void setUniqueID( int id ){ uniqueID = id; } 54 inline int getUniqueID() const { return uniqueID; }; 55 inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } 56 inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } }; 57 inline int getHostID() { return this->hostID; } 42 58 43 59 inline int getOwner(){ return owner; } … … 55 71 int owner; 56 72 int hostID; 57 int state; 73 58 74 std::list<int> synchronizeRequests; 59 75 60 76 protected: 61 77 NetworkStream* networkStream; 78 int state; 62 79 63 80 };
Note: See TracChangeset
for help on using the changeset viewer.