Changeset 7508 in orxonox.OLD for branches/network/src
- Timestamp:
- May 3, 2006, 2:29:17 PM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/synchronizeable.cc
r7459 r7508 96 96 * @return n bytes copied into data 97 97 */ 98 int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int priorityTH ) 99 { 100 #warning implement this 98 int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ) 99 { 100 //make sure this user has his history 101 if ( sentStates.size() <= userId ) 102 sentStates.resize( userId+1 ); 103 104 //calculate needed memory 105 int neededSize = 0; 106 107 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 108 neededSize += (*it)->getSize(); 109 110 assert( neededSize <= maxLength ); 111 112 //remove older states from history than fromStateId 113 StateHistory::iterator it = sentStates[userId].begin(); 114 115 while ( it != sentStates[userId].end() && (*it)->stateId < fromStateId ) 116 it++; 117 118 if ( it != sentStates[userId].begin() ) 119 { 120 for ( StateHistory::iterator it2 = sentStates[userId].begin(); it2 != it; it2++ ) 121 { 122 if ( (*it2)->data != NULL ) 123 { 124 delete (*it2)->data; 125 (*it2)->data = NULL; 126 } 127 } 128 sentStates[userId].erase( sentStates[userId].begin(), it ); 129 } 130 131 //find state to create diff from 132 StateHistoryEntry * stateFrom = NULL; 133 134 it = sentStates[userId].begin(); 135 while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId ) 136 it++; 137 138 if ( it == sentStates[userId].end() ) 139 { 140 StateHistoryEntry * initialEntry = new StateHistoryEntry(); 141 142 initialEntry->stateId = fromStateId; 143 initialEntry->dataLength = 0; 144 initialEntry->data = NULL; 145 146 stateFrom = initialEntry; 147 } 148 else 149 stateFrom = (*it); 150 151 StateHistoryEntry * stateTo = new StateHistoryEntry(); 152 153 stateTo->stateId = stateId; 154 stateTo->dataLength = neededSize; 155 stateTo->data = (byte*)malloc( neededSize ); 156 157 std::list<int>::iterator sizeIter = stateFrom->sizeList.begin(); 158 159 int i = 0; 160 int n; 161 162 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 163 { 164 if ( (*it)->getPriority() >= priorityTH || sizeIter == stateFrom->sizeList.end() ) 165 { 166 n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i ); 167 stateTo->sizeList.push_back( n ); 168 i += n; 169 } 170 else 171 { 172 for ( int j = 0; j<(*sizeIter); j++ ) 173 { 174 assert( i < stateFrom->dataLength ); 175 stateTo->data[i] = stateFrom->data[i]; 176 i++; 177 } 178 stateTo->sizeList.push_back( (*sizeIter) ); 179 } 180 181 if ( sizeIter != stateFrom->sizeList.end() ) 182 sizeIter++; 183 } 184 185 sentStates[userId].push_back( stateTo ); 186 187 //write diff to data 188 for ( i = 0; i<neededSize; i++ ) 189 { 190 if ( i < stateFrom->dataLength ) 191 data[i] = stateTo->data[i] - stateFrom->data[i]; 192 else 193 data[i] = stateTo->data[i]; 194 } 195 196 return neededSize; 101 197 } 102 198 … … 109 205 * @return true on success 110 206 */ 111 bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId ) 112 { 113 #warning implement this 207 bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ) 208 { 209 //make sure this user has his history 210 if ( recvStates.size() <= userId ) 211 recvStates.resize( userId+1 ); 212 213 //create new state 214 StateHistoryEntry * stateTo = new StateHistoryEntry(); 215 stateTo->stateId = stateId; 216 stateTo->dataLength = length; 217 stateTo->data = (byte*)malloc( length ); 218 219 //remove old states 220 StateHistory::iterator it = recvStates[userId].begin(); 221 222 while ( it != recvStates[userId].end() && (*it)->stateId < fromStateId ) 223 it++; 224 225 if ( it != recvStates[userId].begin() ) 226 { 227 for ( StateHistory::iterator it2 = recvStates[userId].begin(); it2 != it; it2++ ) 228 { 229 if ( (*it2)->data != NULL ) 230 { 231 delete (*it2)->data; 232 (*it2)->data = NULL; 233 } 234 } 235 recvStates[userId].erase( recvStates[userId].begin(), it ); 236 } 237 238 //find state to apply diff to 239 StateHistoryEntry * stateFrom = NULL; 240 241 it = recvStates[userId].begin(); 242 while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId ) 243 it++; 244 245 if ( it == recvStates[userId].end() ) 246 { 247 StateHistoryEntry * initialEntry = new StateHistoryEntry(); 248 249 initialEntry->stateId = fromStateId; 250 initialEntry->dataLength = 0; 251 initialEntry->data = NULL; 252 253 stateFrom = initialEntry; 254 } 255 else 256 stateFrom = (*it); 257 258 //apply diff 259 for ( int i = 0; i<length; i++ ) 260 { 261 if ( i < stateFrom->dataLength ) 262 stateTo->data[i] = stateFrom->data[i] + data[i]; 263 else 264 stateTo->data[i] = data[i]; 265 } 266 267 //add state to state history 268 recvStates[userId].push_back( stateTo ); 269 270 int i = 0; 271 272 for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) 273 { 274 i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i ); 275 } 276 277 assert( i == length -1 ); 278 279 return length; 114 280 } 115 281 … … 129 295 void Synchronizeable::registerVar( SynchronizeableVar * var ) 130 296 { 131 #warning implement this 297 syncVarList.push_back( var ); 132 298 } 133 299 … … 140 306 int Synchronizeable::registerVarId( SynchronizeableVar * var ) 141 307 { 142 #warning implement this 143 } 144 145 308 syncVarList.push_back( var ); 309 return syncVarList.size()-1; 310 } 311 312 -
branches/network/src/lib/network/synchronizeable.h
r7444 r7508 28 28 #define STATE_SERVER 1 29 29 30 struct StateHistoryEntry 31 { 32 int stateId; 33 byte * data; 34 int dataLength; 35 std::list<int> sizeList; 36 }; 37 38 typedef std::list<StateHistoryEntry*> StateHistory; 39 40 typedef std::vector<StateHistory> UserStateHistory; 41 30 42 enum { 31 43 PERMISSION_OWNER = 1, … … 33 45 }; 34 46 35 typedef std::vector<SynchronizeableVar > SyncVarList;47 typedef std::vector<SynchronizeableVar*> SyncVarList; 36 48 37 49 class NetworkStream; … … 49 61 virtual void varChangeHandler( std::list<int> & id ); 50 62 51 int getStateDiff( int userId, byte* data, int maxLength, int stateId, int priorityTH );52 bool setStateDiff( int userId, byte* data, int length, int stateId );63 int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ); 64 bool setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ); 53 65 54 66 void registerVar( SynchronizeableVar * var ); … … 80 92 int hostID; //!< my own host id 81 93 bool bSynchronize; //!< do we need beeing synchronized? 94 95 SyncVarList syncVarList; //!< list containing variables to synchronize 96 97 UserStateHistory sentStates; //!< store already sent states to create diffs from 98 UserStateHistory recvStates; //!< store recieved states to apply diffs 82 99 83 100 }; -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_bool.cc
r7459 r7508 21 21 * standard constructor 22 22 */ 23 SynchronizeableBool::SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 0, permission, priority )23 SynchronizeableBool::SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 1, permission, priority ) 24 24 { 25 25 this->vPtrIn = ptrIn; -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_int.cc
r7459 r7508 23 23 * @todo this constructor is not jet implemented - do it 24 24 */ 25 SynchronizeableInt::SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 0, permission, priority )25 SynchronizeableInt::SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, INTSIZE, permission, priority ) 26 26 { 27 27 this->vPtrIn = ptrIn; -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_string.h
r7459 r7508 5 5 6 6 #include "synchronizeable_var/synchronizeable_var.h" 7 #include "converter.h" 7 8 8 9 #ifndef _SYNCHRONIZEABLE_STRING_H … … 27 28 virtual bool hasStaticSize(){ return false; }; 28 29 30 virtual int getSize(){ return vPtrIn->length()+INTSIZE; } 31 29 32 private: 30 33 std::string * vPtrIn; //!< pointer to data (read) -
branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.h
r7459 r7508 45 45 46 46 /** 47 * get size writeToBuf needs 48 * @return size in bytes 49 */ 50 virtual int getSize(){ return length; } 51 52 /** 47 53 * check for permission to write 48 54 * @return true if you can write … … 61 67 */ 62 68 inline void setName( std::string name ) { this->name = name; } 69 70 /** 71 * get priority 72 * @return priority 73 */ 74 inline int getPriority() { return this->priority; } 75 76 /** 77 * set priority 78 * @param p priority 79 */ 80 inline void setPriority( int p ) { this->priority = p; } 81 82 /** 83 * reset priority to variable specific default value 84 */ 85 inline void resetPriority() { this->priority = this->real_priority; } 63 86 64 87
Note: See TracChangeset
for help on using the changeset viewer.