Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network3/src/network/GameStateManager.cc @ 1199

Last change on this file since 1199 was 1199, checked in by scheusso, 16 years ago

diffing work now: wohoo

File size: 12.2 KB
Line 
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, (C) 2007
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29//
30// C++ Implementation: GameStateManager
31//
32// Description:
33//
34//
35// Author:  Oliver Scheuss, (C) 2007
36//
37// Copyright: See COPYING file that comes with this distribution
38//
39//
40
41#include "GameStateManager.h"
42
43#include <utility>
44#include <iostream>
45#include <zlib.h>
46
47#include "core/CoreIncludes.h"
48#include "ClientInformation.h"
49#include "Synchronisable.h"
50
51namespace network
52{
53  GameStateManager::GameStateManager(ClientInformation *head) {
54    id_=0;
55    head_=head;
56  }
57
58  GameStateManager::~GameStateManager() {
59  }
60
61  void GameStateManager::update(){
62    cleanup();
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();
68    return;
69  }
70 
71 
72  /**
73   * this function is used to keep the memory usage low
74   * it tries to delete all the unused gamestates
75   *
76   *
77   */
78  void GameStateManager::cleanup(){
79    std::map<int,int>::iterator it = gameStateUsed.begin();
80    while(it!=gameStateUsed.end()){
81      if( (*it).second <= 0 ){
82        COUT(4) << "GameStateManager: deleting gamestate with id: " << (*it).first << ", uses: " << (*it).second << std::endl;
83        delete[] gameStateMap[(*it).first]->data;
84        delete gameStateMap[(*it).first];
85        gameStateMap.erase((*it).first);
86        gameStateUsed.erase(it++);
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;
90        break;
91      }
92      it++;
93    }
94  }
95
96  GameStateCompressed *GameStateManager::popGameState(int clientID) {
97    //why are we searching the same client's gamestate id as we searched in
98    //Server::sendGameState?
99    int gID = head_->findClient(clientID)->getGamestateID();
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;
102    //chose wheather the next gamestate is the first or not
103    if(gID != GAMESTATEID_INITIAL){
104      // TODO something with the gamestatemap is wrong
105      GameState *client = gameStateMap.find(gID)->second;
106      GameState *server = reference;
107      //head_->findClient(clientID)->setGamestateID(id);
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);
113    } else {
114      COUT(4) << "we got a GAMESTATEID_INITIAL for clientID: " << clientID << std::endl;
115      GameState *server = reference;
116      ackGameState(clientID, reference->id);
117      //head_->findClient(clientID)->setGamestateID(id);
118      return encode(server);
119      // return an undiffed gamestate and set appropriate flags
120    }
121  }
122
123  /**
124  * This function goes through the whole list of synchronisables and
125  * saves all the synchronisables to a flat "list".
126  * @return struct of type gamestate containing the size of the whole gamestate and a pointer linking to the flat list
127  */
128  GameState *GameStateManager::getSnapshot()
129  {
130    //std::cout << "begin getSnapshot" << std::endl;
131    //the size of the gamestate
132    int totalsize=0;
133    int memsize=0;
134    //the size of one specific synchronisable
135    int tempsize=0;
136    // get the start of the Synchronisable list
137    orxonox::Iterator<Synchronisable> it;
138    // struct for return value of Synchronisable::getData()
139    syncData sync;
140
141    GameState *retval=new GameState; //return value
142    retval->id=++id_;
143    COUT(4) << "G.ST.Man: producing gamestate with id: " << retval->id << std::endl;
144    // offset of memory functions
145    int offset=0, size=0;
146    // get total size of gamestate
147    for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){
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];
153    if(!retval->data){
154      COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
155      return NULL;
156    }
157    memsize=size;
158    // go through all Synchronisables
159    for(it = orxonox::ObjectList<Synchronisable>::start(); it; ++it){
160      //get size of the synchronisable
161      tempsize=it->getSize();
162      // add place for data and 3 ints (length,classid,objectid)
163      totalsize+=tempsize+3*sizeof(int);
164      // allocate+tempsize additional space
165      if(totalsize > size){
166        COUT(3) << "G.St.Man: need additional memory" << std::endl;
167//         if(tempsize < 1000){
168//           retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
169//           memsize+=1000;
170//         } else {
171//           retval->data = (unsigned char *)realloc((void *)retval->data, totalsize+1000);
172//           memsize+=tempsize+1000;
173//         }
174//         COUT(5) << "G.St.Man: additional space allocation finished" << std::endl;
175      }
176
177      // run Synchronisable::getData with offset and additional place for 3 ints in between (for ids and length)
178      sync=it->getData((retval->data)+offset+3*sizeof(int));
179      memcpy(retval->data+offset, (void *)&(sync.length), sizeof(int));
180      memcpy(retval->data+offset+sizeof(int), (void *)&(sync.objectID), sizeof(int));
181      memcpy(retval->data+offset+2*sizeof(int), (void *)&(sync.classID), sizeof(int));
182      // increase data pointer
183      offset+=tempsize+3*sizeof(int);
184    }
185    retval->size=totalsize;
186    //#### bugfix
187    retval->diffed = false;
188    //std::cout << "end snapShot" << std::endl;
189    COUT(5) << "G.ST.Man: Gamestate size: " << totalsize << std::endl;
190    COUT(5) << "G.ST.Man: 'estimated' Gamestate size: " << size << std::endl;
191    return retval;
192  }
193
194  //##### ADDED FOR TESTING PURPOSE #####
195  GameStateCompressed* GameStateManager::testCompress( GameState* g ) {
196    return compress_( g );
197  }
198
199  GameState* GameStateManager::testDiff( GameState* a, GameState* b ) {
200    return diff( a, b );
201  }
202  //##### END TESTING PURPOSE #####
203
204  GameStateCompressed *GameStateManager::encode(GameState *a, GameState *b) {
205    COUT(5) << "G.St.Man: this will be a DIFFED gamestate" << std::endl;
206    GameState *r = diff(a,b);
207    //r.diffed = true;
208//     GameState *r = b;
209//     r->diffed = false;
210    //return compress_(r);
211    GameStateCompressed *g = new GameStateCompressed;
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;
218    return g;
219  }
220
221  GameStateCompressed *GameStateManager::encode(GameState *a) {
222    COUT(5) << "G.St.Man: this will be a not diffed gamestate" << std::endl;
223    a->diffed=false;
224    GameStateCompressed *g = new GameStateCompressed;
225    g->base_id = a->base_id;
226    g->id = a->id;
227    g->diffed = a->diffed;
228    g->data = a->data;
229    g->normsize = a->size;
230    g->compsize = a->size;
231    return g;
232  }
233
234  GameState *GameStateManager::diff(GameState *a, GameState *b) {
235    unsigned char *ap = a->data, *bp = b->data;
236    int of=0; // pointers offset
237    int dest_length=0;
238    if(a->size>=b->size)
239      dest_length=a->size;
240    else
241      dest_length=b->size;
242    //unsigned char *dp = (unsigned char *)malloc(dest_length*sizeof(unsigned char));
243    unsigned char *dp = new unsigned char[dest_length*sizeof(unsigned char)];
244    while(of<a->size && of<b->size){
245      *(dp+of)=*(ap+of)^*(bp+of); // do the xor
246      ++of;
247    }
248    if(a->size!=b->size){ // do we have to fill up ?
249      unsigned char n=0;
250      if(a->size<b->size){
251        while(of<dest_length){
252          *(dp+of)=n^*(bp+of);
253          of++;
254        }
255      } else{
256        while(of<dest_length){
257          *(dp+of)=*(ap+of)^n;
258          of++;
259        }
260      }
261    }
262
263    GameState *r = new GameState;
264    r->id = b->id;
265    r->size = dest_length;
266    r->diffed = true;
267    r->base_id = a->id;
268    r->data = dp;
269    return r;
270  }
271
272  GameStateCompressed *GameStateManager::compress_(GameState *a) {
273    //COUT(4) << "G.St.Man: compressing gamestate" << std::endl;
274
275    //COUT(4) << "G.St.Man: a: id: " << a->id << " base_id: " << a->base_id << " size: " << a->size << " diffed: " << a->diffed << std::endl;
276    int size = a->size;
277
278    uLongf buffer = (uLongf)((a->size + 12)*1.01)+1;
279    //COUT(4) << "size: " << size << ", buffer: " << buffer << std::endl;
280    //unsigned char* dest = (unsigned char*)malloc( buffer );
281    unsigned char *dest = new unsigned char[buffer];
282    //COUT(4) << "dest: " << dest << std::endl;
283    int retval;
284    //std::cout << "before ziped " << buffer << std::endl;
285    retval = compress( dest, &buffer, a->data, (uLong)size );
286    //COUT(4) << "bloablabla aft3er compress" << std::endl;
287    //std::cout << "after ziped " << buffer << std::endl;
288
289    switch ( retval ) {
290      case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
291      case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; 
292      return NULL;
293      case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
294      return NULL;
295      case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl;
296      return NULL;
297    }
298
299    GameStateCompressed *compressedGamestate = new GameStateCompressed;
300    compressedGamestate->compsize = buffer;
301//     std::cout << "compressedGamestate.compsize = buffer; " << buffer << std::endl;
302    compressedGamestate->normsize = size;
303//     std::cout << "compressedGamestate.normsize = size; " << size << std::endl;
304    compressedGamestate->id = a->id;
305    compressedGamestate->data = dest;
306    compressedGamestate->diffed = a->diffed;
307    compressedGamestate->base_id = a->base_id;
308    //COUT(5) << "G.St.Man: saved compressed data in GameStateCompressed:" << std::endl;
309    return compressedGamestate;
310  }
311
312  void GameStateManager::ackGameState(int clientID, int gamestateID) {
313    ClientInformation *temp = head_->findClient(clientID);
314    int curid = temp->getGamestateID();
315    COUT(4) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl;
316    // decrease usage of gamestate and save it
317//     deleteUnusedGameState(curid);
318    //increase gamestateused
319    --(gameStateUsed.find(curid)->second);
320    ++(gameStateUsed.find(gamestateID)->second);
321    temp->setGamestateID(gamestateID);
322    /*
323    GameState *old = clientGameState[clientID];
324    deleteUnusedGameState(old);
325    clientGameState[clientID]=idGameState[gamestateID];*/
326  }
327
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;
335  }
336
337}
Note: See TracBrowser for help on using the repository browser.