Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7631 in orxonox.OLD


Ignore:
Timestamp:
May 17, 2006, 8:28:23 AM (18 years ago)
Author:
rennerc
Message:

synchronisation should work now

Location:
branches/network/src
Files:
2 added
24 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/defs/class_id.h

    r7256 r7631  
    330330  CL_HANDSHAKE                  =    0x00000d06,
    331331  CL_NETWORK_GAME_MANAGER       =    0x00000d07,
     332  CL_MESSAGE_MANAGER            =    0x00000d08,
     333  CL_SIMPLE_SYNC                =    0x00000d09,
    332334
    333335
  • branches/network/src/lib/network/Makefile.am

    r7540 r7631  
    2020                      network_game_manager.cc \
    2121                      converter.cc \
     22                      message_manager.cc \
    2223                      \
    2324                      synchronizeable_var/synchronizeable_var.cc \
     
    5051                 converter.h \
    5152                 netdefs.h \
     53                 message_manager.h \
    5254                 \
    5355                 synchronizeable_var/synchronizeable_var.h \
  • branches/network/src/lib/network/converter.cc

    r7230 r7631  
    493493  }
    494494
    495   s[0] = '\0';
     495  s = "";
    496496  s.append( (char*)a+n, length );
    497497
  • branches/network/src/lib/network/network_stream.cc

    r7614 r7631  
    6262  this->init();
    6363  this->peers[0].socket = new UdpSocket( host, port );
     64  this->peers[0].userId = 0;
     65  this->peers[0].isServer = true;
    6466  this->networkProtocol = new NetworkProtocol();
    6567  this->connectionMonitor = new ConnectionMonitor();
     
    357359      peers[clientId].handshake->setUniqueID(clientId);
    358360      peers[clientId].userId = clientId;
     361      peers[clientId].isServer = false;
    359362    } else
    360363    {
     
    369372      peers[clientId].handshake->setUniqueID(clientId);
    370373      peers[clientId].userId = clientId;
     374      peers[clientId].isServer = false;
    371375     
    372376      PRINTF(0)("num sync: %d\n", synchronizeables.size());
     
    400404        delete it->second.handshake;
    401405      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      }
    403411
    404412      NetworkGameManager::getInstance()->signalLeftPlayer(it->second.userId);
     
    632640        int leafClassId;
    633641        if ( INTSIZE > length - offset )
    634           break;
     642        {
     643          offset += syncDataLength;
     644          continue;
     645        }
    635646       
    636647        Converter::byteArrayToInt( buf + offset, &leafClassId );
     
    639650        /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
    640651        /* 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;
    645657        }
    646658        else
     
    650662        {
    651663          PRINTF(1)("Could not fabricate Object with classID %x\n", leafClassId);
    652           break;
     664          offset += syncDataLength;
     665          continue;
    653666        }
    654667       
     
    663676        else
    664677        {
    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);
    666679          delete b;
    667           break;
     680          offset += syncDataLength;
     681          continue;
    668682        }
    669683      }
  • branches/network/src/lib/network/network_stream.h

    r7602 r7631  
    6969
    7070    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; }
    7172
    7273    void debug();
  • branches/network/src/lib/network/synchronizeable.cc

    r7617 r7631  
    9292
    9393
    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
    10995int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH )
    11096{
     
    119105    neededSize += (*it)->getSize();
    120106
    121   assert( neededSize <= maxLength );
     107  if ( !( neededSize <= maxLength ) )
     108  {
     109    PRINTF(0)( "%d > %d\n", neededSize, maxLength );
     110    assert(false);
     111  }
    122112
    123113  //remove older states from history than fromStateId
     
    170160  int i = 0;
    171161  int n;
     162 
     163  bool hasPermission;
    172164
    173165  // now do the actual synchronization: kick all variables to write into a common buffer
    174166  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
    175167  {
    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() )
    178175    {
    179176      (*it)->debug();
     
    182179      i += n;
    183180    }
    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++ )
    191184      {
    192185        assert(false);
     
    287280    stateFrom = (*it);
    288281 
    289   std::list<int> changes;
    290  
    291282  //apply diff
    292283  for ( int i = 0; i<length; i++ )
     
    297288      stateTo->data[i] = data[i];
    298289   
    299     if ( data[i] != 0 )
    300       changes.push_back(i);
    301290  }
    302291
     
    305294 
    306295  int i = 0;
    307 
     296  std::list<int> changes;
     297 
    308298  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
    309299  {
    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    }
    311315  }
    312316
     
    331335void Synchronizeable::registerVar( SynchronizeableVar * var )
    332336{
     337  PRINTF(0)("ADDING var: %s (%s)\n", var->getName().c_str(), this->getName() );
    333338  syncVarList.push_back( var );
    334339}
     
    342347int Synchronizeable::registerVarId( SynchronizeableVar * var )
    343348{
     349  PRINTF(0)("ADDING var: %s (%s)\n", var->getName().c_str(), this->getName() );
    344350  syncVarList.push_back( var );
     351  var->setWatched( true );
     352  var->setVarId( syncVarList.size()-1 );
    345353  return syncVarList.size()-1;
    346354}
    347355
    348 
    349 
     356/**
     357 * removed user's states from memory
     358 * @param userId user to clean
     359 */
     360void 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  
    4040typedef std::vector<StateHistory> UserStateHistory;
    4141
    42 enum {
    43   PERMISSION_SERVER = 1,
    44   PERMISSION_OWNER  = 2,
    45   PERMISSION_ALL    = 4
    46 };
    47 
    4842typedef std::vector<SynchronizeableVar*> SyncVarList;
    4943
     
    6256    virtual void varChangeHandler( std::list<int> & id );
    6357
    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 );
    6661
    6762    void registerVar( SynchronizeableVar * var );
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_bool.cc

    r7578 r7631  
    6060  assert( maxLength >= 1 );
    6161 
     62  bool oldVal = *vPtrOut;
     63 
    6264  *vPtrOut = buf[0] != 0;
     65 
     66  setHasChanged( oldVal != *vPtrOut );
    6367 
    6468  return 1;
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_bool.h

    r7578 r7631  
    1212
    1313  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 );
    1515    virtual ~SynchronizeableBool();
    1616   
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_float.cc

    r7578 r7631  
    6262  assert( maxLength >= FLOATSIZE );
    6363 
     64  float oldVal = *vPtrOut;
     65 
    6466  int res = Converter::byteArrayToFloat( buf, vPtrOut );
     67 
     68  setHasChanged( oldVal != *vPtrOut );
    6569 
    6670  assert( res == FLOATSIZE );
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_float.h

    r7578 r7631  
    1212
    1313  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 );
    1515    virtual ~SynchronizeableFloat();
    1616   
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_int.cc

    r7578 r7631  
    6262  assert( maxLength >= INTSIZE );
    6363 
     64  int oldVal = *vPtrOut;
     65 
    6466  int res = Converter::byteArrayToInt( buf, vPtrOut );
     67 
     68  setHasChanged( oldVal != *vPtrOut );
    6569 
    6670  assert( res == INTSIZE );
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_int.h

    r7578 r7631  
    1212
    1313  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 );
    1515    virtual ~SynchronizeableInt();
    1616   
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_quaternion.cc

    r7614 r7631  
    9898  assert( res > 0 );
    9999  n += res;
     100 
     101  Quaternion oldVal = *vPtrOut;
    100102
    101103  *vPtrOut = Quaternion( Vector(x, y, z), w );
     104 
     105  setHasChanged( ! ( oldVal == *vPtrOut ) );
    102106
    103107  assert( n == 4*FLOATSIZE );
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_quaternion.h

    r7578 r7631  
    1414
    1515  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 );
    1717    virtual ~SynchronizeableQuaternion();
    1818   
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_string.cc

    r7578 r7631  
    6060int SynchronizeableString::readFromBuf( byte * buf, int maxLength )
    6161{
     62  std::string oldVal = *vPtrOut;
     63     
    6264  int res = Converter::byteArrayToString( buf, *vPtrOut, maxLength );
     65 
     66  setHasChanged( oldVal != *vPtrOut );
    6367 
    6468  assert( res > 0 );
     
    7680
    7781
     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 */
     88int 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  
    1515
    1616  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 );
    1818    virtual ~SynchronizeableString();
    1919   
     
    3030    virtual int getSize(){ return vPtrIn->length()+INTSIZE; }
    3131   
     32    virtual int getSizeFromBuf( byte * buf, int maxLength );
     33   
    3234    virtual void debug();
    3335   
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_uint.h

    r7578 r7631  
    1212
    1313  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 );
    1515    virtual ~SynchronizeableUInt();
    1616   
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.cc

    r7559 r7631  
    3535  this->realPriority = priority;
    3636  this->name = name;
     37  this->bWatched = false;
    3738}
    3839
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.h

    r7578 r7631  
    1111#include <assert.h>
    1212
     13enum {
     14  PERMISSION_SERVER = 1,
     15  PERMISSION_OWNER  = 2,
     16  PERMISSION_ALL    = 4
     17};
     18
    1319class SynchronizeableVar {
    1420
    1521  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 );
    1723    virtual ~SynchronizeableVar();
    1824
     
    2228     */
    2329    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; }
    2435
    2536    /**
     
    5566     * @return true if you can write
    5667     */
    57     inline bool checkPremission( int permission ){ return (permission & this->permission) != 0; }
     68    inline bool checkPermission( int permission ){ return (permission & this->permission) != 0; }
    5869
    5970    /**
     
    8798   
    8899    /**
     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    /**
    89132     * print out variable value
    90133     */
     
    94137  private:
    95138    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
    96146
    97147  protected:
     
    99149    void * ptrOut;      //!< pointer to data (write)
    100150    int length;         //!< data length
    101     int permission;     //!< who is allowed to change this var
    102     int priority;       //!< priority assigned to var
    103     int realPriority;  //!< priority assigned to var, increased every time not sent
    104151
    105152
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_vector.h

    r7578 r7631  
    1414
    1515  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 );
    1717    virtual ~SynchronizeableVector();
    1818   
  • branches/network/src/subprojects/network/network_unit_test.cc

    r7617 r7631  
    259259 
    260260  SimpleSync* ss = new SimpleSync("Server");
     261  ss->setSynchronized( true );
    261262
    262263 
     
    292293  netMan->establishConnection("localhost", 9999);
    293294 
    294   SimpleSync* ss = new SimpleSync("SimpleSync");
    295   netMan->connectSynchronizeable( *ss );
    296 
     295//  SimpleSync* ss = new SimpleSync("SimpleSync");
     296//  ss->setSynchronized( true );
     297//  netMan->connectSynchronizeable( *ss );
     298
     299  SimpleSync * ss = NULL;
     300 
    297301  for(;;)
    298302  {
    299303    netMan->synchronize();
     304    ss = dynamic_cast<SimpleSync*>(ClassList::getObject( "Server", CL_SIMPLE_SYNC ) );
    300305    SDL_Delay(1000);
    301     ss->debug();
     306    if (ss)
     307      ss->debug();
    302308  }
    303309
  • branches/network/src/subprojects/network/simple_sync.cc

    r7617 r7631  
    2222#include "simple_sync.h"
    2323
     24#include "class_id.h"
     25#include "fast_factory.h"
     26#include "lib/util/loading/factory.h"
     27
    2428#include "debug.h"
     29
     30CREATE_FACTORY(SimpleSync, CL_SIMPLE_SYNC);
    2531
    2632
     
    3137{
    3238  setName( name );
     39  this->setClassID( CL_SIMPLE_SYNC, "SimpleSync" );
    3340  in = 0;
    3441  out = 1;
     
    4956}
    5057
     58SimpleSync::SimpleSync( const TiXmlElement * root )
     59{
     60  setName( "" );
     61  this->setClassID( CL_SIMPLE_SYNC, "SimpleSync" );
     62  in = 0;
     63  out = 1;
     64  id = this->registerVarId( new SynchronizeableInt( &in, &out, "var", PERMISSION_ALL ) );
     65}
    5166
    5267
     68
  • branches/network/src/subprojects/network/simple_sync.h

    r7617 r7631  
    1414  public:
    1515    SimpleSync( std::string name);
     16    SimpleSync(const TiXmlElement* root = NULL);
    1617    virtual ~SimpleSync();
    1718   
Note: See TracChangeset for help on using the changeset viewer.