Changeset 1800
- Timestamp:
- Sep 20, 2008, 12:51:16 AM (16 years ago)
- Location:
- code/branches/network/src/network
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/network/GamestateManager.cc
r1769 r1800 61 61 62 62 bool GamestateManager::update(){ 63 cleanup();63 // cleanup(); 64 64 return getSnapshot(); 65 65 } … … 92 92 reference = new packet::Gamestate(); 93 93 reference->collectData(++id_); 94 COUT(4) << "inserting gamestate: " << reference << std::endl;95 gamestateMap.insert(std::pair<int, packet::Gamestate*>(id_, reference));96 gamestateUsed[id_]=0;94 //COUT(4) << "inserting gamestate: " << reference << std::endl; 95 //gamestateMap_.insert(std::pair<int, packet::Gamestate*>(id_, reference)); 96 // gamestateUsed[id_]=0; 97 97 return true; 98 98 } … … 104 104 * 105 105 */ 106 void GamestateManager::cleanup(){106 /* void GamestateManager::cleanup(){ 107 107 std::map<int,int>::iterator it = gamestateUsed.begin(); 108 108 while(it!=gamestateUsed.end()){ … … 124 124 it++; 125 125 } 126 } 126 }*/ 127 127 128 128 packet::Gamestate *GamestateManager::popGameState(int clientID) { … … 131 131 packet::Gamestate *gs; 132 132 int gID = ClientInformation::findClient(clientID)->getGamestateID(); 133 //COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << std::endl; 133 gs = reference->doSelection(clientID); 134 // gs = new packet::Gamestate(*reference); 135 // save the (undiffed) gamestate in the clients gamestate map 136 gamestateMap_[clientID].insert(std::pair<int, packet::Gamestate*>(gs->getID(), gs)); 134 137 //chose wheather the next gamestate is the first or not 138 packet::Gamestate *client=NULL; 135 139 if(gID != GAMESTATEID_INITIAL){ 136 packet::Gamestate *client=NULL; 137 std::map<int, packet::Gamestate*>::iterator it = gamestateMap.find(gID); 138 if(it!=gamestateMap.end()) 139 client = it->second; 140 if(client) 141 gs = reference->diff(client); 142 else 143 gs = new packet::Gamestate(*reference); 144 } else { 145 COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl; 146 gs = new packet::Gamestate(*reference); 140 std::map<unsigned int, std::map<int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(clientID); 141 if(clientMap!=gamestateMap_.end()){ 142 std::map<int, packet::Gamestate*>::iterator it = clientMap->second.find(gID); 143 if(it!=clientMap->second.end()) 144 client = it->second; 145 } 146 } 147 if(client){ 148 // COUT(3) << "diffing" << std::endl; 149 gs = gs->diff(client); 150 } 151 else{ 152 // COUT(3) << "not diffing" << std::endl; 153 gs = new packet::Gamestate(*gs); 147 154 } 148 155 assert(gs->compressData()); … … 153 160 bool GamestateManager::ack(int gamestateID, int clientID) { 154 161 ClientInformation *temp = ClientInformation::findClient(clientID); 155 if(temp==0) 156 return false; 162 assert(temp); 157 163 int curid = temp->getGamestateID(); 158 164 159 165 if(gamestateID == 0){ 160 166 temp->setGamestateID(GAMESTATEID_INITIAL); 161 if(curid!=GAMESTATEID_INITIAL){162 assert(gamestateUsed.find(curid)!=gamestateUsed.end());163 --(gamestateUsed.find(curid)->second);164 }165 167 return true; 166 168 } 167 //if(curid > gamestateID)169 168 170 assert(curid<gamestateID); 169 // the network packets got messed up170 //return true;171 171 COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl; 172 // decrease usage of gamestate and save it 173 // deleteUnusedGameState(curid); 174 //increase gamestateused 175 std::map<int, int>::iterator it = gamestateUsed.find(curid); 176 if(curid!=GAMESTATEID_INITIAL){ 177 if(it!=gamestateUsed.end()) 178 --(it->second); 179 } 180 it = gamestateUsed.find(gamestateID); 181 if(it!=gamestateUsed.end()){ 182 ++(it->second); 183 temp->setGamestateID(gamestateID); 184 } 172 std::map<int, packet::Gamestate*>::iterator it, tempit; 173 for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; it++){ 174 delete it->second; 175 tempit=it++; 176 gamestateMap_[clientID].erase(tempit); 177 } 178 temp->setGamestateID(gamestateID); 185 179 return true; 186 180 } 187 181 188 182 void GamestateManager::removeClient(ClientInformation* client){ 189 if(!client) 190 return; 191 if(client->getGamestateID()>=0) 192 gamestateUsed[client->getGamestateID()]--; 183 assert(client); 184 std::map<unsigned int, std::map<int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID()); 185 // first delete all remained gamestates 186 std::map<int, packet::Gamestate*>::iterator it; 187 for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) 188 delete it->second; 189 // now delete the clients gamestatemap 190 gamestateMap_.erase(clientMap); 193 191 } 194 192 -
code/branches/network/src/network/GamestateManager.h
r1763 r1800 81 81 void removeClient(ClientInformation *client); 82 82 private: 83 void cleanup(); // "garbage handler"83 // void cleanup(); // "garbage handler" 84 84 bool processGamestate(packet::Gamestate *gs); 85 85 86 std::map<int, packet::Gamestate*> gamestateMap; //map gsID to gamestate* 87 std::map<int, int> gamestateUsed; // save the number of clients, that use the specific gamestate 86 std::map<unsigned int, std::map<int, packet::Gamestate*> > gamestateMap_; 87 //std::map<int, packet::Gamestate*> gamestateMap; //map gsID to gamestate* 88 //std::map<int, int> gamestateUsed; // save the number of clients, that use the specific gamestate 88 89 std::map<int, packet::Gamestate*> gamestateQueue; 89 90 packet::Gamestate *reference; -
code/branches/network/src/network/Synchronisable.cc
r1794 r1800 364 364 365 365 /** 366 * This function determines, wheter the object should be saved to the bytestream (according to its sync frequency)366 * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction) 367 367 * @param id gamestate id 368 368 * @return true/false 369 369 */ 370 370 bool Synchronisable::isMyTick(unsigned int id){ 371 // return true; 371 return ( (objectMode_&state_)!=0 ); 372 } 373 374 bool Synchronisable::doSelection(unsigned int id){ 372 375 return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0); 373 376 } -
code/branches/network/src/network/Synchronisable.h
r1793 r1800 91 91 bool updateData(unsigned char*& mem, int mode=0x0); 92 92 bool isMyData(unsigned char* mem); 93 bool doSelection(unsigned int id); 93 94 void setObjectMode(int mode); 94 95 void setObjectFrequency(unsigned int freq){ objectFrequency_ = freq; } -
code/branches/network/src/network/packet/Chat.cc
r1793 r1800 55 55 : Packet(data, clientID) 56 56 { 57 messageLength_ = *(unsigned int *) &data[ _MESSAGELENGTH ];57 messageLength_ = *(unsigned int *)(data + _MESSAGELENGTH ); 58 58 } 59 59 -
code/branches/network/src/network/packet/Gamestate.cc
r1775 r1800 54 54 Packet(data, clientID) 55 55 { 56 } 57 58 Gamestate::Gamestate(unsigned char *data) 59 { 60 data_=data; 56 61 } 57 62 … … 96 101 }// stop allocate additional memory 97 102 103 if(it->doSelection(id)) 104 dataMap_[*it]=mem; // save the mem location of the synchronisable data 98 105 if(!it->getData(mem, id, mode)) 99 106 return false; // mem pointer gets automatically increased because of call by reference … … 312 319 } 313 320 321 Gamestate* Gamestate::doSelection(unsigned int clientID){ 322 assert(data_); 323 std::map<Synchronisable *, unsigned char *>::iterator it; 324 unsigned char *ndata, *tdata; 325 unsigned int nsize=0; 326 // calculate the size of the new gamestate 327 for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ 328 if(it->first->doSelection(HEADER->id)) 329 nsize+=*(unsigned int*)it->second; 330 } 331 ndata = new unsigned char[nsize+sizeof(GamestateHeader)]; 332 tdata = ndata; 333 tdata += sizeof(GamestateHeader); 334 for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ 335 if(it->first->doSelection(HEADER->id)){ 336 memcpy(tdata, it->second, *(unsigned int*)it->second); // copy over the saved data of the synchronisable 337 tdata += *(unsigned int*)it->second; 338 } 339 } 340 COUT(3) << "oldsize: " << HEADER->datasize << " newsize: " << nsize << std::endl; 341 *(GamestateHeader *)ndata = *HEADER; //copy over the header 342 GAMESTATE_HEADER(ndata)->datasize = nsize; 343 Gamestate *gs = new Gamestate(ndata); 344 assert(HEADER->datasize>=nsize); 345 return gs; 346 } 347 314 348 Gamestate *Gamestate::undiff(Gamestate *base) 315 349 { -
code/branches/network/src/network/packet/Gamestate.h
r1775 r1800 29 29 #include "Packet.h" 30 30 #include "network/Synchronisable.h" 31 #include <map> 31 32 #ifndef NDEBUG 32 33 #include "util/CRC32.h" … … 62 63 Gamestate(); 63 64 Gamestate(unsigned char *data, int clientID); 65 Gamestate(unsigned char *data); 64 66 65 67 ~Gamestate(); … … 72 74 int getBaseID(); 73 75 Gamestate *diff(Gamestate *base); 76 Gamestate* doSelection(unsigned int clientID); 74 77 Gamestate *undiff(Gamestate *base); 75 78 bool compressData(); … … 77 80 78 81 // Packet functions 82 private: 79 83 virtual unsigned int getSize() const; 80 84 virtual bool process(); … … 84 88 unsigned int calcGamestateSize(unsigned int id, int mode=0x0); 85 89 void removeObject(orxonox::ObjectListIterator<Synchronisable> &it); 86 87 private: 90 std::map<Synchronisable *, unsigned char *> dataMap_; 88 91 }; 89 92
Note: See TracChangeset
for help on using the changeset viewer.