[Orxonox-commit 7228] r11842 - in code/branches/Masterserver_FS18/src/libraries/network: . packet

mdedial at orxonox.net mdedial at orxonox.net
Thu Mar 29 16:04:35 CEST 2018


Author: mdedial
Date: 2018-03-29 16:04:35 +0200 (Thu, 29 Mar 2018)
New Revision: 11842

Modified:
   code/branches/Masterserver_FS18/src/libraries/network/Client.h
   code/branches/Masterserver_FS18/src/libraries/network/ClientConnection.h
   code/branches/Masterserver_FS18/src/libraries/network/ClientConnectionListener.cc
   code/branches/Masterserver_FS18/src/libraries/network/ClientConnectionListener.h
   code/branches/Masterserver_FS18/src/libraries/network/FunctionCallManager.cc
   code/branches/Masterserver_FS18/src/libraries/network/FunctionCallManager.h
   code/branches/Masterserver_FS18/src/libraries/network/GamestateHandler.cc
   code/branches/Masterserver_FS18/src/libraries/network/GamestateHandler.h
   code/branches/Masterserver_FS18/src/libraries/network/GamestateManager.h
   code/branches/Masterserver_FS18/src/libraries/network/MasterServer.cc
   code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.cc
   code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.h
   code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.cc
   code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.h
   code/branches/Masterserver_FS18/src/libraries/network/packet/ClassID.cc
   code/branches/Masterserver_FS18/src/libraries/network/packet/DeleteObjects.cc
   code/branches/Masterserver_FS18/src/libraries/network/packet/FunctionCalls.cc
   code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.cc
   code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.h
   code/branches/Masterserver_FS18/src/libraries/network/packet/ServerInformation.cc
Log:
Clean up some code, add lots of comments


Modified: code/branches/Masterserver_FS18/src/libraries/network/Client.h
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/Client.h	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/Client.h	2018-03-29 14:04:35 UTC (rev 11842)
@@ -89,7 +89,7 @@
     virtual void connectionClosed() override;
   private:
     Client(const Client& copy); // not used
-    virtual bool isServer_() override{return false;}
+    virtual bool isServer_() override { return false; }
     virtual void processPacket(packet::Packet* packet) override;
 
     static Client* singletonPtr_s;

Modified: code/branches/Masterserver_FS18/src/libraries/network/ClientConnection.h
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/ClientConnection.h	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/ClientConnection.h	2018-03-29 14:04:35 UTC (rev 11842)
@@ -35,7 +35,7 @@
 namespace orxonox
 {
 
-  class _NetworkExport ClientConnection: public Connection{
+  class _NetworkExport ClientConnection: public Connection {
   public:
     ClientConnection();
     virtual ~ClientConnection();

Modified: code/branches/Masterserver_FS18/src/libraries/network/ClientConnectionListener.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/ClientConnectionListener.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/ClientConnectionListener.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -30,17 +30,24 @@
 
 #include "core/CoreIncludes.h"
 #include "core/GameMode.h"
-// #include "ClientInformation.h"
 
 namespace orxonox
 {
     RegisterAbstractClass(ClientConnectionListener).inheritsFrom<Listable>();
 
+    /**
+     * Constructor
+     * Register the object
+     */
     ClientConnectionListener::ClientConnectionListener()
     {
         RegisterObject(ClientConnectionListener);
     }
 
+    /**
+     * Call clientConnected() on all ClientConnectionListeners.
+     * @param clientID The ID of the newly connected client
+     */
     void ClientConnectionListener::broadcastClientConnected(unsigned int clientID)
     {
         for (ClientConnectionListener* listener : ObjectList<ClientConnectionListener>())
@@ -47,21 +54,15 @@
             listener->clientConnected(clientID);
     }
 
+    /**
+     * Call clientDisconnected() on all ClientConnectionListeners.
+     * @param clientID The ID of the newly disconnected client
+     */
     void ClientConnectionListener::broadcastClientDisconnected(unsigned int clientID)
     {
         for (ClientConnectionListener* listener : ObjectList<ClientConnectionListener>())
             listener->clientDisconnected(clientID);
     }
-
-//     void ClientConnectionListener::getConnectedClients()
-//     {
-//         ClientInformation* client = ClientInformation::getBegin();
-//         while (client)
-//         {
-//             this->clientConnected(client->getID());
-//             client = client->next();
-//         }
-//     }
 }
 
 

Modified: code/branches/Masterserver_FS18/src/libraries/network/ClientConnectionListener.h
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/ClientConnectionListener.h	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/ClientConnectionListener.h	2018-03-29 14:04:35 UTC (rev 11842)
@@ -34,6 +34,9 @@
 
 namespace orxonox
 {
+    /**
+     * An abstract base class. Derived classes must implement clientConnected() and clientDisconnected().
+     */
     class _NetworkExport ClientConnectionListener : virtual public Listable
     {
         public:
@@ -45,9 +48,6 @@
 
             virtual void clientConnected(unsigned int clientID) = 0;
             virtual void clientDisconnected(unsigned int clientID) = 0;
-
-        protected:
-//             void getConnectedClients();
     };
 }
 

Modified: code/branches/Masterserver_FS18/src/libraries/network/FunctionCallManager.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/FunctionCallManager.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/FunctionCallManager.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -41,35 +41,49 @@
 
 void FunctionCallManager::addCall(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
 {
-  if(sPeerMap_.find(peerID)==sPeerMap_.end())
+  // If the peerID doesn't exist yet in the map...
+  if(sPeerMap_.find(peerID) == sPeerMap_.end())
   {
+    // ... add a new FunctionCalls packet for the peer
     FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
     FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
+
+  // Add a new function call to the peer
   FunctionCallManager::sPeerMap_[peerID]->addCall(functionID, objectID, mt1, mt2, mt3, mt4, mt5);
 }
 
-// Send calls
-
+/**
+ * Send all function calls in sPeerMap_ to a given host, then clear sPeerMap_
+ * @param host The host to send the function calls to
+ */
 void FunctionCallManager::sendCalls(orxonox::Host* host)
 {
   for (const auto& mapEntry : FunctionCallManager::sPeerMap_ )
   {
+    // TODO: This seems rather pointless, as it wouldn't be called anyways if the map was empty.
     assert(!FunctionCallManager::sPeerMap_.empty());
     mapEntry.second->send(host);
   }
+  // TODO: Why is the map cleared here?
   FunctionCallManager::sPeerMap_.clear();
 }
 
+/**
+ * Place an incoming function call in the queue for processing.
+ */
 void FunctionCallManager::bufferIncomingFunctionCall(const orxonox::FunctionCall& fctCall, uint32_t minGamestateID, uint32_t peerID)
 {
   FunctionCallManager::sIncomingFunctionCallBuffer_.emplace_back(fctCall, std::make_pair(minGamestateID, peerID));
 }
 
+/**
+ * Process queue of incoming function calls.
+ */
 void FunctionCallManager::processBufferedFunctionCalls()
 {
   std::vector<std::pair<FunctionCall, std::pair<uint32_t, uint32_t>>>::iterator it = FunctionCallManager::sIncomingFunctionCallBuffer_.begin();
-  while( it!=FunctionCallManager::sIncomingFunctionCallBuffer_.end() )
+  while( it != FunctionCallManager::sIncomingFunctionCallBuffer_.end() )
   {
     OrxAssert( Host::getActiveInstance(), "No Host class existing" );
     if( it->second.first <= Host::getActiveInstance()->getLastReceivedGamestateID(it->second.second) && it->first.execute() )

Modified: code/branches/Masterserver_FS18/src/libraries/network/FunctionCallManager.h
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/FunctionCallManager.h	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/FunctionCallManager.h	2018-03-29 14:04:35 UTC (rev 11842)
@@ -52,8 +52,14 @@
   static void bufferIncomingFunctionCall( const FunctionCall& fctCall, uint32_t minGamestateID, uint32_t peerID );
   static void processBufferedFunctionCalls();
 
+  // Maps peer IDs to function calls
   static std::map<uint32_t, packet::FunctionCalls*>                           sPeerMap_;
+
+  // TODO: What's up with the pair within the pair?
+  // Vector of pairs
+  // The pair consists of the FunctionCall and another pair, which...
   static std::vector<std::pair<FunctionCall,std::pair<uint32_t, uint32_t>>> sIncomingFunctionCallBuffer_;
+
 protected:
   FunctionCallManager();
   ~FunctionCallManager();

Modified: code/branches/Masterserver_FS18/src/libraries/network/GamestateHandler.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/GamestateHandler.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/GamestateHandler.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -31,8 +31,6 @@
 
 namespace orxonox {
 
-// GamestateHandler *GamestateHandler::instance_=nullptr;
-
 GamestateHandler::GamestateHandler()
 {
 }

Modified: code/branches/Masterserver_FS18/src/libraries/network/GamestateHandler.h
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/GamestateHandler.h	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/GamestateHandler.h	2018-03-29 14:04:35 UTC (rev 11842)
@@ -36,7 +36,8 @@
 namespace orxonox {
 
 /**
-    @author Oliver Scheuss
+ * An interface for any class that wants to handle gamestates.
+ * @author Oliver Scheuss
 */
 class _NetworkExport GamestateHandler
 {
@@ -50,8 +51,8 @@
   public:
     virtual bool      addGamestate(packet::Gamestate* gs, unsigned int clientID) = 0;
     virtual bool      ackGamestate(unsigned int gamestateID, unsigned int clientID) = 0;
-    virtual uint32_t  getLastReceivedGamestateID( unsigned int clientID )=0;
-    virtual uint32_t  getCurrentGamestateID()=0;
+    virtual uint32_t  getLastReceivedGamestateID( unsigned int clientID ) = 0;
+    virtual uint32_t  getCurrentGamestateID() = 0;
 };
 
 }

Modified: code/branches/Masterserver_FS18/src/libraries/network/GamestateManager.h
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/GamestateManager.h	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/GamestateManager.h	2018-03-29 14:04:35 UTC (rev 11842)
@@ -57,6 +57,9 @@
   * - creating snapshots of gamestates
   * - writing gamestates to universe
   * - diffing gamestates
+  *
+  * Inherited by Host
+  *
   * EN/DECODATION:
   * a: last Gamestate a client has received
   * b: new Gamestate
@@ -85,7 +88,7 @@
     virtual bool      addGamestate(packet::Gamestate *gs, unsigned int peerID) override;
     virtual bool      ackGamestate(unsigned int gamestateID, unsigned int peerID) override;
     virtual uint32_t  getLastReceivedGamestateID( unsigned int peerID ) override;
-    virtual uint32_t  getCurrentGamestateID() override { if(currentGamestate) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; }
+    virtual uint32_t  getCurrentGamestateID() override { if(currentGamestate_) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; }
 
     bool processGamestates();
     bool sendAck(unsigned int gamestateID, uint32_t peerID);

Modified: code/branches/Masterserver_FS18/src/libraries/network/MasterServer.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/MasterServer.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/MasterServer.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -356,7 +356,9 @@
         eventDisconnect( event ); break;
 
         /* incoming data */
-      case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
+      case ENET_EVENT_TYPE_RECEIVE:
+        eventData( event ); break;
+        
       default: break;
     }
 

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -36,17 +36,25 @@
 namespace packet {
 
 #define PACKET_FLAGS_ACK    0
+// Offset to start of packet ID
 #define _PACKETID           0
+// Offset to start of ACK ID
 #define _ACKID              _PACKETID + sizeof(packet::Type)
 
+/**
+ * Constructor
+ * Acknowledgement.data_ is 40 bits in size:
+ * [0, 7]:   Packet Type
+ * [8, 39]:  Acknowledgement ID
+ */
 Acknowledgement::Acknowledgement( unsigned int id, unsigned int peerID )
  : Packet()
 {
-  flags_ = flags_ | PACKET_FLAGS_ACK;
-  data_=new uint8_t[ getSize() ];
-  *(Type *)(data_ + _PACKETID ) = Type::Acknowledgement;
-  *(uint32_t *)(data_ + _ACKID ) = id;
-  peerID_=peerID;
+  this->flags_ |= PACKET_FLAGS_ACK;
+  this->data_ = new uint8_t[ this->getSize() ];
+  *(Type *)(this->data_ + _PACKETID) = Type::Acknowledgement;
+  *(uint32_t *)(this->data_ + _ACKID) = id;
+  this->peerID_ = peerID;
 }
 
 Acknowledgement::Acknowledgement( uint8_t *data, unsigned int peerID )
@@ -58,11 +66,11 @@
 {
 }
 
-unsigned int Acknowledgement::getSize() const{
+unsigned int Acknowledgement::getSize() const {
   return _ACKID + sizeof(uint32_t);
 }
 
-bool Acknowledgement::process(orxonox::Host* host){
+bool Acknowledgement::process(orxonox::Host* host) {
   orxout(verbose_more, context::packets) << "processing ACK with ID: " << getAckID() << endl;
   bool b = host->ackGamestate(getAckID(), peerID_);
   delete this;
@@ -69,7 +77,7 @@
   return b;
 }
 
-unsigned int Acknowledgement::getAckID(){
+unsigned int Acknowledgement::getAckID() {
   return *(uint32_t *)(data_ + _ACKID);
 }
 

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.h
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.h	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.h	2018-03-29 14:04:35 UTC (rev 11842)
@@ -33,11 +33,11 @@
 #include "Packet.h"
 
 namespace orxonox {
+	
 const unsigned int ACKID_NACK = 0;
+
 namespace packet {
-/**
-    @author
-*/
+
 class _NetworkExport Acknowledgement : public Packet
 {
 public:

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -37,7 +37,7 @@
 
 #define   PACKET_FLAGS_CHAT PacketFlag::Reliable
 
-/* Some lengths */
+/* Some lengths / offsets */
 #define _PACKETID         0
 #define _SOURCEID         _PACKETID + sizeof(Type)
 #define _TARGETID         _SOURCEID + sizeof(uint32_t)
@@ -48,29 +48,29 @@
  : Packet()
 {
   /* Add chat flag to packet flags */
-  flags_ = flags_ | PACKET_FLAGS_CHAT;
+  this->flags_ |= PACKET_FLAGS_CHAT;
 
   /* set message length to length of input string + 1 */
-  messageLength_ = message.length()+1;
+  this->messageLength_ = message.length() + 1;
 
   /* allocate memory for the data */
-  data_=new unsigned char[ getSize() ];
+  this->data_ = new unsigned char[ getSize() ];
 
   *(Type *)(data_ + _PACKETID ) = Type::Chat;
   *(unsigned int *)(data_ + _SOURCEID ) = sourceID;
   *(unsigned int *)(data_ + _TARGETID ) = targetID;
-  *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_;
+  *(unsigned int *)(data_ + _MESSAGELENGTH ) = this->messageLength_;
 
   /* cast the hell out of the message string, and copy it into the
    * data buffer.
    */
-  memcpy( data_+_MESSAGE, static_cast<void*>(const_cast<char*>(message.c_str())), messageLength_ );
+  memcpy( this->data_ + _MESSAGE, static_cast<void*>(const_cast<char*>(message.c_str())), this->messageLength_ );
 }
 
 Chat::Chat( uint8_t* data, unsigned int clientID )
   : Packet(data, clientID)
 {
-  messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH );
+  this->messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH );
 }
 
 Chat::~Chat()
@@ -78,17 +78,19 @@
 }
 
 unsigned int Chat::getSize() const{
-  return _MESSAGE + messageLength_;
+  return _MESSAGE + this->messageLength_;
 }
 
 bool Chat::process(orxonox::Host* host){
-  host->doReceiveChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_SOURCEID), *(uint32_t *)(data_+_TARGETID));
+  host->doReceiveChat(std::string((const char*)this->data_ + _MESSAGE),
+                                  *(uint32_t *)(this->data_+_SOURCEID),
+                                  *(uint32_t *)(this->data_+_TARGETID));
   delete this;
   return true;
 }
 
-unsigned char *Chat::getMessage(){
-  return data_ + _MESSAGE;
+unsigned char *Chat::getMessage() {
+  return this->data_ + _MESSAGE;
 }
 
 } //namespace packet

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.h
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.h	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.h	2018-03-29 14:04:35 UTC (rev 11842)
@@ -54,7 +54,7 @@
   virtual bool process(orxonox::Host* host) override;
 
   /* Get the length of the message (not the full size of the packet) */
-  unsigned int getMessageLength(){ return messageLength_; };
+  unsigned int getMessageLength() { return this->messageLength_; };
 
   /* return message content */
   unsigned char *getMessage();

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/ClassID.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/ClassID.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/ClassID.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -45,16 +45,16 @@
 #define _PACKETID             0
 
 
-ClassID::ClassID( ) : Packet(){
+ClassID::ClassID() : Packet() {
   Identifier *id;
-  unsigned int nrOfClasses=0;
-  unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nrofclasses
+  unsigned int nrOfClasses = 0;
+  unsigned int packetSize = 2 * sizeof(uint32_t); //space for the packetID and for the nrofclasses
   uint32_t network_id;
-  flags_ = flags_ | PACKET_FLAGS_CLASSID;
+  this->flags_ |= PACKET_FLAGS_CLASSID;
   std::queue<std::pair<uint32_t, std::string>> tempQueue;
 
-  //calculate total needed size (for all strings and integers)
-  for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()){
+  // calculate total needed size (for all strings and integers)
+  for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()) {
     id = mapEntry.second;
     if(id == nullptr || !id->hasFactory())
       continue;
@@ -63,32 +63,32 @@
     // now push the network id and the classname to the stack
     tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) );
     ++nrOfClasses;
-    packetSize += (classname.size()+1)+sizeof(network_id)+sizeof(uint32_t);
+    packetSize += (classname.size() + 1) + sizeof(network_id) + sizeof(uint32_t);
   }
 
-  this->data_=new uint8_t[ packetSize ];
+  this->data_ = new uint8_t[ packetSize ];
   //set the appropriate packet id
   assert(this->data_);
   *(Type *)(this->data_ + _PACKETID ) = Type::ClassID;
 
-  uint8_t *temp=data_+sizeof(uint32_t);
+  uint8_t *temp = this->data_ + sizeof(uint32_t);
   // save the number of all classes
-  *(uint32_t*)temp = nrOfClasses;
+  *(uint32_t*) temp = nrOfClasses;
   temp += sizeof(uint32_t);
 
   // now save all classids and classnames
   std::pair<uint32_t, std::string> tempPair;
-  uint32_t tempsize = 2*sizeof(uint32_t); // packetid and nrOfClasses
+  uint32_t tempsize = 2 * sizeof(uint32_t); // packetid and nrOfClasses
   while( !tempQueue.empty() ){
     tempPair = tempQueue.front();
     tempQueue.pop();
-    *(uint32_t*)temp = tempPair.first;
-    *(uint32_t*)(temp+sizeof(uint32_t)) = tempPair.second.size()+1;
-    memcpy(temp+2*sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size()+1);
-    temp+=2*sizeof(uint32_t)+tempPair.second.size()+1;
-    tempsize+=2*sizeof(uint32_t)+tempPair.second.size()+1;
+    *(uint32_t*) temp = tempPair.first;
+    *(uint32_t*) (temp+sizeof(uint32_t)) = tempPair.second.size() + 1;
+    memcpy(temp + 2 * sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size() + 1);
+    temp += 2 * sizeof(uint32_t) + tempPair.second.size() + 1;
+    tempsize += 2 * sizeof(uint32_t) + tempPair.second.size() + 1;
   }
-  assert(tempsize==packetSize);
+  assert(tempsize == packetSize);
 
   orxout(verbose_more, context::packets) << "classid packetSize is " << packetSize << endl;
 
@@ -103,29 +103,29 @@
 {
 }
 
-uint32_t ClassID::getSize() const{
-  uint8_t *temp = data_+sizeof(uint32_t); // packet identification
+uint32_t ClassID::getSize() const {
+  uint8_t *temp = this->data_ + sizeof(uint32_t); // packet identification
   uint32_t totalsize = sizeof(uint32_t); // packet identification
-  uint32_t nrOfClasses = *(uint32_t*)temp;
+  uint32_t nrOfClasses = *(uint32_t*) temp;
   temp += sizeof(uint32_t);
   totalsize += sizeof(uint32_t); // storage size for nr of all classes
 
-  for(unsigned int i=0; i<nrOfClasses; i++){
-    totalsize += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
-    temp += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
+  for(unsigned int i=0; i < nrOfClasses; i++) {
+    totalsize += 2 * sizeof(uint32_t) + *(uint32_t*) (temp + sizeof(uint32_t));
+    temp += 2 * sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
   }
   return totalsize;
 }
 
-
-bool ClassID::process(orxonox::Host* host){
+// TODO: This parses the packet and calls ClassByString()
+// However, the resulting Identifier is discarded...
+bool ClassID::process(orxonox::Host* host) {
   int nrOfClasses;
-  uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid
+  uint8_t *temp = this->data_ + sizeof(uint32_t); //skip the packetid
   uint32_t networkID;
   uint32_t stringsize;
   unsigned char *classname;
 
-
   //clear the map of network ids
   IdentifierManager::getInstance().clearNetworkIDs();
 
@@ -133,21 +133,21 @@
   std::pair<uint32_t, std::string> tempPair;
   Identifier *id;
   // read the total number of classes
-  nrOfClasses = *(uint32_t*)temp;
+  nrOfClasses = *(uint32_t*) temp;
   temp += sizeof(uint32_t);
 
-  for( int i=0; i<nrOfClasses; i++){
-    networkID = *(uint32_t*)temp;
-    stringsize = *(uint32_t*)(temp+sizeof(uint32_t));
-    classname = temp+2*sizeof(uint32_t);
-    id=ClassByString( std::string((const char*)classname) );
+  for( int i = 0; i < nrOfClasses; i++) {
+    networkID = *(uint32_t*) temp;
+    stringsize = *(uint32_t*) (temp + sizeof(uint32_t));
+    classname = temp + 2 * sizeof(uint32_t);
+    id = ClassByString( std::string((const char*) classname) );
     orxout(internal_info, context::packets) << "processing classid: " << networkID << " name: " << classname << " id: " << id << endl;
-    if(id==nullptr){
+    if(id == nullptr) {
       orxout(user_error, context::packets) << "Received a bad classname" << endl;
       abort();
     }
     id->setNetworkID( networkID );
-    temp += 2*sizeof(uint32_t) + stringsize;
+    temp += 2 * sizeof(uint32_t) + stringsize;
   }
   delete this;
   return true;

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/DeleteObjects.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/DeleteObjects.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/DeleteObjects.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -44,7 +44,7 @@
 DeleteObjects::DeleteObjects()
  : Packet()
 {
-  flags_ = flags_ | PACKET_FLAG_DELETE;
+  this->flags_ |= PACKET_FLAG_DELETE;
 }
 
 DeleteObjects::DeleteObjects( uint8_t *data, unsigned int clientID )
@@ -59,7 +59,7 @@
 bool DeleteObjects::fetchIDs()
 {
   unsigned int number = Synchronisable::getNumberOfDeletedObject();
-  if(number==0)
+  if(number == 0)
     return false;
   orxout(verbose, context::packets) << "sending DeleteObjects: ";
   unsigned int size = sizeof(Type) + sizeof(uint32_t)*(number+1);
@@ -82,15 +82,15 @@
 unsigned int DeleteObjects::getSize() const
 {
   assert(data_);
-  return _OBJECTIDS + *(uint32_t*)(data_+_QUANTITY)*sizeof(uint32_t);
+  return _OBJECTIDS + *(uint32_t*) (this->data_ + _QUANTITY) * sizeof(uint32_t);
 }
 
 bool DeleteObjects::process(orxonox::Host* host)
 {
-  for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++)
+  for(unsigned int i = 0; i < *(unsigned int *) (this->data_+_QUANTITY); i++)
   {
     orxout(verbose, context::packets) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << endl;
-    Synchronisable::deleteObject( *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) );
+    Synchronisable::deleteObject( *(uint32_t*)(this->data_ + _OBJECTIDS + i * sizeof(uint32_t)) );
   }
   delete this;
   return true;

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/FunctionCalls.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/FunctionCalls.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/FunctionCalls.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -62,12 +62,12 @@
 {
   assert(isDataENetAllocated());
   
-  uint8_t* temp = data_+sizeof(uint32_t); //skip packetid
-  uint32_t nrOfCalls = *(uint32_t*)temp;
+  uint8_t* temp = data_ + sizeof(uint32_t); //skip packetid
+  uint32_t nrOfCalls = *(uint32_t*) temp;
   temp += sizeof(uint32_t);
-  this->minGamestateID_ = *(uint32_t*)temp;
+  this->minGamestateID_ = *(uint32_t*) temp;
   temp += sizeof(uint32_t);
-  for( unsigned int i = 0; i<nrOfCalls; i++ )
+  for( unsigned int i = 0; i < nrOfCalls; i++ )
   {
     FunctionCall fctCall;
     fctCall.loadData(temp);
@@ -94,11 +94,11 @@
 {
   this->minGamestateID_ = host->getCurrentGamestateID();
   assert(this->functionCalls_.size());
-  data_=new uint8_t[ currentSize_ ];
-  *(Type *)(data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
-  *(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);
+  this->data_ = new uint8_t[ currentSize_ ];
+  *(Type *)(this->data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
+  *(uint32_t*)(this->data_ + sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls
+  *(uint32_t*)(this->data_ + 2 * sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_
+  uint8_t* temp = this->data_ + 3 * sizeof(uint32_t);
   
   while( this->functionCalls_.size() )
   {
@@ -106,7 +106,7 @@
     this->functionCalls_.pop();
   }
   
-  assert( temp==data_+currentSize_ );
+  assert( temp == this->data_ + currentSize_ );
   
   Packet::send(host);
   return true;

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -128,16 +128,23 @@
   }
 }
 
+/**
+ * Send the Packet.
+ * @param host The host which sends the packet
+ */
 bool Packet::send(orxonox::Host* host)
 {
+  // Deny sending incoming packets
   if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional )
   {
     assert(0);
     return false;
   }
+
   if(!enetPacket_)
   {
-    if(!data_){
+    // Deny sending empty packets
+    if(!data_) {
       assert(0);
       return false;
     }
@@ -151,7 +158,7 @@
       // Assures we don't create a packet and destroy it right after in another thread
       // without having a reference in the packetMap_
       Packet::packetMapMutex_.lock();
-      packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;
+      Packet::packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;
       Packet::packetMapMutex_.unlock();
     }
   }
@@ -172,57 +179,49 @@
       break;
   }
 #endif
-//  ENetPacket *temp = enetPacket_;
-//  enetPacket_ = nullptr; // otherwise we have a double free because enet already handles the deallocation of the packet
+
+  // Send via reliable or standard channel respectively
   if( this->flags_ & PacketFlag::Reliable )
     host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_DEFAULT);
   else
     host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_UNRELIABLE);
+
   return true;
 }
 
+/**
+ * Given an ENetPacket, create an Orxonox packet
+ * @param packet The ENetPacket
+ * @param peerID The sender
+ */
 Packet *Packet::createPacket(ENetPacket* packet, uint32_t peerID)
 {
   uint8_t *data = packet->data;
-//   assert(ClientInformation::findClient(&peer->address)->getID() != static_cast<unsigned int>(-2) || !Host::isServer());
-//   unsigned int peerID = ClientInformation::findClient(&peer->address)->getID();
-  // HACK
-//   if( peerID==static_cast<unsigned int>(-2))
-//     peerID = NETWORK_PEER_ID_SERVER;
   Packet *p = nullptr;
-//   orxout(verbose_ultra, context::packets) << "packet type: " << *(Type *)&data[_PACKETID] << endl;
   switch( *(Type *)(data + _PACKETID) )
   {
     case Type::Acknowledgement:
-//       orxout(verbose_more, context::packets) << "ack" << endl;
-    p = new Acknowledgement( data, peerID );
+      p = new Acknowledgement( data, peerID );
       break;
     case Type::Chat:
-//       orxout(verbose_more, context::packets) << "chat" << endl;
       p = new Chat( data, peerID );
       break;
     case Type::ClassID:
-//       orxout(verbose_more, context::packets) << "classid" << endl;
       p = new ClassID( data, peerID );
       break;
     case Type::Gamestate:
-//       orxout(verbose_more, context::packets) << "gamestate" << endl;
       p = new Gamestate( data, peerID );
       break;
     case Type::Welcome:
-//       orxout(verbose_more, context::packets) << "welcome" << endl;
       p = new Welcome( data, peerID );
       break;
     case Type::DeleteObjects:
-//       orxout(verbose_more, context::packets) << "deleteobjects" << endl;
       p = new DeleteObjects( data, peerID );
       break;
     case Type::FunctionCalls:
-//       orxout(verbose_more, context::packets) << "functionCalls" << endl;
       p = new FunctionCalls( data, peerID );
       break;
     case Type::FunctionIDs:
-//       orxout(verbose_more, context::packets) << "functionIDs" << endl;
       p = new FunctionIDs( data, peerID );
       break;
     default:
@@ -246,14 +245,16 @@
 {
   // Get our Packet from a global map with all Packets created in the send() method of Packet.
   Packet::packetMapMutex_.lock();
-  std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket));
+
+  std::map<size_t, Packet*>::iterator it = Packet::packetMap_.find(reinterpret_cast<size_t>(enetPacket));
   assert(it != packetMap_.end());
+
   // Make sure we don't delete it again in the destructor
   it->second->enetPacket_ = nullptr;
   delete it->second;
   packetMap_.erase(it);
+
   Packet::packetMapMutex_.unlock();
-//   orxout(verbose_ultra, context::packets) << "PacketMap size: " << packetMap_.size() << endl;
 }
 
 } // namespace packet

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.h
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.h	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.h	2018-03-29 14:04:35 UTC (rev 11842)
@@ -67,8 +67,11 @@
     static void deletePacket(ENetPacket* packet);
 
     virtual unsigned char* getData(){ return data_; };
-    virtual unsigned int getSize() const =0;
-    virtual bool process(orxonox::Host* host)=0;
+    virtual unsigned int getSize() const = 0;
+
+    // Invoke some sort of action associated with the packet
+    virtual bool process(orxonox::Host* host) = 0;
+    
     inline uint32_t getFlags()
       { return flags_; }
     inline int getPeerID()
@@ -81,10 +84,10 @@
       { return this->requiredGamestateID_; }
 
     virtual bool send(orxonox::Host* host);
+
   protected:
     Packet();
     Packet(uint8_t *data, unsigned int peerID);
-//    Packet(ENetPacket *packet, ENetPeer *peer);
     inline bool isDataENetAllocated() const
       { return bDataENetAllocated_; }
 
@@ -99,7 +102,9 @@
     /** Tells whether data_ was allocated by ENet or ourselves.
         data_ might no correlate with enetPacket_->data. */
     bool bDataENetAllocated_;
+
   private:
+    // All Packets are contained in this map
     static std::map<size_t, Packet *> packetMap_;
     static boost::mutex               packetMapMutex_;
     ENetPacket *enetPacket_;

Modified: code/branches/Masterserver_FS18/src/libraries/network/packet/ServerInformation.cc
===================================================================
--- code/branches/Masterserver_FS18/src/libraries/network/packet/ServerInformation.cc	2018-03-29 14:02:41 UTC (rev 11841)
+++ code/branches/Masterserver_FS18/src/libraries/network/packet/ServerInformation.cc	2018-03-29 14:04:35 UTC (rev 11842)
@@ -49,18 +49,15 @@
 
       // Save Server Round Trip Time
       this->serverRTT_ = event->peer->roundTripTime;
+
       // Save Server Address, leave some space for scope ID
       enet_address_get_host_ip(&event->peer->address, serverIP, 64);
+
       this->serverIP_ = std::string(serverIP);
       // Save ACK
       uint8_t* temp = event->packet->data;
       char* ack = nullptr;
       loadAndIncrease((char*&)ack, temp);
-
-      /* Fabian, what is this used for? it crashes the masterserver, hence commenting it */
-      // written by Oli: this is just to make sure that loadAndIncrease really writes the whole ACK string into char* ack
-//       assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);
-
       // Save Server Name
       loadAndIncrease(this->serverName_, temp);
       delete[] ack;
@@ -73,7 +70,7 @@
 
     void ServerInformation::send(ENetPeer* peer)
     {
-      std::string payload = this->serverName_ + Ogre::StringConverter::toString(this->clientNumber_);
+      std::string payload = this->serverName_ + std::to_string(this->clientNumber_);
       uint32_t size = returnSize(LAN_DISCOVERY_ACK) + returnSize(payload);
       uint8_t* temp = new uint8_t[size];
       uint8_t* temp2 = temp;



More information about the Orxonox-commit mailing list