Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7444 in orxonox.OLD for branches/network/src/lib/network


Ignore:
Timestamp:
Apr 29, 2006, 1:57:48 PM (18 years ago)
Author:
rennerc
Message:

new network system implemented. yet with a lot of empty function bodys

Location:
branches/network/src/lib/network
Files:
17 added
8 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/Makefile.am

    r7026 r7444  
    1515                      handshake.cc \
    1616                      network_game_manager.cc \
    17                       converter.cc
     17                      converter.cc \
     18                      \
     19                      synchronizeable_var/synchronizeable_var.cc \
     20                      synchronizeable_var/synchronizeable_vector.cc \
     21                      synchronizeable_var/synchronizeable_int.cc \
     22                      synchronizeable_var/synchronizeable_quaternion.cc \
     23                      synchronizeable_var/synchronizeable_string.cc \
     24                      synchronizeable_var/synchronizeable_float.cc \
     25                      synchronizeable_var/synchronizeable_bool.cc \
     26                      synchronizeable_var/synchronizeable_uint.cc
    1827
    1928
     
    3241                 network_game_manager.h \
    3342                 converter.h \
    34                  netdefs.h
     43                 netdefs.h \
     44                 \
     45                 synchronizeable_var/synchronizeable_var.h \
     46                 synchronizeable_var/synchronizeable_vector.h \
     47                 synchronizeable_var/synchronizeable_int.h \
     48                 synchronizeable_var/synchronizeable_quaternion.h \
     49                 synchronizeable_var/synchronizeable_string.h \
     50                 synchronizeable_var/synchronizeable_float.h \
     51                 synchronizeable_var/synchronizeable_bool.h \
     52                 synchronizeable_var/synchronizeable_uint.h
    3553
    3654
  • branches/network/src/lib/network/handshake.cc

    r6815 r7444  
    3131
    3232  this->setIsServer(server);
    33   this->clientId = clientId;
    34   this->networkGameManagerId = networkGameManagerId;
    35   this->state = 0;
    36   this->isOk = false;
    37   this->setOwner(0);
     33  this->localState.hostId = clientId;
     34  this->localState.networkManagerId = networkGameManagerId;
     35 
     36  orxId_handler = registerVarId( new SynchronizeableInt( &localState.orxId, &remoteState.orxId, "orxonoxId" ) );
     37  version_handler = registerVarId( new SynchronizeableInt( &localState.version, &remoteState.version, "version" ) );
     38  netManId_handler = registerVarId( new SynchronizeableInt( &localState.networkManagerId, &remoteState.networkManagerId, "networkManagerId" ) );
     39  hostId_handler = registerVarId( new SynchronizeableInt( &localState.hostId, &remoteState.hostId, "hostId" ) );
     40  completed_handler = registerVarId( new SynchronizeableInt( &localState.completed, &remoteState.completed, "completed" ) );
     41  error_handler = registerVarId( new SynchronizeableInt( &localState.error, &remoteState.error, "error" ) );
     42  errorString_handler = registerVarId( new SynchronizeableString( &localState.errorString, &remoteState.errorString, "errorString" ) );
     43
    3844
    3945  this->setSynchronized(true);
     
    4147}
    4248
    43 int Handshake::writeBytes( const byte * data, int length, int sender)
    44 {
    45   PRINTF(5)("Handshake::writeBytes states = %d %d %d %d (%d)\n", hasState( HS_RECVD_INIT ), hasState( HS_RECVD_VER ), hasState( HS_RECVD_HID ), hasState( HS_COMPLETED ), state);
    46 
    47   SYNCHELP_READ_BEGIN();
    48 
    49   if ( hasState( HS_COMPLETED ) )
    50        return 0;
    51 
    52   if ( !hasState( HS_RECVD_INIT ) )
    53   {
    54     if ( length != _INITIAL_DATA_LENGTH )
    55     {
    56       PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);
    57       setState( HS_COMPLETED );
    58       return 0;
    59     }
    60 
    61     if ( strncmp((char*)data, _INITIAL_DATA, length) )
    62     {
    63       PRINTF(0)("initial packed does not match\n");
    64       setState( HS_COMPLETED );
    65       return length;
    66     }
    67 
    68     setState( HS_RECVD_INIT );
    69     PRINTF(0)("got valid initial packet from client %d\n", clientId);
    70     return length;
    71   }
    72 
    73   if ( hasState( HS_RECVD_INIT ) && !hasState( HS_RECVD_VER ) )
    74   {
    75     if ( length != _ORXONOX_VERSION_LENGTH )
    76     {
    77       PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);
    78       setState( HS_COMPLETED );
    79       return 0;
    80     }
    81 
    82     if ( strncmp((char*)data, _ORXONOX_VERSION, length) )
    83     {
    84       PRINTF(0)("versions do not match\n");
    85       setState( HS_COMPLETED );
    86       return length;
    87     }
    88 
    89     setState( HS_RECVD_VER );
    90 
    91     PRINTF(0)("client %d's version does match\n", clientId);
    92     return length;
    93   }
    94 
    95   if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
    96   {
    97     /*if ( length != INTSIZE+INTSIZE )
    98     {
    99       PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, INTSIZE+INTSIZE);
    100       setState( HS_COMPLETED );
    101       return 0;
    102   }*/
    103 
    104     setState( HS_COMPLETED );
    105     setState( HS_RECVD_HID );
    106     this->isOk = true;
    107     SYNCHELP_READ_INT( this->newHostId, NWT_HS_HOST_ID );
    108     SYNCHELP_READ_INT( this->newNetworkGameManagerId, NWT_HS_NGM_ID );
    109 
    110     if ( newHostId == 0 )
    111     {
    112       setState( HS_WAS_REJECT );
    113       isOk = false;
    114       PRINTF(0)("Server did not accept handshake!\n");
    115     }
    116     else
    117     {
    118       PRINTF(0)("got my hostID: %d and networkGameManagerId: %d\n", newHostId, newNetworkGameManagerId);
    119     }
    120     return SYNCHELP_READ_N;
    121   }
    122 
    123 }
    124 
    125 int Handshake::readBytes( byte * data, int maxLength, int * reciever )
    126 {
    127   PRINTF(5)("Handshake::readBytes states = %d %d %d %d (%d)\n", hasState( HS_SENT_INIT ), hasState( HS_SENT_VER ), hasState( HS_SENT_HID ), hasState( HS_COMPLETED ), state);
    128 
    129   SYNCHELP_WRITE_BEGIN();
    130 
    131   if ( hasState( HS_COMPLETED ) )
    132     return 0;
    133 
    134   if ( !hasState( HS_SENT_INIT ) )
    135   {
    136     if ( maxLength < _INITIAL_DATA_LENGTH )
    137     {
    138       PRINTF(0)("buffer too small for _INITIAL_DATA");
    139       setState( HS_COMPLETED );
    140       return 0;
    141     }
    142 
    143     setState( HS_SENT_INIT );
    144     memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH);
    145     if ( this->isServer() )
    146       *reciever = clientId;
    147     return _INITIAL_DATA_LENGTH;
    148   }
    149 
    150   if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) )
    151   {
    152     if ( maxLength < _ORXONOX_VERSION_LENGTH )
    153     {
    154       PRINTF(0)("buffer too small for version number");
    155       setState( HS_COMPLETED );
    156       return 0;
    157     }
    158 
    159     setState( HS_SENT_VER );
    160     memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH);
    161     if ( this->isServer() )
    162       *reciever = clientId;
    163     return _ORXONOX_VERSION_LENGTH;
    164   }
    165 
    166   if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) )
    167   {
    168     if ( maxLength < 2 )
    169     {
    170       PRINTF(0)("buffer too small for ID");
    171       setState( HS_COMPLETED );
    172       return 0;
    173     }
    174 
    175     setState( HS_SENT_HID );
    176     setState( HS_COMPLETED );
    177 
    178     if ( hasState( HS_DO_REJECT ) )
    179     {
    180       isOk = false;
    181       //memcpy(data, (byte*)0, 4);
    182       SYNCHELP_WRITE_INT( 0, NWT_HS_HOST_ID);
    183       SYNCHELP_WRITE_INT( 0, NWT_HS_NGM_ID);
    184     }
    185     else
    186     {
    187       isOk = true;
    188       //memcpy(data, &clientId, 4);
    189       SYNCHELP_WRITE_INT( clientId, NWT_HS_HOST_ID );
    190       SYNCHELP_WRITE_INT( networkGameManagerId, NWT_HS_NGM_ID );
    191     }
    192     *reciever = clientId;
    193     return SYNCHELP_WRITE_N;
    194   }
    195 
    196   return 0;
    197 }
    198 
    199 void Handshake::writeDebug( ) const
     49/**
     50 * handler for changes in synced vars
     51 * @param id id's which have changed
     52 */
     53void Handshake::varChangeHandler( std::list< int > & id )
    20054{
    20155}
    20256
    203 void Handshake::readDebug( ) const
    204 {
    205 }
  • branches/network/src/lib/network/handshake.h

    r6341 r7444  
    1010#include "synchronizeable.h"
    1111
    12 #define _INITIAL_DATA "orxonox"
    13 #define _INITIAL_DATA_LENGTH 7
     12#define _INITIAL_DATA 0xF91337A0
    1413
    15 #define _ORXONOX_VERSION "\x00\x00\x00\x01"
    16 #define _ORXONOX_VERSION_LENGTH 4
     14#define _ORXONOX_VERSION 1
    1715
    18 typedef enum HandshakeState {
    19   HS_SENT_INIT  = 0x00000001,
    20   HS_RECVD_INIT = 0x00000002,
    21   HS_SENT_VER   = 0x00000004,
    22   HS_RECVD_VER  = 0x00000008,
    23   HS_SENT_HID   = 0x00000010,
    24   HS_RECVD_HID  = 0x00000020,
    25   HS_COMPLETED  = 0x00000040,
    26 
    27   HS_DO_REJECT  = 0x00010000,
    28   HS_WAS_REJECT = 0x00020000,
    29 
    30   NUM_STATES
     16struct HandshakeState {
     17  int orxId;
     18  int version;
     19 
     20  int networkManagerId;
     21  int hostId;
     22 
     23  int completed;
     24 
     25  int error;
     26 
     27  std::string errorString;
    3128};
    3229
     
    3431{
    3532  public:
    36     Handshake(bool server, int clientId = 0, int networkGameManagerId = 0);
    37     inline bool       completed(){ return hasState( HS_COMPLETED ); }
    38     inline bool       ok(){ return isOk; }
    39     inline int        getHostId(){ return newHostId; }
    40     inline int        getNetworkGameManagerId(){ return newNetworkGameManagerId; }
    41 
    42     inline void       doReject(){ setState(HS_DO_REJECT); }
    43 
    44     virtual int       writeBytes(const byte* data, int length, int sender);
    45     virtual int       readBytes(byte* data, int maxLength, int * reciever);
    46     virtual void      writeDebug() const;
    47     virtual void      readDebug() const;
     33    Handshake( bool server, int clientId = 0, int networkGameManagerId = 0 );
     34    inline bool       completed(){ return localState.completed != 0 && remoteState.completed != 0; }
     35    inline bool       ok(){ return localState.error == 0 && remoteState.error == 0; }
     36    inline int        getHostId(){ return localState.hostId; }
     37    inline int        getNetworkGameManagerId(){ return localState.networkManagerId; }
     38    inline void       doReject( std::string reason ){ remoteState.error = 1; remoteState.errorString = "the server rejected your connection ["+ reason +"]"; }
     39   
     40    virtual void varChangeHandler( std::list<int> & id );
    4841
    4942  private:
    50     int               state;
    51     int               clientId;
    52     int               networkGameManagerId;
    53     int               newHostId;
    54     int               newNetworkGameManagerId;
    55     bool              isOk;
    56 
    57     inline bool       hasState( int a ){ return (state & a) == a; }
    58     inline void       setState( int a ){ state = state | a; }
    59     inline void       unsetState( int a ){ state = state & (~a); }
     43    HandshakeState localState;
     44    HandshakeState remoteState;
     45   
     46    int netManId_handler;
     47    int hostId_handler;
     48    int completed_handler;
     49    int error_handler;
     50    int errorString_handler;
     51    int orxId_handler;
     52    int version_handler;
    6053
    6154};
  • branches/network/src/lib/network/network_game_manager.cc

    r7354 r7444  
    141141    {
    142142      sendEntityList( sender );
    143       continue;
    144     }
    145     else if ( b == NET_REQUEST_SYNC )
    146     {
    147       if ( !handleRequestSync( i, data, length, sender ) )
    148         return i;
    149143      continue;
    150144    }
     
    532526    s->setSynchronized(true);
    533527    //this->networkStream->connectSynchronizeable( *s );
    534     if ( !isServer() )
    535       s->setIsOutOfSync( true );
     528
    536529    PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID());
    537530
     
    601594}
    602595
    603 /**
    604  * Tell the synchronizeable that a user's synchronizeable is out of sync
    605  * @param uniqueID: unique ID assigned with the entity which is out of sync
    606  * @param userID: user ID who's synchronizeable is out of sync
    607  */
    608 void NetworkGameManager::doRequestSync( int uniqueID, int userID )
    609 {
    610   SynchronizeableList::const_iterator it,e;
    611   it = this->networkStream->getSyncBegin();
    612   e = this->networkStream->getSyncEnd();
    613 
    614   while ( it != e )
    615   {
    616     if ( (*it)->getUniqueID() == uniqueID )
    617     {
    618       (*it)->requestSync( userID );
    619       break;
    620     }
    621     it++;
    622   }
    623 }
    624596
    625597/**
     
    697669  assert(player != NULL);
    698670
    699   s->setIsOutOfSync( true );
    700 
    701671  PRINTF(0)("uniqueID = %d\n", s->getUniqueID());
    702672
     
    902872  return true;
    903873}
    904 
    905 bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender )
    906 {
    907   if ( INTSIZE > length-i )
    908   {
    909     PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
    910     return false;
    911   }
    912   int uniqueID;
    913   i += Converter::byteArrayToInt( &data[i], &uniqueID );
    914 
    915   PRINTF(0)("handleRequestSync %d %d\n", uniqueID, sender);
    916   doRequestSync( uniqueID, sender );
    917 
    918   return true;
    919 }
    920 
    921874
    922875/**
     
    986939}
    987940
    988 void NetworkGameManager::sync( int uniqueID, int owner )
    989 {
    990   /*if ( owner==this->getHostID() )
    991   return;*/
    992 
    993   if ( !isServer() )
    994     executeRequestSync( uniqueID, 0 );
    995   else
    996     executeRequestSync( uniqueID, owner );
    997 }
    998 
    999 void NetworkGameManager::executeRequestSync( int uniqueID, int user )
    1000 {
    1001   PRINTF(0)("executeRequestSync %d %d\n", uniqueID, user);
    1002   if ( user >= outBuffer.size() )
    1003     resizeBufferVector( user );
    1004 
    1005   if ( !writeToClientBuffer( outBuffer[user], (byte)NET_REQUEST_SYNC ) )
    1006     return;
    1007   if ( !writeToClientBuffer( outBuffer[user], uniqueID ) )
    1008     return;
    1009 }
    1010 
     941
  • branches/network/src/lib/network/network_game_manager.h

    r6981 r7444  
    3535 *
    3636 *  REQUEST_ENTITY_LIST: //request the whole world :D
    37  *  REQUEST_SYNC:        >> UNIQUE_ID
    38  *                       << [Sync Info]
    39  *  //REQUEST_SYNC_LIST:   NUMBER, [UNIQUE_ID][0..NUMBER]
    40  *
    4137 *  YOU_ARE_ENTITY:      >> UNIQUE_ID
    4238 *
     
    5450  NET_REQUEST_CREATE,
    5551  NET_REQUEST_REMOVE,
    56   NET_REQUEST_SYNC,
    5752  NET_YOU_ARE_ENTITY,
    5853  NET_REQUEST_ENTITY_LIST,
     
    9186    void sendYouAre( int uniqueID, int userID );
    9287
    93     void sync(int uniqueID, int owner);
    94 
    9588    void sendEntityList(int userID);
    9689
     
    111104    bool handleRemoveEntityList( int& i, const byte* data, int length, int sender );
    112105    bool handleYouAreEntity( int& i, const byte* data, int length, int sender );
    113     bool handleRequestSync( int& i, const byte* data, int length, int sender );
    114106    bool handleRequestPNodePath(int& i, const byte* data, int length, int sender);
    115107    bool handleSendPNodePath(int& i, const byte* data, int length, int sender);
     
    124116    void executeRemoveEntity(int uniqueID);
    125117    void doRemoveEntity(int uniqueID);
    126 
    127     void executeRequestSync( int uniqueID, int user );
    128     void doRequestSync(int uniqueID, int userID);
    129118
    130119    void doYouAre( int uniqueID );
  • branches/network/src/lib/network/network_stream.cc

    r6959 r7444  
    250250
    251251        reciever = 0;
    252         dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever);
     252#warning fix this
     253dataLength = 0;
     254//TODO fix
     255        //dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever);
    253256
    254257        if ( dataLength<=0 ){
     
    345348        for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
    346349        {
     350#warning fix this
     351#if 0
    347352          if ( *it && (*it)->getUniqueID()==header.synchronizeableID )
    348353          {
     
    354359            continue;
    355360          }
     361#endif
    356362        }
    357363
     
    388394    if ( clientId > this->maxConnections )
    389395    {
    390       handshakes[clientId]->doReject();
     396      handshakes[clientId]->doReject( "too many connections" );
    391397      PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId);
    392398    }
  • branches/network/src/lib/network/synchronizeable.cc

    r6753 r7444  
    3636  this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable");
    3737  this->owner = -1;
    38   this->state = 0;
    3938  this->hostID = SharedNetworkData::getInstance()->getHostID();
    4039  this->setIsServer(this->hostID == 0);
    4140  this->uniqueID = NET_UID_UNASSIGNED;
    4241  this->networkStream = NULL;
    43   this->setRequestedSync( false );
    44   this->setIsOutOfSync( !(this->isServer()) );
    4542  this->bSynchronize = false;
    4643
     
    5249    this->setUniqueID(SharedNetworkData::getInstance()->getNewUniqueID());
    5350  }
     51 
     52  this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) );
    5453}
    5554
     
    6463    this->networkStream->disconnectSynchronizeable(*this);
    6564}
    66 
    67 
    68 /**
    69  *  write data to NetworkStream
    70  */
    71 int Synchronizeable::writeBytes(const byte* data, int length, int sender)
    72 {
    73   PRINTF(5)("Synchronizeable::writeBytes was called\n");
    74 }
    75 
    76 
    77 /**
    78  *  read data from NetworkStream
    79  */
    80 int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever)
    81 {
    82   PRINTF(5)("Synchronizeable::readBytes was called\n");
    83 }
    84 
    85 
    86 void Synchronizeable::writeDebug() const
    87 {}
    88 
    89 
    90 void Synchronizeable::readDebug() const
    91 {}
    92 
    9365
    9466/**
     
    10678
    10779/**
    108  * Sets the outofsync flag to a given value
    109  * @param outOfSync: the boolean value which the outofsync flag is to set to
    110  */
    111 void Synchronizeable::setIsOutOfSync(bool outOfSync)
    112 {
    113   if( outOfSync )
    114     this->state = this->state | STATE_OUTOFSYNC;
    115   else
    116     this->state = this->state & (~STATE_OUTOFSYNC);
    117   //PRINTF(0)("isoutofsync %s %d\n", this->getClassName(), state);
    118 }
    119 
    120 
    121 /**
    12280 * Determines if the server flag is set
    12381 * @return true, if the server flag is true, false else
     
    13088
    13189/**
    132  * Determines if the outofsync flag is set
    133  * @return true, if the outofsync flag is true, false else
     90 * get the diff to last acked state of userId
     91 * @param userId user to create diff for
     92 * @param data buffer to copy diff in
     93 * @param maxLength max bytes to copy into data
     94 * @param stateId id of current state
     95 * @param priorityTH tells getStateDiff to not send element with priority \< priorityTH
     96 * @return n bytes copied into data
    13497 */
    135 bool Synchronizeable::isOutOfSync()
     98int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int priorityTH )
    13699{
    137   return (this->state & STATE_OUTOFSYNC) >0;
     100#warning implement this
     101}
     102
     103/**
     104 * sets a new state out of a diff created on another host
     105 * @param userId hostId of user who send me that diff
     106 * @param data pointer to diff
     107 * @param length length of diff
     108 * @param stateId id of current state
     109 * @return true on success
     110 */
     111bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId )
     112{
     113#warning implement this
     114}
     115
     116 /**
     117 * override this function to be notified on change
     118 * of your registred variables.
     119 * @param id id's which have changed
     120 */
     121void varChangeHandler( std::list<int> & id )
     122{
     123#warning implement this
     124}
     125
     126/**
     127 * registers a varable to be synchronized over network
     128 * @param var see src/lib/network/synchronizeable_var/ for available classes
     129 */
     130void Synchronizeable::registerVar( SynchronizeableVar * var )
     131{
     132#warning implement this
     133}
     134
     135/**
     136 * registers a varable to be synchronized over network
     137 * return value is passed to varChangeHandler on change
     138 * @param var see src/lib/network/synchronizeable_var/ for available classes
     139 * @return handle passed to varChangeHandler on changes
     140 */
     141int Synchronizeable::registerVarId( SynchronizeableVar * var )
     142{
     143#warning implement this
    138144}
    139145
    140146
    141 /**
    142  * Determines if the requestedSync flag is set
    143  * @return true, if the requestedSync flag is true, false else
    144  */
    145 bool Synchronizeable::requestedSync()
    146 {
    147   return (this->state & STATE_REQUESTEDSYNC) >0;
    148 }
    149 
    150 
    151 /**
    152  * Sets the requestedsync flag to a given value
    153  * @param requestedSync: the boolean value which the requestedsync flag is to set to
    154  */
    155 void Synchronizeable::setRequestedSync( bool requestedSync )
    156 {
    157   if( requestedSync )
    158     this->state = this->state | STATE_REQUESTEDSYNC;
    159   else
    160     this->state = this->state & (~STATE_REQUESTEDSYNC);
    161 }
    162 
    163 
    164 
  • branches/network/src/lib/network/synchronizeable.h

    r7230 r7444  
    11/*!
    2  * @file connection_monitor.h
     2 * @file synchronizeable.h
    33    \brief interface for all classes that have to be synchronized
    44 */
     
    1010#include "netdefs.h"
    1111#include "converter.h"
    12 
     12#include "vector.h"
     13#include "quaternion.h"
     14#include "synchronizeable_var/synchronizeable_var.h"
     15#include "synchronizeable_var/synchronizeable_vector.h"
     16#include "synchronizeable_var/synchronizeable_quaternion.h"
     17#include "synchronizeable_var/synchronizeable_string.h"
     18#include "synchronizeable_var/synchronizeable_int.h"
     19#include "synchronizeable_var/synchronizeable_float.h"
     20#include "synchronizeable_var/synchronizeable_bool.h"
     21#include "synchronizeable_var/synchronizeable_uint.h"
    1322
    1423
     
    1827//State constants: They have to be of the form 2^n
    1928#define STATE_SERVER 1
    20 #define STATE_OUTOFSYNC 2
    21 #define STATE_REQUESTEDSYNC 4
    2229
    2330enum {
    24   NWT_SS_WE_STATE = 1000000,
    25   NWT_SS_B,
    26   NWT_SS_FLAGS,
    27   NWT_SS_MOUSEDIRX,
    28   NWT_SS_MOUSEDIRY,
    29   NWT_SS_MOUSEDIRZ,
    30   NWT_SS_MOUSEDIRW,
    31   NWT_SS_PN_SYNC,
    32   NWT_SS_VELX,
    33   NWT_SS_VELY,
    34   NWT_SS_VELZ,
    35   NWT_SS_PL_SYNC,
    36   NWT_SS_CO_N,
    37   NWT_SS_CO_CLID,
    38 
    39   NWT_HS_HOST_ID,
    40   NWT_HS_NGM_ID,
    41 
    42   NWT_PL_B,
    43   NWT_PL_FLAGS,
    44   NWT_PL_SCORE,
    45 
    46   NWT_PN_BO_WRITESTATE,
    47   NWT_PN_PARENTMODE,
    48   NWT_PN_COORX,
    49   NWT_PN_COORY,
    50   NWT_PN_COORZ,
    51   NWT_PN_ROTX,
    52   NWT_PN_ROTY,
    53   NWT_PN_ROTZ,
    54   NWT_PN_ROTV,
    55 
    56   NWT_PN_FLAGS,
    57   NWT_PN_SCOORX,
    58   NWT_PN_SCOORY,
    59   NWT_PN_SCOORZ,
    60   NWT_PN_SROTX,
    61   NWT_PN_SROTY,
    62   NWT_PN_SROTZ,
    63   NWT_PN_SROTV,
    64 
    65   NWT_BO_NAME,
    66 
    67   NWT_WE_PN_WRITESTATE,
    68   NWT_WE_PN_MODELFILENAME,
    69   NWT_WE_PN_SCALING,
    70 
    71   NWT_GT_WE_STATE,
    72 
    73   NWT_SB_WE_STATE,
    74   NWT_SB_SIZE,
    75   NWT_SB_TEXTURENAME,
    76 
    77   NWT_TER_WE_STATE,
    78 
    79   NWT_PU_WE_STATE,
    80 
    81   NWT_TPU_WE_STATE,
    82 
    83   NWT_LPU_WE_STATE,
    84 
    85   NWT_WPU_WE_STATE,
    86 
    87   NWT_PPU_WE_STATE,
    88   NWT_PPU_TYPE,
    89   NWT_PPU_VALUE,
    90   NWT_PPU_MINVALUE,
    91   NWT_PPU_MAXVALUE,
    92 
    93   NWT_WAT_STATE,
    94   NWT_WAT_WE_STATE,
    95   NWT_WAT_SIZEX,
    96   NWT_WAT_SIZEY,
    97   NWT_WAT_RESX,
    98   NWT_WAT_RESY,
    99   NWT_WAT_HEIGHT
     31  PERMISSION_OWNER = 1,
     32  PERMISSION_ALL   = 2
    10033};
    10134
    102 
    103 //macros to help writing data in byte buffer
    104 /*
    105  * Important: these macros must be used in
    106  *     SYNCHELP_READ_*:  virtual void      writeBytes(const byte* data, int length, int sender);
    107  *     SYNCHELP_WRITE_*: virtual int       readBytes(byte* data, int maxLength, int * reciever);
    108  * with the same argument names!
    109  *
    110  * id is one int out of that enum on top of this comment it is used to identify
    111  * read/write. when you read a value you have to use exactly the same as you used
    112  * to write or you will see an assertion failing.
    113  *
    114  * SYNCHELP_WRITE_BEGIN()
    115  * SYNCHELP_WRITE_INT(i,id)
    116  * SYNCHELP_WRITE_FLOAT(f,id)
    117  * SYNCHELP_WRITE_BYTE(b,id)
    118  * SYNCHELP_WRITE_STRING(s,id)
    119  * SYNCHELP_WRITE_N
    120  *
    121  * SYNCHELP_READ_BEGIN()
    122  * SYNCHELP_READ_INT(i,id)
    123  * SYNCHELP_READ_FLOAT(f,id)
    124  * SYNCHELP_READ_STRING(s,l,id) l = size of buffer s
    125  * SYNCHELP_READ_STRINGM(s,id)  allocates memory for string! you have to delete this later
    126  * SYNCHELP_READ_BYTE(b,id)
    127  * SYNCHELP_READ_REMAINING() returns the remaining buffer size
    128  * SYNCHELP_READ_NEXTBYTE()  reads the next byte but it is not removed from the buffer
    129  * SYNCHELP_READ_N
    130  *
    131  *
    132  *
    133  * Example 1:
    134  *  SYNCHELP_READ_BEGIN();
    135  *  SYNCHELP_READ_FLOAT(size);
    136  *  SYNCHELP_READ_STRING( textureName, 1024 ); //1024 is the length of textureName
    137  *  delete[] textureName;
    138  *  textureName = NULL;
    139  *  SYNCHELP_READ_STRINGM( texturename );      //this will call new char[strlen()+1]
    140  *
    141  * Example 2:
    142  *  SYNCHELP_WRITE_BEGIN();
    143  *  SYNCHELP_WRITE_FLOAT(this->size);
    144  *  SYNCHELP_WRITE_STRING(this->textureName);
    145  *  return SYNCHELP_WRITE_N;
    146  *
    147  */
    148 
    149 #define SYNCHELP_WRITE_DEBUG(n) {\
    150   __synchelp_write_n = Converter::intToByteArray( n, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
    151   assert( __synchelp_write_n == INTSIZE ); \
    152   __synchelp_write_i += __synchelp_write_n; \
    153 }
    154 
    155 #define SYNCHELP_READ_DEBUG(n) {  \
    156   int nn; \
    157   __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &nn );  \
    158   assert( __synchelp_read_n == INTSIZE ); \
    159   if ( n != nn ) { \
    160     PRINTF(1)("Check your code! read/writes not in right order! read %d instead of %d\n", nn, n); \
    161     assert( false ); \
    162   } \
    163   __synchelp_read_i += __synchelp_read_n; \
    164 }
    165 
    166 #define SYNCHELP_WRITE_BEGIN()    int __synchelp_write_i = 0; \
    167                                   int __synchelp_write_n
    168 #define SYNCHELP_WRITE_INT(i,n) { SYNCHELP_WRITE_DEBUG(n); \
    169                                 __synchelp_write_n = \
    170                                 Converter::intToByteArray( i, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
    171                                 assert( __synchelp_write_n == INTSIZE ); \
    172                                 if ( __synchelp_write_n <= 0) \
    173 { \
    174                                   PRINTF(1)("Buffer is too small to store a int\n"); \
    175                                   return 0; \
    176 } \
    177                                 __synchelp_write_i += __synchelp_write_n; \
    178 }
    179 #define SYNCHELP_WRITE_FLOAT(f,n) { SYNCHELP_WRITE_DEBUG(n); \
    180                                 __synchelp_write_n = \
    181                                 Converter::floatToByteArray( f, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
    182                                 assert( __synchelp_write_n == FLOATSIZE ); \
    183                                 if ( __synchelp_write_n <= 0) \
    184 { \
    185                                   PRINTF(1)("Buffer is too small to store a float\n"); \
    186                                   return 0; \
    187 } \
    188                                 __synchelp_write_i += __synchelp_write_n; \
    189 }
    190 #define SYNCHELP_WRITE_BYTE(b,n) { SYNCHELP_WRITE_DEBUG(n); \
    191                                 \
    192                                 if (maxLength - __synchelp_write_i < 1) \
    193 { \
    194                                   PRINTF(1)("Buffer is too small to store string\n"); \
    195                                   return 0; \
    196 } \
    197                                 data[__synchelp_write_i] = b; \
    198                                 __synchelp_write_i++; \
    199 }
    200 #define SYNCHELP_WRITE_STRING(s,n) { SYNCHELP_WRITE_DEBUG(n); \
    201                                 __synchelp_write_n = \
    202                                 Converter::stringToByteArray( s, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
    203                                 assert( __synchelp_write_n == ((std::string)s).length()+INTSIZE ); \
    204                                 if ( __synchelp_write_n <= 0) \
    205 { \
    206                                   PRINTF(1)("Buffer is too small to store string\n"); \
    207                                   return 0; \
    208 } \
    209                                 __synchelp_write_i += __synchelp_write_n; \
    210 }
    211 #define SYNCHELP_WRITE_N        __synchelp_write_i
    212 #define SYNCHELP_WRITE_FKT(f,n)   { SYNCHELP_WRITE_DEBUG(n); \
    213                                   PRINTF(0)("calling %s with %d left\n", #f, maxLength - __synchelp_write_i);  \
    214                                   byte * spos = data+__synchelp_write_i;  \
    215                                   if (maxLength - __synchelp_write_i < INTSIZE) \
    216 { \
    217                                     PRINTF(1)("Buffer is too small to store more data\n"); \
    218                                     return 0; \
    219 } \
    220                                   __synchelp_write_i += INTSIZE; \
    221                                   __synchelp_write_n = \
    222                                   f( data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
    223                                   __synchelp_write_i += __synchelp_write_n;  \
    224                                   Converter::intToByteArray( __synchelp_write_n, spos, INTSIZE ); \
    225                                 }
    226 
    227 
    228 #define SYNCHELP_READ_BEGIN()     int __synchelp_read_i = 0; \
    229                                   int __synchelp_read_n
    230 
    231 #define SYNCHELP_READ_INT(i,n)       { SYNCHELP_READ_DEBUG(n); \
    232                                     if ( length-__synchelp_read_i < INTSIZE ) \
    233 { \
    234                                       PRINTF(1)("There is not enough data to read an int\n");  \
    235                                       return 0; \
    236 } \
    237                                     __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &i );  \
    238                                     assert( __synchelp_read_n == INTSIZE ); \
    239                                     __synchelp_read_i += __synchelp_read_n; \
    240 }
    241 #define SYNCHELP_READ_FLOAT(f,n)    { SYNCHELP_READ_DEBUG(n); \
    242                                     if ( length-__synchelp_read_i < FLOATSIZE ) \
    243 { \
    244                                       PRINTF(1)("There is not enough data to read a flaot\n");  \
    245                                       return 0; \
    246 } \
    247                                     __synchelp_read_n = Converter::byteArrayToFloat( data+__synchelp_read_i, &f );  \
    248                                     assert( __synchelp_read_n == FLOATSIZE ) ;\
    249                                     __synchelp_read_i += __synchelp_read_n; \
    250 }
    251 #define SYNCHELP_READ_STRING(s,n)    {SYNCHELP_READ_DEBUG(n); \
    252                                     __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, length-__synchelp_read_i );  \
    253                                     assert( __synchelp_read_n == s.length()+INTSIZE ) ;\
    254                                     if ( __synchelp_read_n <0 )  \
    255 { \
    256                                       PRINTF(1)("There is not enough data to read string\n");  \
    257                                       return 0; \
    258 } \
    259                                     __synchelp_read_i += __synchelp_read_n; \
    260 }
    261 #if 0 //not needed any more
    262 #define SYNCHELP_READ_STRINGM(s,n)    { SYNCHELP_READ_DEBUG(n); \
    263                                     __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s );  \
    264                                     assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\
    265                                     if ( __synchelp_read_n <0 )  \
    266 { \
    267                                       PRINTF(1)("There is not enough data to read string\n");  \
    268                                       return 0; \
    269 } \
    270                                     __synchelp_read_i += __synchelp_read_n; \
    271 }
    272 #endif
    273 #define SYNCHELP_READ_BYTE(b,n)      { SYNCHELP_READ_DEBUG(n); \
    274                                     if ( length-__synchelp_read_i < 1 ) \
    275 { \
    276                                       PRINTF(1)("There is not enough data to read a byte\n");  \
    277                                       return 0; \
    278 } \
    279                                     b = data[__synchelp_read_i]; \
    280                                     __synchelp_read_i ++;  \
    281 }
    282 #define SYNCHELP_READ_FKT(f,n)   { SYNCHELP_READ_DEBUG(n); \
    283                                   int s; \
    284                                   if ( length-__synchelp_read_i < INTSIZE ) \
    285 { \
    286                                       PRINTF(1)("There is not enough data to read an int\n");  \
    287                                       return 0; \
    288 } \
    289                                     __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &s );  \
    290                                     assert( __synchelp_read_n == INTSIZE ); \
    291                                     __synchelp_read_i += __synchelp_read_n; \
    292                                   __synchelp_read_i += \
    293                                   f( data+__synchelp_read_i, s, sender); \
    294                                   }
    295 #define SYNCHELP_READ_REMAINING() ( length-__synchelp_read_i )
    296 #define SYNCHELP_READ_NEXTBYTE() ( data[__synchelp_read_i] )
    297 #define SYNCHELP_READ_N           __synchelp_read_i
     35typedef std::vector<SynchronizeableVar> SyncVarList;
    29836
    29937class NetworkStream;
    300 
    30138
    30239class Synchronizeable : virtual public BaseObject
     
    30744    virtual ~Synchronizeable();
    30845
    309     virtual int       writeBytes(const byte* data, int length, int sender);
    310     virtual int       readBytes(byte* data, int maxLength, int * reciever);
    311     virtual void      writeDebug() const;
    312     virtual void      readDebug() const;
    313 
    31446    void setIsServer( bool isServer );
    315     void setIsOutOfSync( bool outOfSync );
    316     void setRequestedSync( bool requestedSync );
    31747    bool isServer();
    318     bool isOutOfSync();
    319     bool requestedSync();
    320 
     48   
     49    virtual void varChangeHandler( std::list<int> & id );
     50   
     51    int getStateDiff( int userId, byte* data, int maxLength, int stateId, int priorityTH );
     52    bool setStateDiff( int userId, byte* data, int length, int stateId );
     53   
     54    void registerVar( SynchronizeableVar * var );
     55    int registerVarId( SynchronizeableVar * var );
     56   
    32157    inline void setUniqueID( int id ){ uniqueID = id; }
    32258    inline int  getUniqueID() const { return uniqueID; }
     
    33167    inline void setSynchronized(bool bSynchronize) { this->bSynchronize = bSynchronize; }
    33268
    333     inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
    334     inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } };
    335 
    33669    inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; }
    33770    inline NetworkStream* getNetworkStream() { return this->networkStream; }
     
    33972
    34073  protected:
    341     NetworkStream*    networkStream;
     74    NetworkStream*    networkStream;  //!< reference network stream we are connected to
    34275    int               state;
    34376
    344 
    34577  private:
    346     int               uniqueID;
    347     int               owner;
    348     int               hostID;
    349     bool              bSynchronize;
    350 
    351     std::list<int>    synchronizeRequests;
     78    int               uniqueID;       //!< unique id assigned to synchronizeable
     79    int               owner;          //!< hostId of owner ( 0 if none / server )
     80    int               hostID;         //!< my own host id
     81    bool              bSynchronize;   //!< do we need beeing synchronized?
    35282
    35383};
Note: See TracChangeset for help on using the changeset viewer.