Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 8, 2008, 11:09:28 AM (16 years ago)
Author:
scheusso
Message:

compressing doesnt work yet (maybe rather decompressing)

File:
1 edited

Legend:

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

    r1739 r1741  
    4141 
    4242
    43 #define GAMESTATE_START(data) data + sizeof(GamestateHeader)
     43#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
    4444#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
    4545#define HEADER GAMESTATE_HEADER(data_)
     
    176176}
    177177
     178bool Gamestate::operator==(packet::Gamestate gs){
     179  unsigned char *d1 = data_+sizeof(GamestateHeader);
     180  unsigned char *d2 = gs.data_+sizeof(GamestateHeader);
     181  assert(!isCompressed());
     182  assert(!gs.isCompressed());
     183  while(d1<data_+HEADER->normsize)
     184  {
     185    if(*d1!=*d2)
     186      return false;
     187    d1++;
     188    d2++;
     189  }
     190  return true;
     191}
     192
    178193bool Gamestate::process()
    179194{
     
    184199{
    185200  assert(HEADER);
    186   uLongf buffer = (uLongf)((HEADER->normsize + 12)*1.01)+1;
     201  uLongf buffer = (uLongf)(((HEADER->normsize + 12)*1.01)+1);
    187202  if(buffer==0)
    188203    return false;
     
    190205  unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];
    191206  unsigned char *dest = GAMESTATE_START(ndata);
     207  //unsigned char *dest = new unsigned char[buffer];
     208  unsigned char *source = GAMESTATE_START(data_);
    192209  int retval;
    193   retval = compress( dest, &buffer, GAMESTATE_START(data_), (uLong)(HEADER->normsize) );
     210  retval = compress( dest, &buffer, source, (uLong)(HEADER->normsize) );
    194211  switch ( retval ) {
    195212    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
    196     case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl;
    197     return false;
    198     case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl;
    199     return false;
    200     case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl;
    201     return false;
    202   }
     213    case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false;
     214    case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false;
     215    case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false;
     216  }
     217#ifndef NDEBUG
     218  //decompress and compare the start and the decompressed data
     219  unsigned char *rdata = new unsigned char[HEADER->normsize+sizeof(GamestateHeader)];
     220  unsigned char *d2 = GAMESTATE_START(rdata);
     221  uLongf length2 = HEADER->normsize;
     222  uncompress(d2, &length2, dest, buffer);
     223  for(unsigned int i=0; i<HEADER->normsize; i++){
     224    assert(*(source+i)==*(d2+i));
     225  }
     226  delete[] rdata;
     227#endif
    203228
    204229  //copy and modify header
    205   HEADER->compsize = buffer;
    206   HEADER->compressed = true;
    207230  *GAMESTATE_HEADER(ndata) = *HEADER;
    208231  //delete old data
     
    210233  //save new data
    211234  data_ = ndata;
     235  HEADER->compsize = buffer;
     236  HEADER->compressed = true;
    212237  assert(HEADER->compressed);
    213238  COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl;
     
    217242{
    218243  assert(HEADER->compressed);
    219   //COUT(4) << "GameStateClient: uncompressing gamestate. id: " << a->id << ", baseid: " << a->base_id << ", normsize: " << a->normsize << ", compsize: " << a->compsize << std::endl;
    220   int normsize = HEADER->normsize;
    221   int compsize = HEADER->compsize;
    222   int bufsize;
    223   if(normsize < compsize)
    224     bufsize = compsize;
    225   else
    226     bufsize = normsize;
    227   if(bufsize==0)
    228     return NULL;
     244  COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", normsize: " << HEADER->normsize << ", compsize: " << HEADER->compsize << std::endl;
     245  unsigned int normsize = HEADER->normsize;
     246  unsigned int compsize = HEADER->compsize;
     247  unsigned int bufsize;
     248  assert(compsize<=normsize);
     249  bufsize = normsize;
     250  assert(bufsize!=0);
    229251  unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];
    230252  unsigned char *dest = ndata + sizeof(GamestateHeader);
     253  unsigned char *source = data_ + sizeof(GamestateHeader);
    231254  int retval;
    232   uLongf length=normsize;
    233   retval = uncompress( dest, &length, data_+sizeof(GamestateHeader), (uLong)compsize );
     255  uLongf length=bufsize;
     256  retval = uncompress( dest, &length, source, (uLong)compsize );
    234257  switch ( retval ) {
    235258    case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break;
     
    239262  }
    240263 
    241   HEADER->compressed = false;
    242264  //copy over the header
    243265  *GAMESTATE_HEADER(ndata) = *HEADER;
    244266  //delete old (compressed data)
    245267  delete[] data_;
    246   //set new pointers and create bytestream
     268  //set new pointers
    247269  data_ = ndata;
    248   //bs_ = new Bytestream(getGs(), GAMESTATE_HEADER->normsize);
    249  
     270  HEADER->compressed = false;
     271  assert(HEADER->normsize==normsize);
     272  assert(HEADER->compsize==compsize);
    250273  return true;
    251274}
     
    279302  GAMESTATE_HEADER(ndata)->diffed = true;
    280303  GAMESTATE_HEADER(ndata)->base_id = base->getID();
    281   Gamestate *g = new Gamestate(ndata, 0);
     304  Gamestate *g = new Gamestate(ndata, getClientID());
    282305  g->flags_=flags_;
    283306  g->packetDirection_ = packetDirection_;
    284   g->clientID_ = clientID_;
    285307  return g;
    286308}
     
    315337  *GAMESTATE_HEADER(ndata) = *HEADER;
    316338  GAMESTATE_HEADER(ndata)->diffed = false;
    317   Gamestate *g = new Gamestate(ndata, 0);
     339  Gamestate *g = new Gamestate(ndata, getClientID());
    318340  g->flags_=flags_;
    319341  g->packetDirection_ = packetDirection_;
    320   g->clientID_ = clientID_;
    321342  assert(!g->isDiffed());
    322343  assert(!g->isCompressed());
Note: See TracChangeset for help on using the changeset viewer.