[5523] | 1 | /*! |
---|
| 2 | * @file connection_monitor.h |
---|
[5550] | 3 | \brief interface for all classes that have to be synchronized |
---|
[5547] | 4 | */ |
---|
[5523] | 5 | |
---|
[5547] | 6 | #ifndef _SYNCHRONIZEABLE_H |
---|
| 7 | #define _SYNCHRONIZEABLE_H |
---|
[5523] | 8 | |
---|
[5997] | 9 | #include "base_object.h" |
---|
[5547] | 10 | #include "netdefs.h" |
---|
[6341] | 11 | #include "converter.h" |
---|
[5547] | 12 | |
---|
[5997] | 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 |
---|
[6341] | 21 | #define STATE_REQUESTEDSYNC 4 |
---|
[5997] | 22 | |
---|
[6341] | 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 ); \ |
---|
[6753] | 64 | assert( __synchelp_write_n == INTSIZE ); \ |
---|
[6341] | 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 ); \ |
---|
[6753] | 74 | assert( __synchelp_write_n == FLOATSIZE ); \ |
---|
[6341] | 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 | } |
---|
[6753] | 91 | #define SYNCHELP_WRITE_STRING(s) { if (s!=NULL) {\ |
---|
[6341] | 92 | __synchelp_write_n = \ |
---|
| 93 | Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \ |
---|
[6753] | 94 | assert( __synchelp_write_n == strlen(s)+INTSIZE ); \ |
---|
| 95 | } else {\ |
---|
[6341] | 96 | __synchelp_write_n = \ |
---|
| 97 | Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \ |
---|
[6753] | 98 | assert( __synchelp_write_n == strlen("")+INTSIZE ); } \ |
---|
[6341] | 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 | } \ |
---|
[6753] | 122 | __synchelp_read_n = Converter::byteArrayToInt( data+__synchelp_read_i, &i ); \ |
---|
| 123 | assert( __synchelp_read_n == INTSIZE ); \ |
---|
| 124 | __synchelp_read_i += __synchelp_read_n; \ |
---|
[6341] | 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 | } \ |
---|
[6753] | 132 | __synchelp_read_n = Converter::byteArrayToFloat( data+__synchelp_read_i, &f ); \ |
---|
| 133 | assert( __synchelp_read_n == FLOATSIZE ) ;\ |
---|
| 134 | __synchelp_read_i += __synchelp_read_n; \ |
---|
[6341] | 135 | } |
---|
| 136 | #define SYNCHELP_READ_STRING(s,l) { \ |
---|
| 137 | __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l ); \ |
---|
[6753] | 138 | assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\ |
---|
[6341] | 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 ); \ |
---|
[6753] | 148 | assert( __synchelp_read_n == strlen(s)+INTSIZE ) ;\ |
---|
[6341] | 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 | |
---|
[6139] | 171 | class NetworkStream; |
---|
[5997] | 172 | |
---|
[6139] | 173 | |
---|
[5581] | 174 | class Synchronizeable : virtual public BaseObject |
---|
[6695] | 175 | { |
---|
| 176 | |
---|
[5804] | 177 | public: |
---|
[5996] | 178 | Synchronizeable(); |
---|
[6695] | 179 | virtual ~Synchronizeable(); |
---|
[5523] | 180 | |
---|
[6341] | 181 | virtual int writeBytes(const byte* data, int length, int sender); |
---|
[6139] | 182 | virtual int readBytes(byte* data, int maxLength, int * reciever); |
---|
[5806] | 183 | virtual void writeDebug() const; |
---|
| 184 | virtual void readDebug() const; |
---|
[6139] | 185 | |
---|
| 186 | void setIsServer( bool isServer ); |
---|
| 187 | void setIsOutOfSync( bool outOfSync ); |
---|
[6341] | 188 | void setRequestedSync( bool requestedSync ); |
---|
[5997] | 189 | bool isServer(); |
---|
| 190 | bool isOutOfSync(); |
---|
[6341] | 191 | bool requestedSync(); |
---|
[6695] | 192 | |
---|
[6341] | 193 | inline void setUniqueID( int id ){ uniqueID = id; } |
---|
[6695] | 194 | inline int getUniqueID() const { return uniqueID; } |
---|
[6341] | 195 | inline int getHostID() { return this->hostID; } |
---|
[5547] | 196 | |
---|
[6139] | 197 | inline int getOwner(){ return owner; } |
---|
| 198 | inline void setOwner(int owner){ this->owner = owner; } |
---|
| 199 | |
---|
[6695] | 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; } |
---|
[6139] | 204 | |
---|
[6695] | 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; } }; |
---|
[5523] | 207 | |
---|
[6695] | 208 | inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; } |
---|
| 209 | inline NetworkStream* getNetworkStream() { return this->networkStream; } |
---|
[6139] | 210 | |
---|
| 211 | |
---|
[6695] | 212 | protected: |
---|
| 213 | NetworkStream* networkStream; |
---|
| 214 | int state; |
---|
[6139] | 215 | |
---|
[6341] | 216 | |
---|
[6695] | 217 | private: |
---|
| 218 | int uniqueID; |
---|
| 219 | int owner; |
---|
| 220 | int hostID; |
---|
| 221 | bool bSynchronize; |
---|
[5547] | 222 | |
---|
[6695] | 223 | std::list<int> synchronizeRequests; |
---|
[6139] | 224 | |
---|
[6695] | 225 | }; |
---|
[5548] | 226 | #endif /* _SYNCHRONIZEABLE_H */ |
---|