Changeset 1751
- Timestamp:
- Sep 9, 2008, 4:31:34 PM (16 years ago)
- Location:
- code/trunk/src
- Files:
-
- 18 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/network/Client.cc
r1735 r1751 61 61 isSynched_=false; 62 62 gameStateFailure_=false; 63 isServer_ = false; 63 64 } 64 65 … … 72 73 isSynched_=false; 73 74 gameStateFailure_=false; 75 isServer_ = false; 74 76 } 75 77 … … 83 85 isSynched_=false; 84 86 gameStateFailure_=false; 87 isServer_ = false; 85 88 } 86 89 … … 151 154 if(client_connection.isConnected() && isSynched_){ 152 155 COUT(4) << "popping partial gamestate: " << std::endl; 153 /*packet::Gamestate *gs = gamestate.getGamestate();156 packet::Gamestate *gs = gamestate.getGamestate(); 154 157 if(gs){ 155 158 COUT(4) << "client tick: sending gs " << gs << std::endl; … … 157 160 COUT(3) << "Problem adding partial gamestate to queue" << std::endl; 158 161 // gs gets automatically deleted by enet callback 159 } */162 } 160 163 } 161 164 ENetEvent *event; … … 168 171 } 169 172 int gameStateID = gamestate.processGamestates(); 170 /*if(gameStateID==GAMESTATEID_INITIAL)173 if(gameStateID==GAMESTATEID_INITIAL) 171 174 if(gameStateFailure_){ 172 packet::Acknowledgement ack(GAMESTATEID_INITIAL, 0);173 if(!ack .send())175 packet::Acknowledgement *ack = new packet::Acknowledgement(GAMESTATEID_INITIAL, 0); 176 if(!ack->send()) 174 177 COUT(3) << "could not (negatively) ack gamestate" << std::endl; 175 178 else … … 183 186 isSynched_=true; 184 187 gameStateFailure_=false; 185 packet::Acknowledgement ack(gameStateID, 0);186 if(!ack .send())188 packet::Acknowledgement *ack = new packet::Acknowledgement(gameStateID, 0); 189 if(!ack->send()) 187 190 COUT(3) << "could not ack gamestate" << std::endl; 188 } */// otherwise we had no gamestate to load191 }// otherwise we had no gamestate to load 189 192 gamestate.cleanup(); 190 193 return; -
code/trunk/src/network/GamestateClient.cc
r1747 r1751 75 75 int id = GAMESTATEID_INITIAL; 76 76 bool b = saveShipCache(); 77 if(processGamestate(tempGamestate_)){ 78 if(b) 79 loadShipCache(); 80 id = tempGamestate_->getID(); 81 } 77 packet::Gamestate *processed = processGamestate(tempGamestate_); 78 assert(processed); 79 //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs 80 tempGamestate_=0; 81 gamestateMap_[processed->getID()]=processed; 82 last_diff_ = processed->getBaseID(); 83 last_gamestate_ = processed->getID(); 84 if(b) 85 loadShipCache(); 86 id = processed->getID(); 82 87 cleanup(); 83 88 return id; … … 130 135 if(myShip_){ 131 136 // unsigned char *data = new unsigned char[myShip_->getSize()]; 132 int size=myShip_->getSize (0x1);137 int size=myShip_->getSize2(0, 0x1); 133 138 if(size==0) 134 139 return false; 135 140 shipCache_ = new unsigned char [size]; 136 141 unsigned char *temp = shipCache_; 137 if(!myShip_->getData 2(temp, 0x1))142 if(!myShip_->getData(temp, 0, 0x1)) 138 143 COUT(3) << "could not save shipCache" << std::endl; 139 144 return true; … … 152 157 } 153 158 154 bool GamestateClient::processGamestate(packet::Gamestate *gs){ 155 assert(gs->decompressData()); 156 if(gs->isDiffed()) 157 assert(gs->undiff(gamestateMap_[gs->getBaseID()])); 158 return gs->spreadData(); 159 packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){ 160 if(gs->isCompressed()) 161 assert(gs->decompressData()); 162 if(gs->isDiffed()){ 163 packet::Gamestate *base = gamestateMap_[gs->getBaseID()]; 164 assert(base); 165 packet::Gamestate *undiffed = gs->undiff(base); 166 delete gs; 167 gs=undiffed; 168 COUT(3) << "successfully undiffed gamestate id: " << undiffed->getID() << std::endl; 169 } 170 if(gs->spreadData()) 171 return gs; 172 else 173 return NULL; 159 174 } 160 175 -
code/trunk/src/network/GamestateClient.h
r1747 r1751 66 66 void cleanup(); 67 67 private: 68 boolprocessGamestate(packet::Gamestate *gs);69 void removeObject(orxonox::ObjectList Iterator<Synchronisable>&it);68 packet::Gamestate *processGamestate(packet::Gamestate *gs); 69 void removeObject(orxonox::ObjectList<Synchronisable>::iterator &it); 70 70 void printGamestateMap(); 71 71 bool saveShipCache(); -
code/trunk/src/network/GamestateManager.cc
r1735 r1751 129 129 //why are we searching the same client's gamestate id as we searched in 130 130 //Server::sendGameState? 131 packet::Gamestate *gs; 131 132 int gID = ClientInformation::findClient(clientID)->getGamestateID(); 132 COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl; 133 // COUT(3) << "gamestatemap: " << &gameStateMap << std::endl; 133 //COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl; 134 134 //chose wheather the next gamestate is the first or not 135 135 if(gID != GAMESTATEID_INITIAL){ 136 // TODO something with the gamestatemap is wrong137 136 packet::Gamestate *client=NULL; 138 137 std::map<int, packet::Gamestate*>::iterator it = gamestateMap.find(gID); 139 138 if(it!=gamestateMap.end()) 140 139 client = it->second; 141 packet::Gamestate *gs;142 140 if(client) 143 141 gs = reference->diff(client); 144 142 else 145 143 gs = new packet::Gamestate(*reference); 146 gs->compressData();147 return gs;148 144 } else { 149 145 COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl; 150 // ackGameState(clientID, reference->id);151 return new packet::Gamestate(*reference);152 // return an undiffed gamestate and set appropriate flags153 }146 gs = new packet::Gamestate(*reference); 147 } 148 assert(gs->compressData()); 149 return gs; 154 150 } 155 151 … … 194 190 if(client->getGamestateID()>=0) 195 191 gamestateUsed[client->getGamestateID()]--; 196 ClientInformation::removeClient(client->getID());197 192 } 198 193 199 194 bool GamestateManager::processGamestate(packet::Gamestate *gs){ 200 assert(gs->decompressData()); 195 if(gs->isCompressed()) 196 assert(gs->decompressData()); 201 197 assert(!gs->isDiffed()); 202 198 return gs->spreadData(); -
code/trunk/src/network/Host.cc
r1735 r1751 1 /* 2 * ORXONOX - the hottest 3D action shooter ever to exist 3 * > www.orxonox.net < 4 * 5 * 6 * License notice: 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 2 11 * of the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 21 * 22 * Author: 23 * Oliver Scheuss <scheusso [at] ee.ethz.ch>, (C) 2008 24 * Co-authors: 25 * ... 26 * 27 */ 28 1 29 #include <assert.h> 2 30 -
code/trunk/src/network/Host.h
r1740 r1751 54 54 virtual ~Host(); 55 55 static Host *instance_; 56 bool isServer_; 56 57 57 58 public: … … 64 65 static void setClientID(unsigned int id){ instance_->clientID_ = id; } 65 66 static void setShipID(unsigned int id){ instance_->shipID_ = id; } 67 static bool isServer(){ return instance_->isServer_; } 66 68 private: 67 69 unsigned int clientID_; -
code/trunk/src/network/Server.cc
r1747 r1751 54 54 #include "packet/Packet.h" 55 55 #include "packet/Welcome.h" 56 #include <util/Convert.h> 56 57 57 58 namespace network … … 68 69 connection = new ConnectionManager(); 69 70 gamestates_ = new GamestateManager(); 71 isServer_ = true; 70 72 } 71 73 … … 74 76 connection = new ConnectionManager(port); 75 77 gamestates_ = new GamestateManager(); 78 isServer_ = true; 76 79 } 77 80 … … 85 88 connection = new ConnectionManager(port, bindAddress); 86 89 gamestates_ = new GamestateManager(); 90 isServer_ = true; 87 91 } 88 92 … … 96 100 connection = new ConnectionManager(port, bindAddress); 97 101 gamestates_ = new GamestateManager(); 102 isServer_ = true; 98 103 } 99 104 … … 260 265 //std::cout << "adding gamestate" << std::endl; 261 266 gs->setClientID(cid); 262 assert(gs->compressData());263 267 if ( !gs->send() ){ 264 268 COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; … … 294 298 return false; 295 299 } 296 if(temp->prev()->getBegin()) { //not good if you use anything else than insertBack 297 temp->prev()->setID(0); //bugfix: not necessary but usefull 300 if(temp==ClientInformation::getBegin()) { //not good if you use anything else than insertBack 298 301 temp->setID(1); 299 302 } … … 324 327 w->setClientID(temp->getID()); 325 328 assert(w->send()); 329 packet::Gamestate *g = new packet::Gamestate(); 330 g->setClientID(temp->getID()); 331 assert(g->collectData(0)); 332 assert(g->compressData()); 333 assert(g->send()); 326 334 return true; 327 335 } … … 349 357 no->setTransDamp(75); 350 358 no->setRotDamp(1.0); 351 no->setCamera( "cam_"+client->getID());359 no->setCamera(std::string("cam_") + convertToString(client->getID())); 352 360 no->create(); 353 no->setBacksync(true);354 361 355 362 return true; … … 365 372 if(!client) 366 373 return false; 374 gamestates_->removeClient(client); 367 375 while(it){ 368 376 if(it->objectID!=client->getShipID()){ -
code/trunk/src/network/Synchronisable.cc
r1735 r1751 63 63 static int idCounter=0; 64 64 datasize=0; 65 objectFrequency_=1; 66 objectMode_=0x1; // by default do not send data to servere 65 67 objectID=idCounter++; 66 68 syncList = new std::list<synchronisableVariable *>; … … 81 83 } 82 84 85 83 86 void Synchronisable::setClient(bool b){ 84 87 if(b) // client … … 95 98 classID = *(unsigned int*)(mem+2*sizeof(unsigned int)); 96 99 100 if(size==3*sizeof(unsigned int)) //not our turn, dont do anything 101 return true; 102 97 103 orxonox::Identifier* id = ID(classID); 98 104 if(!id){ 99 105 COUT(3) << "We could not identify a new object; classid: " << classID << " uint: " << (unsigned int)classID << " objectID: " << objectID << " size: " << size << std::endl; 106 assert(0); 100 107 return false; // most probably the gamestate is corrupted 101 108 } … … 140 147 syncList->push_back(temp); 141 148 } 142 143 144 /**145 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct146 * Difference to the above function:147 * takes a pointer to already allocated memory (must have at least getSize bytes length)148 * structure of the bitstream:149 * (var1_size,var1,var2_size,var2,...)150 * varx_size: size = sizeof(int)151 * varx: size = varx_size152 * @return data containing all variables and their sizes153 */154 syncData Synchronisable::getData(unsigned char *mem, int mode){155 //std::cout << "inside getData" << std::endl;156 if(mode==0x0)157 mode=state_;158 if(classID==0)159 COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;160 this->classID=this->getIdentifier()->getNetworkID();161 std::list<synchronisableVariable *>::iterator i;162 syncData retVal;163 retVal.objectID=this->objectID;164 retVal.classID=this->classID;165 retVal.length=getSize();166 COUT(5) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl;167 retVal.data=mem;168 // copy to location169 int n=0; //offset170 for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){171 //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));172 if( ((*i)->mode & mode) == 0 ){173 COUT(5) << "not getting data: " << std::endl;174 continue; // this variable should only be received175 }176 switch((*i)->type){177 case DATA:178 memcpy( (void *)(retVal.data+n), (void*)((*i)->var), (*i)->size);179 n+=(*i)->size;180 break;181 case STRING:182 memcpy( (void *)(retVal.data+n), (void *)&((*i)->size), sizeof(int) );183 n+=sizeof(int);184 const char *data = ( ( *(std::string *) (*i)->var).c_str());185 memcpy( retVal.data+n, (void*)data, (*i)->size);186 COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;187 n+=(*i)->size;188 break;189 }190 }191 return retVal;192 }193 149 194 150 /** 195 151 * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct 196 * Difference to the above function:152 * Difference to the above function: 197 153 * takes a pointer to already allocated memory (must have at least getSize bytes length) 198 * structure of the bitstream:154 * structure of the bitstream: 199 155 * (var1_size,var1,var2_size,var2,...) 200 156 * varx_size: size = sizeof(int) … … 202 158 * @return data containing all variables and their sizes 203 159 */ 204 bool Synchronisable::getData 2(unsigned char*& mem, int mode){160 bool Synchronisable::getData(unsigned char*& mem, unsigned int id, int mode){ 205 161 //std::cout << "inside getData" << std::endl; 206 162 unsigned int tempsize = 0; … … 209 165 if(classID==0) 210 166 COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl; 211 this->classID=this->getIdentifier()->getNetworkID(); 167 this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this 212 168 std::list<synchronisableVariable *>::iterator i; 213 169 unsigned int size; 214 size=getSize2( mode);170 size=getSize2(id, mode); 215 171 216 172 // start copy header … … 224 180 // end copy header 225 181 182 //if this tick is we dont synchronise, then abort now 183 if(!isMyTick(id)) 184 return true; 226 185 227 186 COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl; … … 254 213 } 255 214 256 /*bool Synchronisable::getData(Bytestream& bs, int mode)257 {258 //std::cout << "inside getData" << std::endl;259 if(mode==0x0)260 mode=state_;261 if(classID==0)262 COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;263 this->classID=this->getIdentifier()->getNetworkID();264 std::list<synchronisableVariable *>::iterator i;265 bs << this->getSize();266 bs << this->objectID;267 bs << this->classID;268 // copy to location269 for(i=syncList->begin(); i!=syncList->end(); ++i){270 if( ((*i)->mode & mode) == 0 ){271 COUT(5) << "not getting data: " << std::endl;272 continue; // this variable should only be received273 }274 switch((*i)->type){275 case DATA:276 bs << *(*i)->var;277 //std::memcpy( (void *)(retVal.data+n), (void*)((*i)->var), (*i)->size);278 //n+=(*i)->size;279 break;280 case STRING:281 bs << *(String *)((*i)->var);282 //memcpy( (void *)(retVal.data+n), (void *)&((*i)->size), sizeof(int) );283 //n+=sizeof(int);284 //const char *data = ( ( *(std::string *) (*i)->var).c_str());285 //std::memcpy( retVal.data+n, (void*)data, (*i)->size);286 //COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;287 //n+=(*i)->size;288 break;289 }290 }291 return true;292 }*/293 294 295 /**296 * This function takes a syncData struct and takes it to update the variables297 * @param vars data of the variables298 * @return true/false299 */300 bool Synchronisable::updateData(syncData vars, int mode){301 if(mode==0x0)302 mode=state_;303 unsigned char *data=vars.data;304 std::list<synchronisableVariable *>::iterator i;305 if(syncList->empty()){306 COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;307 return false;308 }309 COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl;310 for(i=syncList->begin(); i!=syncList->end(); i++){311 if( ((*i)->mode ^ mode) == 0 ){312 COUT(5) << "synchronisable: not updating variable " << std::endl;313 continue; // this variable should only be set314 }315 COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;316 bool callback=false;317 switch((*i)->type){318 case DATA:319 if((*i)->callback) // check whether this variable changed (but only if callback was set)320 if(strncmp((char *)(*i)->var, (char *)data, (*i)->size)!=0)321 callback=true;322 memcpy((void*)(*i)->var, data, (*i)->size);323 data+=(*i)->size;324 break;325 case STRING:326 (*i)->size = *(int *)data;327 COUT(5) << "string size: " << (*i)->size << std::endl;328 data+=sizeof(int);329 if((*i)->callback) // check whether this string changed330 if( *(std::string *)((*i)->var) != std::string((char *)data) )331 callback=true;332 *((std::string *)((*i)->var)) = std::string((const char*)data);333 COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl;334 data += (*i)->size;335 break;336 }337 // call the callback function, if defined338 if(callback && (*i)->callback)339 (*i)->callback->call();340 }341 return true;342 }343 215 344 216 /** … … 368 240 assert(this->objectID==objectID); 369 241 assert(this->classID==classID); 242 if(size==3*sizeof(unsigned int)) //if true, only the header is available 243 return true; 244 //assert(0); 370 245 371 246 COUT(5) << "Synchronisable: objectID " << objectID << ", classID " << classID << " size: " << size << " synchronising data" << std::endl; … … 408 283 * @return amount of bytes 409 284 */ 410 int Synchronisable::getSize(int mode){ 285 int Synchronisable::getSize(unsigned int id, int mode){ 286 if(!isMyTick(id)) 287 return 0; 411 288 int tsize=0; 412 289 if(mode==0x0) … … 435 312 * @return amount of bytes 436 313 */ 437 int Synchronisable::getSize2(int mode){ 438 return 3*sizeof(unsigned int) + getSize( mode ); 314 int Synchronisable::getSize2(unsigned int id, int mode){ 315 return sizeof(synchronisableHeader) + getSize( id, mode ); 316 } 317 318 /** 319 * 320 * @param id 321 * @return 322 */ 323 bool Synchronisable::isMyTick(unsigned int id){ 324 // return true; 325 return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0); 439 326 } 440 327 … … 453 340 } 454 341 455 void Synchronisable::setBacksync(bool sync){ 456 backsync_=sync; 457 } 458 459 bool Synchronisable::getBacksync(){ 460 return backsync_; 461 } 342 void Synchronisable::setObjectMode(int mode){ 343 assert(mode==0x1 || mode==0x2 || mode==0x3); 344 objectMode_=mode; 345 } 346 462 347 463 348 } -
code/trunk/src/network/Synchronisable.h
r1747 r1751 34 34 #include <list> 35 35 #include "core/OrxonoxClass.h" 36 #include "util/XMLIncludes.h" 36 37 #include "NetworkCallback.h" 37 38 … … 43 44 }; 44 45 45 struct sync Data{46 unsigned int length;46 struct synchronisableHeader{ 47 unsigned int size; 47 48 unsigned int objectID; 48 49 unsigned int classID; 49 unsigned char *data;50 50 }; 51 51 … … 67 67 class _NetworkExport Synchronisable : virtual public orxonox::OrxonoxClass{ 68 68 public: 69 69 70 70 virtual ~Synchronisable(); 71 71 unsigned int objectID; … … 73 73 74 74 void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0); 75 // syncData getData(); 76 syncData getData(unsigned char *mem, int mode=0x0); 77 bool getData2(unsigned char*& men, int mode=0x0); 78 //bool getData(Bytestream& bs, int mode=0x0); 79 int getSize(int mode=0x0); 80 int getSize2(int mode=0x0); 81 bool updateData(syncData vars, int mode=0x0); 75 bool getData(unsigned char*& men, unsigned int id, int mode=0x0); 76 int getSize2(unsigned int id, int mode=0x0); 82 77 bool updateData(unsigned char*& mem, int mode=0x0); 83 78 bool isMyData(unsigned char* mem); 84 void setBacksync(bool sync); 85 bool getBacksync(); 79 void setObjectMode(int mode); 80 void setObjectFrequency(unsigned int freq){ objectFrequency_ = freq; } 81 86 82 virtual void registerAllVariables()=0; 87 83 virtual bool create(); … … 92 88 Synchronisable(); 93 89 private: 94 /* bool removeObject(ObjectList<Synchronisable>::iterator it);*/95 90 int getSize(unsigned int id, int mode=0x0); 91 bool isMyTick(unsigned int id); 96 92 std::list<synchronisableVariable *> *syncList; 97 93 int datasize; 98 94 static int state_; // detemines wheter we are server (default) or client 99 95 bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server) 96 unsigned int objectFrequency_; 97 int objectMode_; 100 98 }; 101 99 } -
code/trunk/src/network/packet/Acknowledgement.cc
r1735 r1751 45 45 data_=new unsigned char[ getSize() ]; 46 46 *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Acknowledgement; 47 *(unsigned int *) &data_[ _ACKID ]= id;47 *(unsigned int *)(data_ + _ACKID ) = id; 48 48 clientID_=clientID; 49 49 } -
code/trunk/src/network/packet/Gamestate.cc
r1747 r1751 41 41 namespace packet { 42 42 43 44 #define GAMESTATE_START(data) data + sizeof(GamestateHeader) 43 #define GAMESTATE_START(data) (data + sizeof(GamestateHeader)) 45 44 #define GAMESTATE_HEADER(data) ((GamestateHeader *)data) 46 45 #define HEADER GAMESTATE_HEADER(data_) … … 63 62 { 64 63 int tempsize=0, currentsize=0; 65 assert(data_==0 /*&& bs_==0*/);66 int size = calcGamestateSize( mode);64 assert(data_==0); 65 int size = calcGamestateSize(id, mode); 67 66 68 67 COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl; 69 //retval->data = (unsigned char*)malloc(size);70 68 if(size==0) 71 69 return false; 72 70 data_ = new unsigned char[size + sizeof(GamestateHeader)]; 73 //bs_ = new Bytestream(data_+sizeof(GamestateHeader), size);74 71 if(!data_){ 75 72 COUT(2) << "GameStateManager: could not allocate memory" << std::endl; … … 82 79 orxonox::ObjectList<Synchronisable>::iterator it; 83 80 for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){ 84 tempsize=it->getSize2( mode);81 tempsize=it->getSize2(id, mode); 85 82 86 83 if(currentsize+tempsize > size){ … … 90 87 int addsize=tempsize; 91 88 while(++temp) 92 addsize+=temp->getSize2( mode);89 addsize+=temp->getSize2(id, mode); 93 90 data_ = (unsigned char *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize); 94 91 if(!data_) … … 97 94 }// stop allocate additional memory 98 95 99 if(!it->getData 2(mem, mode))96 if(!it->getData(mem, id, mode)) 100 97 return false; // mem pointer gets automatically increased because of call by reference 101 98 // increase size counter by size of current synchronisable … … 121 118 bool Gamestate::spreadData(int mode) 122 119 { 123 assert(data_ && !HEADER->compressed && !HEADER->diffed); 120 assert(data_); 121 assert(!HEADER->compressed); 122 assert(!HEADER->diffed); 124 123 unsigned int size, objectID, classID; 125 124 unsigned char *mem=data_+sizeof(GamestateHeader); … … 142 141 //fabricate the new synchronisable 143 142 if(!Synchronisable::fabricate(mem, mode)) 144 /*return false*/;143 return false; 145 144 it=orxonox::ObjectList<Synchronisable>::end(); 146 145 } … … 175 174 } 176 175 176 bool Gamestate::operator==(packet::Gamestate gs){ 177 unsigned char *d1 = data_+sizeof(GamestateHeader); 178 unsigned char *d2 = gs.data_+sizeof(GamestateHeader); 179 assert(!isCompressed()); 180 assert(!gs.isCompressed()); 181 while(d1<data_+HEADER->normsize) 182 { 183 if(*d1!=*d2) 184 return false; 185 d1++; 186 d2++; 187 } 188 return true; 189 } 190 177 191 bool Gamestate::process() 178 192 { … … 183 197 { 184 198 assert(HEADER); 185 uLongf buffer = (uLongf)((HEADER->normsize + 12)*1.01)+1; 199 assert(!HEADER->compressed); 200 uLongf buffer = (uLongf)(((HEADER->normsize + 12)*1.01)+1); 186 201 if(buffer==0) 187 202 return false; … … 189 204 unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)]; 190 205 unsigned char *dest = GAMESTATE_START(ndata); 206 //unsigned char *dest = new unsigned char[buffer]; 207 unsigned char *source = GAMESTATE_START(data_); 191 208 int retval; 192 retval = compress( dest, &buffer, GAMESTATE_START(data_), (uLong)(HEADER->normsize) );209 retval = compress( dest, &buffer, source, (uLong)(HEADER->normsize) ); 193 210 switch ( retval ) { 194 211 case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; 195 case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; 196 return false; 197 case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; 198 return false; 199 case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; 200 return false; 201 } 212 case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false; 213 case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false; 214 case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false; 215 } 216 #ifndef NDEBUG 217 //decompress and compare the start and the decompressed data 218 unsigned char *rdata = new unsigned char[HEADER->normsize+sizeof(GamestateHeader)]; 219 unsigned char *d2 = GAMESTATE_START(rdata); 220 uLongf length2 = HEADER->normsize; 221 uncompress(d2, &length2, dest, buffer); 222 for(unsigned int i=0; i<HEADER->normsize; i++){ 223 assert(*(source+i)==*(d2+i)); 224 } 225 delete[] rdata; 226 #endif 202 227 203 228 //copy and modify header 204 HEADER->compsize = buffer; 205 HEADER->compressed = true; 229 #ifndef NDEBUG 230 HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->normsize); 231 #endif 206 232 *GAMESTATE_HEADER(ndata) = *HEADER; 207 233 //delete old data … … 209 235 //save new data 210 236 data_ = ndata; 237 HEADER->compsize = buffer; 238 HEADER->compressed = true; 211 239 assert(HEADER->compressed); 212 240 COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl; … … 215 243 bool Gamestate::decompressData() 216 244 { 245 assert(HEADER); 217 246 assert(HEADER->compressed); 218 //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl; 219 int normsize = HEADER->normsize; 220 int compsize = HEADER->compsize; 221 int bufsize; 222 if(normsize < compsize) 223 bufsize = compsize; 224 else 225 bufsize = normsize; 226 if(bufsize==0) 227 return false; 247 COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", normsize: " << HEADER->normsize << ", compsize: " << HEADER->compsize << std::endl; 248 unsigned int normsize = HEADER->normsize; 249 unsigned int compsize = HEADER->compsize; 250 unsigned int bufsize; 251 assert(compsize<=normsize); 252 bufsize = normsize; 253 assert(bufsize!=0); 228 254 unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)]; 229 255 unsigned char *dest = ndata + sizeof(GamestateHeader); 256 unsigned char *source = data_ + sizeof(GamestateHeader); 230 257 int retval; 231 uLongf length= normsize;232 retval = uncompress( dest, &length, data_+sizeof(GamestateHeader), (uLong)compsize );258 uLongf length=bufsize; 259 retval = uncompress( dest, &length, source, (uLong)compsize ); 233 260 switch ( retval ) { 234 261 case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; … … 237 264 case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false; 238 265 } 239 240 HEADER->compressed = false; 266 #ifndef NDEBUG 267 assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->normsize)); 268 #endif 269 241 270 //copy over the header 242 271 *GAMESTATE_HEADER(ndata) = *HEADER; 243 272 //delete old (compressed data) 244 273 delete[] data_; 245 //set new pointers and create bytestream274 //set new pointers 246 275 data_ = ndata; 247 //bs_ = new Bytestream(getGs(), GAMESTATE_HEADER->normsize); 248 276 HEADER->compressed = false; 277 assert(HEADER->normsize==normsize); 278 assert(HEADER->compsize==compsize); 249 279 return true; 250 280 } … … 252 282 Gamestate *Gamestate::diff(Gamestate *base) 253 283 { 284 assert(HEADER); 285 assert(!HEADER->compressed); 286 assert(!HEADER->diffed); 254 287 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; 255 288 unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); … … 277 310 *GAMESTATE_HEADER(ndata) = *HEADER; 278 311 GAMESTATE_HEADER(ndata)->diffed = true; 279 Gamestate *g = new Gamestate(ndata, 0); 312 GAMESTATE_HEADER(ndata)->base_id = base->getID(); 313 Gamestate *g = new Gamestate(ndata, getClientID()); 314 g->flags_=flags_; 315 g->packetDirection_ = packetDirection_; 280 316 return g; 281 317 } … … 283 319 Gamestate *Gamestate::undiff(Gamestate *base) 284 320 { 285 assert(this && base); 321 assert(this && base);assert(HEADER); 322 assert(HEADER->diffed); 286 323 assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed); 287 324 //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; … … 310 347 *GAMESTATE_HEADER(ndata) = *HEADER; 311 348 GAMESTATE_HEADER(ndata)->diffed = false; 312 Gamestate *g = new Gamestate(ndata, 0); 349 Gamestate *g = new Gamestate(ndata, getClientID()); 350 g->flags_=flags_; 351 g->packetDirection_ = packetDirection_; 352 assert(!g->isDiffed()); 353 assert(!g->isCompressed()); 313 354 return g; 314 355 } 315 356 316 357 317 unsigned int Gamestate::calcGamestateSize( int mode)358 unsigned int Gamestate::calcGamestateSize(unsigned int id, int mode) 318 359 { 319 360 int size=0; … … 322 363 // get total size of gamestate 323 364 for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it) 324 size+=it->getSize2( mode); // size of the actual data of the synchronisable365 size+=it->getSize2(id, mode); // size of the actual data of the synchronisable 325 366 // size+=sizeof(GamestateHeader); 326 367 return size; … … 342 383 } 343 384 385 bool Gamestate::isCompressed(){ 386 return HEADER->compressed; 387 } 388 344 389 int Gamestate::getBaseID(){ 345 390 return HEADER->base_id; -
code/trunk/src/network/packet/Gamestate.h
r1747 r1751 29 29 #include "Packet.h" 30 30 #include "network/Synchronisable.h" 31 //#include "util/Bytestream.h" 31 #ifndef NDEBUG 32 #include "util/CRC32.h" 33 #endif 32 34 #include "core/CoreIncludes.h" 33 35 … … 48 50 bool complete; // wheter it is a complete gamestate or only partial 49 51 bool compressed; 52 #ifndef NDEBUG 53 uint32_t crc32; 54 #endif 50 55 }; 51 56 … … 64 69 int getID(); 65 70 bool isDiffed(); 71 bool isCompressed(); 66 72 int getBaseID(); 67 73 Gamestate *diff(Gamestate *base); … … 74 80 virtual bool process(); 75 81 76 82 bool operator ==(packet::Gamestate gs); 77 83 private: 78 unsigned int calcGamestateSize( int mode=0x0);79 void removeObject(orxonox::ObjectList Iterator<Synchronisable>&it);84 unsigned int calcGamestateSize(unsigned int id, int mode=0x0); 85 void removeObject(orxonox::ObjectList<Synchronisable>::iterator &it); 80 86 81 87 -
code/trunk/src/network/packet/Packet.cc
r1735 r1751 92 92 93 93 Packet::~Packet(){ 94 if(enetPacket_) 94 if(enetPacket_){ 95 assert(enetPacket_->freeCallback==0); 95 96 enet_packet_destroy(enetPacket_); 97 } 96 98 if(data_) 97 99 delete[] data_; … … 109 111 } 110 112 enetPacket_ = enet_packet_create(getData(), getSize(), getFlags()); 111 //enetPacket_->freeCallback = &Packet::deletePacket;112 enetPacket_->freeCallback = &blub;113 enetPacket_->freeCallback = &Packet::deletePacket; 114 // enetPacket_->freeCallback = &blub; 113 115 packetMap_[enetPacket_] = this; 114 116 } 115 /*switch( *(ENUM::Type *)(data_ + _PACKETID) ) 117 #ifndef NDEBUG 118 switch( *(ENUM::Type *)(data_ + _PACKETID) ) 116 119 { 117 120 case ENUM::Acknowledgement: … … 120 123 case ENUM::Gamestate: 121 124 case ENUM::Welcome: 122 COUT(3) << "welcome" << std::endl; 123 p = new Welcome( data, clientID ); 125 break; 124 126 default: 125 assert(0); // TODO: repair this127 assert(0); //there was some error, if this is the case 126 128 break; 127 }*/ 128 network::Host::addPacket( enetPacket_, clientID_); 129 } 130 #endif 131 ENetPacket *temp = enetPacket_; 129 132 enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet 133 network::Host::addPacket( temp, clientID_); 130 134 return true; 131 135 } … … 168 172 void Packet::deletePacket(ENetPacket *packet){ 169 173 assert(packetMap_[packet]); 174 assert(packetMap_[packet]->enetPacket_==0); 170 175 delete packetMap_[packet]; 171 176 } -
code/trunk/src/network/packet/Packet.h
r1735 r1751 78 78 enet_uint32 flags_; 79 79 int clientID_; 80 ENUM::Direction packetDirection_; 80 81 unsigned char *data_; 81 82 private: 82 83 static std::map<ENetPacket *, Packet *> packetMap_; 83 84 ENetPacket *enetPacket_; 84 ENUM::Direction packetDirection_;85 85 }; 86 86 -
code/trunk/src/network/packet/Welcome.cc
r1735 r1751 32 32 #include "Welcome.h" 33 33 #include "network/Host.h" 34 #include "network/Synchronisable.h" 34 35 #include "core/CoreIncludes.h" 35 36 #include <assert.h> … … 79 80 Host::setShipID(shipID); 80 81 COUT(3) << "Welcome set clientId: " << clientID << " shipID: " << shipID << std::endl; 82 Synchronisable::setClient(true); 81 83 delete this; 82 84 return true; -
code/trunk/src/orxonox/objects/Model.cc
r1747 r1751 45 45 CreateFactory(Model); 46 46 47 /** 48 * 49 */ 47 50 Model::Model() 48 51 { 49 52 RegisterObject(Model); 50 53 registerAllVariables(); 54 if(this->isExactlyA(Class(Model))) 55 setObjectFrequency(1); 51 56 } 52 57 -
code/trunk/src/orxonox/objects/SpaceShip.cc
r1747 r1751 78 78 return *it; 79 79 } 80 return 0;80 assert(0); 81 81 } 82 82 … … 165 165 if(network::Host::running()) 166 166 COUT(3) << "this id: " << this->objectID << " myShipID: " << network::Host::getShipID() << std::endl; 167 if(network::Host::running() && objectID == network::Host::getShipID()) 167 if(network::Host::running() && objectID == network::Host::getShipID()){ 168 if(!network::Host::isServer()) 169 setObjectMode(0x3); 168 170 myShip_=true; 171 } 169 172 else 170 173 this->setRadarObjectColour(this->getProjectileColour()); … … 242 245 243 246 // START CREATING ADDITIONAL EFFECTS 244 this->backlight_ = new Backlight(this->maxSpeed_, 0.8); 245 this->attachObject(this->backlight_); 246 this->backlight_->setPosition(-2.35, 0, 0.2); 247 this->backlight_->setColour(this->getProjectileColour()); 248 249 this->smoke_ = new ParticleSpawner(); 250 this->smoke_->setParticle("Orxonox/smoke5", LODParticle::normal, 0, 0, 3); 251 this->attachObject(this->smoke_); 252 253 this->fire_ = new ParticleSpawner(); 254 this->fire_->setParticle("Orxonox/fire3", LODParticle::normal, 0, 0, 1); 255 this->attachObject(this->fire_); 247 /*if(!network::Host::running() || network::Host::isServer()){ 248 this->backlight_ = new Backlight(this->maxSpeed_, 0.8); 249 this->attachObject(this->backlight_); 250 this->backlight_->setPosition(-2.35, 0, 0.2); 251 this->backlight_->setColour(this->getProjectileColour()); 252 253 this->smoke_ = new ParticleSpawner(); 254 this->smoke_->setParticle("Orxonox/smoke5", LODParticle::normal, 0, 0, 3); 255 this->attachObject(this->smoke_); 256 257 this->fire_ = new ParticleSpawner(); 258 this->fire_->setParticle("Orxonox/fire3", LODParticle::normal, 0, 0, 1); 259 this->attachObject(this->fire_); 260 }*/ 256 261 // END CREATING ADDITIONAL EFFECTS 257 262 … … 357 362 if(myShip_){ 358 363 COUT(3) << "requesting focus for camera" << std::endl; 359 this->setBacksync(true);360 364 //CameraHandler::getInstance()->requestFocus(cam_); 361 365 if(this->isExactlyA(Class(SpaceShip))){ … … 463 467 } 464 468 465 projectile->set Backsync(true);469 projectile->setObjectMode(0x3); 466 470 this->timeToReload_ = this->reloadTime_; 467 471 } -
code/trunk/src/util/CMakeLists.txt
r1747 r1751 3 3 Clipboard.cc 4 4 Error.cc 5 CRC32.cc 5 6 ExprParser.cc 6 7 Math.cc
Note: See TracChangeset
for help on using the changeset viewer.