Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/synchronizeable.h @ 7210

Last change on this file since 7210 was 6959, checked in by patrick, 19 years ago

trunk: merged network branche into trunk

File size: 11.7 KB
RevLine 
[5523]1/*!
2 * @file connection_monitor.h
[5550]3    \brief interface for all classes that have to be synchronized
[5547]4 */
[5523]5
[5547]6#ifndef _SYNCHRONIZEABLE_H
7#define _SYNCHRONIZEABLE_H
[5523]8
[5997]9#include "base_object.h"
[5547]10#include "netdefs.h"
[6341]11#include "converter.h"
[5547]12
[5997]13
14
15#include <vector>
16#include <list>
17
18//State constants: They have to be of the form 2^n
19#define STATE_SERVER 1
20#define STATE_OUTOFSYNC 2
[6341]21#define STATE_REQUESTEDSYNC 4
[5997]22
[6815]23enum {
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,
[6868]35  NWT_SS_PL_SYNC,
[6959]36  NWT_SS_CO_N,
37  NWT_SS_CO_CLID,
38
[6815]39  NWT_HS_HOST_ID,
40  NWT_HS_NGM_ID,
[6959]41
42  NWT_PL_B,
[6868]43  NWT_PL_FLAGS,
[6959]44  NWT_PL_SCORE,
45
[6815]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,
[6959]55
[6815]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,
[6959]64
[6815]65  NWT_BO_NAME,
[6959]66
[6815]67  NWT_WE_PN_WRITESTATE,
68  NWT_WE_PN_MODELFILENAME,
69  NWT_WE_PN_SCALING,
[6959]70
[6815]71  NWT_GT_WE_STATE,
[6959]72
[6815]73  NWT_SB_WE_STATE,
74  NWT_SB_SIZE,
75  NWT_SB_TEXTURENAME,
[6959]76
[6815]77  NWT_TER_WE_STATE,
[6959]78
[6815]79  NWT_PU_WE_STATE,
[6959]80
[6815]81  NWT_TPU_WE_STATE,
[6959]82
[6815]83  NWT_LPU_WE_STATE,
[6959]84
[6815]85  NWT_WPU_WE_STATE,
[6959]86
[6815]87  NWT_PPU_WE_STATE,
88  NWT_PPU_TYPE,
89  NWT_PPU_VALUE,
90  NWT_PPU_MINVALUE,
91  NWT_PPU_MAXVALUE,
[6341]92
[6815]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
100};
101
102
[6341]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 *
[6868]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 *
[6341]114 * SYNCHELP_WRITE_BEGIN()
[6868]115 * SYNCHELP_WRITE_INT(i,id)
116 * SYNCHELP_WRITE_FLOAT(f,id)
117 * SYNCHELP_WRITE_BYTE(b,id)
118 * SYNCHELP_WRITE_STRING(s,id)
[6341]119 * SYNCHELP_WRITE_N
120 *
121 * SYNCHELP_READ_BEGIN()
[6868]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
[6341]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
[6868]137 *  delete[] textureName;
138 *  textureName = NULL;
[6959]139 *  SYNCHELP_READ_STRINGM( texturename );      //this will call new char[strlen()+1]
[6341]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 */
[6959]148
[6815]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
[6341]166#define SYNCHELP_WRITE_BEGIN()    int __synchelp_write_i = 0; \
167                                  int __synchelp_write_n
[6815]168#define SYNCHELP_WRITE_INT(i,n) { SYNCHELP_WRITE_DEBUG(n); \
169                                __synchelp_write_n = \
[6341]170                                Converter::intToByteArray( i, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
[6753]171                                assert( __synchelp_write_n == INTSIZE ); \
[6341]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}
[6815]179#define SYNCHELP_WRITE_FLOAT(f,n) { SYNCHELP_WRITE_DEBUG(n); \
180                                __synchelp_write_n = \
[6341]181                                Converter::floatToByteArray( f, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
[6753]182                                assert( __synchelp_write_n == FLOATSIZE ); \
[6341]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}
[6815]190#define SYNCHELP_WRITE_BYTE(b,n) { SYNCHELP_WRITE_DEBUG(n); \
191                                \
[6341]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}
[6815]200#define SYNCHELP_WRITE_STRING(s,n) { SYNCHELP_WRITE_DEBUG(n); \
201                                if (s!=NULL) {\
[6341]202                                __synchelp_write_n = \
203                                Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \
[6753]204                                assert( __synchelp_write_n == strlen(s)+INTSIZE ); \
205                                } else {\
[6341]206                                __synchelp_write_n = \
207                                Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \
[6753]208                                assert( __synchelp_write_n == strlen("")+INTSIZE ); } \
[6341]209                                if ( __synchelp_write_n <= 0) \
210{ \
211                                  PRINTF(1)("Buffer is too small to store string\n"); \
212                                  return 0; \
213} \
214                                __synchelp_write_i += __synchelp_write_n; \
215}
216#define SYNCHELP_WRITE_N        __synchelp_write_i
[6815]217#define SYNCHELP_WRITE_FKT(f,n)   { SYNCHELP_WRITE_DEBUG(n); \
[6341]218                                  __synchelp_write_i += \
219                                  f( data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
220                                }
221
222
223#define SYNCHELP_READ_BEGIN()     int __synchelp_read_i = 0; \
224                                  int __synchelp_read_n
225
[6815]226#define SYNCHELP_READ_INT(i,n)       { SYNCHELP_READ_DEBUG(n); \
[6341]227                                    if ( length-__synchelp_read_i < INTSIZE ) \
228{ \
229                                      PRINTF(1)("There is not enough data to read an int\n");  \
230                                      return 0; \
231} \
[6753]232                                    __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &i );  \
233                                    assert( __synchelp_read_n == INTSIZE ); \
234                                    __synchelp_read_i += __synchelp_read_n; \
[6341]235}
[6815]236#define SYNCHELP_READ_FLOAT(f,n)    { SYNCHELP_READ_DEBUG(n); \
[6341]237                                    if ( length-__synchelp_read_i < FLOATSIZE ) \
238{ \
239                                      PRINTF(1)("There is not enough data to read a flaot\n");  \
240                                      return 0; \
241} \
[6753]242                                    __synchelp_read_n = Converter::byteArrayToFloat( data+__synchelp_read_i, &f );  \
243                                    assert( __synchelp_read_n == FLOATSIZE ) ;\
244                                    __synchelp_read_i += __synchelp_read_n; \
[6341]245}
[6815]246#define SYNCHELP_READ_STRING(s,l,n)    {SYNCHELP_READ_DEBUG(n); \
[6341]247                                    __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l );  \
[6753]248                                    assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\
[6341]249                                    if ( __synchelp_read_n <0 )  \
250{ \
251                                      PRINTF(1)("There is not enough data to read string\n");  \
252                                      return 0; \
253} \
254                                    __synchelp_read_i += __synchelp_read_n; \
255}
[6815]256#define SYNCHELP_READ_STRINGM(s,n)    { SYNCHELP_READ_DEBUG(n); \
[6341]257                                    __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s );  \
[6753]258                                    assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\
[6341]259                                    if ( __synchelp_read_n <0 )  \
260{ \
261                                      PRINTF(1)("There is not enough data to read string\n");  \
262                                      return 0; \
263} \
264                                    __synchelp_read_i += __synchelp_read_n; \
265}
[6815]266#define SYNCHELP_READ_BYTE(b,n)      { SYNCHELP_READ_DEBUG(n); \
[6341]267                                    if ( length-__synchelp_read_i < 1 ) \
268{ \
269                                      PRINTF(1)("There is not enough data to read a byte\n");  \
270                                      return 0; \
271} \
272                                    b = data[__synchelp_read_i]; \
273                                    __synchelp_read_i ++;  \
274}
[6815]275#define SYNCHELP_READ_FKT(f,n)   { SYNCHELP_READ_DEBUG(n); \
[6341]276                                  __synchelp_read_i += \
277                                  f( data+__synchelp_read_i, length-__synchelp_read_i, sender); \
278                                  }
[6815]279#define SYNCHELP_READ_REMAINING() ( length-__synchelp_read_i )
280#define SYNCHELP_READ_NEXTBYTE() ( data[__synchelp_read_i] )
[6341]281#define SYNCHELP_READ_N           __synchelp_read_i
282
[6139]283class NetworkStream;
[5997]284
[6139]285
[5581]286class Synchronizeable : virtual public BaseObject
[6695]287{
288
[5804]289  public:
[5996]290    Synchronizeable();
[6695]291    virtual ~Synchronizeable();
[5523]292
[6341]293    virtual int       writeBytes(const byte* data, int length, int sender);
[6139]294    virtual int       readBytes(byte* data, int maxLength, int * reciever);
[5806]295    virtual void      writeDebug() const;
296    virtual void      readDebug() const;
[6139]297
298    void setIsServer( bool isServer );
299    void setIsOutOfSync( bool outOfSync );
[6341]300    void setRequestedSync( bool requestedSync );
[5997]301    bool isServer();
302    bool isOutOfSync();
[6341]303    bool requestedSync();
[6695]304
[6341]305    inline void setUniqueID( int id ){ uniqueID = id; }
[6695]306    inline int  getUniqueID() const { return uniqueID; }
[6341]307    inline int getHostID() { return this->hostID; }
[5547]308
[6139]309    inline int getOwner(){ return owner; }
310    inline void setOwner(int owner){ this->owner = owner; }
311
[6695]312    /** @returns true if this Synchronizeable has to be synchronized over network */
313    inline bool beSynchronized() { return this->bSynchronize; }
314    /** @param bSynchronize sets the Synchronizeable to be sunchronized or not */
315    inline void setSynchronized(bool bSynchronize) { this->bSynchronize = bSynchronize; }
[6139]316
[6695]317    inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
318    inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } };
[5523]319
[6695]320    inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; }
321    inline NetworkStream* getNetworkStream() { return this->networkStream; }
[6139]322
323
[6695]324  protected:
325    NetworkStream*    networkStream;
326    int               state;
[6139]327
[6341]328
[6695]329  private:
330    int               uniqueID;
331    int               owner;
332    int               hostID;
333    bool              bSynchronize;
[5547]334
[6695]335    std::list<int>    synchronizeRequests;
[6139]336
[6695]337};
[5548]338#endif /* _SYNCHRONIZEABLE_H */
Note: See TracBrowser for help on using the repository browser.