Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6868 was 6868, checked in by bensch, 18 years ago

orxonox/trunk: merged the Network back to the trunk.
merged with command
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6817:HEAD
no conflicts

File size: 11.7 KB
Line 
1/*!
2 * @file connection_monitor.h
3    \brief interface for all classes that have to be synchronized
4 */
5
6#ifndef _SYNCHRONIZEABLE_H
7#define _SYNCHRONIZEABLE_H
8
9#include "base_object.h"
10#include "netdefs.h"
11#include "converter.h"
12
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
21#define STATE_REQUESTEDSYNC 4
22
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,
35  NWT_SS_PL_SYNC,
36 
37  NWT_HS_HOST_ID,
38  NWT_HS_NGM_ID,
39 
40  NWT_PL_FLAGS,
41 
42  NWT_PN_BO_WRITESTATE,
43  NWT_PN_PARENTMODE,
44  NWT_PN_COORX,
45  NWT_PN_COORY,
46  NWT_PN_COORZ,
47  NWT_PN_ROTX,
48  NWT_PN_ROTY,
49  NWT_PN_ROTZ,
50  NWT_PN_ROTV,
51 
52  NWT_PN_FLAGS,
53  NWT_PN_SCOORX,
54  NWT_PN_SCOORY,
55  NWT_PN_SCOORZ,
56  NWT_PN_SROTX,
57  NWT_PN_SROTY,
58  NWT_PN_SROTZ,
59  NWT_PN_SROTV,
60 
61  NWT_BO_NAME,
62 
63  NWT_WE_PN_WRITESTATE,
64  NWT_WE_PN_MODELFILENAME,
65  NWT_WE_PN_SCALING,
66 
67  NWT_GT_WE_STATE,
68 
69  NWT_SB_WE_STATE,
70  NWT_SB_SIZE,
71  NWT_SB_TEXTURENAME,
72 
73  NWT_TER_WE_STATE,
74 
75  NWT_PU_WE_STATE,
76 
77  NWT_TPU_WE_STATE,
78 
79  NWT_LPU_WE_STATE,
80 
81  NWT_WPU_WE_STATE,
82 
83  NWT_PPU_WE_STATE,
84  NWT_PPU_TYPE,
85  NWT_PPU_VALUE,
86  NWT_PPU_MINVALUE,
87  NWT_PPU_MAXVALUE,
88
89  NWT_WAT_STATE,
90  NWT_WAT_WE_STATE,
91  NWT_WAT_SIZEX,
92  NWT_WAT_SIZEY,
93  NWT_WAT_RESX,
94  NWT_WAT_RESY,
95  NWT_WAT_HEIGHT
96};
97
98
99//macros to help writing data in byte buffer
100/*
101 * Important: these macros must be used in
102 *     SYNCHELP_READ_*:  virtual void      writeBytes(const byte* data, int length, int sender);
103 *     SYNCHELP_WRITE_*: virtual int       readBytes(byte* data, int maxLength, int * reciever);
104 * with the same argument names!
105 *
106 * id is one int out of that enum on top of this comment it is used to identify
107 * read/write. when you read a value you have to use exactly the same as you used
108 * to write or you will see an assertion failing.
109 *
110 * SYNCHELP_WRITE_BEGIN()
111 * SYNCHELP_WRITE_INT(i,id)
112 * SYNCHELP_WRITE_FLOAT(f,id)
113 * SYNCHELP_WRITE_BYTE(b,id)
114 * SYNCHELP_WRITE_STRING(s,id)
115 * SYNCHELP_WRITE_N
116 *
117 * SYNCHELP_READ_BEGIN()
118 * SYNCHELP_READ_INT(i,id)
119 * SYNCHELP_READ_FLOAT(f,id)
120 * SYNCHELP_READ_STRING(s,l,id) l = size of buffer s
121 * SYNCHELP_READ_STRINGM(s,id)  allocates memory for string! you have to delete this later
122 * SYNCHELP_READ_BYTE(b,id)
123 * SYNCHELP_READ_REMAINING() returns the remaining buffer size
124 * SYNCHELP_READ_NEXTBYTE()  reads the next byte but it is not removed from the buffer
125 * SYNCHELP_READ_N
126 *
127 *
128 *
129 * Example 1:
130 *  SYNCHELP_READ_BEGIN();
131 *  SYNCHELP_READ_FLOAT(size);
132 *  SYNCHELP_READ_STRING( textureName, 1024 ); //1024 is the length of textureName
133 *  delete[] textureName;
134 *  textureName = NULL;
135 *  SYNCHELP_READ_STRINGM( texturename );      //this will call new char[strlen()+1]
136 *
137 * Example 2:
138 *  SYNCHELP_WRITE_BEGIN();
139 *  SYNCHELP_WRITE_FLOAT(this->size);
140 *  SYNCHELP_WRITE_STRING(this->textureName);
141 *  return SYNCHELP_WRITE_N;
142 *
143 */
144 
145#define SYNCHELP_WRITE_DEBUG(n) {\
146  __synchelp_write_n = Converter::intToByteArray( n, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
147  assert( __synchelp_write_n == INTSIZE ); \
148  __synchelp_write_i += __synchelp_write_n; \
149}
150
151#define SYNCHELP_READ_DEBUG(n) {  \
152  int nn; \
153  __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &nn );  \
154  assert( __synchelp_read_n == INTSIZE ); \
155  if ( n != nn ) { \
156    PRINTF(1)("Check your code! read/writes not in right order! read %d instead of %d\n", nn, n); \
157    assert( false ); \
158  } \
159  __synchelp_read_i += __synchelp_read_n; \
160}
161
162#define SYNCHELP_WRITE_BEGIN()    int __synchelp_write_i = 0; \
163                                  int __synchelp_write_n
164#define SYNCHELP_WRITE_INT(i,n) { SYNCHELP_WRITE_DEBUG(n); \
165                                __synchelp_write_n = \
166                                Converter::intToByteArray( i, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
167                                assert( __synchelp_write_n == INTSIZE ); \
168                                if ( __synchelp_write_n <= 0) \
169{ \
170                                  PRINTF(1)("Buffer is too small to store a int\n"); \
171                                  return 0; \
172} \
173                                __synchelp_write_i += __synchelp_write_n; \
174}
175#define SYNCHELP_WRITE_FLOAT(f,n) { SYNCHELP_WRITE_DEBUG(n); \
176                                __synchelp_write_n = \
177                                Converter::floatToByteArray( f, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
178                                assert( __synchelp_write_n == FLOATSIZE ); \
179                                if ( __synchelp_write_n <= 0) \
180{ \
181                                  PRINTF(1)("Buffer is too small to store a float\n"); \
182                                  return 0; \
183} \
184                                __synchelp_write_i += __synchelp_write_n; \
185}
186#define SYNCHELP_WRITE_BYTE(b,n) { SYNCHELP_WRITE_DEBUG(n); \
187                                \
188                                if (maxLength - __synchelp_write_i < 1) \
189{ \
190                                  PRINTF(1)("Buffer is too small to store string\n"); \
191                                  return 0; \
192} \
193                                data[__synchelp_write_i] = b; \
194                                __synchelp_write_i++; \
195}
196#define SYNCHELP_WRITE_STRING(s,n) { SYNCHELP_WRITE_DEBUG(n); \
197                                if (s!=NULL) {\
198                                __synchelp_write_n = \
199                                Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \
200                                assert( __synchelp_write_n == strlen(s)+INTSIZE ); \
201                                } else {\
202                                __synchelp_write_n = \
203                                Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \
204                                assert( __synchelp_write_n == strlen("")+INTSIZE ); } \
205                                if ( __synchelp_write_n <= 0) \
206{ \
207                                  PRINTF(1)("Buffer is too small to store string\n"); \
208                                  return 0; \
209} \
210                                __synchelp_write_i += __synchelp_write_n; \
211}
212#define SYNCHELP_WRITE_N        __synchelp_write_i
213#define SYNCHELP_WRITE_FKT(f,n)   { SYNCHELP_WRITE_DEBUG(n); \
214                                  __synchelp_write_i += \
215                                  f( data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
216                                }
217
218
219#define SYNCHELP_READ_BEGIN()     int __synchelp_read_i = 0; \
220                                  int __synchelp_read_n
221
222#define SYNCHELP_READ_INT(i,n)       { SYNCHELP_READ_DEBUG(n); \
223                                    if ( length-__synchelp_read_i < INTSIZE ) \
224{ \
225                                      PRINTF(1)("There is not enough data to read an int\n");  \
226                                      return 0; \
227} \
228                                    __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &i );  \
229                                    assert( __synchelp_read_n == INTSIZE ); \
230                                    __synchelp_read_i += __synchelp_read_n; \
231}
232#define SYNCHELP_READ_FLOAT(f,n)    { SYNCHELP_READ_DEBUG(n); \
233                                    if ( length-__synchelp_read_i < FLOATSIZE ) \
234{ \
235                                      PRINTF(1)("There is not enough data to read a flaot\n");  \
236                                      return 0; \
237} \
238                                    __synchelp_read_n = Converter::byteArrayToFloat( data+__synchelp_read_i, &f );  \
239                                    assert( __synchelp_read_n == FLOATSIZE ) ;\
240                                    __synchelp_read_i += __synchelp_read_n; \
241}
242#define SYNCHELP_READ_STRING(s,l,n)    {SYNCHELP_READ_DEBUG(n); \
243                                    __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l );  \
244                                    assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\
245                                    if ( __synchelp_read_n <0 )  \
246{ \
247                                      PRINTF(1)("There is not enough data to read string\n");  \
248                                      return 0; \
249} \
250                                    __synchelp_read_i += __synchelp_read_n; \
251}
252#define SYNCHELP_READ_STRINGM(s,n)    { SYNCHELP_READ_DEBUG(n); \
253                                    __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s );  \
254                                    assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\
255                                    if ( __synchelp_read_n <0 )  \
256{ \
257                                      PRINTF(1)("There is not enough data to read string\n");  \
258                                      return 0; \
259} \
260                                    __synchelp_read_i += __synchelp_read_n; \
261}
262#define SYNCHELP_READ_BYTE(b,n)      { SYNCHELP_READ_DEBUG(n); \
263                                    if ( length-__synchelp_read_i < 1 ) \
264{ \
265                                      PRINTF(1)("There is not enough data to read a byte\n");  \
266                                      return 0; \
267} \
268                                    b = data[__synchelp_read_i]; \
269                                    __synchelp_read_i ++;  \
270}
271#define SYNCHELP_READ_FKT(f,n)   { SYNCHELP_READ_DEBUG(n); \
272                                  __synchelp_read_i += \
273                                  f( data+__synchelp_read_i, length-__synchelp_read_i, sender); \
274                                  }
275#define SYNCHELP_READ_REMAINING() ( length-__synchelp_read_i )
276#define SYNCHELP_READ_NEXTBYTE() ( data[__synchelp_read_i] )
277#define SYNCHELP_READ_N           __synchelp_read_i
278
279class NetworkStream;
280
281
282class Synchronizeable : virtual public BaseObject
283{
284
285  public:
286    Synchronizeable();
287    virtual ~Synchronizeable();
288
289    virtual int       writeBytes(const byte* data, int length, int sender);
290    virtual int       readBytes(byte* data, int maxLength, int * reciever);
291    virtual void      writeDebug() const;
292    virtual void      readDebug() const;
293
294    void setIsServer( bool isServer );
295    void setIsOutOfSync( bool outOfSync );
296    void setRequestedSync( bool requestedSync );
297    bool isServer();
298    bool isOutOfSync();
299    bool requestedSync();
300
301    inline void setUniqueID( int id ){ uniqueID = id; }
302    inline int  getUniqueID() const { return uniqueID; }
303    inline int getHostID() { return this->hostID; }
304
305    inline int getOwner(){ return owner; }
306    inline void setOwner(int owner){ this->owner = owner; }
307
308    /** @returns true if this Synchronizeable has to be synchronized over network */
309    inline bool beSynchronized() { return this->bSynchronize; }
310    /** @param bSynchronize sets the Synchronizeable to be sunchronized or not */
311    inline void setSynchronized(bool bSynchronize) { this->bSynchronize = bSynchronize; }
312
313    inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
314    inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } };
315
316    inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; }
317    inline NetworkStream* getNetworkStream() { return this->networkStream; }
318
319
320  protected:
321    NetworkStream*    networkStream;
322    int               state;
323
324
325  private:
326    int               uniqueID;
327    int               owner;
328    int               hostID;
329    bool              bSynchronize;
330
331    std::list<int>    synchronizeRequests;
332
333};
334#endif /* _SYNCHRONIZEABLE_H */
Note: See TracBrowser for help on using the repository browser.