Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

merged network branch back into trunk

Location:
code/trunk/src/network/packet
Files:
6 edited

Legend:

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

    r1735 r1751  
    4545  data_=new unsigned char[ getSize() ];
    4646  *(ENUM::Type *)(data_ + _PACKETID ) = ENUM::Acknowledgement;
    47   *(unsigned int *)&data_[ _ACKID ] = id;
     47  *(unsigned int *)(data_ + _ACKID ) = id;
    4848  clientID_=clientID;
    4949}
  • 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;
  • code/trunk/src/network/packet/Gamestate.h

    r1747 r1751  
    2929#include "Packet.h"
    3030#include "network/Synchronisable.h"
    31 //#include "util/Bytestream.h"
     31#ifndef NDEBUG
     32#include "util/CRC32.h"
     33#endif
    3234#include "core/CoreIncludes.h"
    3335
     
    4850  bool complete; // wheter it is a complete gamestate or only partial
    4951  bool compressed;
     52#ifndef NDEBUG
     53  uint32_t crc32;
     54#endif
    5055};
    5156
     
    6469    int getID();
    6570    bool isDiffed();
     71    bool isCompressed();
    6672    int getBaseID();
    6773    Gamestate *diff(Gamestate *base);
     
    7480    virtual bool process();
    7581
    76 
     82    bool operator ==(packet::Gamestate gs);
    7783  private:
    78     unsigned int calcGamestateSize(int mode=0x0);
    79     void removeObject(orxonox::ObjectListIterator<Synchronisable> &it);
     84    unsigned int calcGamestateSize(unsigned int id, int mode=0x0);
     85    void removeObject(orxonox::ObjectList<Synchronisable>::iterator &it);
    8086
    8187
  • code/trunk/src/network/packet/Packet.cc

    r1735 r1751  
    9292
    9393Packet::~Packet(){
    94   if(enetPacket_)
     94  if(enetPacket_){
     95    assert(enetPacket_->freeCallback==0);
    9596    enet_packet_destroy(enetPacket_);
     97  }
    9698  if(data_)
    9799    delete[] data_;
     
    109111    }
    110112    enetPacket_ = enet_packet_create(getData(), getSize(), getFlags());
    111     //enetPacket_->freeCallback = &Packet::deletePacket;
    112     enetPacket_->freeCallback = &blub;
     113    enetPacket_->freeCallback = &Packet::deletePacket;
     114//     enetPacket_->freeCallback = &blub;
    113115    packetMap_[enetPacket_] = this;
    114116  }
    115   /*switch( *(ENUM::Type *)(data_ + _PACKETID) )
     117#ifndef NDEBUG
     118  switch( *(ENUM::Type *)(data_ + _PACKETID) )
    116119  {
    117120    case ENUM::Acknowledgement:
     
    120123    case ENUM::Gamestate:
    121124    case ENUM::Welcome:
    122       COUT(3) << "welcome" << std::endl;
    123       p = new Welcome( data, clientID );
     125      break;
    124126    default:
    125       assert(0); //TODO: repair this
     127      assert(0); //there was some error, if this is the case
    126128      break;
    127   }*/
    128   network::Host::addPacket( enetPacket_, clientID_);
     129  }
     130#endif
     131  ENetPacket *temp = enetPacket_;
    129132  enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
     133  network::Host::addPacket( temp, clientID_);
    130134  return true;
    131135}
     
    168172void Packet::deletePacket(ENetPacket *packet){
    169173  assert(packetMap_[packet]);
     174  assert(packetMap_[packet]->enetPacket_==0);
    170175  delete packetMap_[packet];
    171176}
  • code/trunk/src/network/packet/Packet.h

    r1735 r1751  
    7878    enet_uint32 flags_;
    7979    int clientID_;
     80    ENUM::Direction packetDirection_;
    8081    unsigned char *data_;
    8182  private:
    8283    static std::map<ENetPacket *, Packet *> packetMap_;
    8384    ENetPacket *enetPacket_;
    84     ENUM::Direction packetDirection_;
    8585};
    8686
  • code/trunk/src/network/packet/Welcome.cc

    r1735 r1751  
    3232#include "Welcome.h"
    3333#include "network/Host.h"
     34#include "network/Synchronisable.h"
    3435#include "core/CoreIncludes.h"
    3536#include <assert.h>
     
    7980  Host::setShipID(shipID);
    8081  COUT(3) << "Welcome set clientId: " << clientID << " shipID: " << shipID << std::endl;
     82  Synchronisable::setClient(true);
    8183  delete this;
    8284  return true;
Note: See TracChangeset for help on using the changeset viewer.