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 | enum { |
---|
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 |
---|
100 | }; |
---|
101 | |
---|
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 | if (s!=NULL) {\ |
---|
202 | __synchelp_write_n = \ |
---|
203 | Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \ |
---|
204 | assert( __synchelp_write_n == strlen(s)+INTSIZE ); \ |
---|
205 | } else {\ |
---|
206 | __synchelp_write_n = \ |
---|
207 | Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \ |
---|
208 | assert( __synchelp_write_n == strlen("")+INTSIZE ); } \ |
---|
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 |
---|
217 | #define SYNCHELP_WRITE_FKT(f,n) { SYNCHELP_WRITE_DEBUG(n); \ |
---|
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 | |
---|
226 | #define SYNCHELP_READ_INT(i,n) { SYNCHELP_READ_DEBUG(n); \ |
---|
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 | } \ |
---|
232 | __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &i ); \ |
---|
233 | assert( __synchelp_read_n == INTSIZE ); \ |
---|
234 | __synchelp_read_i += __synchelp_read_n; \ |
---|
235 | } |
---|
236 | #define SYNCHELP_READ_FLOAT(f,n) { SYNCHELP_READ_DEBUG(n); \ |
---|
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 | } \ |
---|
242 | __synchelp_read_n = Converter::byteArrayToFloat( data+__synchelp_read_i, &f ); \ |
---|
243 | assert( __synchelp_read_n == FLOATSIZE ) ;\ |
---|
244 | __synchelp_read_i += __synchelp_read_n; \ |
---|
245 | } |
---|
246 | #define SYNCHELP_READ_STRING(s,l,n) {SYNCHELP_READ_DEBUG(n); \ |
---|
247 | __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l ); \ |
---|
248 | assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\ |
---|
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 | } |
---|
256 | #define SYNCHELP_READ_STRINGM(s,n) { SYNCHELP_READ_DEBUG(n); \ |
---|
257 | __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s ); \ |
---|
258 | assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\ |
---|
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 | } |
---|
266 | #define SYNCHELP_READ_BYTE(b,n) { SYNCHELP_READ_DEBUG(n); \ |
---|
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 | } |
---|
275 | #define SYNCHELP_READ_FKT(f,n) { SYNCHELP_READ_DEBUG(n); \ |
---|
276 | __synchelp_read_i += \ |
---|
277 | f( data+__synchelp_read_i, length-__synchelp_read_i, sender); \ |
---|
278 | } |
---|
279 | #define SYNCHELP_READ_REMAINING() ( length-__synchelp_read_i ) |
---|
280 | #define SYNCHELP_READ_NEXTBYTE() ( data[__synchelp_read_i] ) |
---|
281 | #define SYNCHELP_READ_N __synchelp_read_i |
---|
282 | |
---|
283 | class NetworkStream; |
---|
284 | |
---|
285 | |
---|
286 | class Synchronizeable : virtual public BaseObject |
---|
287 | { |
---|
288 | |
---|
289 | public: |
---|
290 | Synchronizeable(); |
---|
291 | virtual ~Synchronizeable(); |
---|
292 | |
---|
293 | virtual int writeBytes(const byte* data, int length, int sender); |
---|
294 | virtual int readBytes(byte* data, int maxLength, int * reciever); |
---|
295 | virtual void writeDebug() const; |
---|
296 | virtual void readDebug() const; |
---|
297 | |
---|
298 | void setIsServer( bool isServer ); |
---|
299 | void setIsOutOfSync( bool outOfSync ); |
---|
300 | void setRequestedSync( bool requestedSync ); |
---|
301 | bool isServer(); |
---|
302 | bool isOutOfSync(); |
---|
303 | bool requestedSync(); |
---|
304 | |
---|
305 | inline void setUniqueID( int id ){ uniqueID = id; } |
---|
306 | inline int getUniqueID() const { return uniqueID; } |
---|
307 | inline int getHostID() { return this->hostID; } |
---|
308 | |
---|
309 | inline int getOwner(){ return owner; } |
---|
310 | inline void setOwner(int owner){ this->owner = owner; } |
---|
311 | |
---|
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; } |
---|
316 | |
---|
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; } }; |
---|
319 | |
---|
320 | inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; } |
---|
321 | inline NetworkStream* getNetworkStream() { return this->networkStream; } |
---|
322 | |
---|
323 | |
---|
324 | protected: |
---|
325 | NetworkStream* networkStream; |
---|
326 | int state; |
---|
327 | |
---|
328 | |
---|
329 | private: |
---|
330 | int uniqueID; |
---|
331 | int owner; |
---|
332 | int hostID; |
---|
333 | bool bSynchronize; |
---|
334 | |
---|
335 | std::list<int> synchronizeRequests; |
---|
336 | |
---|
337 | }; |
---|
338 | #endif /* _SYNCHRONIZEABLE_H */ |
---|