Changeset 6273 in orxonox.OLD for branches/network/src
- Timestamp:
- Dec 23, 2005, 5:30:22 PM (19 years ago)
- Location:
- branches/network/src
- Files:
-
- 11 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 }; -
branches/network/src/world_entities/skybox.cc
r6142 r6273 22 22 #include "static_model.h" 23 23 #include "material.h" 24 #include "network_game_manager.h" 25 #include "converter.h" 24 26 25 27 using namespace std; … … 210 212 this->setModel(model); 211 213 } 214 215 void SkyBox::writeBytes( const byte * data, int length, int sender ) 216 { 217 setRequestedSync( false ); 218 setIsOutOfSync( false ); 219 220 int flsize = Converter::byteArrayToFloat( data, &size ); 221 Converter::byteArrayToString( data+flsize, textureName, length-flsize ); 222 223 PRINT(0)("GOT DATA: size=%f texture=%s\n", size, textureName); 224 225 this->setSize( size ); 226 this->setTexture( textureName ); 227 this->rebuild(); 228 } 229 230 int SkyBox::readBytes( byte * data, int maxLength, int * reciever ) 231 { 232 if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) 233 { 234 PRINTF(0)("Requesting sync! %d\n", this->getUniqueID()); 235 (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); 236 setRequestedSync( true ); 237 } 238 239 int rec = this->getRequestSync(); 240 if ( rec > 0 ) 241 { 242 PRINTF(0)("Serving client %d which requested sync %d size=%f texture=%s\n", rec, this->getUniqueID(), this->size, this->textureName); 243 *reciever = rec; 244 245 int flsize = Converter::floatToByteArray( this->size, data, maxLength ); 246 247 if ( flsize <= 0 ) 248 { 249 PRINTF(1)("Byte array is too small (%d) to store float\n", maxLength ); 250 return 0; 251 } 252 253 if ( strlen(textureName)+INTSIZE+flsize > maxLength ) 254 { 255 PRINTF(1)("Byte array is too small (%d) to store data\n", maxLength ); 256 return 0; 257 } 258 259 return flsize + Converter::stringToByteArray( textureName, data+flsize, strlen(textureName), maxLength-flsize ); 260 261 } 262 263 *reciever = 0; 264 return 0; 265 } 266 267 void SkyBox::writeDebug( ) const 268 { 269 } 270 271 void SkyBox::readDebug( ) const 272 { 273 } -
branches/network/src/world_entities/skybox.h
r5511 r6273 35 35 void setSize(float size); 36 36 /** assumes jpg as input-format */ 37 void setTexture(const char* name) { this->setTextureAndType (name, "jpg"); };37 void setTexture(const char* name) { strncpy(textureName, name, 1024); this->setTextureAndType (name, "jpg"); }; 38 38 39 39 void setTextureAndType(const char* name, const char* extension); … … 41 41 const char* right, const char* front, const char* back); 42 42 43 virtual void writeBytes(const byte* data, int length, int sender); 44 virtual int readBytes(byte* data, int maxLength, int * reciever); 45 virtual void writeDebug() const; 46 virtual void readDebug() const; 47 43 48 private: 44 49 void rebuild(); 45 50 46 Material** material; //!< Materials for the SkyBox. sorted by number (0-5) top, bottom, left, right, front, back 47 float size; //!< Size of the SkyBox. This should match the frustum maximum range. 51 Material** material; //!< Materials for the SkyBox. sorted by number (0-5) top, bottom, left, right, front, back 52 float size; //!< Size of the SkyBox. This should match the frustum maximum range. 53 char textureName[1024]; //!< Name of the Texture 48 54 49 55 };
Note: See TracChangeset
for help on using the changeset viewer.