Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/synchronizeable.h @ 6747

Last change on this file since 6747 was 6747, checked in by rennerc, 18 years ago

enabled smooth spaceship movement

File size: 8.2 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
23
24//macros to help writing data in byte buffer
25/*
26 * Important: these macros must be used in
27 *     SYNCHELP_READ_*:  virtual void      writeBytes(const byte* data, int length, int sender);
28 *     SYNCHELP_WRITE_*: virtual int       readBytes(byte* data, int maxLength, int * reciever);
29 * with the same argument names!
30 *
31 * SYNCHELP_WRITE_BEGIN()
32 * SYNCHELP_WRITE_INT(i)
33 * SYNCHELP_WRITE_FLOAT(f)
34 * SYNCHELP_WRITE_BYTE(b)
35 * SYNCHELP_WRITE_STRING(s)
36 * SYNCHELP_WRITE_N
37 *
38 * SYNCHELP_READ_BEGIN()
39 * SYNCHELP_READ_INT(i)
40 * SYNCHELP_READ_FLOAT(f)
41 * SYNCHELP_READ_STRING(s,l) l = size of buffer s
42 * SYNCHELP_READ_STRINGM(s)  allocates memory for string! you have to free this after
43 * SYNCHELP_READ_BYTE(b)
44 * SYNCHELP_READ_N
45 *
46 *
47 *
48 * Example 1:
49 *  SYNCHELP_READ_BEGIN();
50 *  SYNCHELP_READ_FLOAT(size);
51 *  SYNCHELP_READ_STRING( textureName, 1024 ); //1024 is the length of textureName
52 *
53 * Example 2:
54 *  SYNCHELP_WRITE_BEGIN();
55 *  SYNCHELP_WRITE_FLOAT(this->size);
56 *  SYNCHELP_WRITE_STRING(this->textureName);
57 *  return SYNCHELP_WRITE_N;
58 *
59 */
60#define SYNCHELP_WRITE_BEGIN()    int __synchelp_write_i = 0; \
61                                  int __synchelp_write_n
62#define SYNCHELP_WRITE_INT(i) { __synchelp_write_n = \
63                                Converter::intToByteArray( i, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
64                                if ( __synchelp_write_n <= 0) \
65{ \
66                                  PRINTF(1)("Buffer is too small to store a int\n"); \
67                                  return 0; \
68} \
69assert( __synchelp_write_n == INTSIZE ); \
70                                __synchelp_write_i += __synchelp_write_n; \
71}
72#define SYNCHELP_WRITE_FLOAT(f) { __synchelp_write_n = \
73                                Converter::floatToByteArray( f, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
74                                if ( __synchelp_write_n <= 0) \
75{ \
76                                  PRINTF(1)("Buffer is too small to store a float\n"); \
77                                  return 0; \
78} \
79                                __synchelp_write_i += __synchelp_write_n; \
80}
81#define SYNCHELP_WRITE_BYTE(b) { \
82                                if (maxLength - __synchelp_write_i < 1) \
83{ \
84                                  PRINTF(1)("Buffer is too small to store string\n"); \
85                                  return 0; \
86} \
87                                data[__synchelp_write_i] = b; \
88                                __synchelp_write_i++; \
89}
90#define SYNCHELP_WRITE_STRING(s) { if (s!=NULL) \
91                                __synchelp_write_n = \
92                                Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \
93                                else \
94                                __synchelp_write_n = \
95                                Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \
96                                if ( __synchelp_write_n <= 0) \
97{ \
98                                  PRINTF(1)("Buffer is too small to store string\n"); \
99                                  return 0; \
100} \
101                                __synchelp_write_i += __synchelp_write_n; \
102}
103#define SYNCHELP_WRITE_N        __synchelp_write_i
104#define SYNCHELP_WRITE_FKT(f)   { \
105                                  __synchelp_write_i += \
106                                  f( data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
107                                }
108
109
110#define SYNCHELP_READ_BEGIN()     int __synchelp_read_i = 0; \
111                                  int __synchelp_read_n
112
113#define SYNCHELP_READ_INT(i)       { \
114                                    if ( length-__synchelp_read_i < INTSIZE ) \
115{ \
116                                      PRINTF(1)("There is not enough data to read an int\n");  \
117                                      return 0; \
118} \
119                                    __synchelp_read_i += Converter::byteArrayToInt( data+__synchelp_read_i, &i );  \
120}
121#define SYNCHELP_READ_FLOAT(f)    { \
122                                    if ( length-__synchelp_read_i < FLOATSIZE ) \
123{ \
124                                      PRINTF(1)("There is not enough data to read a flaot\n");  \
125                                      return 0; \
126} \
127                                    __synchelp_read_i += Converter::byteArrayToFloat( data+__synchelp_read_i, &f );  \
128}
129#define SYNCHELP_READ_STRING(s,l)    { \
130                                    __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l );  \
131                                    if ( __synchelp_read_n <0 )  \
132{ \
133                                      PRINTF(1)("There is not enough data to read string\n");  \
134                                      return 0; \
135} \
136                                    __synchelp_read_i += __synchelp_read_n; \
137}
138#define SYNCHELP_READ_STRINGM(s)    { \
139                                    __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s );  \
140                                    if ( __synchelp_read_n <0 )  \
141{ \
142                                      PRINTF(1)("There is not enough data to read string\n");  \
143                                      return 0; \
144} \
145                                    __synchelp_read_i += __synchelp_read_n; \
146}
147#define SYNCHELP_READ_BYTE(b)      { \
148                                    if ( length-__synchelp_read_i < 1 ) \
149{ \
150                                      PRINTF(1)("There is not enough data to read a byte\n");  \
151                                      return 0; \
152} \
153                                    b = data[__synchelp_read_i]; \
154                                    __synchelp_read_i ++;  \
155}
156#define SYNCHELP_READ_FKT(f)   { \
157                                  __synchelp_read_i += \
158                                  f( data+__synchelp_read_i, length-__synchelp_read_i, sender); \
159                                  }
160#define SYNCHELP_READ_N           __synchelp_read_i
161
162class NetworkStream;
163
164
165class Synchronizeable : virtual public BaseObject
166{
167
168  public:
169    Synchronizeable();
170    virtual ~Synchronizeable();
171
172    virtual int       writeBytes(const byte* data, int length, int sender);
173    virtual int       readBytes(byte* data, int maxLength, int * reciever);
174    virtual void      writeDebug() const;
175    virtual void      readDebug() const;
176
177    void setIsServer( bool isServer );
178    void setIsOutOfSync( bool outOfSync );
179    void setRequestedSync( bool requestedSync );
180    bool isServer();
181    bool isOutOfSync();
182    bool requestedSync();
183
184    inline void setUniqueID( int id ){ uniqueID = id; }
185    inline int  getUniqueID() const { return uniqueID; }
186    inline int getHostID() { return this->hostID; }
187
188    inline int getOwner(){ return owner; }
189    inline void setOwner(int owner){ this->owner = owner; }
190
191    /** @returns true if this Synchronizeable has to be synchronized over network */
192    inline bool beSynchronized() { return this->bSynchronize; }
193    /** @param bSynchronize sets the Synchronizeable to be sunchronized or not */
194    inline void setSynchronized(bool bSynchronize) { this->bSynchronize = bSynchronize; }
195
196    inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
197    inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } };
198
199    inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; }
200    inline NetworkStream* getNetworkStream() { return this->networkStream; }
201
202
203  protected:
204    NetworkStream*    networkStream;
205    int               state;
206
207
208  private:
209    int               uniqueID;
210    int               owner;
211    int               hostID;
212    bool              bSynchronize;
213
214    std::list<int>    synchronizeRequests;
215
216};
217#endif /* _SYNCHRONIZEABLE_H */
Note: See TracBrowser for help on using the repository browser.