Changeset 1199 for code/branches/network3/src/network/GameStateManager.cc
- Timestamp:
- Apr 28, 2008, 11:09:34 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network3/src/network/GameStateManager.cc
r1173 r1199 52 52 { 53 53 GameStateManager::GameStateManager(ClientInformation *head) { 54 id =0;54 id_=0; 55 55 head_=head; 56 56 } … … 61 61 void GameStateManager::update(){ 62 62 cleanup(); 63 reference = getSnapshot(id); 64 gameStateMap.insert(std::pair<int, GameState*>(id, reference)); 65 gameStateUsed[id]=0; 66 ++id; 63 reference = getSnapshot(); 64 COUT(4) << "inserting gamestate: " << reference << std::endl; 65 gameStateMap.insert(std::pair<int, GameState*>(id_, reference)); 66 gameStateUsed[id_]=0; 67 printGameStates(); 67 68 return; 68 69 } … … 79 80 while(it!=gameStateUsed.end()){ 80 81 if( (*it).second <= 0 ){ 81 free(gameStateMap[(*it).first]->data); 82 COUT(4) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl; 83 delete[] gameStateMap[(*it).first]->data; 82 84 delete gameStateMap[(*it).first]; 83 85 gameStateMap.erase((*it).first); 84 86 gameStateUsed.erase(it++); 85 }else //as soon as we got a used gamestate break here because we could use newer gamestates in future 87 continue; 88 }else if(id_-it->first<=KEEP_GAMESTATES){ //as soon as we got a used gamestate break here because we could use newer gamestates in future but only if we do not exceed KEEP_GAMESTATES # of gamestates in cache 89 COUT(4) << "breaking " << std::endl; 86 90 break; 91 } 92 it++; 87 93 } 88 94 } … … 92 98 //Server::sendGameState? 93 99 int gID = head_->findClient(clientID)->getGamestateID(); 94 COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id << " diffed from: " << gID << " (not diffed yet)" << std::endl;95 100 COUT(4) << "G.St.Man: popgamestate: sending gstate_id: " << id_ << " diffed from: " << gID << " (not diffed yet)" << std::endl; 101 // COUT(3) << "gamestatemap: " << &gameStateMap << std::endl; 96 102 //chose wheather the next gamestate is the first or not 97 103 if(gID != GAMESTATEID_INITIAL){ 98 GameState *client = gameStateMap[gID]; 104 // TODO something with the gamestatemap is wrong 105 GameState *client = gameStateMap.find(gID)->second; 99 106 GameState *server = reference; 100 107 //head_->findClient(clientID)->setGamestateID(id); 101 return encode(client, server); 108 COUT(3) << "client: " << client << " server: " << server << " gamestatemap: " << &gameStateMap << std::endl; 109 if(client) 110 return encode(client, server); 111 else 112 return encode(server); 102 113 } else { 114 COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl; 103 115 GameState *server = reference; 116 ackGameState(clientID, reference->id); 104 117 //head_->findClient(clientID)->setGamestateID(id); 105 118 return encode(server); … … 113 126 * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list 114 127 */ 115 GameState *GameStateManager::getSnapshot( int id)128 GameState *GameStateManager::getSnapshot() 116 129 { 117 130 //std::cout << "begin getSnapshot" << std::endl; 118 131 //the size of the gamestate 119 132 int totalsize=0; 120 int memsize= 1000;133 int memsize=0; 121 134 //the size of one specific synchronisable 122 135 int tempsize=0; … … 127 140 128 141 GameState *retval=new GameState; //return value 129 retval->id= id++;142 retval->id=++id_; 130 143 COUT(4) << "G.ST.Man: producing gamestate with id: " << retval->id << std::endl; 131 144 // offset of memory functions … … 133 146 // get total size of gamestate 134 147 for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){ 135 size+=it->getSize(); 136 size+=3*sizeof(int); 137 } 138 retval->data = (unsigned char*)malloc(size); 148 size+=it->getSize(); // size of the actual data of the synchronisable 149 size+=3*sizeof(int); // size of datasize, classID and objectID 150 } 151 //retval->data = (unsigned char*)malloc(size); 152 retval->data = new unsigned char[size]; 139 153 if(!retval->data){ 140 154 COUT(2) << "GameStateManager: could not allocate memory" << std::endl; … … 148 162 // add place for data and 3 ints (length,classid,objectid) 149 163 totalsize+=tempsize+3*sizeof(int); 150 // allocate additional space151 if( (totalsize+tempsize) > memsize){152 COUT( 5) << "G.St.Man: need additional memory" << std::endl;164 // allocate+tempsize additional space 165 if(totalsize > size){ 166 COUT(3) << "G.St.Man: need additional memory" << std::endl; 153 167 // if(tempsize < 1000){ 154 168 // retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000); … … 190 204 GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) { 191 205 COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl; 192 //GameStater = diff(a,b);206 GameState *r = diff(a,b); 193 207 //r.diffed = true; 194 GameState *r = b;195 r->diffed = false;208 // GameState *r = b; 209 // r->diffed = false; 196 210 //return compress_(r); 197 211 GameStateCompressed *g = new GameStateCompressed; 198 g->base_id = b->base_id;199 g->id = b->id;200 g->diffed = b->diffed;201 g->data = b->data;202 g->normsize = b->size;203 g->compsize = b->size;212 g->base_id = r->base_id; 213 g->id = r->id; 214 g->diffed = r->diffed; 215 g->data = r->data; 216 g->normsize = r->size; 217 g->compsize = r->size; 204 218 return g; 205 219 } … … 226 240 else 227 241 dest_length=b->size; 228 unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 242 //unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char)); 243 unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)]; 229 244 while(of<a->size && of<b->size){ 230 245 *(dp+of)=*(ap+of)^*(bp+of); // do the xor … … 263 278 uLongf buffer = (uLongf)((a->size + 12)*1.01)+1; 264 279 //COUT(4) << "size: " << size << ", buffer: " << buffer << std::endl; 265 unsigned char* dest = (unsigned char*)malloc( buffer ); 280 //unsigned char* dest = (unsigned char*)malloc( buffer ); 281 unsigned char *dest = new unsigned char[buffer]; 266 282 //COUT(4) << "dest: " << dest << std::endl; 267 283 int retval; … … 296 312 void GameStateManager::ackGameState(int clientID, int gamestateID) { 297 313 ClientInformation *temp = head_->findClient(clientID); 298 int curid = temp->getID(); 314 int curid = temp->getGamestateID(); 315 COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl; 299 316 // decrease usage of gamestate and save it 300 deleteUnusedGameState(curid);317 // deleteUnusedGameState(curid); 301 318 //increase gamestateused 302 ++gameStateUsed.find(gamestateID)->second; 319 --(gameStateUsed.find(curid)->second); 320 ++(gameStateUsed.find(gamestateID)->second); 303 321 temp->setGamestateID(gamestateID); 304 322 /* … … 308 326 } 309 327 310 bool GameStateManager::deleteUnusedGameState(int gamestateID) { 311 int used = --(gameStateUsed.find(gamestateID)->second); 312 if(id-gamestateID>KEEP_GAMESTATES && used==0){ 313 // delete gamestate 314 delete gameStateMap.find(gamestateID)->second; 315 gameStateMap.erase(gamestateID); 316 return true; 317 } 318 return false; 328 bool GameStateManager::printGameStates() { 329 std::map<int, GameState*>::iterator it; 330 COUT(4) << "gamestates: "; 331 for(it = gameStateMap.begin(); it!=gameStateMap.end(); it++){ 332 COUT(4) << (*it).first << ":" << (*it).second << " | "; 333 } 334 COUT(4) << std::endl; 319 335 } 320 336
Note: See TracChangeset
for help on using the changeset viewer.