Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 29, 2018, 4:04:35 PM (6 years ago)
Author:
mdedial
Message:

Clean up some code, add lots of comments

Location:
code/branches/Masterserver_FS18/src/libraries/network/packet
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.cc

    r11071 r11842  
    3737
    3838#define PACKET_FLAGS_ACK    0
     39// Offset to start of packet ID
    3940#define _PACKETID           0
     41// Offset to start of ACK ID
    4042#define _ACKID              _PACKETID + sizeof(packet::Type)
    4143
     44/**
     45 * Constructor
     46 * Acknowledgement.data_ is 40 bits in size:
     47 * [0, 7]:   Packet Type
     48 * [8, 39]:  Acknowledgement ID
     49 */
    4250Acknowledgement::Acknowledgement( unsigned int id, unsigned int peerID )
    4351 : Packet()
    4452{
    45   flags_ = flags_ | PACKET_FLAGS_ACK;
    46   data_=new uint8_t[ getSize() ];
    47   *(Type *)(data_ + _PACKETID ) = Type::Acknowledgement;
    48   *(uint32_t *)(data_ + _ACKID ) = id;
    49   peerID_=peerID;
     53  this->flags_ |= PACKET_FLAGS_ACK;
     54  this->data_ = new uint8_t[ this->getSize() ];
     55  *(Type *)(this->data_ + _PACKETID) = Type::Acknowledgement;
     56  *(uint32_t *)(this->data_ + _ACKID) = id;
     57  this->peerID_ = peerID;
    5058}
    5159
     
    5967}
    6068
    61 unsigned int Acknowledgement::getSize() const{
     69unsigned int Acknowledgement::getSize() const {
    6270  return _ACKID + sizeof(uint32_t);
    6371}
    6472
    65 bool Acknowledgement::process(orxonox::Host* host){
     73bool Acknowledgement::process(orxonox::Host* host) {
    6674  orxout(verbose_more, context::packets) << "processing ACK with ID: " << getAckID() << endl;
    6775  bool b = host->ackGamestate(getAckID(), peerID_);
     
    7078}
    7179
    72 unsigned int Acknowledgement::getAckID(){
     80unsigned int Acknowledgement::getAckID() {
    7381  return *(uint32_t *)(data_ + _ACKID);
    7482}
  • code/branches/Masterserver_FS18/src/libraries/network/packet/Acknowledgement.h

    r11071 r11842  
    3434
    3535namespace orxonox {
     36       
    3637const unsigned int ACKID_NACK = 0;
     38
    3739namespace packet {
    38 /**
    39     @author
    40 */
     40
    4141class _NetworkExport Acknowledgement : public Packet
    4242{
  • code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.cc

    r11071 r11842  
    3838#define   PACKET_FLAGS_CHAT PacketFlag::Reliable
    3939
    40 /* Some lengths */
     40/* Some lengths / offsets */
    4141#define _PACKETID         0
    4242#define _SOURCEID         _PACKETID + sizeof(Type)
     
    4949{
    5050  /* Add chat flag to packet flags */
    51   flags_ = flags_ | PACKET_FLAGS_CHAT;
     51  this->flags_ |= PACKET_FLAGS_CHAT;
    5252
    5353  /* set message length to length of input string + 1 */
    54   messageLength_ = message.length()+1;
     54  this->messageLength_ = message.length() + 1;
    5555
    5656  /* allocate memory for the data */
    57   data_=new unsigned char[ getSize() ];
     57  this->data_ = new unsigned char[ getSize() ];
    5858
    5959  *(Type *)(data_ + _PACKETID ) = Type::Chat;
    6060  *(unsigned int *)(data_ + _SOURCEID ) = sourceID;
    6161  *(unsigned int *)(data_ + _TARGETID ) = targetID;
    62   *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_;
     62  *(unsigned int *)(data_ + _MESSAGELENGTH ) = this->messageLength_;
    6363
    6464  /* cast the hell out of the message string, and copy it into the
    6565   * data buffer.
    6666   */
    67   memcpy( data_+_MESSAGE, static_cast<void*>(const_cast<char*>(message.c_str())), messageLength_ );
     67  memcpy( this->data_ + _MESSAGE, static_cast<void*>(const_cast<char*>(message.c_str())), this->messageLength_ );
    6868}
    6969
     
    7171  : Packet(data, clientID)
    7272{
    73   messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH );
     73  this->messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH );
    7474}
    7575
     
    7979
    8080unsigned int Chat::getSize() const{
    81   return _MESSAGE + messageLength_;
     81  return _MESSAGE + this->messageLength_;
    8282}
    8383
    8484bool Chat::process(orxonox::Host* host){
    85   host->doReceiveChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_SOURCEID), *(uint32_t *)(data_+_TARGETID));
     85  host->doReceiveChat(std::string((const char*)this->data_ + _MESSAGE),
     86                                  *(uint32_t *)(this->data_+_SOURCEID),
     87                                  *(uint32_t *)(this->data_+_TARGETID));
    8688  delete this;
    8789  return true;
    8890}
    8991
    90 unsigned char *Chat::getMessage(){
    91   return data_ + _MESSAGE;
     92unsigned char *Chat::getMessage() {
     93  return this->data_ + _MESSAGE;
    9294}
    9395
  • code/branches/Masterserver_FS18/src/libraries/network/packet/Chat.h

    r11071 r11842  
    5555
    5656  /* Get the length of the message (not the full size of the packet) */
    57   unsigned int getMessageLength(){ return messageLength_; };
     57  unsigned int getMessageLength() { return this->messageLength_; };
    5858
    5959  /* return message content */
  • code/branches/Masterserver_FS18/src/libraries/network/packet/ClassID.cc

    r11071 r11842  
    4646
    4747
    48 ClassID::ClassID( ) : Packet(){
     48ClassID::ClassID() : Packet() {
    4949  Identifier *id;
    50   unsigned int nrOfClasses=0;
    51   unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nrofclasses
     50  unsigned int nrOfClasses = 0;
     51  unsigned int packetSize = 2 * sizeof(uint32_t); //space for the packetID and for the nrofclasses
    5252  uint32_t network_id;
    53   flags_ = flags_ | PACKET_FLAGS_CLASSID;
     53  this->flags_ |= PACKET_FLAGS_CLASSID;
    5454  std::queue<std::pair<uint32_t, std::string>> tempQueue;
    5555
    56   //calculate total needed size (for all strings and integers)
    57   for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()){
     56  // calculate total needed size (for all strings and integers)
     57  for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()) {
    5858    id = mapEntry.second;
    5959    if(id == nullptr || !id->hasFactory())
     
    6464    tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) );
    6565    ++nrOfClasses;
    66     packetSize += (classname.size()+1)+sizeof(network_id)+sizeof(uint32_t);
     66    packetSize += (classname.size() + 1) + sizeof(network_id) + sizeof(uint32_t);
    6767  }
    6868
    69   this->data_=new uint8_t[ packetSize ];
     69  this->data_ = new uint8_t[ packetSize ];
    7070  //set the appropriate packet id
    7171  assert(this->data_);
    7272  *(Type *)(this->data_ + _PACKETID ) = Type::ClassID;
    7373
    74   uint8_t *temp=data_+sizeof(uint32_t);
     74  uint8_t *temp = this->data_ + sizeof(uint32_t);
    7575  // save the number of all classes
    76   *(uint32_t*)temp = nrOfClasses;
     76  *(uint32_t*) temp = nrOfClasses;
    7777  temp += sizeof(uint32_t);
    7878
    7979  // now save all classids and classnames
    8080  std::pair<uint32_t, std::string> tempPair;
    81   uint32_t tempsize = 2*sizeof(uint32_t); // packetid and nrOfClasses
     81  uint32_t tempsize = 2 * sizeof(uint32_t); // packetid and nrOfClasses
    8282  while( !tempQueue.empty() ){
    8383    tempPair = tempQueue.front();
    8484    tempQueue.pop();
    85     *(uint32_t*)temp = tempPair.first;
    86     *(uint32_t*)(temp+sizeof(uint32_t)) = tempPair.second.size()+1;
    87     memcpy(temp+2*sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size()+1);
    88     temp+=2*sizeof(uint32_t)+tempPair.second.size()+1;
    89     tempsize+=2*sizeof(uint32_t)+tempPair.second.size()+1;
     85    *(uint32_t*) temp = tempPair.first;
     86    *(uint32_t*) (temp+sizeof(uint32_t)) = tempPair.second.size() + 1;
     87    memcpy(temp + 2 * sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size() + 1);
     88    temp += 2 * sizeof(uint32_t) + tempPair.second.size() + 1;
     89    tempsize += 2 * sizeof(uint32_t) + tempPair.second.size() + 1;
    9090  }
    91   assert(tempsize==packetSize);
     91  assert(tempsize == packetSize);
    9292
    9393  orxout(verbose_more, context::packets) << "classid packetSize is " << packetSize << endl;
     
    104104}
    105105
    106 uint32_t ClassID::getSize() const{
    107   uint8_t *temp = data_+sizeof(uint32_t); // packet identification
     106uint32_t ClassID::getSize() const {
     107  uint8_t *temp = this->data_ + sizeof(uint32_t); // packet identification
    108108  uint32_t totalsize = sizeof(uint32_t); // packet identification
    109   uint32_t nrOfClasses = *(uint32_t*)temp;
     109  uint32_t nrOfClasses = *(uint32_t*) temp;
    110110  temp += sizeof(uint32_t);
    111111  totalsize += sizeof(uint32_t); // storage size for nr of all classes
    112112
    113   for(unsigned int i=0; i<nrOfClasses; i++){
    114     totalsize += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
    115     temp += 2*sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
     113  for(unsigned int i=0; i < nrOfClasses; i++) {
     114    totalsize += 2 * sizeof(uint32_t) + *(uint32_t*) (temp + sizeof(uint32_t));
     115    temp += 2 * sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));
    116116  }
    117117  return totalsize;
    118118}
    119119
    120 
    121 bool ClassID::process(orxonox::Host* host){
     120// TODO: This parses the packet and calls ClassByString()
     121// However, the resulting Identifier is discarded...
     122bool ClassID::process(orxonox::Host* host) {
    122123  int nrOfClasses;
    123   uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid
     124  uint8_t *temp = this->data_ + sizeof(uint32_t); //skip the packetid
    124125  uint32_t networkID;
    125126  uint32_t stringsize;
    126127  unsigned char *classname;
    127 
    128128
    129129  //clear the map of network ids
     
    134134  Identifier *id;
    135135  // read the total number of classes
    136   nrOfClasses = *(uint32_t*)temp;
     136  nrOfClasses = *(uint32_t*) temp;
    137137  temp += sizeof(uint32_t);
    138138
    139   for( int i=0; i<nrOfClasses; i++){
    140     networkID = *(uint32_t*)temp;
    141     stringsize = *(uint32_t*)(temp+sizeof(uint32_t));
    142     classname = temp+2*sizeof(uint32_t);
    143     id=ClassByString( std::string((const char*)classname) );
     139  for( int i = 0; i < nrOfClasses; i++) {
     140    networkID = *(uint32_t*) temp;
     141    stringsize = *(uint32_t*) (temp + sizeof(uint32_t));
     142    classname = temp + 2 * sizeof(uint32_t);
     143    id = ClassByString( std::string((const char*) classname) );
    144144    orxout(internal_info, context::packets) << "processing classid: " << networkID << " name: " << classname << " id: " << id << endl;
    145     if(id==nullptr){
     145    if(id == nullptr) {
    146146      orxout(user_error, context::packets) << "Received a bad classname" << endl;
    147147      abort();
    148148    }
    149149    id->setNetworkID( networkID );
    150     temp += 2*sizeof(uint32_t) + stringsize;
     150    temp += 2 * sizeof(uint32_t) + stringsize;
    151151  }
    152152  delete this;
  • code/branches/Masterserver_FS18/src/libraries/network/packet/DeleteObjects.cc

    r11071 r11842  
    4545 : Packet()
    4646{
    47   flags_ = flags_ | PACKET_FLAG_DELETE;
     47  this->flags_ |= PACKET_FLAG_DELETE;
    4848}
    4949
     
    6060{
    6161  unsigned int number = Synchronisable::getNumberOfDeletedObject();
    62   if(number==0)
     62  if(number == 0)
    6363    return false;
    6464  orxout(verbose, context::packets) << "sending DeleteObjects: ";
     
    8383{
    8484  assert(data_);
    85   return _OBJECTIDS + *(uint32_t*)(data_+_QUANTITY)*sizeof(uint32_t);
     85  return _OBJECTIDS + *(uint32_t*) (this->data_ + _QUANTITY) * sizeof(uint32_t);
    8686}
    8787
    8888bool DeleteObjects::process(orxonox::Host* host)
    8989{
    90   for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++)
     90  for(unsigned int i = 0; i < *(unsigned int *) (this->data_+_QUANTITY); i++)
    9191  {
    9292    orxout(verbose, context::packets) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << endl;
    93     Synchronisable::deleteObject( *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) );
     93    Synchronisable::deleteObject( *(uint32_t*)(this->data_ + _OBJECTIDS + i * sizeof(uint32_t)) );
    9494  }
    9595  delete this;
  • code/branches/Masterserver_FS18/src/libraries/network/packet/FunctionCalls.cc

    r11071 r11842  
    6363  assert(isDataENetAllocated());
    6464 
    65   uint8_t* temp = data_+sizeof(uint32_t); //skip packetid
    66   uint32_t nrOfCalls = *(uint32_t*)temp;
     65  uint8_t* temp = data_ + sizeof(uint32_t); //skip packetid
     66  uint32_t nrOfCalls = *(uint32_t*) temp;
    6767  temp += sizeof(uint32_t);
    68   this->minGamestateID_ = *(uint32_t*)temp;
     68  this->minGamestateID_ = *(uint32_t*) temp;
    6969  temp += sizeof(uint32_t);
    70   for( unsigned int i = 0; i<nrOfCalls; i++ )
     70  for( unsigned int i = 0; i < nrOfCalls; i++ )
    7171  {
    7272    FunctionCall fctCall;
     
    9595  this->minGamestateID_ = host->getCurrentGamestateID();
    9696  assert(this->functionCalls_.size());
    97   data_=new uint8_t[ currentSize_ ];
    98   *(Type *)(data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
    99   *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls
    100   *(uint32_t*)(data_+2*sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_
    101   uint8_t* temp = data_+3*sizeof(uint32_t);
     97  this->data_ = new uint8_t[ currentSize_ ];
     98  *(Type *)(this->data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
     99  *(uint32_t*)(this->data_ + sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls
     100  *(uint32_t*)(this->data_ + 2 * sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_
     101  uint8_t* temp = this->data_ + 3 * sizeof(uint32_t);
    102102 
    103103  while( this->functionCalls_.size() )
     
    107107  }
    108108 
    109   assert( temp==data_+currentSize_ );
     109  assert( temp == this->data_ + currentSize_ );
    110110 
    111111  Packet::send(host);
  • code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.cc

    r11071 r11842  
    129129}
    130130
     131/**
     132 * Send the Packet.
     133 * @param host The host which sends the packet
     134 */
    131135bool Packet::send(orxonox::Host* host)
    132136{
     137  // Deny sending incoming packets
    133138  if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional )
    134139  {
     
    136141    return false;
    137142  }
     143
    138144  if(!enetPacket_)
    139145  {
    140     if(!data_){
     146    // Deny sending empty packets
     147    if(!data_) {
    141148      assert(0);
    142149      return false;
     
    152159      // without having a reference in the packetMap_
    153160      Packet::packetMapMutex_.lock();
    154       packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;
     161      Packet::packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;
    155162      Packet::packetMapMutex_.unlock();
    156163    }
     
    173180  }
    174181#endif
    175 //  ENetPacket *temp = enetPacket_;
    176 //  enetPacket_ = nullptr; // otherwise we have a double free because enet already handles the deallocation of the packet
     182
     183  // Send via reliable or standard channel respectively
    177184  if( this->flags_ & PacketFlag::Reliable )
    178185    host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_DEFAULT);
    179186  else
    180187    host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_UNRELIABLE);
     188
    181189  return true;
    182190}
    183191
     192/**
     193 * Given an ENetPacket, create an Orxonox packet
     194 * @param packet The ENetPacket
     195 * @param peerID The sender
     196 */
    184197Packet *Packet::createPacket(ENetPacket* packet, uint32_t peerID)
    185198{
    186199  uint8_t *data = packet->data;
    187 //   assert(ClientInformation::findClient(&peer->address)->getID() != static_cast<unsigned int>(-2) || !Host::isServer());
    188 //   unsigned int peerID = ClientInformation::findClient(&peer->address)->getID();
    189   // HACK
    190 //   if( peerID==static_cast<unsigned int>(-2))
    191 //     peerID = NETWORK_PEER_ID_SERVER;
    192200  Packet *p = nullptr;
    193 //   orxout(verbose_ultra, context::packets) << "packet type: " << *(Type *)&data[_PACKETID] << endl;
    194201  switch( *(Type *)(data + _PACKETID) )
    195202  {
    196203    case Type::Acknowledgement:
    197 //       orxout(verbose_more, context::packets) << "ack" << endl;
    198     p = new Acknowledgement( data, peerID );
     204      p = new Acknowledgement( data, peerID );
    199205      break;
    200206    case Type::Chat:
    201 //       orxout(verbose_more, context::packets) << "chat" << endl;
    202207      p = new Chat( data, peerID );
    203208      break;
    204209    case Type::ClassID:
    205 //       orxout(verbose_more, context::packets) << "classid" << endl;
    206210      p = new ClassID( data, peerID );
    207211      break;
    208212    case Type::Gamestate:
    209 //       orxout(verbose_more, context::packets) << "gamestate" << endl;
    210213      p = new Gamestate( data, peerID );
    211214      break;
    212215    case Type::Welcome:
    213 //       orxout(verbose_more, context::packets) << "welcome" << endl;
    214216      p = new Welcome( data, peerID );
    215217      break;
    216218    case Type::DeleteObjects:
    217 //       orxout(verbose_more, context::packets) << "deleteobjects" << endl;
    218219      p = new DeleteObjects( data, peerID );
    219220      break;
    220221    case Type::FunctionCalls:
    221 //       orxout(verbose_more, context::packets) << "functionCalls" << endl;
    222222      p = new FunctionCalls( data, peerID );
    223223      break;
    224224    case Type::FunctionIDs:
    225 //       orxout(verbose_more, context::packets) << "functionIDs" << endl;
    226225      p = new FunctionIDs( data, peerID );
    227226      break;
     
    247246  // Get our Packet from a global map with all Packets created in the send() method of Packet.
    248247  Packet::packetMapMutex_.lock();
    249   std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket));
     248
     249  std::map<size_t, Packet*>::iterator it = Packet::packetMap_.find(reinterpret_cast<size_t>(enetPacket));
    250250  assert(it != packetMap_.end());
     251
    251252  // Make sure we don't delete it again in the destructor
    252253  it->second->enetPacket_ = nullptr;
    253254  delete it->second;
    254255  packetMap_.erase(it);
     256
    255257  Packet::packetMapMutex_.unlock();
    256 //   orxout(verbose_ultra, context::packets) << "PacketMap size: " << packetMap_.size() << endl;
    257258}
    258259
  • code/branches/Masterserver_FS18/src/libraries/network/packet/Packet.h

    r11071 r11842  
    6868
    6969    virtual unsigned char* getData(){ return data_; };
    70     virtual unsigned int getSize() const =0;
    71     virtual bool process(orxonox::Host* host)=0;
     70    virtual unsigned int getSize() const = 0;
     71
     72    // Invoke some sort of action associated with the packet
     73    virtual bool process(orxonox::Host* host) = 0;
     74   
    7275    inline uint32_t getFlags()
    7376      { return flags_; }
     
    8285
    8386    virtual bool send(orxonox::Host* host);
     87
    8488  protected:
    8589    Packet();
    8690    Packet(uint8_t *data, unsigned int peerID);
    87 //    Packet(ENetPacket *packet, ENetPeer *peer);
    8891    inline bool isDataENetAllocated() const
    8992      { return bDataENetAllocated_; }
     
    100103        data_ might no correlate with enetPacket_->data. */
    101104    bool bDataENetAllocated_;
     105
    102106  private:
     107    // All Packets are contained in this map
    103108    static std::map<size_t, Packet *> packetMap_;
    104109    static boost::mutex               packetMapMutex_;
  • code/branches/Masterserver_FS18/src/libraries/network/packet/ServerInformation.cc

    r11083 r11842  
    5050      // Save Server Round Trip Time
    5151      this->serverRTT_ = event->peer->roundTripTime;
     52
    5253      // Save Server Address, leave some space for scope ID
    5354      enet_address_get_host_ip(&event->peer->address, serverIP, 64);
     55
    5456      this->serverIP_ = std::string(serverIP);
    5557      // Save ACK
     
    5759      char* ack = nullptr;
    5860      loadAndIncrease((char*&)ack, temp);
    59 
    60       /* Fabian, what is this used for? it crashes the masterserver, hence commenting it */
    61       // written by Oli: this is just to make sure that loadAndIncrease really writes the whole ACK string into char* ack
    62 //       assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);
    63 
    6461      // Save Server Name
    6562      loadAndIncrease(this->serverName_, temp);
     
    7471    void ServerInformation::send(ENetPeer* peer)
    7572    {
    76       std::string payload = this->serverName_ + Ogre::StringConverter::toString(this->clientNumber_);
     73      std::string payload = this->serverName_ + std::to_string(this->clientNumber_);
    7774      uint32_t size = returnSize(LAN_DISCOVERY_ACK) + returnSize(payload);
    7875      uint8_t* temp = new uint8_t[size];
Note: See TracChangeset for help on using the changeset viewer.