Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 22, 2010, 7:24:24 PM (14 years ago)
Author:
dafrick
Message:

Merging presentation2 branch back to trunk.

Location:
code/trunk
Files:
21 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/network/packet/Acknowledgement.cc

    r6417 r7801  
    3131#include "util/Debug.h"
    3232#include "network/GamestateHandler.h"
     33#include "network/Host.h"
    3334
    3435namespace orxonox {
     
    3940#define _ACKID              _PACKETID + sizeof(packet::Type::Value)
    4041
    41 Acknowledgement::Acknowledgement( unsigned int id, unsigned int clientID )
     42Acknowledgement::Acknowledgement( unsigned int id, unsigned int peerID )
    4243 : Packet()
    4344{
     
    4647  *(Type::Value *)(data_ + _PACKETID ) = Type::Acknowledgement;
    4748  *(uint32_t *)(data_ + _ACKID ) = id;
    48   clientID_=clientID;
     49  peerID_=peerID;
    4950}
    5051
    51 Acknowledgement::Acknowledgement( uint8_t *data, unsigned int clientID )
    52   : Packet(data, clientID)
     52Acknowledgement::Acknowledgement( uint8_t *data, unsigned int peerID )
     53  : Packet(data, peerID)
    5354{
    5455}
     
    6263}
    6364
    64 bool Acknowledgement::process(){
     65bool Acknowledgement::process(orxonox::Host* host){
    6566  COUT(5) << "processing ACK with ID: " << getAckID() << endl;
    66   bool b = GamestateHandler::ackGamestate(getAckID(), clientID_);
     67  bool b = host->ackGamestate(getAckID(), peerID_);
    6768  delete this;
    6869  return b;
  • code/trunk/src/libraries/network/packet/Acknowledgement.h

    r6073 r7801  
    4242{
    4343public:
    44   Acknowledgement( unsigned int id, unsigned int clientID );
    45   Acknowledgement( uint8_t* data, unsigned int clientID );
     44  Acknowledgement( unsigned int id, unsigned int peerID );
     45  Acknowledgement( uint8_t* data, unsigned int peerID );
    4646  ~Acknowledgement();
    4747
    4848  inline unsigned int getSize() const;
    49   bool process();
     49  virtual bool process(orxonox::Host* host);
    5050
    5151  unsigned int getAckID();
  • code/trunk/src/libraries/network/packet/Chat.cc

    r7163 r7801  
    8080}
    8181
    82 bool Chat::process(){
    83   bool b = Host::incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID));
     82bool Chat::process(orxonox::Host* host){
     83  bool b = host->incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID));
    8484  delete this;
    8585  return b;
  • code/trunk/src/libraries/network/packet/Chat.h

    r7163 r7801  
    5252
    5353  /* process chat message packet and remove it afterwards */
    54   bool process();
     54  virtual bool process(orxonox::Host* host);
    5555
    5656  /* Get the length of the message (not the full size of the packet) */
  • code/trunk/src/libraries/network/packet/ClassID.cc

    r7163 r7801  
    120120
    121121
    122 bool ClassID::process(){
     122bool ClassID::process(orxonox::Host* host){
    123123  int nrOfClasses;
    124124  uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid
  • code/trunk/src/libraries/network/packet/ClassID.h

    r6417 r7801  
    4848
    4949  uint32_t getSize() const;
    50   bool process();
     50  virtual bool process(orxonox::Host* host);
    5151
    5252private:
  • code/trunk/src/libraries/network/packet/DeleteObjects.cc

    r6417 r7801  
    5757}
    5858
    59 bool DeleteObjects::fetchIDs(){
     59bool DeleteObjects::fetchIDs()
     60{
    6061  unsigned int number = Synchronisable::getNumberOfDeletedObject();
    6162  if(number==0)
     
    7980}
    8081
    81 unsigned int DeleteObjects::getSize() const{
     82unsigned int DeleteObjects::getSize() const
     83{
    8284  assert(data_);
    8385  return _OBJECTIDS + *(uint32_t*)(data_+_QUANTITY)*sizeof(uint32_t);
    8486}
    8587
    86 bool DeleteObjects::process(){
    87   for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++){
     88bool DeleteObjects::process(orxonox::Host* host)
     89{
     90  for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++)
     91  {
    8892    COUT(4) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << std::endl;
    8993    Synchronisable::deleteObject( *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) );
  • code/trunk/src/libraries/network/packet/DeleteObjects.h

    r6073 r7801  
    3232#include "Packet.h"
    3333
    34 namespace orxonox {
    35 namespace packet {
     34namespace orxonox
     35{
     36namespace packet
     37{
    3638/**
    3739    @author
     
    4749
    4850  inline unsigned int getSize() const;
    49   bool process();
     51  virtual bool process(orxonox::Host* host);
    5052
    5153private:
  • code/trunk/src/libraries/network/packet/FunctionCalls.cc

    r7495 r7801  
    3232#include "network/FunctionCall.h"
    3333#include "network/FunctionCallManager.h"
     34#include "network/GamestateHandler.h"
    3435
    3536namespace orxonox {
     
    4041const unsigned int FUNCTIONCALLS_MEM_ALLOCATION = 1000;
    4142
    42 FunctionCalls::FunctionCalls()
    43  : Packet()
     43FunctionCalls::FunctionCalls():
     44  Packet(), minGamestateID_(GAMESTATEID_INITIAL)
    4445{
    4546  flags_ = flags_ | PACKET_FLAGS_FUNCTIONCALLS;
    46   currentSize_ = 2*sizeof(uint32_t); // for packetid and nrOfCalls
     47  currentSize_ = 3*sizeof(uint32_t); // for packetid, nrOfCalls and minGamestateID_
    4748}
    4849
    49 FunctionCalls::FunctionCalls( uint8_t* data, unsigned int clientID )
    50   : Packet(data, clientID)
     50FunctionCalls::FunctionCalls( uint8_t* data, unsigned int clientID ):
     51  Packet(data, clientID), minGamestateID_(GAMESTATEID_INITIAL)
    5152{
    5253}
     
    5758
    5859
    59 bool FunctionCalls::process(){
     60bool FunctionCalls::process(orxonox::Host* host)
     61{
    6062  assert(isDataENetAllocated());
    6163 
    6264  uint8_t* temp = data_+sizeof(uint32_t); //skip packetid
    6365  uint32_t nrOfCalls = *(uint32_t*)temp;
     66  temp += sizeof(uint32_t);
     67  this->minGamestateID_ = *(uint32_t*)temp;
    6468  temp += sizeof(uint32_t);
    6569  for( unsigned int i = 0; i<nrOfCalls; i++ )
     
    6872    fctCall.loadData(temp);
    6973    if( !fctCall.execute() )
    70       FunctionCallManager::bufferIncomingFunctionCall( fctCall );
     74    {
     75      FunctionCallManager::bufferIncomingFunctionCall( fctCall, minGamestateID_, this->getPeerID() );
     76    }
    7177  }
    7278 
     
    7581}
    7682
    77 void FunctionCalls::addCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
     83void FunctionCalls::addCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5)
     84{
    7885  assert(!isDataENetAllocated());
    7986 
     
    8390}
    8491
    85 void FunctionCalls::addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
     92void FunctionCalls::addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5)
     93{
    8694  assert(!isDataENetAllocated());
    8795 
     
    9199}
    92100
    93 bool FunctionCalls::send()
     101bool FunctionCalls::send(orxonox::Host* host)
    94102{
     103  this->minGamestateID_ = host->getCurrentGamestateID();
    95104  assert(this->functionCalls_.size());
    96105  data_=new uint8_t[ currentSize_ ];
    97106  *(Type::Value *)(data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
    98   *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls to 0
    99   uint8_t* temp = data_+2*sizeof(uint32_t);
     107  *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls
     108  *(uint32_t*)(data_+2*sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_
     109  uint8_t* temp = data_+3*sizeof(uint32_t);
    100110 
    101111  while( this->functionCalls_.size() )
     
    107117  assert( temp==data_+currentSize_ );
    108118 
    109   Packet::send();
     119  Packet::send(host);
    110120  return true;
    111121}
  • code/trunk/src/libraries/network/packet/FunctionCalls.h

    r7490 r7801  
    5454  inline unsigned int getSize() const
    5555    { assert(!this->isDataENetAllocated()); return currentSize_; }
    56   bool process();
     56  virtual bool process(orxonox::Host* host);
    5757
    5858  void addCallStatic( uint32_t networkID, const MultiType* mt1=0, const MultiType* mt2=0, const MultiType* mt3=0, const MultiType* mt4=0, const MultiType* mt5=0);
    5959  void addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1=0, const MultiType* mt2=0, const MultiType* mt3=0, const MultiType* mt4=0, const MultiType* mt5=0);
    60   virtual bool send();
     60  virtual bool send(orxonox::Host* host);
    6161private:
    6262  std::queue<orxonox::FunctionCall> functionCalls_;
    6363  unsigned int                      clientID_;
     64  uint32_t                          minGamestateID_;
    6465  uint32_t                          currentSize_;
    6566};
  • code/trunk/src/libraries/network/packet/FunctionIDs.cc

    r6417 r7801  
    4646
    4747
    48 FunctionIDs::FunctionIDs( ) : Packet(){
     48FunctionIDs::FunctionIDs( ) : Packet()
     49{
    4950  unsigned int nrOfFunctions=0;
    5051  unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nroffunctions
     
    5556  //calculate total needed size (for all strings and integers)
    5657  ObjectList<NetworkFunctionBase>::iterator it;
    57   for(it = ObjectList<NetworkFunctionBase>::begin(); it; ++it){
     58  for(it = ObjectList<NetworkFunctionBase>::begin(); it; ++it)
     59  {
    5860    const std::string& functionname = it->getName();
    5961    networkID = it->getNetworkID();
     
    7678  // now save all classids and classnames
    7779  std::pair<uint32_t, std::string> tempPair;
    78   while( !tempQueue.empty() ){
     80  while( !tempQueue.empty() )
     81  {
    7982    tempPair = tempQueue.front();
    8083    tempQueue.pop();
     
    98101}
    99102
    100 uint32_t FunctionIDs::getSize() const{
     103uint32_t FunctionIDs::getSize() const
     104{
    101105  assert(this->data_);
    102106  uint8_t *temp = data_+sizeof(uint32_t); // packet identification
     
    114118
    115119
    116 bool FunctionIDs::process(){
     120bool FunctionIDs::process(orxonox::Host* host)
     121{
    117122  int nrOfFunctions;
    118123  uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid
     
    127132  temp += sizeof(uint32_t);
    128133
    129   for( int i=0; i<nrOfFunctions; i++){
     134  for( int i=0; i<nrOfFunctions; i++)
     135  {
    130136    networkID = *(uint32_t*)temp;
    131137    stringsize = *(uint32_t*)(temp+sizeof(uint32_t));
  • code/trunk/src/libraries/network/packet/FunctionIDs.h

    r6417 r7801  
    4747  ~FunctionIDs();
    4848
    49   uint32_t getSize() const;
    50   bool process();
     49  virtual uint32_t getSize() const;
     50  virtual bool process(orxonox::Host* host);
    5151
    5252private:
  • code/trunk/src/libraries/network/packet/Gamestate.cc

    r7163 r7801  
    3636#include "network/synchronisable/Synchronisable.h"
    3737#include "network/GamestateHandler.h"
     38#include "network/Host.h"
    3839
    3940namespace orxonox {
     
    4344#define GAMESTATE_START(data) (data + GamestateHeader::getSize())
    4445
    45 #define PACKET_FLAG_GAMESTATE  PacketFlag::Reliable
     46// #define PACKET_FLAG_GAMESTATE  PacketFlag::Reliable
     47#define PACKET_FLAG_GAMESTATE  0
    4648
    4749inline bool memzero( uint8_t* data, uint32_t datalength)
     
    6567
    6668Gamestate::Gamestate():
    67   header_(0)
     69  header_()
    6870{
    6971  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
     
    7274
    7375Gamestate::Gamestate(uint8_t *data, unsigned int clientID):
    74   Packet(data, clientID)
     76  Packet(data, clientID), header_(data)
    7577{
    7678  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
    77   header_ = new GamestateHeader(data_);
    78 }
    79 
    80 
    81 Gamestate::Gamestate(uint8_t *data)
     79}
     80
     81
     82Gamestate::Gamestate(uint8_t *data):
     83  header_(data)
    8284{
    8385  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
    8486  data_ = data;
    85   header_ = new GamestateHeader(data_);
    8687}
    8788
    8889
    8990Gamestate::Gamestate(const Gamestate& g) :
    90     Packet( *(Packet*)&g ), nrOfVariables_(0)
     91  Packet( *(Packet*)&g ), header_(this->data_), nrOfVariables_(0)
    9192{
    9293  flags_ = flags_ | PACKET_FLAG_GAMESTATE;
    93   header_ = new GamestateHeader(data_);
    9494  sizes_ = g.sizes_;
    9595}
     
    9898Gamestate::~Gamestate()
    9999{
    100   if( header_ )
    101     delete header_;
    102100}
    103101
     
    105103bool Gamestate::collectData(int id, uint8_t mode)
    106104{
    107   assert(this->header_==0); // make sure the header didn't exist before
    108105  uint32_t tempsize=0, currentsize=0;
    109106  assert(data_==0);
     
    120117  }
    121118
    122   // create the header object
    123   assert( header_ == 0 );
    124   header_ = new GamestateHeader(data_);
     119  // tell the gamestate header where to store the data
     120  header_.setData(this->data_);
    125121
    126122  //start collect data synchronisable by synchronisable
     
    142138      assert(0); // if we don't use multithreading this part shouldn't be neccessary
    143139      // start allocate additional memory
    144       COUT(3) << "G.St.Man: need additional memory" << std::endl;
     140      COUT(3) << "Gamestate: need additional memory" << std::endl;
    145141      ObjectList<Synchronisable>::iterator temp = it;
    146142      uint32_t addsize=tempsize;
     
    161157
    162158  //start write gamestate header
    163   header_->setDataSize( currentsize );
    164   header_->setID( id );
    165   header_->setBaseID( GAMESTATEID_INITIAL );
    166   header_->setDiffed( false );
    167   header_->setComplete( true );
    168   header_->setCompressed( false );
     159  header_.setDataSize( currentsize );
     160  header_.setID( id );
     161  header_.setBaseID( GAMESTATEID_INITIAL );
     162  header_.setDiffed( false );
     163  header_.setComplete( true );
     164  header_.setCompressed( false );
    169165  //stop write gamestate header
    170166
    171   COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl;
    172   COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
     167  COUT(5) << "Gamestate: Gamestate size: " << currentsize << std::endl;
     168  COUT(5) << "Gamestate: 'estimated' (and corrected) Gamestate size: " << size << std::endl;
    173169  return true;
    174170}
     
    177173bool Gamestate::spreadData(uint8_t mode)
    178174{
    179   COUT(4) << "processing gamestate with id " << header_->getID() << endl;
     175  COUT(5) << "processing gamestate with id " << header_.getID() << endl;
    180176  assert(data_);
    181   assert(!header_->isCompressed());
     177  assert(!header_.isCompressed());
    182178  uint8_t *mem=data_+GamestateHeader::getSize();
    183179  Synchronisable *s;
    184 
     180 
    185181  // update the data of the objects we received
    186   while(mem < data_+GamestateHeader::getSize()+header_->getDataSize())
     182  while(mem < data_+GamestateHeader::getSize()+header_.getDataSize())
    187183  {
    188184    SynchronisableHeader objectheader(mem);
     
    197193      else
    198194      {
     195//         COUT(4) << "not creating object of classid " << objectheader.getClassID() << endl;
    199196        mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() );
    200197      }
     
    202199    else
    203200    {
     201//       COUT(4) << "updating object of classid " << objectheader.getClassID() << endl;
    204202      bool b = s->updateData(mem, mode);
    205203      assert(b);
    206204    }
    207205  }
     206  assert(mem-data_ == GamestateHeader::getSize()+header_.getDataSize());
     207 
    208208   // In debug mode, check first, whether there are no duplicate objectIDs
    209209#ifndef NDEBUG
     
    249249{
    250250  assert(data_);
    251   if(header_->isCompressed())
    252     return header_->getCompSize()+GamestateHeader::getSize();
     251  if(header_.isCompressed())
     252    return header_.getCompSize()+GamestateHeader::getSize();
    253253  else
    254254  {
    255     return header_->getDataSize()+GamestateHeader::getSize();
     255    return header_.getDataSize()+GamestateHeader::getSize();
    256256  }
    257257}
     
    271271
    272272
    273 bool Gamestate::process()
    274 {
    275   return GamestateHandler::addGamestate(this, getClientID());
     273bool Gamestate::process(orxonox::Host* host)
     274{
     275  return host->addGamestate(this, getPeerID());
    276276}
    277277
     
    280280{
    281281  assert(data_);
    282   assert(!header_->isCompressed());
    283   uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1);
     282  assert(!header_.isCompressed());
     283  uLongf buffer = (uLongf)(((header_.getDataSize() + 12)*1.01)+1);
    284284  if(buffer==0)
    285285    return false;
     
    289289  uint8_t *source = data_ + GamestateHeader::getSize();
    290290  int retval;
    291   retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) );
     291  retval = compress( dest, &buffer, source, (uLong)(header_.getDataSize()) );
    292292  switch ( retval )
    293293  {
     
    299299
    300300  //copy and modify header
    301   GamestateHeader *temp = header_;
    302   header_ = new GamestateHeader(ndata, temp);
     301  GamestateHeader *temp = new GamestateHeader(data_);
     302  header_.setData(ndata);
     303  header_ = *temp;
    303304  delete temp;
    304305  //delete old data
     
    306307  //save new data
    307308  data_ = ndata;
    308   header_->setCompSize( buffer );
    309   header_->setCompressed( true );
    310   COUT(0) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;
     309  header_.setCompSize( buffer );
     310  header_.setCompressed( true );
     311  COUT(4) << "gamestate compress datasize: " << header_.getDataSize() << " compsize: " << header_.getCompSize() << std::endl;
    311312  return true;
    312313}
     
    316317{
    317318  assert(data_);
    318   assert(header_->isCompressed());
    319   COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl;
    320   uint32_t datasize = header_->getDataSize();
    321   uint32_t compsize = header_->getCompSize();
     319  assert(header_.isCompressed());
     320  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_.getID() << ", baseid: " << header_.getBaseID() << ", datasize: " << header_.getDataSize() << ", compsize: " << header_.getCompSize() << std::endl;
     321  uint32_t datasize = header_.getDataSize();
     322  uint32_t compsize = header_.getCompSize();
    322323  uint32_t bufsize;
    323324  bufsize = datasize;
     
    338339
    339340  //copy over the header
    340   GamestateHeader *temp = header_;
    341   header_ = new GamestateHeader( data_, header_ );
     341  GamestateHeader* temp = new GamestateHeader( data_ );
     342  header_.setData(ndata);
     343  header_ = *temp;
    342344  delete temp;
    343345
     
    357359  //set new pointers
    358360  data_ = ndata;
    359   header_->setCompressed( false );
    360   assert(header_->getDataSize()==datasize);
    361   assert(header_->getCompSize()==compsize);
     361  header_.setCompressed( false );
     362  assert(header_.getDataSize()==datasize);
     363  assert(header_.getCompSize()==compsize);
    362364  return true;
    363365}
    364366
    365367
     368inline void /*Gamestate::*/diffObject( uint8_t*& newDataPtr, uint8_t*& origDataPtr, uint8_t*& baseDataPtr, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
     369{
     370  assert( objectHeader.getDataSize() == SynchronisableHeader(baseDataPtr).getDataSize() );
     371 
     372  uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData
     373  // Check whether the whole object stayed the same
     374  if( memcmp( origDataPtr+objectOffset, baseDataPtr+objectOffset, objectHeader.getDataSize()) == 0 )
     375  {
     376//     COUT(4) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl;
     377    origDataPtr += objectOffset + objectHeader.getDataSize(); // skip the whole object
     378    baseDataPtr += objectOffset + objectHeader.getDataSize();
     379    sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables();
     380  }
     381  else
     382  {
     383    // Now start to diff the Object
     384    SynchronisableHeaderLight newObjectHeader(newDataPtr);
     385    newObjectHeader = objectHeader; // copy over the objectheader
     386    VariableID variableID = 0;
     387    uint32_t diffedObjectOffset = SynchronisableHeaderLight::getSize();
     388    // iterate through all variables
     389    while( objectOffset < objectHeader.getDataSize()+SynchronisableHeader::getSize() )
     390    {
     391      // check whether variable changed and write id and copy over variable to the new stream
     392      // otherwise skip variable
     393      uint32_t varSize = *sizes;
     394      assert( varSize == Synchronisable::getSynchronisable(objectHeader.getObjectID())->getVarSize(variableID) );
     395      if ( varSize != 0 )
     396      {
     397        if ( memcmp(origDataPtr+objectOffset, baseDataPtr+objectOffset, varSize) != 0 )
     398        {
     399          *(VariableID*)(newDataPtr+diffedObjectOffset) = variableID; // copy over the variableID
     400          diffedObjectOffset += sizeof(VariableID);
     401          memcpy( newDataPtr+diffedObjectOffset, origDataPtr+objectOffset, varSize );
     402          diffedObjectOffset += varSize;
     403          objectOffset += varSize;
     404        }
     405        else
     406        {
     407          objectOffset += varSize;
     408        }
     409      }
     410
     411      ++variableID;
     412      ++sizes;
     413    }
     414   
     415    // if there are variables from this object with 0 size left in sizes
     416    if( Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() != variableID )
     417      sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() - variableID;
     418   
     419    newObjectHeader.setDiffed(true);
     420    newObjectHeader.setDataSize(diffedObjectOffset-SynchronisableHeaderLight::getSize());
     421    assert(objectOffset == objectHeader.getDataSize()+SynchronisableHeader::getSize());
     422    assert(newObjectHeader.getDataSize()>0);
     423   
     424    origDataPtr += objectOffset;
     425    baseDataPtr += objectOffset;
     426    newDataPtr += diffedObjectOffset;
     427  }
     428}
     429
     430inline void /*Gamestate::*/copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
     431{
     432  //       COUT(4) << "docopy" << endl;
     433  // Just copy over the whole Object
     434  memcpy( newData, origData, objectHeader.getDataSize()+SynchronisableHeader::getSize() );
     435  SynchronisableHeader(newData).setDiffed(false);
     436 
     437  newData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
     438  origData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
     439//   SynchronisableHeader baseHeader( baseData );
     440//   baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize();
     441  //       COUT(4) << "copy " << h.getObjectID() << endl;
     442  //       COUT(4) << "copy " << h.getObjectID() << ":";
     443  sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables();
     444//   for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i )
     445//   {
     446//     //         COUT(4) << " " << *sizes;
     447//     ++sizes;
     448//   }
     449    //       COUT(4) << endl;
     450}
     451
     452inline bool findObject(uint8_t*& dataPtr, uint8_t* endPtr, SynchronisableHeader& objectHeader)
     453{
     454  // Some assertions to make sure the dataPtr is valid (pointing to a SynchronisableHeader)
     455  {
     456    SynchronisableHeader htemp2(dataPtr);
     457    assert(htemp2.getClassID()<500);
     458    assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
     459    assert(htemp2.isDiffed()==false);
     460  }
     461  uint32_t objectID = objectHeader.getObjectID();
     462  while ( dataPtr < endPtr )
     463  {
     464    SynchronisableHeader htemp(dataPtr);
     465    assert( htemp.getDataSize()!=0 );
     466    if ( htemp.getObjectID() == objectID )
     467    {
     468      assert( objectHeader.getClassID() == htemp.getClassID() );
     469      assert( objectHeader.getCreatorID() == htemp.getCreatorID() );
     470      return true;
     471    }
     472    {
     473      if( dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize() < endPtr )
     474      {
     475        SynchronisableHeader htemp2(dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize());
     476        assert(htemp2.getClassID()<500);
     477        assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
     478        assert(htemp2.isDiffed()==false);
     479      }
     480    }
     481    dataPtr += htemp.getDataSize()+SynchronisableHeader::getSize();
     482   
     483  }
     484  assert(dataPtr == endPtr);
     485 
     486  return false;
     487}
     488
    366489Gamestate* Gamestate::diffVariables(Gamestate *base)
    367490{
    368491  assert(this && base); assert(data_ && base->data_);
    369   assert(!header_->isCompressed() && !base->header_->isCompressed());
    370   assert(!header_->isDiffed());
     492  assert(!header_.isCompressed() && !base->header_.isCompressed());
     493  assert(!header_.isDiffed());
     494  assert( header_.getDataSize() && base->header_.getDataSize() );
    371495
    372496
    373497  // *** first do a raw diff of the two gamestates
    374498
    375   uint8_t *baseData = GAMESTATE_START(base->data_);
    376   uint8_t *origData = GAMESTATE_START(this->data_);
    377   uint32_t origLength = header_->getDataSize();
    378   uint32_t baseLength = base->header_->getDataSize();
    379 
    380   assert( origLength && baseLength );
    381 
    382   uint8_t *nData = new uint8_t[origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_]; // this is the maximum size needed in the worst case
    383   uint8_t *dest = GAMESTATE_START(nData);
    384 
    385   uint32_t baseOffset = 0; //offset in the diffed stream
    386   uint32_t origOffset = 0; //offset in the new stream with removed 0's
    387   std::vector<uint32_t>::iterator sizes = this->sizes_.begin();
    388 
    389   while( origOffset < origLength )
     499  uint8_t *baseDataPtr = GAMESTATE_START(base->data_);
     500  uint8_t *origDataPtr = GAMESTATE_START(this->data_);
     501  uint8_t *origDataEnd = origDataPtr + header_.getDataSize();
     502  uint8_t *baseDataEnd = baseDataPtr + base->header_.getDataSize();
     503//   uint32_t origLength = header_.getDataSize();
     504//   uint32_t baseLength = base->header_.getDataSize();
     505
     506  // Allocate new space for diffed gamestate
     507  uint32_t newDataSize = header_.getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_;
     508  uint8_t *newData = new uint8_t[newDataSize]; // this is the maximum size needed in the worst case
     509  uint8_t *destDataPtr = GAMESTATE_START(newData);
     510
     511  std::vector<uint32_t>::iterator sizesIt = this->sizes_.begin();
     512
     513  while( origDataPtr < origDataEnd )
    390514  {
    391515    //iterate through all objects
    392516
    393     SynchronisableHeader h(origData+origOffset);
     517    SynchronisableHeader origHeader(origDataPtr);
    394518
    395519    // Find (if possible) the current object in the datastream of the old gamestate
    396520    // Start at the current offset position
    397     if(baseOffset >= baseLength)
    398       baseOffset = 0;
    399     uint8_t* temp = baseData + baseOffset;
    400     uint32_t objectID = h.getObjectID();
    401     assert(temp < baseData+baseLength);
    402     assert(dest < nData + origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_);
    403     assert(sizes != this->sizes_.end());
    404     while ( temp < baseData+baseLength )
    405     {
    406       SynchronisableHeader htemp(temp);
    407       assert( htemp.getDataSize()!=0 );
    408       if ( htemp.getObjectID() == objectID )
    409       {
    410         assert( h.getClassID() == htemp.getClassID() );
    411         goto DODIFF;
    412       }
    413 //       {
    414 //         SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize());
    415 //         if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength )
    416 //         {
    417 //           assert(htemp2.getClassID()<500);
    418 //           assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
    419 //           assert(htemp2.isDiffed()==false);
    420 //         }
    421 //       }
    422       temp += htemp.getDataSize()+SynchronisableHeader::getSize();
     521    if(baseDataPtr == baseDataEnd)
     522      baseDataPtr = GAMESTATE_START(base->data_);
     523    uint8_t* oldBaseDataPtr = baseDataPtr;
     524   
     525    assert(baseDataPtr < baseDataEnd);
     526    assert(destDataPtr < newData + newDataSize);
     527    assert(sizesIt != this->sizes_.end());
     528   
     529    assert(Synchronisable::getSynchronisable(origHeader.getObjectID()));
     530    assert(ClassByID(origHeader.getClassID()));
     531    assert(origHeader.getDataSize() < 500);
     532   
     533    bool diffedObject = false;
     534    if( findObject(baseDataPtr, baseDataEnd, origHeader) )
     535    {
     536      SynchronisableHeader baseHeader(baseDataPtr);
     537      assert(Synchronisable::getSynchronisable(baseHeader.getObjectID()));
     538      assert(ClassByID(baseHeader.getClassID()));
     539      assert(baseHeader.getDataSize() < 500);
     540      if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() )
     541      {
     542//         COUT(4) << "diffing object in order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     543        diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
     544        diffedObject = true;
     545      }
     546      else
     547      {
     548//         COUT(4) << "copy object because of different data sizes (1): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     549        copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
     550        assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
     551      }
    423552       
    424553    }
    425     // If not found start looking at the beginning
    426     assert( temp==baseData+baseLength );
    427     temp = baseData;
    428 //     {
    429 //       SynchronisableHeader htemp2(temp);
    430 //       if( temp < baseData+baseLength )
    431 //       {
    432 //         assert(htemp2.getClassID()<500);
    433 //         assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
    434 //         assert(htemp2.isDiffed()==false);
    435 //       }
    436 //     }
    437     while ( temp < baseData+baseOffset )
    438     {
    439       SynchronisableHeader htemp(temp);
    440       if ( htemp.getObjectID() == objectID )
    441       {
    442         assert( h.getClassID() == htemp.getClassID() );
    443         goto DODIFF;
    444       }
    445 //       {
    446 //         SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize());
    447 //         if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength )
    448 //         {
    449 //           assert(htemp2.getClassID()<500);
    450 //           assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
    451 //           assert(htemp2.isDiffed()==false);
    452 //         }
    453 //       }
    454       temp += htemp.getDataSize()+SynchronisableHeader::getSize();
    455     }
    456     // Object is new, thus never transmitted -> just copy over
    457     goto DOCOPY;
    458 
    459 
    460 DODIFF:
    461     {
    462 //       COUT(4) << "dodiff" << endl;
    463 //       if(baseOffset==0)
    464 //       {
    465 //         assert(origOffset==0);
    466 //       }
    467       uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData
    468       // Check whether the whole object stayed the same
    469       if( memcmp( origData+origOffset+objectOffset, temp+objectOffset, h.getDataSize()) == 0 )
    470       {
    471 //         COUT(4) << "skip object" << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
    472         origOffset += objectOffset+ h.getDataSize(); // skip the whole object
    473         baseOffset = temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
    474         sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables();
     554    else
     555    {
     556      assert( baseDataPtr == baseDataEnd );
     557      baseDataPtr = GAMESTATE_START(base->data_);
     558      if( findObject(baseDataPtr, oldBaseDataPtr, origHeader) )
     559      {
     560        SynchronisableHeader baseHeader(baseDataPtr);
     561        assert(Synchronisable::getSynchronisable(baseHeader.getObjectID()));
     562        assert(ClassByID(baseHeader.getClassID()));
     563        assert(baseHeader.getDataSize() < 500);
     564        if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() )
     565        {
     566//           COUT(4) << "diffing object out of order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     567          diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
     568          diffedObject = true;
     569        }
     570        else
     571        {
     572//           COUT(4) << "copy object because of different data sizes (2): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     573          copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
     574          assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
     575        }
    475576      }
    476577      else
    477578      {
    478 //         if( Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() == "Bot" )
    479 //           COUT(0) << "blub" << endl;
    480 //         COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
    481 //         COUT(4) << "diff " << h.getObjectID() << ":";
    482         // Now start to diff the Object
    483         SynchronisableHeaderLight h2(dest);
    484         h2 = h; // copy over the objectheader
    485         VariableID variableID = 0;
    486         uint32_t newObjectOffset = SynchronisableHeaderLight::getSize();
    487         // iterate through all variables
    488         while( objectOffset < h.getDataSize()+SynchronisableHeader::getSize() )
    489         {
    490           // check whether variable changed and write id and copy over variable to the new stream
    491           // otherwise skip variable
    492           assert(sizes != this->sizes_.end());
    493           uint32_t varSize = *sizes;
    494           assert( varSize == Synchronisable::getSynchronisable(h.getObjectID())->getVarSize(variableID) );
    495           if ( varSize != 0 )
    496           {
    497             if ( memcmp(origData+origOffset+objectOffset, temp+objectOffset, varSize) != 0 )
    498             {
    499 //               COUT(4) << "copy variable" << endl;
    500               *(VariableID*)(dest+newObjectOffset) = variableID; // copy over the variableID
    501               newObjectOffset += sizeof(VariableID);
    502               memcpy( dest+newObjectOffset, origData+origOffset+objectOffset, varSize );
    503               newObjectOffset += varSize;
    504               objectOffset += varSize;
    505             }
    506             else
    507             {
    508 //               COUT(4) << "skip variable" << endl;
    509               objectOffset += varSize;
    510             }
    511           }
    512 //           else
    513 //             COUT(4) << "varsize 0" << endl;
    514 
    515           ++variableID;
    516           ++sizes;
    517         }
    518        
    519         if( Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() != variableID )
    520           sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() - variableID;
    521 //         COUT(4) << endl;
    522         h2.setDiffed(true);
    523         h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize());
    524         assert(objectOffset == h.getDataSize()+SynchronisableHeader::getSize());
    525         origOffset += objectOffset;
    526 //         baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
    527         //baseOffset += objectOffset;
    528 //         SynchronisableHeader htemp(temp);
    529 //         baseOffset += SynchronisableHeader::getSize() + htemp.getDataSize();
    530 //         {
    531 //           SynchronisableHeader htemp2( baseData+(temp-baseData+objectOffset) );
    532 //           if( baseData+(temp-baseData+objectOffset) < baseData+baseLength )
    533 //           {
    534 //             assert(htemp2.getClassID()<500);
    535 //             assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
    536 //             assert(htemp2.isDiffed()==false);
    537 //           }
    538 //         }
    539         baseOffset = temp-baseData + objectOffset;
    540         dest += newObjectOffset;
    541       }
    542 
    543       continue;
    544     }
    545 
    546 DOCOPY:
    547     {
    548 //       COUT(4) << "docopy" << endl;
    549       // Just copy over the whole Object
    550       memcpy( dest, origData+origOffset, h.getDataSize()+SynchronisableHeader::getSize() );
    551       dest += h.getDataSize()+SynchronisableHeader::getSize();
    552       origOffset += h.getDataSize()+SynchronisableHeader::getSize();
    553       assert( Synchronisable::getSynchronisable(h.getObjectID()) );
    554 //       COUT(4) << "copy " << h.getObjectID() << endl;
    555 //       COUT(4) << "copy " << h.getObjectID() << ":";
    556       //sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables();
    557       for( unsigned int i = 0; i < Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); ++i )
    558       {
    559 //         COUT(4) << " " << *sizes;
    560         ++sizes;
    561       }
    562 //       COUT(4) << endl;
    563       assert(sizes != this->sizes_.end() || origOffset>=origLength);
    564       continue;
    565     }
    566   }
    567 
    568 
    569   Gamestate *g = new Gamestate(nData, getClientID());
    570   assert(g->header_);
    571   *(g->header_) = *header_;
    572   g->header_->setBaseID( base->getID() );
    573   g->header_->setDataSize(dest - nData - GamestateHeader::getSize());
     579//         COUT(4) << "copy object: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
     580        assert(baseDataPtr == oldBaseDataPtr);
     581        copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt);
     582        assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd);
     583      }
     584    }
     585  }
     586  assert(sizesIt==this->sizes_.end());
     587
     588
     589  Gamestate *g = new Gamestate(newData, getPeerID());
     590  (g->header_) = header_;
     591  g->header_.setBaseID( base->getID() );
     592  g->header_.setDataSize(destDataPtr - newData - GamestateHeader::getSize());
    574593  g->flags_=flags_;
    575594  g->packetDirection_ = packetDirection_;
     
    579598
    580599
    581 Gamestate* Gamestate::diffData(Gamestate *base)
     600/*Gamestate* Gamestate::diffData(Gamestate *base)
    582601{
    583602  assert(this && base); assert(data_ && base->data_);
    584   assert(!header_->isCompressed() && !base->header_->isCompressed());
    585   assert(!header_->isDiffed());
     603  assert(!header_.isCompressed() && !base->header_.isCompressed());
     604  assert(!header_.isDiffed());
    586605
    587606  uint8_t *basep = GAMESTATE_START(base->data_);
    588607  uint8_t *gs = GAMESTATE_START(this->data_);
    589   uint32_t dest_length = header_->getDataSize();
     608  uint32_t dest_length = header_.getDataSize();
    590609
    591610  if(dest_length==0)
     
    595614  uint8_t *dest = GAMESTATE_START(ndata);
    596615
    597   rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
     616  rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
    598617#ifndef NDEBUG
    599618  uint8_t *dest2 = new uint8_t[dest_length];
    600   rawDiff( dest2, dest, basep, header_->getDataSize(), base->header_->getDataSize() );
     619  rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() );
    601620  assert( memcmp( dest2, gs, dest_length) == 0 );
    602621  delete dest2;
     
    606625  assert(g->header_);
    607626  *(g->header_) = *header_;
    608   g->header_->setDiffed( true );
    609   g->header_->setBaseID( base->getID() );
     627  g->header_.setDiffed( true );
     628  g->header_.setBaseID( base->getID() );
    610629  g->flags_=flags_;
    611630  g->packetDirection_ = packetDirection_;
     
    619638{
    620639  assert(this && base); assert(data_ && base->data_);
    621   assert(!header_->isCompressed() && !base->header_->isCompressed());
    622   assert(header_->isDiffed());
     640  assert(!header_.isCompressed() && !base->header_.isCompressed());
     641  assert(header_.isDiffed());
    623642
    624643  uint8_t *basep = GAMESTATE_START(base->data_);
    625644  uint8_t *gs = GAMESTATE_START(this->data_);
    626   uint32_t dest_length = header_->getDataSize();
     645  uint32_t dest_length = header_.getDataSize();
    627646
    628647  if(dest_length==0)
     
    632651  uint8_t *dest = ndata + GamestateHeader::getSize();
    633652
    634   rawDiff( dest, gs, basep, header_->getDataSize(), base->header_->getDataSize() );
     653  rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );
    635654
    636655  Gamestate *g = new Gamestate(ndata, getClientID());
    637656  assert(g->header_);
    638657  *(g->header_) = *header_;
    639   g->header_->setDiffed( false );
     658  g->header_.setDiffed( false );
    640659  g->flags_=flags_;
    641660  g->packetDirection_ = packetDirection_;
     
    670689  }
    671690  assert(j==datalength);
    672 }
    673 
    674 
    675 Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
     691}*/
     692
     693
     694/*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
    676695  assert(data_);
    677696  std::list<obj>::iterator it;
    678697
    679698  // allocate memory for new data
    680   uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()];
     699  uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()];
    681700  // create a gamestate out of it
    682701  Gamestate *gs = new Gamestate(gdata);
     
    694713
    695714  //call TrafficControl
    696   TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), dataVector_ );
     715  TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ );
    697716
    698717  //copy in the zeros
     
    725744#ifndef NDEBUG
    726745  uint32_t origsize = destsize;
    727   while ( origsize < header_->getDataSize() )
     746  while ( origsize < header_.getDataSize() )
    728747  {
    729748    SynchronisableHeader oldobjectheader(origdata);
     
    732751    origsize += objectsize;
    733752  }
    734   assert(origsize==header_->getDataSize());
     753  assert(origsize==header_.getDataSize());
    735754  assert(destsize!=0);
    736755#endif
    737   gs->header_->setDataSize( destsize );
     756  gs->header_.setDataSize( destsize );
    738757  return gs;
    739 }
    740 
    741 
    742 uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode)
     758}*/
     759
     760
     761uint32_t Gamestate::calcGamestateSize(uint32_t id, uint8_t mode)
    743762{
    744763  uint32_t size = 0;
  • code/trunk/src/libraries/network/packet/Gamestate.h

    r7163 r7801  
    4242#include "Packet.h"
    4343
    44 namespace orxonox {
     44namespace orxonox
     45{
    4546
    46 namespace packet {
     47namespace packet
     48{
     49   
     50static const uint8_t GAMESTATE_MODE_SERVER = 0x1;
     51static const uint8_t GAMESTATE_MODE_CLIENT = 0x2;
    4752
    48 class _NetworkExport GamestateHeader{
     53class _NetworkExport GamestateHeader
     54{
    4955  public:
    50     GamestateHeader(uint8_t *data){ assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; }
    51     GamestateHeader(uint8_t *data, GamestateHeader* h)
    52     { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }
     56    GamestateHeader(){ data_=0; }
     57    GamestateHeader(uint8_t* data)
     58      { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; }
     59    /*GamestateHeader(uint8_t* data, GamestateHeader* h)
     60      { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }*/
     61    void setData(uint8_t* data)
     62      { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; }
    5363    static inline uint32_t getSize()
    54     { return 21; }
     64      { return 21; }
    5565
    56     inline int32_t getID() const
    57     { assert(data_); return *(int32_t*)(data_+4); }
    58     inline void setID(int32_t id)
    59     { assert(data_); *(int32_t*)(data_+4) = id; }
     66    inline uint32_t getID() const
     67      { assert(data_); return *(uint32_t*)(data_+4); }
     68    inline void setID(uint32_t id)
     69      { assert(data_); *(uint32_t*)(data_+4) = id; }
    6070
    61     inline int32_t getBaseID() const
    62     { assert(data_); return *(int32_t*)(data_+8); }
    63     inline void setBaseID(int32_t id)
    64     { assert(data_); *(int32_t*)(data_+8) = id; }
     71    inline uint32_t getBaseID() const
     72      { assert(data_); return *(uint32_t*)(data_+8); }
     73    inline void setBaseID(uint32_t id)
     74      { assert(data_); *(uint32_t*)(data_+8) = id; }
    6575
    6676    inline uint32_t getDataSize() const
    67     { assert(data_); return *(uint32_t*)(data_+12); }
     77      { assert(data_); return *(uint32_t*)(data_+12); }
    6878    inline void setDataSize(uint32_t size)
    69     { assert(data_); *(uint32_t*)(data_+12) = size; }
     79      { assert(data_); *(uint32_t*)(data_+12) = size; }
    7080
    7181    inline uint32_t getCompSize() const
     
    7585
    7686    inline bool isDiffed() const
    77     { assert(data_); return *(int8_t*)(data_+20) & 0x1; }
     87      { assert(data_); return *(int8_t*)(data_+20) & 0x1; }
    7888    inline void setDiffed(bool b)
    79     { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }
     89      { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }
    8090
    8191    inline bool isComplete() const
    82     { assert(data_); return *(int8_t*)(data_+20) & 0x2; }
     92      { assert(data_); return *(int8_t*)(data_+20) & 0x2; }
    8393    inline void setComplete(bool b)
    84     { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }
     94      { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }
    8595
    8696    inline bool isCompressed() const
    87     { assert(data_); return *(int8_t*)(data_+20) & 0x4; }
     97      { assert(data_); return *(int8_t*)(data_+20) & 0x4; }
    8898    inline void setCompressed(bool b)
    89     { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }
     99      { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }
    90100
    91101    inline void operator=(GamestateHeader& h)
    92     { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); }
     102      { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); }
    93103  private:
    94     uint8_t *data_;
     104    uint8_t* data_;
    95105
    96106};
     
    99109    @author Oliver Scheuss
    100110*/
    101 class _NetworkExport Gamestate: public Packet{
     111class _NetworkExport Gamestate: public Packet
     112{
    102113  public:
    103114    Gamestate();
     
    110121    bool collectData(int id, uint8_t mode=0x0);
    111122    bool spreadData( uint8_t mode=0x0);
    112     inline int32_t getID() const { return header_->getID(); }
    113     inline bool isDiffed() const { return header_->isDiffed(); }
    114     inline bool isCompressed() const { return header_->isCompressed(); }
    115     inline int32_t getBaseID() const { return header_->getBaseID(); }
    116     inline uint32_t getDataSize() const { return header_->getDataSize(); }
     123    inline uint32_t getID() const { return header_.getID(); }
     124    inline bool isDiffed() const { return header_.isDiffed(); }
     125    inline bool isCompressed() const { return header_.isCompressed(); }
     126    inline int32_t getBaseID() const { return header_.getBaseID(); }
     127    inline uint32_t getDataSize() const { return header_.getDataSize(); }
    117128    Gamestate* diffVariables(Gamestate *base);
    118     Gamestate* diffData(Gamestate *base);
    119     Gamestate *undiff(Gamestate *base);
    120     Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);
     129//     Gamestate* diffData(Gamestate *base);
     130//     Gamestate *undiff(Gamestate *base);
     131//     Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);
    121132    bool compressData();
    122133    bool decompressData();
     
    125136    // Packet functions
    126137  private:
    127     void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength);
    128     inline uint32_t findObject( const SynchronisableHeader& header, uint8_t* mem, uint32_t dataLength, uint32_t startPosition = 0 );
     138//     void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength);
     139//     inline uint32_t findObject( const SynchronisableHeader& header, uint8_t* mem, uint32_t dataLength, uint32_t startPosition = 0 );
    129140    virtual uint32_t getSize() const;
    130     virtual inline bool process();
    131     uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0);
     141    virtual bool process(orxonox::Host* host);
     142    uint32_t calcGamestateSize(uint32_t id, uint8_t mode=0x0);
     143//     inline void diffObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes );
     144//     inline void copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes );
    132145   
    133146    std::list<obj>          dataVector_;
    134     GamestateHeader*        header_;
     147    GamestateHeader         header_;
    135148    std::vector<uint32_t>   sizes_;
    136149    uint32_t                nrOfVariables_;
  • code/trunk/src/libraries/network/packet/Packet.cc

    r7163 r7801  
    3535#include <enet/enet.h>
    3636#include <boost/static_assert.hpp>
     37#include <boost/thread/mutex.hpp>
    3738
    3839#include "util/Debug.h"
     
    5354
    5455// Make sure we assume the right values
    55 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable)   == static_cast<int>(ENET_PACKET_FLAG_RELIABLE));
    56 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequence) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED));
    57 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate) == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE));
     56BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable)    == static_cast<int>(ENET_PACKET_FLAG_RELIABLE));
     57BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequenced) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED));
     58BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate)  == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE));
    5859
    5960#define PACKET_FLAG_DEFAULT PacketFlag::NoAllocate
     
    6162
    6263std::map<size_t, Packet *> Packet::packetMap_;
     64boost::mutex Packet::packetMapMutex_;
    6365
    6466Packet::Packet()
     
    6668  flags_ = PACKET_FLAG_DEFAULT;
    6769  packetDirection_ = Direction::Outgoing;
    68   clientID_=0;
     70  peerID_=0;
    6971  data_=0;
    7072  enetPacket_=0;
     
    7274}
    7375
    74 Packet::Packet(uint8_t *data, unsigned int clientID)
     76Packet::Packet(uint8_t *data, unsigned int peerID)
    7577{
    7678  flags_ = PACKET_FLAG_DEFAULT;
    7779  packetDirection_ = Direction::Incoming;
    78   clientID_=clientID;
     80  peerID_=peerID;
    7981  data_=data;
    8082  enetPacket_=0;
     
    8789  flags_=p.flags_;
    8890  packetDirection_ = p.packetDirection_;
    89   clientID_ = p.clientID_;
     91  peerID_ = p.peerID_;
    9092  if(p.data_){
    9193    data_ = new uint8_t[p.getSize()];
     
    123125}
    124126
    125 bool Packet::send(){
     127bool Packet::send(orxonox::Host* host){
    126128  if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional ){
    127129    assert(0);
     
    142144      // Assures we don't create a packet and destroy it right after in another thread
    143145      // without having a reference in the packetMap_
     146      Packet::packetMapMutex_.lock();
    144147      packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;
     148      Packet::packetMapMutex_.unlock();
    145149    }
    146150  }
     
    164168//  ENetPacket *temp = enetPacket_;
    165169//  enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
    166   if(!Host::addPacket( enetPacket_, clientID_))
    167     enet_packet_destroy(this->enetPacket_); // if we could not add the packet to the enet queue delete it manually
     170  if( this->flags_ & PacketFlag::Reliable )
     171    host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_DEFAULT);
     172  else
     173    host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_UNRELIABLE);
    168174  return true;
    169175}
     
    172178  uint8_t *data = packet->data;
    173179  assert(ClientInformation::findClient(&peer->address)->getID() != static_cast<unsigned int>(-2) || !Host::isServer());
    174   unsigned int clientID = ClientInformation::findClient(&peer->address)->getID();
     180  unsigned int peerID = ClientInformation::findClient(&peer->address)->getID();
     181  // HACK
     182  if( peerID==static_cast<unsigned int>(-2))
     183    peerID = NETWORK_PEER_ID_SERVER;
    175184  Packet *p = 0;
    176   COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl;
     185//   COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl;
    177186  switch( *(Type::Value *)(data + _PACKETID) )
    178187  {
    179188    case Type::Acknowledgement:
    180       COUT(5) << "ack" << std::endl;
    181       p = new Acknowledgement( data, clientID );
     189//       COUT(5) << "ack" << std::endl;
     190    p = new Acknowledgement( data, peerID );
    182191      break;
    183192    case Type::Chat:
    184       COUT(5) << "chat" << std::endl;
    185       p = new Chat( data, clientID );
     193//       COUT(5) << "chat" << std::endl;
     194      p = new Chat( data, peerID );
    186195      break;
    187196    case Type::ClassID:
    188       COUT(5) << "classid" << std::endl;
    189       p = new ClassID( data, clientID );
     197//       COUT(5) << "classid" << std::endl;
     198      p = new ClassID( data, peerID );
    190199      break;
    191200    case Type::Gamestate:
    192       COUT(5) << "gamestate" << std::endl;
    193       // TODO: remove brackets
    194       p = new Gamestate( data, clientID );
     201//       COUT(5) << "gamestate" << std::endl;
     202      p = new Gamestate( data, peerID );
    195203      break;
    196204    case Type::Welcome:
    197       COUT(5) << "welcome" << std::endl;
    198       p = new Welcome( data, clientID );
     205//       COUT(5) << "welcome" << std::endl;
     206      p = new Welcome( data, peerID );
    199207      break;
    200208    case Type::DeleteObjects:
    201       COUT(5) << "deleteobjects" << std::endl;
    202       p = new DeleteObjects( data, clientID );
     209//       COUT(5) << "deleteobjects" << std::endl;
     210      p = new DeleteObjects( data, peerID );
    203211      break;
    204212    case Type::FunctionCalls:
    205       COUT(5) << "functionCalls" << std::endl;
    206       p = new FunctionCalls( data, clientID );
     213//       COUT(5) << "functionCalls" << std::endl;
     214      p = new FunctionCalls( data, peerID );
    207215      break;
    208216    case Type::FunctionIDs:
    209       COUT(5) << "functionIDs" << std::endl;
    210       p = new FunctionIDs( data, clientID );
     217//       COUT(5) << "functionIDs" << std::endl;
     218      p = new FunctionIDs( data, peerID );
    211219      break;
    212220    default:
    213       assert(0); //TODO: repair this
     221      assert(0);
    214222      break;
    215223  }
     
    229237void Packet::deletePacket(ENetPacket *enetPacket){
    230238  // Get our Packet from a global map with all Packets created in the send() method of Packet.
     239  Packet::packetMapMutex_.lock();
    231240  std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket));
    232241  assert(it != packetMap_.end());
     
    235244  delete it->second;
    236245  packetMap_.erase(it);
    237   COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl;
     246  Packet::packetMapMutex_.unlock();
     247//   COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl;
    238248}
    239249
  • code/trunk/src/libraries/network/packet/Packet.h

    r7490 r7801  
    6868    virtual unsigned char *getData(){ return data_; };
    6969    virtual unsigned int getSize() const =0;
    70     virtual bool process()=0;
     70    virtual bool process(orxonox::Host* host)=0;
    7171    inline uint32_t getFlags()
    7272      { return flags_; }
    73     inline int getClientID()
    74       { return clientID_; }
    75     inline void setClientID( int id )
    76       { clientID_ = id; }
     73    inline int getPeerID()
     74      { return peerID_; }
     75    inline void setPeerID( int id )
     76      { peerID_ = id; }
     77    inline bool isReliable()
     78      { return this->flags_ & PacketFlag::Reliable; }
     79    inline uint32_t getRequiredGamestateID()
     80      { return this->requiredGamestateID_; }
    7781
    78     virtual bool send();
     82    virtual bool send(orxonox::Host* host);
    7983  protected:
    8084    Packet();
    81     Packet(uint8_t *data, unsigned int clientID);
     85    Packet(uint8_t *data, unsigned int peerID);
    8286//    Packet(ENetPacket *packet, ENetPeer *peer);
    8387    inline bool isDataENetAllocated() const
     
    8589
    8690    uint32_t flags_;
    87     unsigned int clientID_;
     91    unsigned int peerID_;
     92    uint32_t requiredGamestateID_;
    8893    Direction::Value packetDirection_;
    8994    /** Pointer to the data. Be careful when deleting it because it might
     
    96101  private:
    97102    static std::map<size_t, Packet *> packetMap_;
     103    static boost::mutex               packetMapMutex_;
    98104    ENetPacket *enetPacket_;
    99105};
  • code/trunk/src/libraries/network/packet/ServerInformation.cc

    r7461 r7801  
    2121 *
    2222 *   Author:
    23  *      Fabian 'x3n' Landau
     23 *      Oliver Scheuss
    2424 *   Co-authors:
    2525 *      ...
     
    5858      char* ack = new char[strlen(LAN_DISCOVERY_ACK)+1];
    5959      loadAndIncrease((char*&)ack, temp);
    60       assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);
     60
     61      /* Fabian, what is this used for? it crashes the masterserver, hence commenting it */
     62      // written by Oli: this is just to make sure that loadAndIncrease really writes the whole ACK string into char* ack
     63//       assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);
     64
    6165      // Save Server Name
    6266      loadAndIncrease(this->serverName_, temp);
  • code/trunk/src/libraries/network/packet/ServerInformation.h

    r7459 r7801  
    2121 *
    2222 *   Author:
    23  *      Fabian 'x3n' Landau
     23 *      Oliver Scheuss
    2424 *   Co-authors:
    2525 *      ...
     
    5050        std::string   getServerName() { return this->serverName_; }
    5151        void          setServerName(std::string name) { this->serverName_ = name; }
     52        void          setServerIP( std::string IP ) { this->serverIP_ = IP; }
    5253        uint32_t      getServerRTT() { return this->serverRTT_; }
    5354       
  • code/trunk/src/libraries/network/packet/Welcome.cc

    r5781 r7801  
    7373}
    7474
    75 bool Welcome::process(){
     75bool Welcome::process(orxonox::Host* host){
    7676  uint32_t clientID;
    7777  clientID = *(uint32_t *)(data_ + _CLIENTID );
    7878  assert(*(uint32_t *)(data_ + _ENDIANTEST ) == 0xFEDC4321);
    79   Host::setClientID(clientID);
     79  host->setClientID(clientID);
    8080  COUT(3) << "Welcome set clientId: " << clientID << endl;
    8181  Synchronisable::setClient(true);
  • code/trunk/src/libraries/network/packet/Welcome.h

    r6073 r7801  
    4747  uint8_t *getData();
    4848  inline unsigned int getSize() const;
    49   bool process();
     49  virtual bool process(orxonox::Host* host);
    5050
    5151private:
Note: See TracChangeset for help on using the changeset viewer.