Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 12, 2008, 7:40:47 PM (16 years ago)
Author:
scheusso
Message:

merged network branch back to trunk

Location:
code/trunk/src/network/packet
Files:
12 edited
2 copied

Legend:

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

    r1769 r1907  
    4040#define _ACKID              _PACKETID + sizeof(network::packet::ENUM::Type)
    4141 
    42 Acknowledgement::Acknowledgement( unsigned int id, int clientID )
     42Acknowledgement::Acknowledgement( unsigned int id, unsigned int clientID )
    4343 : Packet()
    4444{
    4545  flags_ = flags_ | PACKET_FLAGS_ACK;
    46   data_=new unsigned char[ getSize() ];
     46  data_=new uint8_t[ getSize() ];
    4747  *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Acknowledgement;
    48   *(unsigned int *)(data_ + _ACKID ) = id;
     48  *(uint32_t *)(data_ + _ACKID ) = id;
    4949  clientID_=clientID;
    5050}
    5151
    52 Acknowledgement::Acknowledgement( unsigned char *data, int clientID )
     52Acknowledgement::Acknowledgement( uint8_t *data, unsigned int clientID )
    5353  : Packet(data, clientID)
    5454{
     
    6060
    6161unsigned int Acknowledgement::getSize() const{
    62   return _ACKID + sizeof(unsigned int);
     62  return _ACKID + sizeof(uint32_t);
    6363}
    6464
     
    7070
    7171unsigned int Acknowledgement::getAckID(){
    72   return *(unsigned int *)(data_ + _ACKID);
     72  return *(uint32_t *)(data_ + _ACKID);
    7373}
    7474
  • code/trunk/src/network/packet/Acknowledgement.h

    r1763 r1907  
    4040{
    4141public:
    42   Acknowledgement( unsigned int id, int clientID );
    43   Acknowledgement( unsigned char* data, int clientID );
     42  Acknowledgement( unsigned int id, unsigned int clientID );
     43  Acknowledgement( uint8_t* data, unsigned int clientID );
    4444  ~Acknowledgement();
    4545 
  • code/trunk/src/network/packet/Chat.cc

    r1763 r1907  
    2929#include "Chat.h"
    3030#include <assert.h>
     31#include "network/Host.h"
    3132
    3233namespace network {
    3334namespace packet {
    3435 
    35 #define PACKET_FLAGS_CHAT ENET_PACKET_FLAG_RELIABLE
    36 #define _PACKETID         0
    37 #define _MESSAGELENGTH    _PACKETID + sizeof(ENUM::Type)
    38 #define _MESSAGE          _MESSAGELENGTH + sizeof(unsigned int)
     36#define   PACKET_FLAGS_CHAT ENET_PACKET_FLAG_RELIABLE
     37#define   _PACKETID         0
     38const int _PLAYERID     =   _PACKETID + sizeof(ENUM::Type);
     39#define   _MESSAGELENGTH    _PLAYERID + sizeof(uint32_t)
     40#define   _MESSAGE          _MESSAGELENGTH + sizeof(uint32_t)
    3941
    40 Chat::Chat( std::string& message, int clientID )
     42Chat::Chat( std::string message, unsigned int playerID )
    4143 : Packet()
    4244{
     
    4446  messageLength_ = message.length()+1;
    4547  data_=new unsigned char[ getSize() ];
    46   *(ENUM::Type *)&data_[ _PACKETID ] = ENUM::Chat;
    47   *(unsigned int *)&data_[ _MESSAGELENGTH ] = messageLength_;
    48   memcpy( &data_[ _MESSAGE ], (void *)message.c_str(), messageLength_ );
    49   clientID_=clientID;
     48  *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Chat;
     49  *(unsigned int *)(data_ + _PLAYERID ) = playerID;
     50  *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_;
     51  memcpy( data_+_MESSAGE, (void *)message.c_str(), messageLength_ );
    5052}
    5153
    52 Chat::Chat( unsigned char *data, int clientID )
     54Chat::Chat( uint8_t* data, unsigned int clientID )
    5355  : Packet(data, clientID)
    5456{
    55   messageLength_ = *(unsigned int *)&data[ _MESSAGELENGTH ];
     57  messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH );
    5658}
    5759
     
    6567
    6668bool Chat::process(){
    67   //TODO: change this !!!
    68   assert(0);
     69  bool b = Host::incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID));
    6970  delete this;
    70   return true;
     71  return b;
    7172}
    7273
    7374unsigned char *Chat::getMessage(){
    74   return &data_[ _MESSAGE ];
     75  return data_ + _MESSAGE;
    7576}
    7677
  • code/trunk/src/network/packet/Chat.h

    r1837 r1907  
    1616{
    1717public:
    18   Chat( std::string& message, int clientID );
    19   Chat( unsigned char* data, int clientID );
     18  Chat( std::string message, unsigned int playerID );
     19  Chat( uint8_t* data, unsigned int clientID );
    2020  ~Chat();
    2121 
     
    2626  unsigned char *getMessage();
    2727private:
    28   unsigned int messageLength_;
    29   int clientID_;
     28  uint32_t messageLength_;
     29  unsigned int clientID_;
    3030};
    3131
  • code/trunk/src/network/packet/ClassID.cc

    r1856 r1907  
    4040#define _PACKETID             0
    4141#define _CLASSID              _PACKETID + sizeof(ENUM::Type)
    42 #define _CLASSNAMELENGTH      _CLASSID + sizeof(unsigned int)
     42#define _CLASSNAMELENGTH      _CLASSID + sizeof(uint32_t)
    4343#define _CLASSNAME            _CLASSNAMELENGTH + sizeof(classNameLength_)
    4444
     
    5757}
    5858
    59 ClassID::ClassID( unsigned char *data, int clientID )
     59ClassID::ClassID( uint8_t* data, unsigned int clientID )
    6060  : Packet(data, clientID)
    6161{
     
    6868
    6969unsigned int ClassID::getSize() const{
    70   return sizeof(network::packet::ENUM::Type) + 2*sizeof(unsigned int) + classNameLength_;
     70  return sizeof(network::packet::ENUM::Type) + 2*sizeof(uint32_t) + classNameLength_;
    7171}
    7272
     
    8282
    8383unsigned int ClassID::getClassID(){
    84   return *(unsigned int *)&data_[ _CLASSID ];
     84  return *(uint32_t *)(data_ + _CLASSID);
    8585}
    8686
  • code/trunk/src/network/packet/ClassID.h

    r1763 r1907  
    4343public:
    4444  ClassID( unsigned int classID, std::string className );
    45   ClassID( unsigned char* data, int clientID );
     45  ClassID( uint8_t* data, unsigned int clientID );
    4646  ~ClassID();
    4747 
     
    5353  unsigned char *getClassName();
    5454private:
    55   unsigned int classNameLength_;
     55  uint32_t classNameLength_;
    5656};
    5757
  • code/trunk/src/network/packet/Gamestate.cc

    r1767 r1907  
    4646#define HEADER GAMESTATE_HEADER(data_)
    4747
     48 
     49#define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
     50 
    4851Gamestate::Gamestate()
    4952{
    50 }
    51 
    52 Gamestate::Gamestate(unsigned char *data, int clientID):
     53  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     54}
     55
     56Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
    5357    Packet(data, clientID)
    5458{
     59  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     60}
     61
     62Gamestate::Gamestate(uint8_t *data)
     63{
     64  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     65  data_=data;
    5566}
    5667
     
    7485    return false;
    7586  }
    76 
     87 
     88#ifndef NDEBUG
     89  std::list<Synchronisable*> slist;
     90  std::list<Synchronisable*>::iterator iit;
     91#endif
    7792  //start collect data synchronisable by synchronisable
    78   unsigned char *mem=data_;
     93  uint8_t *mem=data_;
    7994  mem+=sizeof(GamestateHeader);
    8095  orxonox::ObjectList<Synchronisable>::iterator it;
    8196  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it){
    82     tempsize=it->getSize2(id, mode);
     97    tempsize=it->getSize(id, mode);
    8398
    8499    if(currentsize+tempsize > size){
     
    88103      int addsize=tempsize;
    89104      while(++temp)
    90         addsize+=temp->getSize2(id, mode);
    91       data_ = (unsigned char *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
     105        addsize+=temp->getSize(id, mode);
     106      data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize);
    92107      if(!data_)
    93108        return false;
     
    95110    }// stop allocate additional memory
    96111
     112#ifndef NDEBUG
     113    for(iit=slist.begin(); iit!=slist.end(); iit++)
     114      assert((*iit)!=*it);
     115    slist.push_back(*it);
     116#endif
     117   
     118    //if(it->doSelection(id))
     119    dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
    97120    if(!it->getData(mem, id, mode))
    98121      return false; // mem pointer gets automatically increased because of call by reference
     
    105128  HEADER->packetType = ENUM::Gamestate;
    106129  assert( *(ENUM::Type *)(data_) == ENUM::Gamestate);
    107   HEADER->normsize = currentsize;
     130  HEADER->datasize = currentsize;
    108131  HEADER->id = id;
    109132  HEADER->diffed = false;
     
    122145  assert(!HEADER->compressed);
    123146  assert(!HEADER->diffed);
    124   unsigned int size, objectID, classID;
    125   unsigned char *mem=data_+sizeof(GamestateHeader);
     147  uint8_t *mem=data_+sizeof(GamestateHeader);
    126148    // get the start of the Synchronisable list
    127   orxonox::ObjectList<Synchronisable>::iterator it=orxonox::ObjectList<Synchronisable>::begin();
    128 
    129   while(mem < data_+sizeof(GamestateHeader)+HEADER->normsize){
    130       // extract synchronisable header
    131     size = *(unsigned int *)mem;
    132     objectID = *(unsigned int*)(mem+sizeof(unsigned int));
    133     classID = *(unsigned int*)(mem+2*sizeof(unsigned int));
    134 
    135     if(!it || it->objectID!=objectID || it->classID!=classID){
    136         // bad luck ;)
    137         // delete the synchronisable (obviously seems to be deleted on the server)
    138       while(it && it->objectID!=objectID)
    139         removeObject(it);
    140 
    141       if(!it){
    142         //fabricate the new synchronisable
    143         if(!Synchronisable::fabricate(mem, mode))
    144           return false;
    145         it=orxonox::ObjectList<Synchronisable>::end();
    146       }else{
    147         if(! it->updateData(mem, mode))
    148         {
    149           COUT(1) << "We couldn't update objectID: " \
    150             << objectID << "; classID: " << classID << std::endl;
    151         }
    152       }
    153     } else
     149  //orxonox::ObjectList<Synchronisable>::iterator it=orxonox::ObjectList<Synchronisable>::begin();
     150  Synchronisable *s;
     151
     152  // update the data of the objects we received
     153  while(mem < data_+sizeof(GamestateHeader)+HEADER->datasize){
     154    synchronisableHeader *objectheader = (synchronisableHeader*)mem;
     155
     156    s = Synchronisable::getSynchronisable( objectheader->objectID );
     157    if(!s)
    154158    {
    155         // we have our object
    156       if(! it->updateData(mem, mode))
    157       {
    158         COUT(1) << "We couldn't update objectID: " \
    159             << objectID << "; classID: " << classID << std::endl;
    160       }
    161     }
    162     ++it;
     159      s = Synchronisable::fabricate(mem, mode);
     160      assert(s);
     161//       if(!s)
     162//         return false;
     163    }
     164    else
     165    {
     166      bool b = s->updateData(mem, mode);
     167      assert(b);
     168      //if(!s->updateData(mem, mode))
     169        //return false;
     170    }
    163171  }
    164172
    165173  return true;
    166174}
     175
     176
    167177
    168178int Gamestate::getID(){
     
    177187  else
    178188  {
    179     return HEADER->normsize+sizeof(GamestateHeader);
     189    return HEADER->datasize+sizeof(GamestateHeader);
    180190  }
    181191}
    182192
    183193bool Gamestate::operator==(packet::Gamestate gs){
    184   unsigned char *d1 = data_+sizeof(GamestateHeader);
    185   unsigned char *d2 = gs.data_+sizeof(GamestateHeader);
     194  uint8_t *d1 = data_+sizeof(GamestateHeader);
     195  uint8_t *d2 = gs.data_+sizeof(GamestateHeader);
    186196  assert(!isCompressed());
    187197  assert(!gs.isCompressed());
    188   while(d1<data_+HEADER->normsize)
     198  while(d1<data_+HEADER->datasize)
    189199  {
    190200    if(*d1!=*d2)
     
    201211}
    202212
     213
     214
    203215bool Gamestate::compressData()
    204216{
    205217  assert(HEADER);
    206218  assert(!HEADER->compressed);
    207   uLongf buffer = (uLongf)(((HEADER->normsize + 12)*1.01)+1);
     219  uLongf buffer = (uLongf)(((HEADER->datasize + 12)*1.01)+1);
    208220  if(buffer==0)
    209221    return false;
    210222
    211   unsigned char *ndata = new unsigned char[buffer+sizeof(GamestateHeader)];
    212   unsigned char *dest = GAMESTATE_START(ndata);
     223  uint8_t *ndata = new uint8_t[buffer+sizeof(GamestateHeader)];
     224  uint8_t *dest = GAMESTATE_START(ndata);
    213225  //unsigned char *dest = new unsigned char[buffer];
    214   unsigned char *source = GAMESTATE_START(data_);
     226  uint8_t *source = GAMESTATE_START(data_);
    215227  int retval;
    216   retval = compress( dest, &buffer, source, (uLong)(HEADER->normsize) );
     228  retval = compress( dest, &buffer, source, (uLong)(HEADER->datasize) );
    217229  switch ( retval ) {
    218230    case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break;
     
    223235#ifndef NDEBUG
    224236  //decompress and compare the start and the decompressed data
    225   unsigned char *rdata = new unsigned char[HEADER->normsize+sizeof(GamestateHeader)];
    226   unsigned char *d2 = GAMESTATE_START(rdata);
    227   uLongf length2 = HEADER->normsize;
     237  uint8_t *rdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
     238  uint8_t *d2 = GAMESTATE_START(rdata);
     239  uLongf length2 = HEADER->datasize;
    228240  uncompress(d2, &length2, dest, buffer);
    229   for(unsigned int i=0; i<HEADER->normsize; i++){
     241  for(unsigned int i=0; i<HEADER->datasize; i++){
    230242    assert(*(source+i)==*(d2+i));
    231243  }
     
    235247  //copy and modify header
    236248#ifndef NDEBUG
    237   HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->normsize);
     249  HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->datasize);
    238250#endif
    239251  *GAMESTATE_HEADER(ndata) = *HEADER;
     
    245257  HEADER->compressed = true;
    246258  assert(HEADER->compressed);
    247   COUT(3) << "gamestate compress normsize: " << HEADER->normsize << " compsize: " << HEADER->compsize << std::endl;
     259  COUT(3) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
    248260  return true;
    249261}
     
    252264  assert(HEADER);
    253265  assert(HEADER->compressed);
    254   COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", normsize: " << HEADER->normsize << ", compsize: " << HEADER->compsize << std::endl;
    255   unsigned int normsize = HEADER->normsize;
     266  COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
     267  unsigned int datasize = HEADER->datasize;
    256268  unsigned int compsize = HEADER->compsize;
    257269  unsigned int bufsize;
    258   assert(compsize<=normsize);
    259   bufsize = normsize;
     270  assert(compsize<=datasize);
     271  bufsize = datasize;
    260272  assert(bufsize!=0);
    261   unsigned char *ndata = new unsigned char[bufsize + sizeof(GamestateHeader)];
    262   unsigned char *dest = ndata + sizeof(GamestateHeader);
    263   unsigned char *source = data_ + sizeof(GamestateHeader);
     273  uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)];
     274  uint8_t *dest = ndata + sizeof(GamestateHeader);
     275  uint8_t *source = data_ + sizeof(GamestateHeader);
    264276  int retval;
    265277  uLongf length=bufsize;
     
    272284  }
    273285#ifndef NDEBUG
    274   assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->normsize));
     286  assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->datasize));
    275287#endif
    276288
     
    282294  data_ = ndata;
    283295  HEADER->compressed = false;
    284   assert(HEADER->normsize==normsize);
     296  assert(HEADER->datasize==datasize);
    285297  assert(HEADER->compsize==compsize);
    286298  return true;
     
    293305  assert(!HEADER->diffed);
    294306  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
    295   unsigned char *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
     307  uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
    296308  unsigned int of=0; // pointers offset
    297309  unsigned int dest_length=0;
    298   dest_length=HEADER->normsize;
     310  dest_length=HEADER->datasize;
    299311  if(dest_length==0)
    300312    return NULL;
    301   unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];
    302   unsigned char *dest = ndata + sizeof(GamestateHeader);
    303   while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){
     313  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
     314  uint8_t *dest = ndata + sizeof(GamestateHeader);
     315  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
    304316    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
    305317    ++of;
    306318  }
    307   if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){
    308     unsigned char n=0;
    309     if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){
     319  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
     320    uint8_t n=0;
     321    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
    310322      while(of<dest_length){
    311323        *(dest+of)=n^*(gs+of);
     
    324336}
    325337
     338Gamestate* Gamestate::doSelection(unsigned int clientID){
     339  assert(data_);
     340  std::map<unsigned int, Synchronisable *>::iterator it;
     341 
     342  // allocate memory for new data
     343  uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
     344  // create a gamestate out of it
     345  Gamestate *gs = new Gamestate(gdata);
     346  uint8_t *newdata = gdata + sizeof(GamestateHeader);
     347  uint8_t *origdata = GAMESTATE_START(data_);
     348 
     349  //copy the GamestateHeader
     350  *(GamestateHeader*)gdata = *HEADER;
     351 
     352  synchronisableHeader *oldobjectheader, *newobjectheader;
     353  unsigned int objectOffset;
     354 
     355  //copy in the zeros
     356  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
     357    oldobjectheader = (synchronisableHeader*)origdata;
     358    newobjectheader = (synchronisableHeader*)newdata;
     359    unsigned int objectsize = oldobjectheader->size;
     360    assert(it->second->objectID==oldobjectheader->objectID);
     361    *newobjectheader = *oldobjectheader;
     362    objectOffset=sizeof(uint8_t)+sizeof(bool); //skip the size and the availableDate variables in the objectheader
     363    if(it->second->doSelection(HEADER->id)){
     364      newobjectheader->dataAvailable=true; //TODO: probably not neccessary
     365      while(objectOffset<objectsize){
     366        *(newdata + objectOffset)=*(origdata + objectOffset);    // copy the data
     367        objectOffset++;
     368      }
     369    }else{
     370      newobjectheader->dataAvailable=false;
     371      while(objectOffset<objectsize){
     372        *(newdata+objectOffset)=0;    // set to 0
     373        objectOffset++;
     374      }
     375      assert(objectOffset==objectsize);
     376    }
     377    newdata += objectsize;
     378    origdata += objectsize;
     379  }
     380  return gs;
     381}
     382
     383
     384Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){
     385  // asserts
     386  assert(data_);
     387  assert(base->data_);
     388  assert(!GAMESTATE_HEADER(base->data_)->diffed);
     389  assert(!GAMESTATE_HEADER(base->data_)->compressed);
     390  assert(!HEADER->compressed);
     391  assert(!HEADER->diffed);
     392 
     393  //preparations
     394  std::map<unsigned int, Synchronisable *>::iterator it;
     395  uint8_t *origdata, *basedata, *destdata, *ndata;
     396  unsigned int objectOffset, streamOffset=0;    //data offset
     397  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
     398  synchronisableHeader *origheader;
     399  synchronisableHeader *destheader;
     400 
     401  origdata = GAMESTATE_START(this->data_);
     402  basedata = GAMESTATE_START(base->data_);
     403  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
     404  destdata = ndata + sizeof(GamestateHeader);
     405 
     406  // do the diff
     407  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
     408    assert(streamOffset<HEADER->datasize);
     409    bool sendData = it->second->doSelection(HEADER->id);
     410    origheader = (synchronisableHeader *)(origdata+streamOffset);
     411    destheader = (synchronisableHeader *)(destdata+streamOffset);
     412   
     413    //copy and partially diff the object header
     414    assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
     415    *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff)
     416    *(bool*)(destdata+sizeof(uint32_t)) = sendData;
     417    if(sendData){
     418      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it)
     419      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it)
     420    }else{
     421      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
     422      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
     423    }
     424    objectOffset=sizeof(synchronisableHeader);
     425    streamOffset+=sizeof(synchronisableHeader);
     426   
     427    //now handle the object data or fill with zeros
     428    while(objectOffset<origheader->size ){
     429     
     430      if(sendData && streamOffset<minsize)
     431        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
     432      else if(sendData)
     433        *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
     434      else
     435        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
     436     
     437      objectOffset++;
     438      streamOffset++;
     439    }
     440    destdata+=objectOffset;
     441    origdata+=objectOffset;
     442    basedata+=objectOffset;
     443  }
     444 
     445  //copy over the gamestate header and set the diffed flag
     446  *(GamestateHeader *)ndata = *HEADER; //copy over the header
     447  Gamestate *gs = new Gamestate(ndata);
     448  GAMESTATE_HEADER(ndata)->diffed=true;
     449  return gs;
     450}
     451
     452Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){
     453  // asserts
     454  assert(data_);
     455  assert(base->data_);
     456  assert(!GAMESTATE_HEADER(base->data_)->diffed);
     457  assert(!GAMESTATE_HEADER(base->data_)->compressed);
     458  assert(!HEADER->compressed);
     459  assert(HEADER->diffed);
     460 
     461  //preparations
     462  std::map<unsigned int, Synchronisable *>::iterator it;
     463  uint8_t *origdata, *basedata, *destdata, *ndata;
     464  unsigned int objectOffset, streamOffset=0;    //data offset
     465  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
     466  synchronisableHeader *origheader;
     467  synchronisableHeader *destheader;
     468 
     469  origdata = GAMESTATE_START(this->data_);
     470  basedata = GAMESTATE_START(base->data_);
     471  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
     472  destdata = ndata + sizeof(GamestateHeader);
     473 
     474  // do the undiff
     475  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
     476    assert(streamOffset<HEADER->datasize);
     477    origheader = (synchronisableHeader *)(origdata+streamOffset);
     478    destheader = (synchronisableHeader *)(destdata+streamOffset);
     479    bool sendData;
     480   
     481    //copy and partially diff the object header
     482    assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
     483    *(unsigned int*)destdata = *(unsigned int*)origdata; //size (do not diff)
     484    *(bool*)(destdata+sizeof(unsigned int)) = *(bool*)(origdata+sizeof(unsigned int));
     485    sendData = *(bool*)(origdata+sizeof(unsigned int));
     486    if(sendData){
     487      *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = *(unsigned int*)(basedata+sizeof(unsigned int)+sizeof(bool)) ^ *(unsigned int*)(origdata+sizeof(unsigned int)+sizeof(bool)); //objectid (diff it)
     488      *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = *(unsigned int*)(basedata+2*sizeof(unsigned int)+sizeof(bool)) ^ *(unsigned int*)(origdata+2*sizeof(unsigned int)+sizeof(bool)); //classid (diff it)
     489    }else{
     490      *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = 0;
     491      *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0;
     492    }
     493    objectOffset=sizeof(synchronisableHeader);
     494    streamOffset+=sizeof(synchronisableHeader);
     495   
     496    //now handle the object data or fill with zeros
     497    while(objectOffset<origheader->size ){
     498     
     499      if(sendData && streamOffset<minsize)
     500        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
     501      else if(sendData)
     502        *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short)
     503      else
     504        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
     505     
     506      objectOffset++;
     507      streamOffset++;
     508    }
     509    destdata+=objectOffset;
     510    origdata+=objectOffset;
     511    basedata+=objectOffset;
     512  }
     513 
     514  //copy over the gamestate header and set the diffed flag
     515  *(GamestateHeader *)ndata = *HEADER; //copy over the header
     516  Gamestate *gs = new Gamestate(ndata);
     517  GAMESTATE_HEADER(ndata)->diffed=false;
     518  return gs;
     519}
     520
    326521Gamestate *Gamestate::undiff(Gamestate *base)
    327522{
     
    330525  assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed);
    331526  //unsigned char *basep = base->getGs()/*, *gs = getGs()*/;
    332   unsigned char *basep = GAMESTATE_START(base->data_);
    333   unsigned char *gs = GAMESTATE_START(this->data_);
     527  uint8_t *basep = GAMESTATE_START(base->data_);
     528  uint8_t *gs = GAMESTATE_START(this->data_);
    334529  unsigned int of=0; // pointers offset
    335530  unsigned int dest_length=0;
    336   dest_length=HEADER->normsize;
     531  dest_length=HEADER->datasize;
    337532  if(dest_length==0)
    338533    return NULL;
    339   unsigned char *ndata = new unsigned char[dest_length*sizeof(unsigned char)+sizeof(GamestateHeader)];
    340   unsigned char *dest = ndata + sizeof(GamestateHeader);
    341   while(of < GAMESTATE_HEADER(base->data_)->normsize && of < HEADER->normsize){
     534  uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)];
     535  uint8_t *dest = ndata + sizeof(GamestateHeader);
     536  while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){
    342537    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
    343538    ++of;
    344539  }
    345   if(GAMESTATE_HEADER(base->data_)->normsize!=HEADER->normsize){
    346     unsigned char n=0;
    347     if(GAMESTATE_HEADER(base->data_)->normsize < HEADER->normsize){
     540  if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){
     541    uint8_t n=0;
     542    if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){
    348543      while(of < dest_length){
    349544        *(dest+of)=n^*(gs+of);
     
    370565    // get total size of gamestate
    371566  for(it = orxonox::ObjectList<Synchronisable>::begin(); it; ++it)
    372     size+=it->getSize2(id, mode); // size of the actual data of the synchronisable
     567    size+=it->getSize(id, mode); // size of the actual data of the synchronisable
    373568//  size+=sizeof(GamestateHeader);
    374569  return size;
  • code/trunk/src/network/packet/Gamestate.h

    r1763 r1907  
    2929#include "Packet.h"
    3030#include "network/Synchronisable.h"
     31#include <map>
    3132#ifndef NDEBUG
    3233#include "util/CRC32.h"
    3334#endif
     35
    3436
    3537#ifndef NETWORK_PACKETGAMESTATE_H
     
    4244struct GamestateHeader{
    4345  ENUM::Type packetType;
    44   int id; // id of the gamestate
    45   unsigned int compsize;
    46   unsigned int normsize;
    47   int base_id; // id of the base-gamestate diffed from
    48   bool diffed; // wheter diffed or not
    49   bool complete; // wheter it is a complete gamestate or only partial
    50   bool compressed;
     46  int32_t id; // id of the gamestate
     47  uint32_t compsize;
     48  uint32_t datasize;
     49  int32_t base_id; // id of the base-gamestate diffed from
     50  bool diffed:1; // wheter diffed or not
     51  bool complete:1; // wheter it is a complete gamestate or only partial
     52  bool compressed:1;
    5153#ifndef NDEBUG
    5254  uint32_t crc32;
     
    5557
    5658/**
    57         @author
     59        @author Oliver Scheuss
    5860*/
    5961class Gamestate: public Packet{
    6062  public:
    6163    Gamestate();
    62     Gamestate(unsigned char *data, int clientID);
     64    Gamestate(uint8_t *data, unsigned int clientID);
     65    Gamestate(uint8_t *data);
    6366
    6467    ~Gamestate();
     
    7174    int getBaseID();
    7275    Gamestate *diff(Gamestate *base);
     76    Gamestate* intelligentDiff(Gamestate *base, unsigned int clientID);
    7377    Gamestate *undiff(Gamestate *base);
     78    Gamestate* intelligentUnDiff(Gamestate *base);
     79    Gamestate* doSelection(unsigned int clientID);
    7480    bool compressData();
    7581    bool decompressData();
    7682
    7783    // Packet functions
     84  private:
    7885    virtual unsigned int getSize() const;
    7986    virtual bool process();
     
    8390    unsigned int calcGamestateSize(unsigned int id, int mode=0x0);
    8491    void removeObject(orxonox::ObjectListIterator<Synchronisable> &it);
    85 
    86 
    87     //Bytestream *bs_;
    88     //GamestateHeader *header_;
     92    std::map<unsigned int, Synchronisable*> dataMap_;
    8993};
    9094
  • code/trunk/src/network/packet/Packet.cc

    r1763 r1907  
    4242#include "Gamestate.h"
    4343#include "Welcome.h"
     44#include "DeleteObjects.h"
    4445#include "network/Host.h"
    4546#include "core/CoreIncludes.h"
     
    6667}
    6768
    68 Packet::Packet(unsigned char *data, int clientID){
     69Packet::Packet(uint8_t *data, unsigned int clientID){
    6970  flags_ = PACKET_FLAG_DEFAULT;
    7071  packetDirection_ = ENUM::Incoming;
     
    7475}
    7576
    76 /*Packet::Packet(ENetPacket *packet, ENetPeer *peer){
    77   packetDirection_ = ENUM::Incoming;
    78   enetPacket_ = packet;
    79   clientID_ = ClientInformation::findClient(&peer->address)->getID();
    80   data_ = packet->data;
    81 }*/
    8277
    8378Packet::Packet(const Packet &p){
     
    8782  clientID_ = p.clientID_;
    8883  if(p.data_){
    89     data_ = new unsigned char[p.getSize()];
     84    data_ = new uint8_t[p.getSize()];
    9085    memcpy(data_, p.data_, p.getSize());
    9186  }else
     
    125120    case ENUM::Gamestate:
    126121    case ENUM::Welcome:
     122    case ENUM::DeleteObjects:
    127123      break;
    128124    default:
     
    138134
    139135Packet *Packet::createPacket(ENetPacket *packet, ENetPeer *peer){
    140   unsigned char *data = packet->data;
     136  uint8_t *data = packet->data;
    141137  unsigned int clientID = ClientInformation::findClient(&peer->address)->getID();
    142138  Packet *p;
    143   COUT(3) << "packet type: " << *(ENUM::Type *)&data[_PACKETID] << std::endl;
     139  COUT(5) << "packet type: " << *(ENUM::Type *)&data[_PACKETID] << std::endl;
    144140  switch( *(ENUM::Type *)(data + _PACKETID) )
    145141  {
    146142    case ENUM::Acknowledgement:
    147       COUT(3) << "ack" << std::endl;
     143      COUT(4) << "ack" << std::endl;
    148144      p = new Acknowledgement( data, clientID );
    149145      break;
    150146    case ENUM::Chat:
    151       COUT(3) << "chat" << std::endl;
     147      COUT(4) << "chat" << std::endl;
    152148      p = new Chat( data, clientID );
    153149      break;
    154150    case ENUM::ClassID:
    155       COUT(3) << "classid" << std::endl;
     151      COUT(4) << "classid" << std::endl;
    156152      p = new ClassID( data, clientID );
    157153      break;
    158154    case ENUM::Gamestate:
    159       COUT(3) << "gamestate" << std::endl;
     155      COUT(4) << "gamestate" << std::endl;
    160156      // TODO: remove brackets
    161157      p = new Gamestate( data, clientID );
    162158      break;
    163159    case ENUM::Welcome:
    164       COUT(3) << "welcome" << std::endl;
     160      COUT(4) << "welcome" << std::endl;
    165161      p = new Welcome( data, clientID );
     162      break;
     163    case ENUM::DeleteObjects:
     164      COUT(4) << "deleteobjects" << std::endl;
     165      p = new DeleteObjects( data, clientID );
    166166      break;
    167167    default:
  • code/trunk/src/network/packet/Packet.h

    r1763 r1907  
    3232#include <enet/enet.h>
    3333
     34#include "util/Integers.h"
     35
    3436namespace network {
    3537
     
    4749    ClassID,
    4850    Chat,
    49     Welcome
     51    Welcome,
     52    DeleteObjects
    5053  };
    5154}
     
    7477  protected:
    7578    Packet();
    76     Packet(unsigned char *data, int clientID);
     79    Packet(uint8_t *data, unsigned int clientID);
    7780//    Packet(ENetPacket *packet, ENetPeer *peer);
    7881    enet_uint32 flags_;
    79     int clientID_;
     82    unsigned int clientID_;
    8083    ENUM::Direction packetDirection_;
    81     unsigned char *data_;
     84    uint8_t *data_;
    8285  private:
    8386    static std::map<ENetPacket *, Packet *> packetMap_;
  • code/trunk/src/network/packet/Welcome.cc

    r1763 r1907  
    4242#define _PACKETID             0
    4343#define _CLIENTID             _PACKETID + sizeof(ENUM::Type)
    44 #define _SHIPID               _CLIENTID + sizeof(unsigned int)
     44#define _SHIPID               _CLIENTID + sizeof(uint32_t)
    4545 
    4646  Welcome::Welcome( unsigned int clientID, unsigned int shipID )
     
    4949  flags_ = flags_ | PACKET_FLAGS_CLASSID;
    5050  assert(getSize());
    51   data_=new unsigned char[ getSize() ];
     51  data_=new uint8_t[ getSize() ];
    5252  assert(data_);
    5353  *(packet::ENUM::Type *)(data_ + _PACKETID ) = packet::ENUM::Welcome;
    54   *(unsigned int *)&data_[ _CLIENTID ] = clientID;
    55   *(unsigned int *)&data_[ _SHIPID ] = shipID;
     54  *(uint32_t *)&data_[ _CLIENTID ] = clientID;
     55  *(uint32_t *)&data_[ _SHIPID ] = shipID;
    5656}
    5757
    58 Welcome::Welcome( unsigned char *data, int clientID )
     58Welcome::Welcome( uint8_t* data, unsigned int clientID )
    5959  : Packet(data, clientID)
    6060{
     
    6565}
    6666
    67 unsigned char *Welcome::getData(){
     67uint8_t *Welcome::getData(){
    6868  return data_;
    6969}
    7070
    7171unsigned int Welcome::getSize() const{
    72   return sizeof(network::packet::ENUM::Type) + 2*sizeof(unsigned int);
     72  return sizeof(network::packet::ENUM::Type) + 2*sizeof(uint32_t);
    7373}
    7474
    7575bool Welcome::process(){
    7676  unsigned int shipID, clientID;
    77   clientID = *(unsigned int *)&data_[ _CLIENTID ];
    78   shipID = *(unsigned int *)&data_[ _SHIPID ];
     77  clientID = *(uint32_t *)&data_[ _CLIENTID ];
     78  shipID = *(uint32_t *)&data_[ _SHIPID ];
    7979  Host::setClientID(clientID);
    8080  Host::setShipID(shipID);
  • code/trunk/src/network/packet/Welcome.h

    r1763 r1907  
    4141public:
    4242  Welcome( unsigned int clientID, unsigned int shipID );
    43   Welcome( unsigned char* data, int clientID );
     43  Welcome( uint8_t* data, unsigned int clientID );
    4444  virtual ~Welcome();
    4545 
    46   unsigned char *getData();
     46  uint8_t *getData();
    4747  inline unsigned int getSize() const;
    4848  bool process();
Note: See TracChangeset for help on using the changeset viewer.