Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6273 in orxonox.OLD for branches


Ignore:
Timestamp:
Dec 23, 2005, 5:30:22 PM (19 years ago)
Author:
rennerc
Message:

converter: added functions for strings
network_protocol: length and id are now int
network_game_manager: fixed some more bugs :D
skybox: is loaded on client corectly now :)

Location:
branches/network/src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/converter.cc

    r6229 r6273  
    9292    return -1;
    9393  }
    94  
     94
    9595  const int mod = 256; // = 2^8
    9696
     
    405405  return FLOATSIZE;
    406406}
     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 */
     415int 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 */
     437int 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  
    3232    static int floatToByteArray(float x, byte* a, int length);
    3333    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
    3538    //Test
    3639    static char* floatToBinString(float x);
  • branches/network/src/lib/network/network_game_manager.cc

    r6257 r6273  
    353353    return;
    354354
    355   if ( userID > outBuffer.size() )
     355  if ( userID >= outBuffer.size() )
    356356    resizeBufferVector( userID );
    357357
     
    368368    return;
    369369
    370   PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );
     370  //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );
    371371
    372372  while ( it != e )
     
    378378        return;
    379379
    380       PRINTF(0)("SendEntityList: ClassID = %x\n", (*it)->getRealClassID());
     380      //PRINTF(0)("SendEntityList: ClassID = %x\n", (*it)->getRealClassID());
    381381
    382382      if ( !writeToClientBuffer( outBuffer[userID], (*it)->getUniqueID() ) )
     
    428428  else
    429429  {
    430     PRINTF(0)("Fabricated entity: %s\n", b->getClassName());
     430    //PRINTF(0)("Fabricated entity: %s\n", b->getClassName());
    431431  }
    432432
     
    437437    s->setOwner( owner );
    438438    this->networkStream->connectSynchronizeable( *s );
     439    if ( !isServer() )
     440      s->setIsOutOfSync( true );
    439441  }
    440442  else
     
    769771}
    770772
     773void 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
     784void 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  
    7777    void sendYouAre( int uniqueID, int userID );
    7878
    79     void sync(int uniqueID);
     79    void sync(int uniqueID, int owner);
    8080
    8181    void sendEntityList(int userID);
     
    8989    void requestRemoveEntity(int uniqueID);
    9090    void executeRemoveEntity(int uniqueID);
     91
     92    void executeRequestSync( int uniqueID, int user );
    9193
    9294    void doCreateEntity(ClassID classID, int uniqueID, int owner);
  • branches/network/src/lib/network/network_protocol.cc

    r6139 r6273  
    3030#include "debug.h"
    3131
     32#include "converter.h"
     33
    3234/* using namespace std is default, this needs to be here */
    3335using namespace std;
     
    4143  /* set the class id for the base object */
    4244  this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol");
    43   this->headerLength = sizeof(Header);
     45  this->headerLength = INTSIZE+FLOATSIZE;
    4446}
    4547
     
    7476  //Now create the header
    7577  /* 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
    7781  /* data length*/
    78   data[1] = length;
     82  //data[1] = length;
     83  res = Converter::intToByteArray( length, data+res, bufferLength-res );
    7984
    8085
     
    105110
    106111  /* unique ID */
    107   h.synchronizeableID = data[0];
     112  //h.synchronizeableID = data[0];
     113  int res = Converter::byteArrayToInt( data, &(h.synchronizeableID) );
    108114  /* data length*/
    109   h.length = data[1];
     115  //h.length = data[1];
     116  Converter::byteArrayToInt( data+res, &(h.length) );
    110117
    111118
  • branches/network/src/lib/network/network_protocol.h

    r6139 r6273  
    1414typedef struct Header
    1515{
    16   byte synchronizeableID;
    17   byte length;
     16  int synchronizeableID;
     17  int length;
    1818};
    1919
  • branches/network/src/lib/network/network_stream.cc

    r6256 r6273  
    215215  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
    216216  {
    217     if ( (*it)!=NULL && (*it)->getOwner() == myHostId )
     217    if ( (*it)!=NULL /*&& (*it)->getOwner() == myHostId*/ )
    218218    {
    219219      do {
     
    276276          continue;
    277277
    278         PRINTF(5)("read %d bytes from socket\n", dataLength);
    279278        header = networkProtocol->extractHeader(upBuffer, dataLength);
    280279        dataLength -= sizeof(header);
     280
     281        PRINTF(0)("read %d bytes from socket uniqueID = %d\n", dataLength, header.synchronizeableID);
    281282
    282283        if ( dataLength != header.length )
  • branches/network/src/lib/network/synchronizeable.cc

    r6252 r6273  
    3030  this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable");
    3131  owner = 0;
     32  state = 0;
    3233  hostID = NetworkManager::getInstance()->getHostID();
    3334  this->setIsServer(this->hostID == 0);
    3435  uniqueID = -1;
    3536  this->networkStream = NULL;
     37  this->setRequestedSync( false );
    3638}
    3739
     
    9496  else
    9597    this->state = this->state & (~STATE_OUTOFSYNC);
     98  //PRINTF(0)("isoutofsync %s %d\n", this->getClassName(), state);
    9699}
    97100
     
    102105bool Synchronizeable::isServer()
    103106{
    104   return this->state & STATE_SERVER == STATE_SERVER;
     107  return (this->state & STATE_SERVER) >0;
    105108}
    106109
     
    111114bool Synchronizeable::isOutOfSync()
    112115{
    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 */
     123bool 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 */
     132void Synchronizeable::setRequestedSync( bool requestedSync )
     133{
     134  if( requestedSync )
     135    this->state = this->state | STATE_REQUESTEDSYNC;
     136  else
     137    this->state = this->state & (~STATE_REQUESTEDSYNC);
    114138}
    115139
  • branches/network/src/lib/network/synchronizeable.h

    r6250 r6273  
    99#include "base_object.h"
    1010#include "netdefs.h"
     11#include "converter.h"
    1112
    1213
     
    1819#define STATE_SERVER 1
    1920#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()
    2032
    2133class NetworkStream;
     
    3547    void setIsServer( bool isServer );
    3648    void setIsOutOfSync( bool outOfSync );
     49    void setRequestedSync( bool requestedSync );
    3750    bool isServer();
    3851    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; }
    4258
    4359    inline int getOwner(){ return owner; }
     
    5571    int owner;
    5672    int hostID;
    57     int state;
     73
    5874    std::list<int> synchronizeRequests;
    5975
    6076  protected:
    6177    NetworkStream* networkStream;
     78    int state;
    6279
    6380  };
  • branches/network/src/world_entities/skybox.cc

    r6142 r6273  
    2222#include "static_model.h"
    2323#include "material.h"
     24#include "network_game_manager.h"
     25#include "converter.h"
    2426
    2527using namespace std;
     
    210212  this->setModel(model);
    211213}
     214
     215void 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
     230int 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
     267void SkyBox::writeDebug( ) const
     268{
     269}
     270
     271void SkyBox::readDebug( ) const
     272{
     273}
  • branches/network/src/world_entities/skybox.h

    r5511 r6273  
    3535  void setSize(float size);
    3636  /** 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"); };
    3838
    3939  void setTextureAndType(const char* name, const char* extension);
     
    4141                   const char* right, const char* front, const char* back);
    4242
     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
    4348 private:
    4449  void rebuild();
    4550
    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
    4854
    4955};
Note: See TracChangeset for help on using the changeset viewer.