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 | * |
---|
32 | * Example 1: |
---|
33 | * SYNCHELP_READ_BEGIN(); |
---|
34 | * SYNCHELP_READ_FLOAT(size); |
---|
35 | * SYNCHELP_READ_STRING( textureName, 1024 ); //1024 is the length of textureName |
---|
36 | * |
---|
37 | * Example 2: |
---|
38 | * SYNCHELP_WRITE_BEGIN(); |
---|
39 | * SYNCHELP_WRITE_FLOAT(this->size); |
---|
40 | * SYNCHELP_WRITE_STRING(this->textureName); |
---|
41 | * return SYNCHELP_WRITE_N; |
---|
42 | * |
---|
43 | */ |
---|
44 | #define SYNCHELP_WRITE_BEGIN() int __synchelp_write_i = 0; \ |
---|
45 | int __synchelp_write_n |
---|
46 | #define SYNCHELP_WRITE_RESET() { __synchelp_write_i = 0; __synchelp_write_err = false; } |
---|
47 | #define SYNCHELP_WRITE_INT(i) { __synchelp_write_n = \ |
---|
48 | Converter::intToByteArray( i, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ |
---|
49 | if ( __synchelp_write_n <= 0) \ |
---|
50 | { \ |
---|
51 | PRINTF(1)("Buffer is too small to store a int\n"); \ |
---|
52 | return 0; \ |
---|
53 | } \ |
---|
54 | __synchelp_write_i += __synchelp_write_n; \ |
---|
55 | } |
---|
56 | #define SYNCHELP_WRITE_FLOAT(f) { __synchelp_write_n = \ |
---|
57 | Converter::floatToByteArray( f, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ |
---|
58 | if ( __synchelp_write_n <= 0) \ |
---|
59 | { \ |
---|
60 | PRINTF(1)("Buffer is too small to store a float\n"); \ |
---|
61 | return 0; \ |
---|
62 | } \ |
---|
63 | __synchelp_write_i += __synchelp_write_n; \ |
---|
64 | } |
---|
65 | #define SYNCHELP_WRITE_BYTE(b) { \ |
---|
66 | if (maxLength - __synchelp_write_i < 1) \ |
---|
67 | { \ |
---|
68 | PRINTF(1)("Buffer is too small to store string\n"); \ |
---|
69 | return 0; \ |
---|
70 | } \ |
---|
71 | data[__synchelp_write_i] = b; \ |
---|
72 | __synchelp_write_i++; \ |
---|
73 | } |
---|
74 | #define SYNCHELP_WRITE_STRING(s) { __synchelp_write_n = \ |
---|
75 | Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \ |
---|
76 | if ( __synchelp_write_n <= 0) \ |
---|
77 | { \ |
---|
78 | PRINTF(1)("Buffer is too small to store string\n"); \ |
---|
79 | return 0; \ |
---|
80 | } \ |
---|
81 | __synchelp_write_i += __synchelp_write_n; \ |
---|
82 | } |
---|
83 | #define SYNCHELP_WRITE_N __synchelp_write_i |
---|
84 | |
---|
85 | |
---|
86 | #define SYNCHELP_READ_BEGIN() int __synchelp_read_i = 0; \ |
---|
87 | int __synchelp_read_n |
---|
88 | |
---|
89 | #define SYNCHELP_READ_INT(i) { \ |
---|
90 | if ( length-__synchelp_read_i < INTSIZE ) \ |
---|
91 | { \ |
---|
92 | PRINTF(1)("There is not enough data to read an int\n"); \ |
---|
93 | return; \ |
---|
94 | } \ |
---|
95 | __synchelp_read_i += Converter::byteArrayToInt( data+__synchelp_read_i, &i ); \ |
---|
96 | } |
---|
97 | #define SYNCHELP_READ_FLOAT(f) { \ |
---|
98 | if ( length-__synchelp_read_i < FLOATSIZE ) \ |
---|
99 | { \ |
---|
100 | PRINTF(1)("There is not enough data to read a flaot\n"); \ |
---|
101 | return; \ |
---|
102 | } \ |
---|
103 | __synchelp_read_i += Converter::byteArrayToFloat( data+__synchelp_read_i, &f ); \ |
---|
104 | } |
---|
105 | #define SYNCHELP_READ_STRING(s,l) { \ |
---|
106 | __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l ); \ |
---|
107 | if ( __synchelp_read_n <0 ) \ |
---|
108 | { \ |
---|
109 | PRINTF(1)("There is not enough data to read string\n"); \ |
---|
110 | return; \ |
---|
111 | } \ |
---|
112 | } |
---|
113 | #define SYNCHELP_READ_BYTE(b) { \ |
---|
114 | if ( length-__synchelp_read_i < 1 ) \ |
---|
115 | { \ |
---|
116 | PRINTF(1)("There is not enough data to read a byte\n"); \ |
---|
117 | return; \ |
---|
118 | } \ |
---|
119 | b = data[__synchelp_read_i]; \ |
---|
120 | __synchelp_read_i ++; \ |
---|
121 | } |
---|
122 | |
---|
123 | class NetworkStream; |
---|
124 | |
---|
125 | |
---|
126 | class Synchronizeable : virtual public BaseObject |
---|
127 | { |
---|
128 | public: |
---|
129 | Synchronizeable(); |
---|
130 | ~Synchronizeable(); |
---|
131 | |
---|
132 | virtual void writeBytes(const byte* data, int length, int sender); |
---|
133 | virtual int readBytes(byte* data, int maxLength, int * reciever); |
---|
134 | virtual void writeDebug() const; |
---|
135 | virtual void readDebug() const; |
---|
136 | |
---|
137 | void setIsServer( bool isServer ); |
---|
138 | void setIsOutOfSync( bool outOfSync ); |
---|
139 | void setRequestedSync( bool requestedSync ); |
---|
140 | bool isServer(); |
---|
141 | bool isOutOfSync(); |
---|
142 | bool requestedSync(); |
---|
143 | inline void setUniqueID( int id ){ uniqueID = id; } |
---|
144 | inline int getUniqueID() const { return uniqueID; }; |
---|
145 | inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } |
---|
146 | inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } }; |
---|
147 | inline int getHostID() { return this->hostID; } |
---|
148 | |
---|
149 | inline int getOwner(){ return owner; } |
---|
150 | inline void setOwner(int owner){ this->owner = owner; } |
---|
151 | |
---|
152 | inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; } |
---|
153 | |
---|
154 | private: |
---|
155 | |
---|
156 | int uniqueID; |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | //static std::vector<Synchronizeable*> classList; |
---|
161 | int owner; |
---|
162 | int hostID; |
---|
163 | |
---|
164 | std::list<int> synchronizeRequests; |
---|
165 | |
---|
166 | protected: |
---|
167 | NetworkStream* networkStream; |
---|
168 | int state; |
---|
169 | |
---|
170 | }; |
---|
171 | #endif /* _SYNCHRONIZEABLE_H */ |
---|