Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 1, 2008, 7:04:09 PM (16 years ago)
Author:
landauf
Message:

merged objecthierarchy branch back to trunk

Location:
code/trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/network/packet/Acknowledgement.h

    r1907 r2087  
    2929#define NETWORKACKNOLEDGEMENT_H
    3030
     31#include "../NetworkPrereqs.h"
    3132#include "Packet.h"
    3233
     
    3536namespace packet {
    3637/**
    37         @author 
     38        @author
    3839*/
    39 class Acknowledgement : public Packet
     40class _NetworkExport Acknowledgement : public Packet
    4041{
    4142public:
     
    4344  Acknowledgement( uint8_t* data, unsigned int clientID );
    4445  ~Acknowledgement();
    45  
     46
    4647  inline unsigned int getSize() const;
    4748  bool process();
    48  
     49
    4950  unsigned int getAckID();
    5051private:
  • code/trunk/src/network/packet/Chat.h

    r1907 r2087  
    22#ifndef NETWORKCHAT_H
    33#define NETWORKCHAT_H
     4
     5#include "../NetworkPrereqs.h"
    46
    57#include <string>
     
    1113namespace packet {
    1214/**
    13         @author 
     15        @author
    1416*/
    15 class Chat : public Packet
     17class _NetworkExport Chat : public Packet
    1618{
    1719public:
     
    1921  Chat( uint8_t* data, unsigned int clientID );
    2022  ~Chat();
    21  
     23
    2224  inline unsigned int getSize() const;
    2325  bool process();
    24  
     26
    2527  unsigned int getMessageLength(){ return messageLength_; };
    2628  unsigned char *getMessage();
  • code/trunk/src/network/packet/ClassID.h

    r1907 r2087  
    2929#define NETWORKCLASSID_H
    3030
     31#include "../NetworkPrereqs.h"
     32
    3133#include <string>
    3234
     
    3739
    3840/**
    39         @author 
     41        @author
    4042*/
    41 class ClassID : public Packet
     43class _NetworkExport ClassID : public Packet
    4244{
    4345public:
     
    4547  ClassID( uint8_t* data, unsigned int clientID );
    4648  ~ClassID();
    47  
     49
    4850  inline unsigned int getSize() const;
    4951  bool process();
    50  
     52
    5153  unsigned int getClassID();
    5254  unsigned int getClassNameLength(){ return classNameLength_; }
  • code/trunk/src/network/packet/DeleteObjects.h

    r1907 r2087  
    2929#define NETWORKPACKETDELETEOBJECTS_H
    3030
     31#include "../NetworkPrereqs.h"
     32
    3133#include "Packet.h"
    3234
     
    3537namespace packet {
    3638/**
    37         @author 
     39        @author
    3840*/
    39 class DeleteObjects : public Packet
     41class _NetworkExport DeleteObjects : public Packet
    4042{
    4143public:
     
    4345  DeleteObjects( uint8_t* data, unsigned int clientID );
    4446  ~DeleteObjects();
    45  
     47
    4648  bool fetchIDs();
    47  
     49
    4850  inline unsigned int getSize() const;
    4951  bool process();
    50  
     52
    5153private:
    5254};
  • code/trunk/src/network/packet/Gamestate.cc

    r1907 r2087  
    4646#define HEADER GAMESTATE_HEADER(data_)
    4747
    48  
     48
    4949#define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
    50  
     50
    5151Gamestate::Gamestate()
    5252{
     
    8585    return false;
    8686  }
    87  
     87
    8888#ifndef NDEBUG
    8989  std::list<Synchronisable*> slist;
     
    115115    slist.push_back(*it);
    116116#endif
    117    
     117
    118118    //if(it->doSelection(id))
    119119    dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
     
    157157    if(!s)
    158158    {
    159       s = Synchronisable::fabricate(mem, mode);
    160       assert(s);
    161 //       if(!s)
    162 //         return false;
     159      Synchronisable::fabricate(mem, mode);
    163160    }
    164161    else
     
    257254  HEADER->compressed = true;
    258255  assert(HEADER->compressed);
    259   COUT(3) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
     256  COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
    260257  return true;
    261258}
     
    264261  assert(HEADER);
    265262  assert(HEADER->compressed);
    266   COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
     263  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
    267264  unsigned int datasize = HEADER->datasize;
    268265  unsigned int compsize = HEADER->compsize;
    269266  unsigned int bufsize;
    270   assert(compsize<=datasize);
     267//  assert(compsize<=datasize);
    271268  bufsize = datasize;
    272269  assert(bufsize!=0);
     
    289286  //copy over the header
    290287  *GAMESTATE_HEADER(ndata) = *HEADER;
    291   //delete old (compressed data)
    292   delete[] data_;
     288
     289  if (this->bDataENetAllocated_){
     290    // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will
     291    // deallocated it anyway. So data and packet stay together.
     292    this->bDataENetAllocated_ = false;
     293  }
     294  else{
     295    // We allocated the memory in the first place (unlikely). So we destroy the old data
     296    // and overwrite it with the new decompressed data.
     297    delete[] this->data_;
     298  }
     299
    293300  //set new pointers
    294301  data_ = ndata;
     
    339346  assert(data_);
    340347  std::map<unsigned int, Synchronisable *>::iterator it;
    341  
     348
    342349  // allocate memory for new data
    343350  uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
     
    346353  uint8_t *newdata = gdata + sizeof(GamestateHeader);
    347354  uint8_t *origdata = GAMESTATE_START(data_);
    348  
     355
    349356  //copy the GamestateHeader
    350357  *(GamestateHeader*)gdata = *HEADER;
    351  
     358
    352359  synchronisableHeader *oldobjectheader, *newobjectheader;
    353360  unsigned int objectOffset;
    354  
     361
    355362  //copy in the zeros
    356363  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
     
    360367    assert(it->second->objectID==oldobjectheader->objectID);
    361368    *newobjectheader = *oldobjectheader;
    362     objectOffset=sizeof(uint8_t)+sizeof(bool); //skip the size and the availableDate variables in the objectheader
     369    objectOffset=sizeof(uint8_t)+sizeof(bool); //skip the size and the availableData variables in the objectheader
    363370    if(it->second->doSelection(HEADER->id)){
    364371      newobjectheader->dataAvailable=true; //TODO: probably not neccessary
     
    390397  assert(!HEADER->compressed);
    391398  assert(!HEADER->diffed);
    392  
     399
    393400  //preparations
    394401  std::map<unsigned int, Synchronisable *>::iterator it;
    395402  uint8_t *origdata, *basedata, *destdata, *ndata;
    396403  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; 
     404  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
    398405  synchronisableHeader *origheader;
    399406  synchronisableHeader *destheader;
    400  
     407
    401408  origdata = GAMESTATE_START(this->data_);
    402409  basedata = GAMESTATE_START(base->data_);
    403410  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
    404411  destdata = ndata + sizeof(GamestateHeader);
    405  
     412
    406413  // do the diff
    407414  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
     
    410417    origheader = (synchronisableHeader *)(origdata+streamOffset);
    411418    destheader = (synchronisableHeader *)(destdata+streamOffset);
    412    
     419
    413420    //copy and partially diff the object header
    414421    assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
     
    420427    }else{
    421428      *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
    422       *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; 
     429      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
    423430    }
    424431    objectOffset=sizeof(synchronisableHeader);
    425432    streamOffset+=sizeof(synchronisableHeader);
    426    
     433
    427434    //now handle the object data or fill with zeros
    428435    while(objectOffset<origheader->size ){
    429      
     436
    430437      if(sendData && streamOffset<minsize)
    431438        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
     
    434441      else
    435442        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
    436      
     443
    437444      objectOffset++;
    438445      streamOffset++;
     
    442449    basedata+=objectOffset;
    443450  }
    444  
     451
    445452  //copy over the gamestate header and set the diffed flag
    446453  *(GamestateHeader *)ndata = *HEADER; //copy over the header
     
    458465  assert(!HEADER->compressed);
    459466  assert(HEADER->diffed);
    460  
     467
    461468  //preparations
    462469  std::map<unsigned int, Synchronisable *>::iterator it;
    463470  uint8_t *origdata, *basedata, *destdata, *ndata;
    464471  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; 
     472  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
    466473  synchronisableHeader *origheader;
    467474  synchronisableHeader *destheader;
    468  
     475
    469476  origdata = GAMESTATE_START(this->data_);
    470477  basedata = GAMESTATE_START(base->data_);
    471478  ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
    472479  destdata = ndata + sizeof(GamestateHeader);
    473  
     480
    474481  // do the undiff
    475482  for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
     
    478485    destheader = (synchronisableHeader *)(destdata+streamOffset);
    479486    bool sendData;
    480    
     487
    481488    //copy and partially diff the object header
    482489    assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
     
    489496    }else{
    490497      *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = 0;
    491       *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0; 
     498      *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0;
    492499    }
    493500    objectOffset=sizeof(synchronisableHeader);
    494501    streamOffset+=sizeof(synchronisableHeader);
    495    
     502
    496503    //now handle the object data or fill with zeros
    497504    while(objectOffset<origheader->size ){
    498      
     505
    499506      if(sendData && streamOffset<minsize)
    500507        *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
     
    503510      else
    504511        *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
    505      
     512
    506513      objectOffset++;
    507514      streamOffset++;
     
    511518    basedata+=objectOffset;
    512519  }
    513  
     520
    514521  //copy over the gamestate header and set the diffed flag
    515522  *(GamestateHeader *)ndata = *HEADER; //copy over the header
  • code/trunk/src/network/packet/Gamestate.h

    r1907 r2087  
    2727 */
    2828
     29
     30#ifndef NETWORK_PACKETGAMESTATE_H
     31#define NETWORK_PACKETGAMESTATE_H
     32
     33#include "../NetworkPrereqs.h"
     34
    2935#include "Packet.h"
    3036#include "network/Synchronisable.h"
     
    3440#endif
    3541
    36 
    37 #ifndef NETWORK_PACKETGAMESTATE_H
    38 #define NETWORK_PACKETGAMESTATE_H
    39 
    4042namespace network {
    4143
    4244namespace packet {
    4345
    44 struct GamestateHeader{
     46struct _NetworkExport GamestateHeader{
    4547  ENUM::Type packetType;
    4648  int32_t id; // id of the gamestate
     
    5961        @author Oliver Scheuss
    6062*/
    61 class Gamestate: public Packet{
     63class _NetworkExport Gamestate: public Packet{
    6264  public:
    6365    Gamestate();
  • code/trunk/src/network/packet/Packet.cc

    r1907 r2087  
    4949
    5050namespace packet{
    51  
     51
    5252#define PACKET_FLAG_DEFAULT ENET_PACKET_FLAG_NO_ALLOCATE
    5353#define _PACKETID           0
    54  
     54
    5555std::map<ENetPacket *, Packet *> Packet::packetMap_;
    56  
     56
    5757Packet::Packet(){
    5858  flags_ = PACKET_FLAG_DEFAULT;
     
    6161  data_=0;
    6262  enetPacket_=0;
     63  bDataENetAllocated_ = false;
    6364}
    6465
     
    7374  data_=data;
    7475  enetPacket_=0;
     76  bDataENetAllocated_ = false;
    7577}
    7678
     
    8688  }else
    8789    data_=0;
    88 }
    89 
     90  bDataENetAllocated_ = p.bDataENetAllocated_;
     91}
     92
     93/**
     94@brief
     95    Destroys a packet completely.
     96   
     97    That also means destroying the ENetPacket if one exists. There
     98*/
    9099Packet::~Packet(){
    91   if(enetPacket_){
    92     assert(enetPacket_->freeCallback==0);
     100  // Deallocate data_ memory if necessary.
     101  if (this->bDataENetAllocated_){
     102    // In this case ENet allocated data_ and will destroy it.
     103  }
     104  else if (this->data_) {
     105    // This destructor was probably called as a consequence to ENet executing our callback.
     106    // It simply serves us to be able to deallocate the packet content (data_) ourselves since
     107    // we have created it in the first place.
     108    delete[] this->data_;
     109  }
     110
     111  // Destroy the ENetPacket if necessary.
     112  // Note: For the case ENet used the callback to destroy the packet, we have already set
     113  // enetPacket_ to NULL to avoid destroying it again.
     114  if (this->enetPacket_){
     115    // enetPacket_->data gets destroyed too by ENet if it was allocated by it.
    93116    enet_packet_destroy(enetPacket_);
    94117  }
    95   if(data_)
    96     delete[] data_;
    97118}
    98119
     
    107128      return false;
    108129    }
     130    // We deliver ENet the data address so that it doesn't memcpy everything again.
     131    // --> We have to delete data_ ourselves!
    109132    enetPacket_ = enet_packet_create(getData(), getSize(), getFlags());
    110133    enetPacket_->freeCallback = &Packet::deletePacket;
    111 //     enetPacket_->freeCallback = &blub;
     134    // Add the packet to a global list so we can access it again once enet calls our
     135    // deletePacket method. We can of course only give a one argument function to the ENet C library.
    112136    packetMap_[enetPacket_] = this;
    113137  }
    114 #ifndef NDEBUG 
     138#ifndef NDEBUG
    115139  switch( *(ENUM::Type *)(data_ + _PACKETID) )
    116140  {
     
    127151  }
    128152#endif
    129   ENetPacket *temp = enetPacket_;
    130   enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
    131   network::Host::addPacket( temp, clientID_);
     153//  ENetPacket *temp = enetPacket_;
     154//  enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
     155  network::Host::addPacket( enetPacket_, clientID_);
    132156  return true;
    133157}
     
    135159Packet *Packet::createPacket(ENetPacket *packet, ENetPeer *peer){
    136160  uint8_t *data = packet->data;
     161  assert(ClientInformation::findClient(&peer->address)->getID() != (unsigned int)-2 || !Host::isServer());
    137162  unsigned int clientID = ClientInformation::findClient(&peer->address)->getID();
    138163  Packet *p;
     
    169194      break;
    170195  }
     196
     197  // Data was created by ENet
     198  p->bDataENetAllocated_ = true;
     199
    171200  return p;
    172201}
    173202
    174 void Packet::deletePacket(ENetPacket *packet){
    175   assert(packetMap_[packet]);
    176   assert(packetMap_[packet]->enetPacket_==0);
    177   delete packetMap_[packet];
     203/**
     204@brief
     205    ENet calls this method whenever it wants to destroy a packet that contains
     206    data we allocated ourselves.
     207*/
     208void Packet::deletePacket(ENetPacket *enetPacket){
     209  // Get our Packet from a gloabal map with all Packets created in the send() method of Packet.
     210  std::map<ENetPacket*, Packet*>::iterator it = packetMap_.find(enetPacket);
     211  assert(it != packetMap_.end());
     212  // Make sure we don't delete it again in the destructor
     213  it->second->enetPacket_ = 0;
     214  delete it->second;
     215  //packetMap_.erase(it);
     216  COUT(4) << "PacketMap size: " << packetMap_.size() << std::endl;
    178217}
    179218
  • code/trunk/src/network/packet/Packet.h

    r1907 r2087  
    2929#define NETWORKPACKET_H
    3030
     31#include "../NetworkPrereqs.h"
     32
    3133#include <map>
    3234#include <enet/enet.h>
     
    3739
    3840namespace packet{
    39  
     41
    4042namespace ENUM{
    4143  enum Direction{
     
    5355  };
    5456}
    55  
     57
    5658/**
    5759        @author Oliver Scheuss <scheusso [at] ee.ethz.ch>
    5860*/
    59 class Packet{
     61class _NetworkExport Packet{
    6062  public:
    6163    Packet(const Packet &p);
     
    6365    static Packet *createPacket(ENetPacket *packet, ENetPeer *peer);
    6466    static void deletePacket(ENetPacket *packet);
    65    
     67
    6668    virtual unsigned char *getData(){ return data_; };
    6769    virtual unsigned int getSize() const =0;
     
    7375    void setClientID( int id )
    7476      { clientID_ = id; }
    75    
     77
    7678    bool send();
    7779  protected:
     
    8284    unsigned int clientID_;
    8385    ENUM::Direction packetDirection_;
     86    /** Pointer to the data. Be careful when deleting it because it might
     87        point to a location that was allocated by ENet.
     88        See bDataENetAllocated_ */
    8489    uint8_t *data_;
     90    /** Tells whether data_ was allocated by ENet or ourselves.
     91        data_ might no correlate with enetPacket_->data. */
     92    bool bDataENetAllocated_;
    8593  private:
    8694    static std::map<ENetPacket *, Packet *> packetMap_;
  • code/trunk/src/network/packet/Welcome.h

    r1907 r2087  
    2929#define NETWORKWELCOME_H
    3030
     31#include "../NetworkPrereqs.h"
     32
    3133#include "Packet.h"
    3234
     
    3537
    3638/**
    37         @author 
     39        @author
    3840*/
    39 class Welcome : public Packet
     41class _NetworkExport Welcome : public Packet
    4042{
    4143public:
     
    4345  Welcome( uint8_t* data, unsigned int clientID );
    4446  virtual ~Welcome();
    45  
     47
    4648  uint8_t *getData();
    4749  inline unsigned int getSize() const;
    4850  bool process();
    49  
     51
    5052private:
    5153};
Note: See TracChangeset for help on using the changeset viewer.