Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 13, 2009, 6:41:08 PM (16 years ago)
Author:
scheusso
Message:

finally got rid of these structs in our network datastream
this means we should now be able to play with gcc and msvc compiled versions together

Location:
code/branches/presentation/src/network/packet
Files:
2 edited

Legend:

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

    r2575 r2655  
    4444namespace packet {
    4545
     46#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
    4647
    4748#define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
    4849
     50// Gamestate::Gamestate()
     51// {
     52//   flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     53// }
     54
    4955Gamestate::Gamestate()
    5056{
    5157  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     58  header_ = 0;
    5259}
    5360
     
    5663{
    5764  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     65  header_ = new GamestateHeader(data_);
    5866}
    5967
     
    6270  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
    6371  data_=data;
     72  header_ = new GamestateHeader(data_);
    6473}
    6574
     
    7180bool Gamestate::collectData(int id, uint8_t mode)
    7281{
     82  assert(this->header_==0); // make sure the header didn't exist before
    7383  uint32_t tempsize=0, currentsize=0;
    7484  assert(data_==0);
     
    7888  if(size==0)
    7989    return false;
    80   data_ = new unsigned char[size + sizeof(GamestateHeader)];
     90  data_ = new uint8_t[size + GamestateHeader::getSize()];
    8191  if(!data_){
    8292    COUT(2) << "GameStateManager: could not allocate memory" << std::endl;
    8393    return false;
    8494  }
     95 
     96  // create the header object
     97  header_ = new GamestateHeader(data_);
    8598
    8699  //start collect data synchronisable by synchronisable
    87100  uint8_t *mem=data_;
    88   mem+=sizeof(GamestateHeader);
     101  mem += GamestateHeader::getSize();
    89102  ObjectList<Synchronisable>::iterator it;
    90103  for(it = ObjectList<Synchronisable>::begin(); it; ++it){
     
    100113      while(++temp)
    101114        addsize+=temp->getSize(id, mode);
    102       data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
     115      data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize);
    103116      if(!data_)
    104117        return false;
     
    119132
    120133  //start write gamestate header
    121   HEADER->packetType = ENUM::Gamestate;
    122   HEADER->datasize = currentsize;
    123   HEADER->id = id;
    124   HEADER->diffed = false;
    125   HEADER->complete = true;
    126   HEADER->compressed = false;
     134  header_->setDataSize( currentsize );
     135  header_->setID( id );
     136  header_->setDiffed( false );
     137  header_->setComplete( true );
     138  header_->setCompressed( false );
    127139  //stop write gamestate header
    128140
     
    135147{
    136148  assert(data_);
    137   assert(!HEADER->compressed);
    138   assert(!HEADER->diffed);
    139   uint8_t *mem=data_+sizeof(GamestateHeader);
     149  assert(!header_->isCompressed());
     150  assert(!header_->isDiffed());
     151  uint8_t *mem=data_+GamestateHeader::getSize();
    140152    // get the start of the Synchronisable list
    141153  //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin();
     
    143155
    144156  // update the data of the objects we received
    145   while(mem < data_+sizeof(GamestateHeader)+HEADER->datasize){
    146     synchronisableHeader *objectheader = (synchronisableHeader*)mem;
    147 
    148     s = Synchronisable::getSynchronisable( objectheader->objectID );
     157  while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()){
     158    SynchronisableHeader objectheader(mem);
     159
     160    s = Synchronisable::getSynchronisable( objectheader.getObjectID() );
    149161    if(!s)
    150162    {
    151163      if (!Core::isMaster())
     164      {
    152165        Synchronisable::fabricate(mem, mode);
     166      }
    153167      else
    154         mem += objectheader->size;
     168      {
     169        mem += objectheader.getDataSize();
     170      }
    155171//         COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl;
    156172//       else
     
    196212{
    197213  assert(data_);
    198   if(HEADER->compressed)
    199     return HEADER->compsize+sizeof(GamestateHeader);
     214  if(header_->isCompressed())
     215    return header_->getCompSize()+GamestateHeader::getSize();
    200216  else
    201217  {
    202     return HEADER->datasize+sizeof(GamestateHeader);
     218    return header_->getDataSize()+GamestateHeader::getSize();
    203219  }
    204220}
    205221
    206222bool Gamestate::operator==(packet::Gamestate gs){
    207   uint8_t *d1 = data_+sizeof(GamestateHeader);
    208   uint8_t *d2 = gs.data_+sizeof(GamestateHeader);
     223  uint8_t *d1 = data_+GamestateHeader::getSize();
     224  uint8_t *d2 = gs.data_+GamestateHeader::getSize();
    209225  assert(!isCompressed());
    210226  assert(!gs.isCompressed());
    211   while(d1<data_+HEADER->datasize)
     227  while(d1<data_+header_->getDataSize())
    212228  {
    213229    if(*d1!=*d2)
     
    228244bool Gamestate::compressData()
    229245{
    230   assert(HEADER);
    231   assert(!HEADER->compressed);
    232   uLongf buffer = (uLongf)(((HEADER->datasize + 12)*1.01)+1);
     246  assert(data_);
     247  assert(!header_->isCompressed());
     248  uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1);
    233249  if(buffer==0)
    234250    return false;
    235251
    236   uint8_t *ndata = new uint8_t[buffer+sizeof(GamestateHeader)];
    237   uint8_t *dest = GAMESTATE_START(ndata);
     252  uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()];
     253  uint8_t *dest = ndata + GamestateHeader::getSize();
    238254  //unsigned char *dest = new unsigned char[buffer];
    239   uint8_t *source = GAMESTATE_START(data_);
     255  uint8_t *source = data_ + GamestateHeader::getSize();
    240256  int retval;
    241   retval = compress( dest, &buffer, source, (uLong)(HEADER->datasize) );
     257  retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) );
    242258  switch ( retval ) {
    243259    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
     
    248264
    249265  //copy and modify header
    250   *GAMESTATE_HEADER(ndata) = *HEADER;
     266  GamestateHeader *temp = header_;
     267  header_ = new GamestateHeader(ndata, temp);
     268  delete temp;
    251269  //delete old data
    252270  delete[] data_;
    253271  //save new data
    254272  data_ = ndata;
    255   HEADER->compsize = buffer;
    256   HEADER->compressed = true;
    257   assert(HEADER->compressed);
    258   COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
     273  header_->setCompSize( buffer );
     274  header_->setCompressed( true );
     275  COUT(5) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;
    259276  return true;
    260277}
    261278bool Gamestate::decompressData()
    262279{
    263   assert(HEADER);
    264   assert(HEADER->compressed);
    265   COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
    266   uint32_t datasize = HEADER->datasize;
    267   uint32_t compsize = HEADER->compsize;
     280  assert(data_);
     281  assert(header_->isCompressed());
     282  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl;
     283  uint32_t datasize = header_->getDataSize();
     284  uint32_t compsize = header_->getCompSize();
    268285  uint32_t bufsize;
    269286//  assert(compsize<=datasize);
    270287  bufsize = datasize;
    271288  assert(bufsize!=0);
    272   uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)];
    273   uint8_t *dest = ndata + sizeof(GamestateHeader);
    274   uint8_t *source = data_ + sizeof(GamestateHeader);
     289  uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()];
     290  uint8_t *dest = ndata + GamestateHeader::getSize();
     291  uint8_t *source = data_ + GamestateHeader::getSize();
    275292  int retval;
    276293  uLongf length=bufsize;
     
    284301
    285302  //copy over the header
    286   *GAMESTATE_HEADER(ndata) = *HEADER;
     303  GamestateHeader *temp = header_;
     304  header_ = new GamestateHeader( data_, header_ );
     305  delete temp;
    287306
    288307  if (this->bDataENetAllocated_){
     
    299318  //set new pointers
    300319  data_ = ndata;
    301   HEADER->compressed = false;
    302   assert(HEADER->datasize==datasize);
    303   assert(HEADER->compsize==compsize);
     320  header_->setCompressed( false );
     321  assert(header_->getDataSize()==datasize);
     322  assert(header_->getCompSize()==compsize);
    304323  return true;
    305324}
     
    307326Gamestate *Gamestate::diff(Gamestate *base)
    308327{
    309   assert(HEADER);
    310   assert(!HEADER->compressed);
    311   assert(!HEADER->diffed);
     328  assert(data_);
     329  assert(!header_->isCompressed());
     330  assert(!header_->isDiffed());
     331  GamestateHeader diffHeader(base->data_);
    312332  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
    313333  uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
    314334  uint32_t of=0; // pointers offset
    315335  uint32_t dest_length=0;
    316   dest_length=HEADER->datasize;
     336  dest_length=header_->getDataSize();
    317337  if(dest_length==0)
    318338    return NULL;
    319   uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
    320   uint8_t *dest = ndata + sizeof(GamestateHeader);
    321   while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
     339  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
     340  uint8_t *dest = ndata + GamestateHeader::getSize();
     341  while(of < diffHeader.getDataSize() && of < header_->getDataSize()){
    322342    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
    323343    ++of;
    324344  }
    325   if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
     345  if(diffHeader.getDataSize()!=header_->getDataSize()){
    326346    uint8_t n=0;
    327     if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
     347    if(diffHeader.getDataSize() < header_->getDataSize()){
    328348      while(of<dest_length){
    329349        *(dest+of)=n^*(gs+of);
     
    333353  }
    334354
    335   *GAMESTATE_HEADER(ndata) = *HEADER;
    336   GAMESTATE_HEADER(ndata)->diffed = true;
    337   GAMESTATE_HEADER(ndata)->base_id = base->getID();
    338355  Gamestate *g = new Gamestate(ndata, getClientID());
     356  *(g->header_) = *header_;
     357  g->header_->setDiffed( true );
     358  g->header_->setBaseID( base->getID() );
    339359  g->flags_=flags_;
    340360  g->packetDirection_ = packetDirection_;
     
    347367
    348368  // allocate memory for new data
    349   uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
     369  uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
    350370  // create a gamestate out of it
    351371  Gamestate *gs = new Gamestate(gdata);
    352   uint8_t *newdata = gdata + sizeof(GamestateHeader);
     372  uint8_t *newdata = gdata + GamestateHeader::getSize();
    353373  uint8_t *origdata = GAMESTATE_START(data_);
    354374
    355375  //copy the GamestateHeader
    356   *(GamestateHeader*)gdata = *HEADER;
    357 
    358   synchronisableHeader *oldobjectheader, *newobjectheader;
     376  assert(gs->header_);
     377  *(gs->header_) = *header_;
     378
    359379  uint32_t objectOffset;
    360380  unsigned int objectsize, destsize=0;
     
    363383
    364384  //call TrafficControl
    365   TrafficControl::getInstance()->processObjectList( clientID, HEADER->id, &dataMap_ );
     385  TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), &dataMap_ );
    366386
    367387  //copy in the zeros
     
    371391//    if(it->second->getSize(HEADER->id)==0) // merged from objecthierarchy2, doesn't work anymore; TODO: change this
    372392//      continue;                            // merged from objecthierarchy2, doesn't work anymore; TODO: change this
    373     oldobjectheader = (synchronisableHeader*)origdata;
    374     newobjectheader = (synchronisableHeader*)newdata;
     393    SynchronisableHeader oldobjectheader(origdata);
     394    SynchronisableHeader newobjectheader(newdata);
    375395    if ( (*it).objSize == 0 )
    376396    {
     
    380400//     object = Synchronisable::getSynchronisable( (*it).objID );
    381401//     assert(object->objectID == oldobjectheader->objectID);
    382     objectsize = oldobjectheader->size;
    383     objectOffset=sizeof(synchronisableHeader); //skip the size and the availableData variables in the objectheader
    384     if ( (*it).objID == oldobjectheader->objectID ){
     402    objectsize = oldobjectheader.getDataSize();
     403    objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader
     404    if ( (*it).objID == oldobjectheader.getObjectID() ){
    385405      memcpy(newdata, origdata, objectsize);
    386       assert(newobjectheader->dataAvailable==true);
     406      assert(newobjectheader.isDataAvailable()==true);
    387407      ++it;
    388408    }else{
    389       *newobjectheader = *oldobjectheader;
    390       newobjectheader->dataAvailable=false;
     409      newobjectheader = oldobjectheader;
     410      newobjectheader.setDataAvailable(false);
    391411      memset(newdata+objectOffset, 0, objectsize-objectOffset);
    392412    }
     
    397417#ifndef NDEBUG
    398418  uint32_t origsize = destsize;
    399   while ( origsize < HEADER->datasize )
     419  while ( origsize < header_->getDataSize() )
    400420  {
    401     oldobjectheader = (synchronisableHeader*)origdata;
    402     objectsize = oldobjectheader->size;
     421    SynchronisableHeader oldobjectheader(origdata);
     422    objectsize = oldobjectheader.getDataSize();
    403423    origdata += objectsize;
    404424    origsize += objectsize;
    405425  }
    406   assert(origsize==HEADER->datasize);
     426  assert(origsize==header_->getDataSize());
    407427  assert(destsize!=0);
    408428#endif
    409   ((GamestateHeader*)gdata)->datasize = destsize;
     429  gs->header_->setDataSize( destsize );
    410430  return gs;
    411431}
     
    414434Gamestate *Gamestate::undiff(Gamestate *base)
    415435{
    416   assert(this && base);assert(HEADER);
    417   assert(HEADER->diffed);
    418   assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
     436  assert(this && base);assert(data_);
     437  assert(header_->isDiffed());
     438  assert(!header_->isCompressed() && !base->header_->isCompressed());
    419439  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
    420440  uint8_t *basep = GAMESTATE_START(base->data_);
     
    422442  uint32_t of=0; // pointers offset
    423443  uint32_t dest_length=0;
    424   dest_length=HEADER->datasize;
     444  dest_length=header_->getDataSize();
    425445  if(dest_length==0)
    426446    return NULL;
    427   uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
    428   uint8_t *dest = ndata + sizeof(GamestateHeader);
    429   while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
     447  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
     448  uint8_t *dest = ndata + GamestateHeader::getSize();
     449  while(of < base->header_->getDataSize() && of < header_->getDataSize()){
    430450    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
    431451    ++of;
    432452  }
    433   if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
     453  if(base->header_->getDataSize()!=header_->getDataSize()){
    434454    uint8_t n=0;
    435     if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
     455    if(base->header_->getDataSize() < header_->getDataSize()){
    436456      while(of < dest_length){
    437457        *(dest+of)=n^*(gs+of);
     
    440460    }
    441461  }
    442   *GAMESTATE_HEADER(ndata) = *HEADER;
    443   GAMESTATE_HEADER(ndata)->diffed = false;
    444462  Gamestate *g = new Gamestate(ndata, getClientID());
     463  assert(g->header_);
     464  *(g->header_) = *header_;
     465  g->header_->setDiffed( false );
    445466  g->flags_=flags_;
    446467  g->packetDirection_ = packetDirection_;
     
    463484
    464485} //namespace packet
    465 
    466486} //namespace orxonox
  • code/branches/presentation/src/network/packet/Gamestate.h

    r2575 r2655  
    4646namespace packet {
    4747
    48 #define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
    49 #define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
    50 #define HEADER GAMESTATE_HEADER(data_)
     48class _NetworkExport GamestateHeader{
     49  public:
     50    GamestateHeader(uint8_t *data){ data_ = data; *(uint32_t*)data_ = ENUM::Gamestate; }
     51    GamestateHeader(uint8_t *data, GamestateHeader* h)
     52      { data_=data; memcpy(data_, h->data_, getSize()); }
     53    static inline uint32_t getSize()
     54      { return 21; }
    5155
    52 struct _NetworkExport GamestateHeader{
    53   ENUM::Type packetType;
    54   int32_t id; // id of the gamestate
    55   uint32_t compsize;
    56   uint32_t datasize;
    57   int32_t base_id; // id of the base-gamestate diffed from
    58   bool diffed:1; // wheter diffed or not
    59   bool complete:1; // wheter it is a complete gamestate or only partial
    60   bool compressed:1;
     56    inline int32_t getID() const
     57      { return *(int32_t*)(data_+4); }
     58    inline void setID(int32_t id)
     59      { *(int32_t*)(data_+4) = id; }
     60
     61    inline int32_t getBaseID() const
     62      { return *(int32_t*)(data_+8); }
     63    inline void setBaseID(int32_t id)
     64      { *(int32_t*)(data_+8) = id; }
     65
     66    inline uint32_t getDataSize() const
     67      { return *(uint32_t*)(data_+12); }
     68    inline void setDataSize(uint32_t size)
     69      { *(uint32_t*)(data_+12) = size; }
     70
     71    inline uint32_t getCompSize() const
     72      { return *(uint32_t*)(data_+16); }
     73    inline void setCompSize(uint32_t size)
     74      { *(uint32_t*)(data_+16) = size; }
     75
     76    inline bool isDiffed() const
     77      { return *(int8_t*)(data_+20) & 0x1; }
     78    inline void setDiffed(bool b)
     79      { *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }
     80
     81    inline bool isComplete() const
     82      { return *(int8_t*)(data_+20) & 0x2; }
     83    inline void setComplete(bool b)
     84      { *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }
     85
     86    inline bool isCompressed() const
     87      { return *(int8_t*)(data_+20) & 0x4; }
     88    inline void setCompressed(bool b)
     89      { *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }
     90
     91    inline void operator=(GamestateHeader& h)
     92      { memcpy( data_, h.data_, getSize()); }
     93  private:
     94    uint8_t *data_;
     95//#define GAMESTATE_START(data) (data + sizeof(GamestateHeader))
     96//#define GAMESTATE_HEADER(data) ((GamestateHeader *)data)
     97//#define HEADER GAMESTATE_HEADER(data_)
     98
    6199};
    62100
     
    74112    bool collectData(int id, uint8_t mode=0x0);
    75113    bool spreadData( uint8_t mode=0x0);
    76     inline int32_t getID() const { return HEADER->id; }
    77     inline bool isDiffed() const { return HEADER->diffed; }
    78     inline bool isCompressed() const { return HEADER->compressed; }
    79     inline int32_t getBaseID() const { return HEADER->base_id; }
     114    inline int32_t getID() const { return header_->getID(); }
     115    inline bool isDiffed() const { return header_->isDiffed(); }
     116    inline bool isCompressed() const { return header_->isCompressed(); }
     117    inline int32_t getBaseID() const { return header_->getBaseID(); }
    80118    Gamestate *diff(Gamestate *base);
    81119    Gamestate *undiff(Gamestate *base);
     
    93131    uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0);
    94132    std::list<obj> dataMap_;
     133    GamestateHeader* header_;
    95134};
    96135
Note: See TracChangeset for help on using the changeset viewer.