[Orxonox-commit 3064] r7759 - in code/branches/network5/src/libraries/network: . packet synchronisable

scheusso at orxonox.net scheusso at orxonox.net
Tue Dec 14 20:54:01 CET 2010


Author: scheusso
Date: 2010-12-14 20:54:00 +0100 (Tue, 14 Dec 2010)
New Revision: 7759

Modified:
   code/branches/network5/src/libraries/network/Client.cc
   code/branches/network5/src/libraries/network/FunctionCallManager.cc
   code/branches/network5/src/libraries/network/FunctionCallManager.h
   code/branches/network5/src/libraries/network/GamestateClient.cc
   code/branches/network5/src/libraries/network/GamestateClient.h
   code/branches/network5/src/libraries/network/GamestateHandler.h
   code/branches/network5/src/libraries/network/GamestateManager.cc
   code/branches/network5/src/libraries/network/GamestateManager.h
   code/branches/network5/src/libraries/network/MasterServerComm.cc
   code/branches/network5/src/libraries/network/NetworkPrereqs.h
   code/branches/network5/src/libraries/network/Server.cc
   code/branches/network5/src/libraries/network/packet/FunctionCalls.cc
   code/branches/network5/src/libraries/network/packet/FunctionCalls.h
   code/branches/network5/src/libraries/network/packet/Gamestate.cc
   code/branches/network5/src/libraries/network/packet/Packet.cc
   code/branches/network5/src/libraries/network/synchronisable/Synchronisable.h
Log:
a lot of changes:
- some fixes (mostly gamestate:diff)
- FunctionCall buffering (if Gamestate is not recent enough)


Modified: code/branches/network5/src/libraries/network/Client.cc
===================================================================
--- code/branches/network5/src/libraries/network/Client.cc	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/Client.cc	2010-12-14 19:54:00 UTC (rev 7759)
@@ -163,7 +163,7 @@
         if(gs){
           COUT(4) << "client tick: sending gs " << gs << std::endl;
           if( !gs->send() )
-            COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
+            COUT(2) << "Problem adding partial gamestate to queue" << std::endl;
         // gs gets automatically deleted by enet callback
         }
         FunctionCallManager::sendCalls();

Modified: code/branches/network5/src/libraries/network/FunctionCallManager.cc
===================================================================
--- code/branches/network5/src/libraries/network/FunctionCallManager.cc	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/FunctionCallManager.cc	2010-12-14 19:54:00 UTC (rev 7759)
@@ -29,11 +29,12 @@
 #include "FunctionCallManager.h"
 #include "packet/FunctionCalls.h"
 #include "core/GameMode.h"
+#include "GamestateHandler.h"
 
 namespace orxonox {
 
 std::map<uint32_t, packet::FunctionCalls*> FunctionCallManager::sClientMap_;
-std::vector<FunctionCall> FunctionCallManager::sIncomingFunctionCallBuffer_;
+std::vector<std::pair<FunctionCall, std::pair<uint32_t, uint32_t> > > FunctionCallManager::sIncomingFunctionCallBuffer_;
 
 // Static calls
 
@@ -163,21 +164,24 @@
   FunctionCallManager::sClientMap_.clear();
 }
 
-void FunctionCallManager::bufferIncomingFunctionCall(const orxonox::FunctionCall& fctCall)
+void FunctionCallManager::bufferIncomingFunctionCall(const orxonox::FunctionCall& fctCall, uint32_t minGamestateID, uint32_t clientID)
 {
-  if( !GameMode::isMaster() )
-    FunctionCallManager::sIncomingFunctionCallBuffer_.push_back( fctCall );
+  FunctionCallManager::sIncomingFunctionCallBuffer_.push_back( std::make_pair(fctCall, std::make_pair(minGamestateID, clientID)));
 }
 
 void FunctionCallManager::processBufferedFunctionCalls()
 {
-  std::vector<FunctionCall>::iterator it = FunctionCallManager::sIncomingFunctionCallBuffer_.begin();
+  std::vector<std::pair<FunctionCall, std::pair<uint32_t, uint32_t> > >::iterator it = FunctionCallManager::sIncomingFunctionCallBuffer_.begin();
   while( it!=FunctionCallManager::sIncomingFunctionCallBuffer_.end() )
   {
-    if( it->execute() )
+    uint32_t minGamestateID = it->second.first;
+    uint32_t clientID       = it->second.second;
+    if( minGamestateID <= GamestateHandler::getInstance()->getLastProcessedGamestateID(clientID) && it->first.execute() )
       FunctionCallManager::sIncomingFunctionCallBuffer_.erase(it);
     else
+    {
       ++it;
+    }
   }
 }
 

Modified: code/branches/network5/src/libraries/network/FunctionCallManager.h
===================================================================
--- code/branches/network5/src/libraries/network/FunctionCallManager.h	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/FunctionCallManager.h	2010-12-14 19:54:00 UTC (rev 7759)
@@ -33,6 +33,7 @@
 
 #include <map>
 #include <vector>
+#include <utility>
 #include "util/UtilPrereqs.h"
 #include "FunctionCall.h"
 
@@ -60,11 +61,11 @@
 
   static void sendCalls();
   
-  static void bufferIncomingFunctionCall( const FunctionCall& fctCall );
+  static void bufferIncomingFunctionCall( const FunctionCall& fctCall, uint32_t minGamestateID, uint32_t clientID );
   static void processBufferedFunctionCalls();
 
-  static std::map<uint32_t, packet::FunctionCalls*> sClientMap_;
-  static std::vector<FunctionCall>                  sIncomingFunctionCallBuffer_;
+  static std::map<uint32_t, packet::FunctionCalls*>                           sClientMap_;
+  static std::vector<std::pair<FunctionCall,std::pair<uint32_t, uint32_t> > > sIncomingFunctionCallBuffer_;
 protected:
   FunctionCallManager();
   ~FunctionCallManager();

Modified: code/branches/network5/src/libraries/network/GamestateClient.cc
===================================================================
--- code/branches/network5/src/libraries/network/GamestateClient.cc	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/GamestateClient.cc	2010-12-14 19:54:00 UTC (rev 7759)
@@ -38,19 +38,22 @@
 
 namespace orxonox
 {
-  struct _NetworkExport GameStateItem{
+  struct _NetworkExport GameStateItem
+  {
     packet::Gamestate *state;
     unsigned int id;
   };
 
-  GamestateClient::GamestateClient() {
+  GamestateClient::GamestateClient()
+  {
     COUT(5) << "this: " << this << std::endl;
-    last_diff_=0;
-    last_gamestate_=GAMESTATEID_INITIAL-1;
+    lastAckedGamestateID_=GAMESTATEID_INITIAL-1;
+    lastProcessedGamestateID_=GAMESTATEID_INITIAL-1;
     tempGamestate_=NULL;
   }
 
-  GamestateClient::~GamestateClient() {
+  GamestateClient::~GamestateClient()
+  {
       std::map<unsigned int, packet::Gamestate *>::iterator it;
       for ( it = this->gamestateMap_.begin(); it != this->gamestateMap_.end(); ++it )
           delete it->second;
@@ -58,12 +61,15 @@
           delete this->tempGamestate_;
   }
 
-  bool GamestateClient::ack(unsigned int gamestateID, unsigned int clientID){
+  bool GamestateClient::ack(unsigned int gamestateID, unsigned int clientID)
+  {
     return true;
   }
 
-  bool GamestateClient::add(packet::Gamestate *gs, unsigned int clientID){
-    if(tempGamestate_!=NULL){
+  bool GamestateClient::add(packet::Gamestate *gs, unsigned int clientID)
+  {
+    if(tempGamestate_!=NULL)
+    {
       //delete the obsolete gamestate
       if(tempGamestate_->getID()>gs->getID())
         return false;
@@ -73,7 +79,8 @@
     return true;
   }
 
-  bool GamestateClient::processGamestates(){
+  bool GamestateClient::processGamestates()
+  {
     if(tempGamestate_==NULL)
       return false;
     bool isDiffed = tempGamestate_->isDiffed();
@@ -84,15 +91,18 @@
     //now call the queued callbacks
     NetworkCallbackManager::callCallbacks();
 
-    if (!processed){
+    if (!processed)
+    {
+      assert(0);
       sendAck(0);
       return false;
     }
     //successfully loaded data from gamestate. now save gamestate for diff and delete the old gs
     tempGamestate_=NULL;
     gamestateMap_[processed->getID()]=processed;
+    lastProcessedGamestateID_ = processed->getID();
     if(isDiffed)
-      last_diff_ = processed->getBaseID();
+      lastAckedGamestateID_ = processed->getBaseID();
     id = processed->getID();
     sendAck(id);
     return true;
@@ -104,25 +114,30 @@
   * @param it iterator of the list pointing to the object
   * @return iterator pointing to the next object in the list
   */
-  void GamestateClient::removeObject(ObjectListIterator<Synchronisable> &it) {
+  void GamestateClient::removeObject(ObjectListIterator<Synchronisable> &it)
+  {
     ObjectListIterator<Synchronisable> temp=it;
     ++it;
     temp->destroy(); // or delete?
   }
 
-  packet::Gamestate *GamestateClient::getGamestate(){
+  packet::Gamestate *GamestateClient::getGamestate()
+  {
     packet::Gamestate *gs = new packet::Gamestate();
-    if(!gs->collectData(0,0x2)){
+    if(!gs->collectData(this->getLastProcessedGamestateID(NETWORK_PEER_ID_SERVER), 0x2))
+    {
       delete gs;
       return 0;
     }
     return gs;
   }
 
-  void GamestateClient::cleanup(){
+  void GamestateClient::cleanup()
+  {
     std::map<unsigned int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin();
-    while(it!=gamestateMap_.end()){
-      if(it->first>=last_diff_)
+    while(it!=gamestateMap_.end())
+    {
+      if(it->first>=lastAckedGamestateID_)
         break;
       // otherwise delete that stuff
       delete it->second;
@@ -132,29 +147,35 @@
     tempGamestate_=NULL;
   }
 
-  void GamestateClient::printGamestateMap(){
+  void GamestateClient::printGamestateMap()
+  {
     std::map<unsigned int, packet::Gamestate*>::iterator it;
     COUT(4) << "gamestates: ";
-    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){
+    for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++)
+    {
       COUT(4) << it->first << ':' << it->second << '|';
     }
     COUT(4) << std::endl;
 
   }
 
-  bool GamestateClient::sendAck(unsigned int gamestateID){
+  bool GamestateClient::sendAck(unsigned int gamestateID)
+  {
     packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0);
-    if(!ack->send()){
+    if(!ack->send())
+    {
       COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
       return false;
     }
-    else{
+    else
+    {
       COUT(5) << "acked a gamestate: " << gamestateID << std::endl;
       return true;
     }
   }
 
-  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){
+  packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs)
+  {
     if(gs->isCompressed())
     {
       bool b = gs->decompressData();

Modified: code/branches/network5/src/libraries/network/GamestateClient.h
===================================================================
--- code/branches/network5/src/libraries/network/GamestateClient.h	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/GamestateClient.h	2010-12-14 19:54:00 UTC (rev 7759)
@@ -56,8 +56,10 @@
     GamestateClient();
     ~GamestateClient();
 
-    bool add(packet::Gamestate *gs, unsigned int clientID);
-    bool ack(unsigned int gamestateID, unsigned int clientID);
+    virtual bool      add(packet::Gamestate *gs, unsigned int clientID);
+    virtual bool      ack(unsigned int gamestateID, unsigned int clientID);
+    virtual uint32_t  getLastProcessedGamestateID(unsigned int clientID) { return this->lastProcessedGamestateID_; }
+    virtual uint32_t  getCurrentGamestateID(){ return this->lastProcessedGamestateID_; }
 
     bool processGamestates();
     packet::Gamestate *getGamestate();
@@ -68,8 +70,8 @@
     void printGamestateMap();
     bool sendAck(unsigned int gamestateID);
 
-    unsigned int           last_diff_;
-    unsigned int           last_gamestate_;
+    unsigned int           lastAckedGamestateID_;
+    unsigned int           lastProcessedGamestateID_;
     std::map<unsigned int, packet::Gamestate *> gamestateMap_;
     packet::Gamestate *tempGamestate_; // we save the received gamestates here during processQueue
 

Modified: code/branches/network5/src/libraries/network/GamestateHandler.h
===================================================================
--- code/branches/network5/src/libraries/network/GamestateHandler.h	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/GamestateHandler.h	2010-12-14 19:54:00 UTC (rev 7759)
@@ -31,6 +31,8 @@
 
 #include "NetworkPrereqs.h"
 
+#include <cassert>
+
 namespace orxonox {
 
 /**
@@ -38,10 +40,10 @@
 */
 class _NetworkExport GamestateHandler{
   private:
-    virtual bool add(packet::Gamestate *gs, unsigned int clientID)=0;
-    virtual bool ack(unsigned int gamestateID, unsigned int clientID)=0;
+    virtual bool      add(packet::Gamestate *gs, unsigned int clientID)=0;
+    virtual bool      ack(unsigned int gamestateID, unsigned int clientID)=0;
 
-    static GamestateHandler *instance_;
+    static GamestateHandler* instance_;
 
 
   protected:
@@ -49,8 +51,12 @@
     virtual ~GamestateHandler();
 
   public:
-    static bool addGamestate(packet::Gamestate *gs, unsigned int clientID){ return instance_->add(gs, clientID); }
-    static bool ackGamestate(unsigned int gamestateID, unsigned int clientID){ return instance_->ack(gamestateID, clientID); }
+    static bool     addGamestate(packet::Gamestate *gs, unsigned int clientID){ return instance_->add(gs, clientID); }
+    static bool     ackGamestate(unsigned int gamestateID, unsigned int clientID){ return instance_->ack(gamestateID, clientID); }
+    static GamestateHandler* getInstance(){ assert(instance_); return instance_; }
+    
+    virtual uint32_t  getLastProcessedGamestateID( unsigned int clientID )=0;
+    virtual uint32_t  getCurrentGamestateID()=0;
 };
 
 }

Modified: code/branches/network5/src/libraries/network/GamestateManager.cc
===================================================================
--- code/branches/network5/src/libraries/network/GamestateManager.cc	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/GamestateManager.cc	2010-12-14 19:54:00 UTC (rev 7759)
@@ -257,6 +257,15 @@
     TrafficControl::processAck(clientID, gamestateID);
     return true;
   }
+  
+  uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int clientID)
+  {
+    assert( this->lastProcessedGamestateID_.find(clientID) != this->lastProcessedGamestateID_.end() );
+    if( this->lastProcessedGamestateID_.find(clientID) != this->lastProcessedGamestateID_.end() )
+      return this->lastProcessedGamestateID_[clientID];
+    else
+      return GAMESTATEID_INITIAL;
+  }
 
   void GamestateManager::removeClient(ClientInformation* client){
     assert(client);
@@ -276,7 +285,13 @@
        assert(b);
     }
     assert(!gs->isDiffed());
-    return gs->spreadData(0x1);
+    if( gs->spreadData(0x1) )
+    {
+      this->lastProcessedGamestateID_[gs->getClientID()] = gs->getID();
+      return true;
+    }
+    else
+      return false;
   }
 
 }

Modified: code/branches/network5/src/libraries/network/GamestateManager.h
===================================================================
--- code/branches/network5/src/libraries/network/GamestateManager.h	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/GamestateManager.h	2010-12-14 19:54:00 UTC (rev 7759)
@@ -45,6 +45,7 @@
 #include <map>
 #include "GamestateHandler.h"
 #include "core/CorePrereqs.h"
+#include "packet/Gamestate.h"
 
 namespace orxonox
 {
@@ -70,7 +71,11 @@
     GamestateManager();
     ~GamestateManager();
 
-    bool add(packet::Gamestate *gs, unsigned int clientID);
+    virtual bool      add(packet::Gamestate *gs, unsigned int clientID);
+    virtual bool      ack(unsigned int gamestateID, unsigned int clientID);
+    virtual uint32_t  getLastProcessedGamestateID( unsigned int clientID );
+    virtual uint32_t  getCurrentGamestateID(){ return reference->getID(); }
+    
     bool processGamestates();
     bool update();
     void sendGamestates();
@@ -79,13 +84,13 @@
 
     bool getSnapshot();
 
-    bool ack(unsigned int gamestateID, unsigned int clientID);
     void removeClient(ClientInformation *client);
   private:
     bool processGamestate(packet::Gamestate *gs);
 
     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> > gamestateMap_;
     std::map<unsigned int, packet::Gamestate*> gamestateQueue;
+    std::map<unsigned int, uint32_t> lastProcessedGamestateID_;
     packet::Gamestate *reference;
     TrafficControl *trafficControl_;
     unsigned int id_;

Modified: code/branches/network5/src/libraries/network/MasterServerComm.cc
===================================================================
--- code/branches/network5/src/libraries/network/MasterServerComm.cc	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/MasterServerComm.cc	2010-12-14 19:54:00 UTC (rev 7759)
@@ -88,7 +88,7 @@
     }
 
     /* Wait up to 2 seconds for the connection attempt to succeed. */
-    if (enet_host_service (this->client, this->event, 2000) > 0 &&
+    if (enet_host_service (this->client, this->event, 500) > 0 &&
         this->event->type == ENET_EVENT_TYPE_CONNECT )
       COUT(3) << "Connection to master server succeeded.\n";
     else

Modified: code/branches/network5/src/libraries/network/NetworkPrereqs.h
===================================================================
--- code/branches/network5/src/libraries/network/NetworkPrereqs.h	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/NetworkPrereqs.h	2010-12-14 19:54:00 UTC (rev 7759)
@@ -68,6 +68,7 @@
   extern const char* LAN_DISCOVERY_MESSAGE;
   extern const char* LAN_DISCOVERY_ACK;
   static const unsigned int LAN_DISCOVERY_PORT      = 55557;
+  static const unsigned int NETWORK_PEER_ID_SERVER = 0;
 }
 
 //-----------------------------------------------------------------------
@@ -82,9 +83,9 @@
     {
       enum Value
       {
-        Reliable   = 1,
-        Unsequence = 2,
-        NoAllocate = 4
+        Reliable    = 1,
+        Unsequenced = 2,
+        NoAllocate  = 4
       };
     }
   }

Modified: code/branches/network5/src/libraries/network/Server.cc
===================================================================
--- code/branches/network5/src/libraries/network/Server.cc	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/Server.cc	2010-12-14 19:54:00 UTC (rev 7759)
@@ -216,6 +216,7 @@
     {
       // process incoming gamestates
       GamestateManager::processGamestates();
+      FunctionCallManager::processBufferedFunctionCalls();
 
       // send function calls to clients
       FunctionCallManager::sendCalls();

Modified: code/branches/network5/src/libraries/network/packet/FunctionCalls.cc
===================================================================
--- code/branches/network5/src/libraries/network/packet/FunctionCalls.cc	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/packet/FunctionCalls.cc	2010-12-14 19:54:00 UTC (rev 7759)
@@ -31,6 +31,7 @@
 #include <cassert>
 #include "network/FunctionCall.h"
 #include "network/FunctionCallManager.h"
+#include "network/GamestateHandler.h"
 
 namespace orxonox {
 namespace packet {
@@ -39,15 +40,15 @@
 #define   _PACKETID         0
 const unsigned int FUNCTIONCALLS_MEM_ALLOCATION = 1000;
 
-FunctionCalls::FunctionCalls()
- : Packet()
+FunctionCalls::FunctionCalls():
+  Packet(), minGamestateID_(GAMESTATEID_INITIAL)
 {
   flags_ = flags_ | PACKET_FLAGS_FUNCTIONCALLS;
-  currentSize_ = 2*sizeof(uint32_t); // for packetid and nrOfCalls
+  currentSize_ = 3*sizeof(uint32_t); // for packetid, nrOfCalls and minGamestateID_
 }
 
-FunctionCalls::FunctionCalls( uint8_t* data, unsigned int clientID )
-  : Packet(data, clientID)
+FunctionCalls::FunctionCalls( uint8_t* data, unsigned int clientID ): 
+  Packet(data, clientID), minGamestateID_(GAMESTATEID_INITIAL)
 {
 }
 
@@ -62,12 +63,16 @@
   uint8_t* temp = data_+sizeof(uint32_t); //skip packetid
   uint32_t nrOfCalls = *(uint32_t*)temp;
   temp += sizeof(uint32_t);
+  this->minGamestateID_ = *(uint32_t*)temp;
+  temp += sizeof(uint32_t);
   for( unsigned int i = 0; i<nrOfCalls; i++ )
   {
     FunctionCall fctCall;
     fctCall.loadData(temp);
-    if( !fctCall.execute() )
-      FunctionCallManager::bufferIncomingFunctionCall( fctCall );
+    if( this->minGamestateID_ > GamestateHandler::getInstance()->getLastProcessedGamestateID(this->getClientID()) || !fctCall.execute() )
+    {
+      FunctionCallManager::bufferIncomingFunctionCall( fctCall, minGamestateID_, this->getClientID() );
+    }
   }
   
   delete this;
@@ -92,11 +97,13 @@
 
 bool FunctionCalls::send()
 {
+  this->minGamestateID_ = GamestateHandler::getInstance()->getCurrentGamestateID();
   assert(this->functionCalls_.size());
   data_=new uint8_t[ currentSize_ ];
   *(Type::Value *)(data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
-  *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls to 0
-  uint8_t* temp = data_+2*sizeof(uint32_t);
+  *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls
+  *(uint32_t*)(data_+2*sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_
+  uint8_t* temp = data_+3*sizeof(uint32_t);
   
   while( this->functionCalls_.size() )
   {

Modified: code/branches/network5/src/libraries/network/packet/FunctionCalls.h
===================================================================
--- code/branches/network5/src/libraries/network/packet/FunctionCalls.h	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/packet/FunctionCalls.h	2010-12-14 19:54:00 UTC (rev 7759)
@@ -61,6 +61,7 @@
 private:
   std::queue<orxonox::FunctionCall> functionCalls_;
   unsigned int                      clientID_;
+  uint32_t                          minGamestateID_;
   uint32_t                          currentSize_;
 };
 

Modified: code/branches/network5/src/libraries/network/packet/Gamestate.cc
===================================================================
--- code/branches/network5/src/libraries/network/packet/Gamestate.cc	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/packet/Gamestate.cc	2010-12-14 19:54:00 UTC (rev 7759)
@@ -42,7 +42,9 @@
 
 #define GAMESTATE_START(data) (data + GamestateHeader::getSize())
 
-#define PACKET_FLAG_GAMESTATE  0 //PacketFlag::Reliable
+// #define PACKET_FLAG_GAMESTATE  PacketFlag::Unsequenced
+// #define PACKET_FLAG_GAMESTATE  0
+#define PACKET_FLAG_GAMESTATE  PacketFlag::Reliable
 
 inline bool memzero( uint8_t* data, uint32_t datalength)
 {
@@ -363,11 +365,6 @@
 
 inline void /*Gamestate::*/diffObject( uint8_t*& newDataPtr, uint8_t*& origDataPtr, uint8_t*& baseDataPtr, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes )
 {
-  //       COUT(4) << "dodiff" << endl;
-  //       if(baseOffset==0)
-  //       {
-  //         assert(origOffset==0);
-  //       }
   assert( objectHeader.getDataSize() == SynchronisableHeader(baseDataPtr).getDataSize() );
   
   uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData
@@ -381,10 +378,6 @@
   }
   else
   {
-    //         if( Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() == "Bot" )
-    //           COUT(0) << "blub" << endl;
-    //         COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl;
-    //         COUT(4) << "diff " << h.getObjectID() << ":";
     // Now start to diff the Object
     SynchronisableHeaderLight newObjectHeader(newDataPtr);
     newObjectHeader = objectHeader; // copy over the objectheader
@@ -395,14 +388,12 @@
     {
       // check whether variable changed and write id and copy over variable to the new stream
       // otherwise skip variable
-//       assert(sizes != this->sizes_.end());
       uint32_t varSize = *sizes;
       assert( varSize == Synchronisable::getSynchronisable(objectHeader.getObjectID())->getVarSize(variableID) );
       if ( varSize != 0 )
       {
         if ( memcmp(origDataPtr+objectOffset, baseDataPtr+objectOffset, varSize) != 0 )
         {
-          //               COUT(4) << "copy variable" << endl;
           *(VariableID*)(newDataPtr+diffedObjectOffset) = variableID; // copy over the variableID
           diffedObjectOffset += sizeof(VariableID);
           memcpy( newDataPtr+diffedObjectOffset, origDataPtr+objectOffset, varSize );
@@ -411,39 +402,24 @@
         }
         else
         {
-          //               COUT(4) << "skip variable" << endl;
           objectOffset += varSize;
         }
       }
-//           else
-//             COUT(4) << "varsize 0" << endl;
 
       ++variableID;
       ++sizes;
     }
-            
+    
+    // if there are variables from this object with 0 size left in sizes
     if( Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() != variableID )
       sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() - variableID;
-    //         COUT(4) << endl;
     
     newObjectHeader.setDiffed(true);
     newObjectHeader.setDataSize(diffedObjectOffset-SynchronisableHeaderLight::getSize());
     assert(objectOffset == objectHeader.getDataSize()+SynchronisableHeader::getSize());
     assert(newObjectHeader.getDataSize()>0);
+    
     origDataPtr += objectOffset;
-    //         baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData;
-    //baseOffset += objectOffset;
-    //         SynchronisableHeader htemp(temp);
-    //         baseOffset += SynchronisableHeader::getSize() + htemp.getDataSize();
-    //         {
-      //           SynchronisableHeader htemp2( baseData+(temp-baseData+objectOffset) );
-    //           if( baseData+(temp-baseData+objectOffset) < baseData+baseLength )
-    //           {
-      //             assert(htemp2.getClassID()<500);
-    //             assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000);
-    //             assert(htemp2.isDiffed()==false);
-    //           }
-    //         }
     baseDataPtr += objectOffset;
     newDataPtr += diffedObjectOffset;
   }
@@ -454,6 +430,8 @@
   //       COUT(4) << "docopy" << endl;
   // Just copy over the whole Object
   memcpy( newData, origData, objectHeader.getDataSize()+SynchronisableHeader::getSize() );
+  SynchronisableHeader(newData).setDiffed(false);
+  
   newData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
   origData += objectHeader.getDataSize()+SynchronisableHeader::getSize();
 //   SynchronisableHeader baseHeader( baseData );
@@ -546,9 +524,17 @@
     assert(destDataPtr < newData + newDataSize);
     assert(sizesIt != this->sizes_.end());
     
+    assert(Synchronisable::getSynchronisable(origHeader.getObjectID()));
+    assert(ClassByID(origHeader.getClassID()));
+    assert(origHeader.getDataSize() < 500);
+    
     bool diffedObject = false;
     if( findObject(baseDataPtr, baseDataEnd, origHeader) )
     {
+      SynchronisableHeader baseHeader(baseDataPtr);
+      assert(Synchronisable::getSynchronisable(baseHeader.getObjectID()));
+      assert(ClassByID(baseHeader.getClassID()));
+      assert(baseHeader.getDataSize() < 500);
       if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() )
       {
 //         COUT(4) << "diffing object in order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;
@@ -569,6 +555,10 @@
       baseDataPtr = GAMESTATE_START(base->data_);
       if( findObject(baseDataPtr, oldBaseDataPtr, origHeader) )
       {
+        SynchronisableHeader baseHeader(baseDataPtr);
+        assert(Synchronisable::getSynchronisable(baseHeader.getObjectID()));
+        assert(ClassByID(baseHeader.getClassID()));
+        assert(baseHeader.getDataSize() < 500);
         if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() )
         {
 //           COUT(4) << "diffing object out of order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl;

Modified: code/branches/network5/src/libraries/network/packet/Packet.cc
===================================================================
--- code/branches/network5/src/libraries/network/packet/Packet.cc	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/packet/Packet.cc	2010-12-14 19:54:00 UTC (rev 7759)
@@ -52,9 +52,9 @@
 namespace packet{
 
 // Make sure we assume the right values
-BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable)   == static_cast<int>(ENET_PACKET_FLAG_RELIABLE));
-BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequence) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED));
-BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate) == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE));
+BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable)    == static_cast<int>(ENET_PACKET_FLAG_RELIABLE));
+BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequenced) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED));
+BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate)  == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE));
 
 #define PACKET_FLAG_DEFAULT PacketFlag::NoAllocate
 #define _PACKETID           0
@@ -173,44 +173,43 @@
   assert(ClientInformation::findClient(&peer->address)->getID() != static_cast<unsigned int>(-2) || !Host::isServer());
   unsigned int clientID = ClientInformation::findClient(&peer->address)->getID();
   Packet *p = 0;
-  COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl;
+//   COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl;
   switch( *(Type::Value *)(data + _PACKETID) )
   {
     case Type::Acknowledgement:
-      COUT(5) << "ack" << std::endl;
+//       COUT(5) << "ack" << std::endl;
       p = new Acknowledgement( data, clientID );
       break;
     case Type::Chat:
-      COUT(5) << "chat" << std::endl;
+//       COUT(5) << "chat" << std::endl;
       p = new Chat( data, clientID );
       break;
     case Type::ClassID:
-      COUT(5) << "classid" << std::endl;
+//       COUT(5) << "classid" << std::endl;
       p = new ClassID( data, clientID );
       break;
     case Type::Gamestate:
-      COUT(5) << "gamestate" << std::endl;
-      // TODO: remove brackets
+//       COUT(5) << "gamestate" << std::endl;
       p = new Gamestate( data, clientID );
       break;
     case Type::Welcome:
-      COUT(5) << "welcome" << std::endl;
+//       COUT(5) << "welcome" << std::endl;
       p = new Welcome( data, clientID );
       break;
     case Type::DeleteObjects:
-      COUT(5) << "deleteobjects" << std::endl;
+//       COUT(5) << "deleteobjects" << std::endl;
       p = new DeleteObjects( data, clientID );
       break;
     case Type::FunctionCalls:
-      COUT(5) << "functionCalls" << std::endl;
+//       COUT(5) << "functionCalls" << std::endl;
       p = new FunctionCalls( data, clientID );
       break;
     case Type::FunctionIDs:
-      COUT(5) << "functionIDs" << std::endl;
+//       COUT(5) << "functionIDs" << std::endl;
       p = new FunctionIDs( data, clientID );
       break;
     default:
-      assert(0); //TODO: repair this
+      assert(0);
       break;
   }
 
@@ -234,7 +233,7 @@
   it->second->enetPacket_ = 0;
   delete it->second;
   packetMap_.erase(it);
-  COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl;
+//   COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl;
 }
 
 } // namespace packet

Modified: code/branches/network5/src/libraries/network/synchronisable/Synchronisable.h
===================================================================
--- code/branches/network5/src/libraries/network/synchronisable/Synchronisable.h	2010-12-12 22:13:01 UTC (rev 7758)
+++ code/branches/network5/src/libraries/network/synchronisable/Synchronisable.h	2010-12-14 19:54:00 UTC (rev 7759)
@@ -64,33 +64,29 @@
       VeryLow     = 100
     };
   }
-  
-  typedef uint8_t VariableID;
 
-  /**
-   * @brief: stores information about a Synchronisable
+    /**
+   * @brief: stores information about a Synchronisable (light version)
    *
-   * This class stores the information about a Synchronisable (objectID_, classID_, creatorID_, dataSize)
+   * This class stores the information about a Synchronisable (objectID_, dataSize)
    * in an emulated bitset.
    * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
    * Bit 32 is a bool and defines whether the variables are stored in diff mode
    * Byte 5 to 8: objectID_
-   * Byte 9 to 12: classID_
-   * Byte 13 to 16: creatorID_
    */
-  class _NetworkExport SynchronisableHeader{
-    friend class SynchronisableHeaderLight;
-    private:
+  class _NetworkExport SynchronisableHeaderLight
+  {
+    protected:
       uint8_t* data_;
     public:
-      SynchronisableHeader(uint8_t* data)
+      SynchronisableHeaderLight(uint8_t* data)
         { data_ = data; }
       inline static uint32_t getSize()
-        { return 14; }
+        { return 6; }
       inline uint16_t getDataSize() const
-        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 15 bits
+        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits
       inline void setDataSize(uint16_t size)
-        { *(uint16_t*)(data_) = (size & 0x7FFF) | (*(uint16_t*)(data_) & 0x8000 ); }
+        { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); }
       inline bool isDiffed() const
         { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
       inline void setDiffed( bool b)
@@ -99,50 +95,46 @@
         { return *(uint32_t*)(data_+2); }
       inline void setObjectID(uint32_t objectID_)
         { *(uint32_t*)(data_+2) = objectID_; }
-      inline uint32_t getClassID() const
-        { return *(uint32_t*)(data_+6); }
-      inline void setClassID(uint32_t classID_)
-        { *(uint32_t*)(data_+6) = classID_; }
-      inline uint32_t getCreatorID() const
-        { return *(uint32_t*)(data_+10); }
-      inline void setCreatorID(uint32_t creatorID_)
-        { *(uint32_t*)(data_+10) = creatorID_; }
-      inline void operator=(SynchronisableHeader& h)
-        { memcpy(data_, h.data_, getSize()); }
+      inline void operator=(SynchronisableHeaderLight& h)
+        { memcpy(data_, h.data_, SynchronisableHeaderLight::getSize()); }
   };
-
-    /**
-   * @brief: stores information about a Synchronisable (light version)
+  
+  typedef uint8_t VariableID;
+  
+  /**
+   * @brief: stores information about a Synchronisable
    *
-   * This class stores the information about a Synchronisable (objectID_, dataSize)
+   * This class stores the information about a Synchronisable (objectID_, classID_, creatorID_, dataSize)
    * in an emulated bitset.
    * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
    * Bit 32 is a bool and defines whether the variables are stored in diff mode
    * Byte 5 to 8: objectID_
+   * Byte 9 to 12: classID_
+   * Byte 13 to 16: creatorID_
    */
-  class _NetworkExport SynchronisableHeaderLight{
-    private:
-      uint8_t* data_;
+  class _NetworkExport SynchronisableHeader: public SynchronisableHeaderLight
+  {
     public:
-      SynchronisableHeaderLight(uint8_t* data)
-        { data_ = data; }
+      SynchronisableHeader(uint8_t* data): SynchronisableHeaderLight(data)
+        {}
       inline static uint32_t getSize()
-        { return 6; }
-      inline uint16_t getDataSize() const
-        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits
-      inline void setDataSize(uint16_t size)
-        { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); }
-      inline bool isDiffed() const
-        { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
-      inline void setDiffed( bool b)
-        { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }
-      inline uint32_t getObjectID() const
-        { return *(uint32_t*)(data_+2); }
-      inline void setObjectID(uint32_t objectID_)
-        { *(uint32_t*)(data_+2) = objectID_; }
+        { return SynchronisableHeaderLight::getSize()+8; }
+      inline uint32_t getClassID() const
+        { return *(uint32_t*)(data_+SynchronisableHeaderLight::getSize()); }
+      inline void setClassID(uint32_t classID_)
+        { *(uint32_t*)(data_+SynchronisableHeaderLight::getSize()) = classID_; }
+      inline uint32_t getCreatorID() const
+        { return *(uint32_t*)(data_+SynchronisableHeaderLight::getSize()+4); }
+      inline void setCreatorID(uint32_t creatorID_)
+        { *(uint32_t*)(data_+SynchronisableHeaderLight::getSize()+4) = creatorID_; }
       inline void operator=(SynchronisableHeader& h)
         { memcpy(data_, h.data_, getSize()); }
   };
+  
+//   inline void operator=(SynchronisableHeaderLight& h1, SynchronisableHeader& h2)
+//   {
+//     memcpy(h1.data_, h2.data_, h1.getSize());
+//   }
 
   /**
   * This class is the base class of all the Objects in the universe that need to be synchronised over the network




More information about the Orxonox-commit mailing list