Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 9, 2008, 4:31:34 PM (16 years ago)
Author:
scheusso
Message:

merged network branch back into trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/network/packet/Gamestate.cc

    r1747 r1751  
    4141namespace packet {
    4242
    43 
    44 #define GAMESTATE_START(data) data + sizeof(GamestateHeader)
     43#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
    4544#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
    4645#define HEADER GAMESTATE_HEADER(data_)
     
    6362{
    6463  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);
    6766
    6867  COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl;
    69     //retval->data = (unsigned char*)malloc(size);
    7068  if(size==0)
    7169    return false;
    7270  data_ = new unsigned char[size + sizeof(GamestateHeader)];
    73   //bs_ = new Bytestream(data_+sizeof(GamestateHeader), size);
    7471  if(!data_){
    7572    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
     
    8279  orxonox::ObjectList<Synchronisable>::iterator it;
    8380  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){
    84     tempsize=it->getSize2(mode);
     81    tempsize=it->getSize2(id, mode);
    8582
    8683    if(currentsize+tempsize > size){
     
    9087      int addsize=tempsize;
    9188      while(++temp)
    92         addsize+=temp->getSize2(mode);
     89        addsize+=temp->getSize2(id, mode);
    9390      data_ = (unsigned char *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
    9491      if(!data_)
     
    9794    }// stop allocate additional memory
    9895
    99     if(!it->getData2(mem, mode))
     96    if(!it->getData(mem, id, mode))
    10097      return false; // mem pointer gets automatically increased because of call by reference
    10198    // increase size counter by size of current synchronisable
     
    121118bool Gamestate::spreadData(int mode)
    122119{
    123   assert(data_ && !HEADER->compressed && !HEADER->diffed);
     120  assert(data_);
     121  assert(!HEADER->compressed);
     122  assert(!HEADER->diffed);
    124123  unsigned int size, objectID, classID;
    125124  unsigned char *mem=data_+sizeof(GamestateHeader);
     
    142141        //fabricate the new synchronisable
    143142        if(!Synchronisable::fabricate(mem, mode))
    144           /*return false*/;
     143          return false;
    145144        it=orxonox::ObjectList<Synchronisable>::end();
    146145      }
     
    175174}
    176175
     176bool 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
    177191bool Gamestate::process()
    178192{
     
    183197{
    184198  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);
    186201  if(buffer==0)
    187202    return false;
     
    189204  unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];
    190205  unsigned char *dest = GAMESTATE_START(ndata);
     206  //unsigned char *dest = new unsigned char[buffer];
     207  unsigned char *source = GAMESTATE_START(data_);
    191208  int retval;
    192   retval = compress( dest, &buffer, GAMESTATE_START(data_), (uLong)(HEADER->normsize) );
     209  retval = compress( dest, &buffer, source, (uLong)(HEADER->normsize) );
    193210  switch ( retval ) {
    194211    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
    202227
    203228  //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
    206232  *GAMESTATE_HEADER(ndata) = *HEADER;
    207233  //delete old data
     
    209235  //save new data
    210236  data_ = ndata;
     237  HEADER->compsize = buffer;
     238  HEADER->compressed = true;
    211239  assert(HEADER->compressed);
    212240  COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl;
     
    215243bool Gamestate::decompressData()
    216244{
     245  assert(HEADER);
    217246  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);
    228254  unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];
    229255  unsigned char *dest = ndata + sizeof(GamestateHeader);
     256  unsigned char *source = data_ + sizeof(GamestateHeader);
    230257  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 );
    233260  switch ( retval ) {
    234261    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
     
    237264    case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false;
    238265  }
    239 
    240   HEADER->compressed = false;
     266#ifndef NDEBUG
     267  assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->normsize));
     268#endif
     269 
    241270  //copy over the header
    242271  *GAMESTATE_HEADER(ndata) = *HEADER;
    243272  //delete old (compressed data)
    244273  delete[] data_;
    245   //set new pointers and create bytestream
     274  //set new pointers
    246275  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);
    249279  return true;
    250280}
     
    252282Gamestate *Gamestate::diff(Gamestate *base)
    253283{
     284  assert(HEADER);
     285  assert(!HEADER->compressed);
     286  assert(!HEADER->diffed);
    254287  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
    255288  unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
     
    277310  *GAMESTATE_HEADER(ndata) = *HEADER;
    278311  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_;
    280316  return g;
    281317}
     
    283319Gamestate *Gamestate::undiff(Gamestate *base)
    284320{
    285   assert(this && base);
     321  assert(this && base);assert(HEADER);
     322  assert(HEADER->diffed);
    286323  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
    287324  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
     
    310347  *GAMESTATE_HEADER(ndata) = *HEADER;
    311348  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());
    313354  return g;
    314355}
    315356
    316357
    317 unsigned int Gamestate::calcGamestateSize(int mode)
     358unsigned int Gamestate::calcGamestateSize(unsigned int id, int mode)
    318359{
    319360  int size=0;
     
    322363    // get total size of gamestate
    323364  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it)
    324     size+=it->getSize2(mode); // size of the actual data of the synchronisable
     365    size+=it->getSize2(id, mode); // size of the actual data of the synchronisable
    325366//  size+=sizeof(GamestateHeader);
    326367  return size;
     
    342383  }
    343384
     385  bool Gamestate::isCompressed(){
     386    return HEADER->compressed;
     387  }
     388 
    344389  int Gamestate::getBaseID(){
    345390    return HEADER->base_id;
Note: See TracChangeset for help on using the changeset viewer.