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 | assert( __synchelp_write_n == INTSIZE ); \ |
---|
65 | if ( __synchelp_write_n <= 0) \ |
---|
66 | { \ |
---|
67 | PRINTF(1)("Buffer is too small to store a int\n"); \ |
---|
68 | return 0; \ |
---|
69 | } \ |
---|
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 | assert( __synchelp_write_n == FLOATSIZE ); \ |
---|
75 | if ( __synchelp_write_n <= 0) \ |
---|
76 | { \ |
---|
77 | PRINTF(1)("Buffer is too small to store a float\n"); \ |
---|
78 | return 0; \ |
---|
79 | } \ |
---|
80 | __synchelp_write_i += __synchelp_write_n; \ |
---|
81 | } |
---|
82 | #define SYNCHELP_WRITE_BYTE(b) { \ |
---|
83 | if (maxLength - __synchelp_write_i < 1) \ |
---|
84 | { \ |
---|
85 | PRINTF(1)("Buffer is too small to store string\n"); \ |
---|
86 | return 0; \ |
---|
87 | } \ |
---|
88 | data[__synchelp_write_i] = b; \ |
---|
89 | __synchelp_write_i++; \ |
---|
90 | } |
---|
91 | #define SYNCHELP_WRITE_STRING(s) { if (s!=NULL) {\ |
---|
92 | __synchelp_write_n = \ |
---|
93 | Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \ |
---|
94 | assert( __synchelp_write_n == strlen(s)+INTSIZE ); \ |
---|
95 | } else {\ |
---|
96 | __synchelp_write_n = \ |
---|
97 | Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \ |
---|
98 | assert( __synchelp_write_n == strlen("")+INTSIZE ); } \ |
---|
99 | if ( __synchelp_write_n <= 0) \ |
---|
100 | { \ |
---|
101 | PRINTF(1)("Buffer is too small to store string\n"); \ |
---|
102 | return 0; \ |
---|
103 | } \ |
---|
104 | __synchelp_write_i += __synchelp_write_n; \ |
---|
105 | } |
---|
106 | #define SYNCHELP_WRITE_N __synchelp_write_i |
---|
107 | #define SYNCHELP_WRITE_FKT(f) { \ |
---|
108 | __synchelp_write_i += \ |
---|
109 | f( data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | #define SYNCHELP_READ_BEGIN() int __synchelp_read_i = 0; \ |
---|
114 | int __synchelp_read_n |
---|
115 | |
---|
116 | #define SYNCHELP_READ_INT(i) { \ |
---|
117 | if ( length-__synchelp_read_i < INTSIZE ) \ |
---|
118 | { \ |
---|
119 | PRINTF(1)("There is not enough data to read an int\n"); \ |
---|
120 | return 0; \ |
---|
121 | } \ |
---|
122 | __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &i ); \ |
---|
123 | assert( __synchelp_read_n == INTSIZE ); \ |
---|
124 | __synchelp_read_i += __synchelp_read_n; \ |
---|
125 | } |
---|
126 | #define SYNCHELP_READ_FLOAT(f) { \ |
---|
127 | if ( length-__synchelp_read_i < FLOATSIZE ) \ |
---|
128 | { \ |
---|
129 | PRINTF(1)("There is not enough data to read a flaot\n"); \ |
---|
130 | return 0; \ |
---|
131 | } \ |
---|
132 | __synchelp_read_n = Converter::byteArrayToFloat( data+__synchelp_read_i, &f ); \ |
---|
133 | assert( __synchelp_read_n == FLOATSIZE ) ;\ |
---|
134 | __synchelp_read_i += __synchelp_read_n; \ |
---|
135 | } |
---|
136 | #define SYNCHELP_READ_STRING(s,l) { \ |
---|
137 | __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l ); \ |
---|
138 | assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\ |
---|
139 | if ( __synchelp_read_n <0 ) \ |
---|
140 | { \ |
---|
141 | PRINTF(1)("There is not enough data to read string\n"); \ |
---|
142 | return 0; \ |
---|
143 | } \ |
---|
144 | __synchelp_read_i += __synchelp_read_n; \ |
---|
145 | } |
---|
146 | #define SYNCHELP_READ_STRINGM(s) { \ |
---|
147 | __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s ); \ |
---|
148 | assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\ |
---|
149 | if ( __synchelp_read_n <0 ) \ |
---|
150 | { \ |
---|
151 | PRINTF(1)("There is not enough data to read string\n"); \ |
---|
152 | return 0; \ |
---|
153 | } \ |
---|
154 | __synchelp_read_i += __synchelp_read_n; \ |
---|
155 | } |
---|
156 | #define SYNCHELP_READ_BYTE(b) { \ |
---|
157 | if ( length-__synchelp_read_i < 1 ) \ |
---|
158 | { \ |
---|
159 | PRINTF(1)("There is not enough data to read a byte\n"); \ |
---|
160 | return 0; \ |
---|
161 | } \ |
---|
162 | b = data[__synchelp_read_i]; \ |
---|
163 | __synchelp_read_i ++; \ |
---|
164 | } |
---|
165 | #define SYNCHELP_READ_FKT(f) { \ |
---|
166 | __synchelp_read_i += \ |
---|
167 | f( data+__synchelp_read_i, length-__synchelp_read_i, sender); \ |
---|
168 | } |
---|
169 | #define SYNCHELP_READ_N __synchelp_read_i |
---|
170 | |
---|
171 | class NetworkStream; |
---|
172 | |
---|
173 | |
---|
174 | class Synchronizeable : virtual public BaseObject |
---|
175 | { |
---|
176 | |
---|
177 | public: |
---|
178 | Synchronizeable(); |
---|
179 | virtual ~Synchronizeable(); |
---|
180 | |
---|
181 | virtual int writeBytes(const byte* data, int length, int sender); |
---|
182 | virtual int readBytes(byte* data, int maxLength, int * reciever); |
---|
183 | virtual void writeDebug() const; |
---|
184 | virtual void readDebug() const; |
---|
185 | |
---|
186 | void setIsServer( bool isServer ); |
---|
187 | void setIsOutOfSync( bool outOfSync ); |
---|
188 | void setRequestedSync( bool requestedSync ); |
---|
189 | bool isServer(); |
---|
190 | bool isOutOfSync(); |
---|
191 | bool requestedSync(); |
---|
192 | |
---|
193 | inline void setUniqueID( int id ){ uniqueID = id; } |
---|
194 | inline int getUniqueID() const { return uniqueID; } |
---|
195 | inline int getHostID() { return this->hostID; } |
---|
196 | |
---|
197 | inline int getOwner(){ return owner; } |
---|
198 | inline void setOwner(int owner){ this->owner = owner; } |
---|
199 | |
---|
200 | /** @returns true if this Synchronizeable has to be synchronized over network */ |
---|
201 | inline bool beSynchronized() { return this->bSynchronize; } |
---|
202 | /** @param bSynchronize sets the Synchronizeable to be sunchronized or not */ |
---|
203 | inline void setSynchronized(bool bSynchronize) { this->bSynchronize = bSynchronize; } |
---|
204 | |
---|
205 | inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } |
---|
206 | inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } }; |
---|
207 | |
---|
208 | inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; } |
---|
209 | inline NetworkStream* getNetworkStream() { return this->networkStream; } |
---|
210 | |
---|
211 | |
---|
212 | protected: |
---|
213 | NetworkStream* networkStream; |
---|
214 | int state; |
---|
215 | |
---|
216 | |
---|
217 | private: |
---|
218 | int uniqueID; |
---|
219 | int owner; |
---|
220 | int hostID; |
---|
221 | bool bSynchronize; |
---|
222 | |
---|
223 | std::list<int> synchronizeRequests; |
---|
224 | |
---|
225 | }; |
---|
226 | #endif /* _SYNCHRONIZEABLE_H */ |
---|