Changeset 6341 in orxonox.OLD for trunk/src/lib/network/synchronizeable.h
- Timestamp:
- Dec 30, 2005, 1:57:12 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/synchronizeable.h
r6139 r6341 9 9 #include "base_object.h" 10 10 #include "netdefs.h" 11 #include "converter.h" 11 12 12 13 … … 18 19 #define STATE_SERVER 1 19 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 } \ 69 __synchelp_write_i += __synchelp_write_n; \ 70 } 71 #define SYNCHELP_WRITE_FLOAT(f) { __synchelp_write_n = \ 72 Converter::floatToByteArray( f, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ 73 if ( __synchelp_write_n <= 0) \ 74 { \ 75 PRINTF(1)("Buffer is too small to store a float\n"); \ 76 return 0; \ 77 } \ 78 __synchelp_write_i += __synchelp_write_n; \ 79 } 80 #define SYNCHELP_WRITE_BYTE(b) { \ 81 if (maxLength - __synchelp_write_i < 1) \ 82 { \ 83 PRINTF(1)("Buffer is too small to store string\n"); \ 84 return 0; \ 85 } \ 86 data[__synchelp_write_i] = b; \ 87 __synchelp_write_i++; \ 88 } 89 #define SYNCHELP_WRITE_STRING(s) { if (s!=NULL) \ 90 __synchelp_write_n = \ 91 Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \ 92 else \ 93 __synchelp_write_n = \ 94 Converter::stringToByteArray( "", data+__synchelp_write_i, strlen(""), maxLength-__synchelp_write_i ); \ 95 if ( __synchelp_write_n <= 0) \ 96 { \ 97 PRINTF(1)("Buffer is too small to store string\n"); \ 98 return 0; \ 99 } \ 100 __synchelp_write_i += __synchelp_write_n; \ 101 } 102 #define SYNCHELP_WRITE_N __synchelp_write_i 103 #define SYNCHELP_WRITE_FKT(f) { \ 104 __synchelp_write_i += \ 105 f( data+__synchelp_write_i, maxLength-__synchelp_write_i ); \ 106 } 107 108 109 #define SYNCHELP_READ_BEGIN() int __synchelp_read_i = 0; \ 110 int __synchelp_read_n 111 112 #define SYNCHELP_READ_INT(i) { \ 113 if ( length-__synchelp_read_i < INTSIZE ) \ 114 { \ 115 PRINTF(1)("There is not enough data to read an int\n"); \ 116 return 0; \ 117 } \ 118 __synchelp_read_i += Converter::byteArrayToInt( data+__synchelp_read_i, &i ); \ 119 } 120 #define SYNCHELP_READ_FLOAT(f) { \ 121 if ( length-__synchelp_read_i < FLOATSIZE ) \ 122 { \ 123 PRINTF(1)("There is not enough data to read a flaot\n"); \ 124 return 0; \ 125 } \ 126 __synchelp_read_i += Converter::byteArrayToFloat( data+__synchelp_read_i, &f ); \ 127 } 128 #define SYNCHELP_READ_STRING(s,l) { \ 129 __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l ); \ 130 if ( __synchelp_read_n <0 ) \ 131 { \ 132 PRINTF(1)("There is not enough data to read string\n"); \ 133 return 0; \ 134 } \ 135 __synchelp_read_i += __synchelp_read_n; \ 136 } 137 #define SYNCHELP_READ_STRINGM(s) { \ 138 __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s ); \ 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_BYTE(b) { \ 147 if ( length-__synchelp_read_i < 1 ) \ 148 { \ 149 PRINTF(1)("There is not enough data to read a byte\n"); \ 150 return 0; \ 151 } \ 152 b = data[__synchelp_read_i]; \ 153 __synchelp_read_i ++; \ 154 } 155 #define SYNCHELP_READ_FKT(f) { \ 156 __synchelp_read_i += \ 157 f( data+__synchelp_read_i, length-__synchelp_read_i, sender); \ 158 } 159 #define SYNCHELP_READ_N __synchelp_read_i 20 160 21 161 class NetworkStream; … … 25 165 { 26 166 public: 27 28 Synchronizeable(const char* name);29 167 Synchronizeable(); 30 168 ~Synchronizeable(); 31 169 32 virtual void writeBytes(const byte* data, int length);170 virtual int writeBytes(const byte* data, int length, int sender); 33 171 virtual int readBytes(byte* data, int maxLength, int * reciever); 34 172 virtual void writeDebug() const; … … 37 175 void setIsServer( bool isServer ); 38 176 void setIsOutOfSync( bool outOfSync ); 177 void setRequestedSync( bool requestedSync ); 39 178 bool isServer(); 40 179 bool isOutOfSync(); 41 void setUniqueID( int id ){ uniqueID = id; } 42 int getUniqueID() const { return uniqueID; }; 43 void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } 180 bool requestedSync(); 181 inline void setUniqueID( int id ){ uniqueID = id; } 182 inline int getUniqueID() const { return uniqueID; }; 183 inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } 184 inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } }; 185 inline int getHostID() { return this->hostID; } 44 186 45 187 inline int getOwner(){ return owner; } … … 57 199 int owner; 58 200 int hostID; 201 202 std::list<int> synchronizeRequests; 203 204 protected: 205 NetworkStream* networkStream; 59 206 int state; 60 std::list<int> synchronizeRequests;61 62 NetworkStream* networkStream;63 207 64 208 };
Note: See TracChangeset
for help on using the changeset viewer.