| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | ### File Specific: |
|---|
| 13 | main-programmer: Silvan Nellen |
|---|
| 14 | co-programmer: Benjamin Wuest |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #define DEBUG_MODULE_NETWORK |
|---|
| 18 | |
|---|
| 19 | #include "shared_network_data.h" |
|---|
| 20 | #include "network_stream.h" |
|---|
| 21 | #include "netdefs.h" |
|---|
| 22 | |
|---|
| 23 | #include "state.h" |
|---|
| 24 | |
|---|
| 25 | #include <cassert> |
|---|
| 26 | |
|---|
| 27 | #include "synchronizeable.h" |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * default constructor |
|---|
| 33 | */ |
|---|
| 34 | Synchronizeable::Synchronizeable() |
|---|
| 35 | { |
|---|
| 36 | this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); |
|---|
| 37 | this->owner = -1; |
|---|
| 38 | this->hostID = SharedNetworkData::getInstance()->getHostID(); |
|---|
| 39 | this->setIsServer(this->hostID == 0); |
|---|
| 40 | this->uniqueID = NET_UID_UNASSIGNED; |
|---|
| 41 | this->networkStream = NULL; |
|---|
| 42 | this->bSynchronize = false; |
|---|
| 43 | |
|---|
| 44 | if( State::isOnline()) |
|---|
| 45 | { |
|---|
| 46 | NetworkStream* nd = SharedNetworkData::getInstance()->getDefaultSyncStream(); |
|---|
| 47 | assert(nd != NULL); |
|---|
| 48 | nd->connectSynchronizeable(*this); |
|---|
| 49 | this->setUniqueID(SharedNetworkData::getInstance()->getNewUniqueID()); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /* make sure loadClassId is first synced var because this is read by networkStream */ |
|---|
| 53 | assert( syncVarList.size() == 0 ); |
|---|
| 54 | mLeafClassId = this->registerVarId( new SynchronizeableInt( (int*)&this->getLeafClassID(), (int*)&this->getLeafClassID(), "leafClassId" ) ); |
|---|
| 55 | |
|---|
| 56 | this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) ); |
|---|
| 57 | this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) ); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * default destructor deletes all unneded stuff |
|---|
| 64 | */ |
|---|
| 65 | Synchronizeable::~Synchronizeable() |
|---|
| 66 | { |
|---|
| 67 | if ( this->networkStream ) |
|---|
| 68 | this->networkStream->disconnectSynchronizeable(*this); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Sets the server flag to a given value |
|---|
| 73 | * @param isServer: the boolean value which the server flag is to set to |
|---|
| 74 | */ |
|---|
| 75 | void Synchronizeable::setIsServer(bool isServer) |
|---|
| 76 | { |
|---|
| 77 | if( isServer ) |
|---|
| 78 | this->state = this->state | STATE_SERVER; |
|---|
| 79 | else |
|---|
| 80 | this->state = this->state & (~STATE_SERVER); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Determines if the server flag is set |
|---|
| 86 | * @return true, if the server flag is true, false else |
|---|
| 87 | */ |
|---|
| 88 | bool Synchronizeable::isServer() |
|---|
| 89 | { |
|---|
| 90 | return (this->state & STATE_SERVER) >0; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * get the diff to last acked state of userId |
|---|
| 96 | * |
|---|
| 97 | * each synchrinizeable defines its own stack of states received and sent over the network. The stack contains |
|---|
| 98 | * a per user entry for the last sent/received state This function returns a delta compressed state of the |
|---|
| 99 | * synchronizeable. This state will be transmitted over the network to the other participants |
|---|
| 100 | * |
|---|
| 101 | * @param userId user to create diff for |
|---|
| 102 | * @param data buffer to copy diff in |
|---|
| 103 | * @param maxLength max bytes to copy into data |
|---|
| 104 | * @param stateId id of current state |
|---|
| 105 | * @param fromStateId the reference state for the delta state |
|---|
| 106 | * @param priorityTH tells getStateDiff to not send element with priority \< priorityTH |
|---|
| 107 | * @return n bytes copied into data |
|---|
| 108 | */ |
|---|
| 109 | int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH ) |
|---|
| 110 | { |
|---|
| 111 | //make sure this user has his history |
|---|
| 112 | if ( sentStates.size() <= userId ) |
|---|
| 113 | sentStates.resize( userId+1 ); |
|---|
| 114 | |
|---|
| 115 | //calculate needed memory |
|---|
| 116 | int neededSize = 0; |
|---|
| 117 | |
|---|
| 118 | for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) |
|---|
| 119 | neededSize += (*it)->getSize(); |
|---|
| 120 | |
|---|
| 121 | assert( neededSize <= maxLength ); |
|---|
| 122 | |
|---|
| 123 | //remove older states from history than fromStateId |
|---|
| 124 | StateHistory::iterator it = sentStates[userId].begin(); |
|---|
| 125 | |
|---|
| 126 | while ( it != sentStates[userId].end() && (*it)->stateId < fromStateId ) |
|---|
| 127 | it++; |
|---|
| 128 | |
|---|
| 129 | if ( it != sentStates[userId].begin() ) |
|---|
| 130 | { |
|---|
| 131 | for ( StateHistory::iterator it2 = sentStates[userId].begin(); it2 != it; it2++ ) |
|---|
| 132 | { |
|---|
| 133 | if ( (*it2)->data != NULL ) |
|---|
| 134 | { |
|---|
| 135 | delete (*it2)->data; |
|---|
| 136 | (*it2)->data = NULL; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | sentStates[userId].erase( sentStates[userId].begin(), it ); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | //find state to create diff from |
|---|
| 143 | StateHistoryEntry * stateFrom = NULL; |
|---|
| 144 | |
|---|
| 145 | it = sentStates[userId].begin(); |
|---|
| 146 | while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId ) |
|---|
| 147 | it++; |
|---|
| 148 | |
|---|
| 149 | if ( it == sentStates[userId].end() ) |
|---|
| 150 | { |
|---|
| 151 | StateHistoryEntry * initialEntry = new StateHistoryEntry(); |
|---|
| 152 | |
|---|
| 153 | initialEntry->stateId = fromStateId; |
|---|
| 154 | initialEntry->dataLength = 0; |
|---|
| 155 | initialEntry->data = NULL; |
|---|
| 156 | |
|---|
| 157 | stateFrom = initialEntry; |
|---|
| 158 | } |
|---|
| 159 | else |
|---|
| 160 | stateFrom = (*it); |
|---|
| 161 | |
|---|
| 162 | StateHistoryEntry * stateTo = new StateHistoryEntry(); |
|---|
| 163 | |
|---|
| 164 | stateTo->stateId = stateId; |
|---|
| 165 | stateTo->dataLength = neededSize; |
|---|
| 166 | stateTo->data = (byte*)malloc( neededSize ); |
|---|
| 167 | |
|---|
| 168 | std::list<int>::iterator sizeIter = stateFrom->sizeList.begin(); |
|---|
| 169 | |
|---|
| 170 | int i = 0; |
|---|
| 171 | int n; |
|---|
| 172 | |
|---|
| 173 | // now do the actual synchronization: kick all variables to write into a common buffer |
|---|
| 174 | for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) |
|---|
| 175 | { |
|---|
| 176 | if ( (*it)->getPriority() >= priorityTH || sizeIter == stateFrom->sizeList.end() ) |
|---|
| 177 | { |
|---|
| 178 | (*it)->debug(); |
|---|
| 179 | n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i ); |
|---|
| 180 | stateTo->sizeList.push_back( n ); |
|---|
| 181 | i += n; |
|---|
| 182 | } |
|---|
| 183 | else if ( ! ( |
|---|
| 184 | this->isServer() && (*it)->checkPremission( PERMISSION_SERVER ) || |
|---|
| 185 | this->owner == this->hostID && (*it)->checkPremission( PERMISSION_OWNER ) || |
|---|
| 186 | (*it)->checkPremission( PERMISSION_ALL ) |
|---|
| 187 | ) ) |
|---|
| 188 | { |
|---|
| 189 | for ( int j = 0; j < (*it)->getSize(); it++ ) |
|---|
| 190 | { |
|---|
| 191 | assert(false); |
|---|
| 192 | stateTo->data[i] = 0; |
|---|
| 193 | i++; |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | else |
|---|
| 197 | { |
|---|
| 198 | for ( int j = 0; j<(*sizeIter); j++ ) |
|---|
| 199 | { |
|---|
| 200 | assert( i < stateFrom->dataLength ); |
|---|
| 201 | stateTo->data[i] = stateFrom->data[i]; |
|---|
| 202 | i++; |
|---|
| 203 | } |
|---|
| 204 | stateTo->sizeList.push_back( (*sizeIter) ); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | if ( sizeIter != stateFrom->sizeList.end() ) |
|---|
| 208 | sizeIter++; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | sentStates[userId].push_back( stateTo ); |
|---|
| 212 | |
|---|
| 213 | assert( i == neededSize ); |
|---|
| 214 | |
|---|
| 215 | //write diff to data |
|---|
| 216 | for ( i = 0; i<neededSize; i++ ) |
|---|
| 217 | { |
|---|
| 218 | if ( i < stateFrom->dataLength ) |
|---|
| 219 | data[i] = stateTo->data[i] - stateFrom->data[i]; |
|---|
| 220 | else |
|---|
| 221 | data[i] = stateTo->data[i]; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | return neededSize; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | /** |
|---|
| 228 | * sets a new state out of a diff created on another host |
|---|
| 229 | * @param userId hostId of user who send me that diff |
|---|
| 230 | * @param data pointer to diff |
|---|
| 231 | * @param length length of diff |
|---|
| 232 | * @param stateId id of current state |
|---|
| 233 | * @param fromStateId id of the base state id |
|---|
| 234 | * @return number bytes read |
|---|
| 235 | * @todo check for permissions |
|---|
| 236 | */ |
|---|
| 237 | int Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId ) |
|---|
| 238 | { |
|---|
| 239 | //make sure this user has his history |
|---|
| 240 | if ( recvStates.size() <= userId ) |
|---|
| 241 | recvStates.resize( userId+1 ); |
|---|
| 242 | |
|---|
| 243 | //create new state |
|---|
| 244 | StateHistoryEntry * stateTo = new StateHistoryEntry(); |
|---|
| 245 | stateTo->stateId = stateId; |
|---|
| 246 | stateTo->dataLength = length; |
|---|
| 247 | stateTo->data = (byte*)malloc( length ); |
|---|
| 248 | |
|---|
| 249 | //remove old states |
|---|
| 250 | StateHistory::iterator it = recvStates[userId].begin(); |
|---|
| 251 | |
|---|
| 252 | while ( it != recvStates[userId].end() && (*it)->stateId < fromStateId ) |
|---|
| 253 | it++; |
|---|
| 254 | |
|---|
| 255 | if ( it != recvStates[userId].begin() ) |
|---|
| 256 | { |
|---|
| 257 | for ( StateHistory::iterator it2 = recvStates[userId].begin(); it2 != it; it2++ ) |
|---|
| 258 | { |
|---|
| 259 | if ( (*it2)->data != NULL ) |
|---|
| 260 | { |
|---|
| 261 | delete (*it2)->data; |
|---|
| 262 | (*it2)->data = NULL; |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | recvStates[userId].erase( recvStates[userId].begin(), it ); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | //find state to apply diff to |
|---|
| 269 | StateHistoryEntry * stateFrom = NULL; |
|---|
| 270 | |
|---|
| 271 | it = recvStates[userId].begin(); |
|---|
| 272 | while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId ) |
|---|
| 273 | it++; |
|---|
| 274 | |
|---|
| 275 | if ( it == recvStates[userId].end() ) |
|---|
| 276 | { |
|---|
| 277 | StateHistoryEntry * initialEntry = new StateHistoryEntry(); |
|---|
| 278 | |
|---|
| 279 | initialEntry->stateId = fromStateId; |
|---|
| 280 | initialEntry->dataLength = 0; |
|---|
| 281 | initialEntry->data = NULL; |
|---|
| 282 | |
|---|
| 283 | stateFrom = initialEntry; |
|---|
| 284 | } |
|---|
| 285 | else |
|---|
| 286 | stateFrom = (*it); |
|---|
| 287 | |
|---|
| 288 | std::list<int> changes; |
|---|
| 289 | |
|---|
| 290 | //apply diff |
|---|
| 291 | for ( int i = 0; i<length; i++ ) |
|---|
| 292 | { |
|---|
| 293 | if ( i < stateFrom->dataLength ) |
|---|
| 294 | stateTo->data[i] = stateFrom->data[i] + data[i]; |
|---|
| 295 | else |
|---|
| 296 | stateTo->data[i] = data[i]; |
|---|
| 297 | |
|---|
| 298 | if ( data[i] != 0 ) |
|---|
| 299 | changes.push_back(i); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | //add state to state history |
|---|
| 303 | recvStates[userId].push_back( stateTo ); |
|---|
| 304 | |
|---|
| 305 | int i = 0; |
|---|
| 306 | |
|---|
| 307 | for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ ) |
|---|
| 308 | { |
|---|
| 309 | i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i ); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | this->varChangeHandler( changes ); |
|---|
| 313 | |
|---|
| 314 | return i; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | /** |
|---|
| 318 | * override this function to be notified on change |
|---|
| 319 | * of your registred variables. |
|---|
| 320 | * @param id id's which have changed |
|---|
| 321 | */ |
|---|
| 322 | void Synchronizeable::varChangeHandler( std::list<int> & id ) |
|---|
| 323 | { |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | /** |
|---|
| 327 | * registers a varable to be synchronized over network |
|---|
| 328 | * @param var see src/lib/network/synchronizeable_var/ for available classes |
|---|
| 329 | */ |
|---|
| 330 | void Synchronizeable::registerVar( SynchronizeableVar * var ) |
|---|
| 331 | { |
|---|
| 332 | syncVarList.push_back( var ); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | /** |
|---|
| 336 | * registers a varable to be synchronized over network |
|---|
| 337 | * return value is passed to varChangeHandler on change |
|---|
| 338 | * @param var see src/lib/network/synchronizeable_var/ for available classes |
|---|
| 339 | * @return handle passed to varChangeHandler on changes |
|---|
| 340 | */ |
|---|
| 341 | int Synchronizeable::registerVarId( SynchronizeableVar * var ) |
|---|
| 342 | { |
|---|
| 343 | syncVarList.push_back( var ); |
|---|
| 344 | return syncVarList.size()-1; |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|