Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 30, 2015, 11:34:25 PM (9 years ago)
Author:
landauf
Message:

made some enums in network library strongly typed. for most other enums in network this isn't possible because they are often used like flags (converted to int and compared with binary operators).
packet::Type now uses uint8_t as underlying type which reduces the network traffic (by default the type was int)

Location:
code/branches/cpp11_v2/src/libraries/network
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v2/src/libraries/network/Connection.cc

    r10919 r11006  
    8181  {
    8282//     this->overallMutex_->lock();
    83     outgoingEvent outEvent = { peerID, outgoingEventType::disconnectPeer, nullptr, 0 };
     83    outgoingEvent outEvent = { peerID, OutgoingEventType::disconnectPeer, nullptr, 0 };
    8484   
    8585    this->outgoingEventsMutex_->lock();
     
    9191  void Connection::disconnectPeers()
    9292  {
    93     outgoingEvent outEvent = { 0, outgoingEventType::disconnectPeers, nullptr, 0 };
     93    outgoingEvent outEvent = { 0, OutgoingEventType::disconnectPeers, nullptr, 0 };
    9494   
    9595    this->outgoingEventsMutex_->lock();
     
    101101  {
    102102//     this->overallMutex_->lock();
    103     outgoingEvent outEvent = { peerID, outgoingEventType::sendPacket, packet, channelID };
     103    outgoingEvent outEvent = { peerID, OutgoingEventType::sendPacket, packet, channelID };
    104104   
    105105    this->outgoingEventsMutex_->lock();
     
    112112  {
    113113//     this->overallMutex_->lock();
    114     outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID };
     114    outgoingEvent outEvent = { 0, OutgoingEventType::broadcastPacket, packet, channelID };
    115115   
    116116    this->outgoingEventsMutex_->lock();
     
    197197    switch( event.type )
    198198    {
    199       case outgoingEventType::sendPacket:
     199      case OutgoingEventType::sendPacket:
    200200        // check whether the peer is still/already in the peer list
    201201        if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
     
    211211        }
    212212        break;
    213       case outgoingEventType::disconnectPeer:
     213      case OutgoingEventType::disconnectPeer:
    214214        if( this->peerMap_.find(event.peerID) != this->peerMap_.end() )
    215215        {
     
    223223        }
    224224        break;
    225       case outgoingEventType::disconnectPeers:
     225      case OutgoingEventType::disconnectPeers:
    226226        disconnectPeersInternal();
    227227        break;
    228       case outgoingEventType::broadcastPacket:
     228      case OutgoingEventType::broadcastPacket:
    229229        enet_host_broadcast( this->host_, event.channelID, event.packet );
    230230        break;
     
    271271      switch( inEvent.type )
    272272      {
    273         case incomingEventType::peerConnect:
     273        case IncomingEventType::peerConnect:
    274274          addPeer(inEvent.peerID);
    275275          break;
    276         case incomingEventType::peerDisconnect:
     276        case IncomingEventType::peerDisconnect:
    277277          removePeer(inEvent.peerID);
    278278          break;
    279         case incomingEventType::receivePacket:
     279        case IncomingEventType::receivePacket:
    280280          processPacket(inEvent.packet);
    281281          break;
     
    322322   
    323323    // create new peerEvent and return it
    324     incomingEvent inEvent = { peerID, incomingEventType::peerConnect, nullptr };
     324    incomingEvent inEvent = { peerID, IncomingEventType::peerConnect, nullptr };
    325325    return inEvent;
    326326  }
     
    337337   
    338338    // create new peerEvent and return it
    339     incomingEvent inEvent = { peerID, incomingEventType::peerDisconnect, nullptr };
     339    incomingEvent inEvent = { peerID, IncomingEventType::peerDisconnect, nullptr };
    340340    return inEvent;
    341341  }
     
    351351   
    352352    // create new peerEvent and return it
    353     incomingEvent inEvent = { peerID, incomingEventType::receivePacket, p };
     353    incomingEvent inEvent = { peerID, IncomingEventType::receivePacket, p };
    354354    return inEvent;
    355355  }
  • code/branches/cpp11_v2/src/libraries/network/Connection.h

    r10774 r11006  
    6060  const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME = 5;
    6161 
    62   namespace incomingEventType
     62  enum class IncomingEventType
    6363  {
    64     enum Value
    65     {
    66       receivePacket   = 1,  // incoming packet
    67       peerConnect     = 2,  // incoming connect request
    68       peerDisconnect  = 3   // incoming disconnect request
    69     };
    70    
    71   }
     64    receivePacket   = 1,  // incoming packet
     65    peerConnect     = 2,  // incoming connect request
     66    peerDisconnect  = 3   // incoming disconnect request
     67  };
    7268 
    73   namespace outgoingEventType
     69  enum class OutgoingEventType
    7470  {
    75     enum Value
    76     {
    77       sendPacket      = 1,  // outgoing packet
    78       broadcastPacket = 2,  // outgoing broadcast packet
    79       disconnectPeer  = 3,  // outgoing disconnect request
    80       disconnectPeers = 4   // outgoing disconnect request
    81     };
    82    
    83   }
     71    sendPacket      = 1,  // outgoing packet
     72    broadcastPacket = 2,  // outgoing broadcast packet
     73    disconnectPeer  = 3,  // outgoing disconnect request
     74    disconnectPeers = 4   // outgoing disconnect request
     75  };
    8476 
    8577  struct _NetworkExport incomingEvent
    8678  {
    87     uint32_t                  peerID;
    88     incomingEventType::Value type;
    89     packet::Packet*           packet;
     79    uint32_t          peerID;
     80    IncomingEventType type;
     81    packet::Packet*   packet;
    9082  };
    9183 
    9284  struct _NetworkExport outgoingEvent
    9385  {
    94     uint32_t                  peerID;
    95     outgoingEventType::Value type;
    96     ENetPacket*               packet;
    97     ENetChannelID             channelID;
     86    uint32_t          peerID;
     87    OutgoingEventType type;
     88    ENetPacket*       packet;
     89    ENetChannelID     channelID;
    9890  };
    9991 
  • code/branches/cpp11_v2/src/libraries/network/NetworkPrereqs.h

    r10994 r11006  
    8989    namespace PacketFlag
    9090    {
    91       enum Value
    92       {
    93         Reliable    = 1,
    94         Unsequenced = 2,
    95         NoAllocate  = 4
    96       };
     91      static constexpr uint32_t Reliable    = 1;
     92      static constexpr uint32_t Unsequenced = 2;
     93      static constexpr uint32_t NoAllocate  = 4;
    9794    }
    9895  }
     
    10198  {
    10299    typedef int Type;
    103     static const Type NETWORK_FUNCTION = 6;
     100    static constexpr Type NETWORK_FUNCTION = 6;
    104101  }
    105102}
  • code/branches/cpp11_v2/src/libraries/network/packet/Acknowledgement.cc

    r8858 r11006  
    3838#define PACKET_FLAGS_ACK    0
    3939#define _PACKETID           0
    40 #define _ACKID              _PACKETID + sizeof(packet::Type::Value)
     40#define _ACKID              _PACKETID + sizeof(packet::Type)
    4141
    4242Acknowledgement::Acknowledgement( unsigned int id, unsigned int peerID )
     
    4545  flags_ = flags_ | PACKET_FLAGS_ACK;
    4646  data_=new uint8_t[ getSize() ];
    47   *(Type::Value *)(data_ + _PACKETID ) = Type::Acknowledgement;
     47  *(Type *)(data_ + _PACKETID ) = Type::Acknowledgement;
    4848  *(uint32_t *)(data_ + _ACKID ) = id;
    4949  peerID_=peerID;
  • code/branches/cpp11_v2/src/libraries/network/packet/Chat.cc

    r8858 r11006  
    4040/* Some lengths */
    4141#define _PACKETID         0
    42 #define _SOURCEID         _PACKETID + sizeof(Type::Value)
     42#define _SOURCEID         _PACKETID + sizeof(Type)
    4343#define _TARGETID         _SOURCEID + sizeof(uint32_t)
    4444#define _MESSAGELENGTH    _TARGETID + sizeof(uint32_t)
     
    5757  data_=new unsigned char[ getSize() ];
    5858
    59   *(Type::Value *)(data_ + _PACKETID ) = Type::Chat;
     59  *(Type *)(data_ + _PACKETID ) = Type::Chat;
    6060  *(unsigned int *)(data_ + _SOURCEID ) = sourceID;
    6161  *(unsigned int *)(data_ + _TARGETID ) = targetID;
  • code/branches/cpp11_v2/src/libraries/network/packet/ClassID.cc

    r10919 r11006  
    7070  //set the appropriate packet id
    7171  assert(this->data_);
    72   *(Type::Value *)(this->data_ + _PACKETID ) = Type::ClassID;
     72  *(Type *)(this->data_ + _PACKETID ) = Type::ClassID;
    7373
    7474  uint8_t *temp=data_+sizeof(uint32_t);
  • code/branches/cpp11_v2/src/libraries/network/packet/DeleteObjects.cc

    r8858 r11006  
    3939#define PACKET_FLAG_DELETE  PacketFlag::Reliable
    4040#define _PACKETID           0
    41 #define _QUANTITY           _PACKETID + sizeof(Type::Value)
     41#define _QUANTITY           _PACKETID + sizeof(Type)
    4242#define _OBJECTIDS          _QUANTITY + sizeof(uint32_t)
    4343
     
    6363    return false;
    6464  orxout(verbose, context::packets) << "sending DeleteObjects: ";
    65   unsigned int size = sizeof(Type::Value) + sizeof(uint32_t)*(number+1);
     65  unsigned int size = sizeof(Type) + sizeof(uint32_t)*(number+1);
    6666  data_ = new uint8_t[size];
    6767  uint8_t *tdata = data_;
    68   *reinterpret_cast<Type::Value*>(tdata) = Type::DeleteObjects;
    69   tdata += sizeof(Type::Value);
     68  *reinterpret_cast<Type*>(tdata) = Type::DeleteObjects;
     69  tdata += sizeof(Type);
    7070  *(uint32_t *)tdata = number;
    7171  tdata += sizeof(uint32_t);
  • code/branches/cpp11_v2/src/libraries/network/packet/FunctionCalls.cc

    r10624 r11006  
    9696  assert(this->functionCalls_.size());
    9797  data_=new uint8_t[ currentSize_ ];
    98   *(Type::Value *)(data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
     98  *(Type *)(data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID
    9999  *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls
    100100  *(uint32_t*)(data_+2*sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_
  • code/branches/cpp11_v2/src/libraries/network/packet/FunctionIDs.cc

    r10769 r11006  
    7171  //set the appropriate packet id
    7272  assert(this->data_);
    73   *(Type::Value *)(this->data_ + _PACKETID ) = Type::FunctionIDs;
     73  *(Type *)(this->data_ + _PACKETID ) = Type::FunctionIDs;
    7474
    7575  uint8_t *temp=data_+sizeof(uint32_t);
  • code/branches/cpp11_v2/src/libraries/network/packet/Gamestate.h

    r10994 r11006  
    5656    GamestateHeader(){ data_=nullptr; }
    5757    GamestateHeader(uint8_t* data)
    58       { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; }
     58      { assert(data); data_ = data; *(Type*)data_ = Type::Gamestate; }
    5959    /*GamestateHeader(uint8_t* data, GamestateHeader* h)
    6060      { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }*/
    6161    void setData(uint8_t* data)
    62       { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; }
     62      { assert(data); data_ = data; *(Type*)data_ = Type::Gamestate; }
    6363    static inline uint32_t getSize()
    6464      { return 21; }
  • code/branches/cpp11_v2/src/libraries/network/packet/Packet.cc

    r10774 r11006  
    157157  }
    158158#ifndef NDEBUG
    159   switch( *(Type::Value *)(data_ + _PACKETID) )
     159  switch( *(Type *)(data_ + _PACKETID) )
    160160  {
    161161    case Type::Acknowledgement:
     
    191191//     peerID = NETWORK_PEER_ID_SERVER;
    192192  Packet *p = nullptr;
    193 //   orxout(verbose_ultra, context::packets) << "packet type: " << *(Type::Value *)&data[_PACKETID] << endl;
    194   switch( *(Type::Value *)(data + _PACKETID) )
     193//   orxout(verbose_ultra, context::packets) << "packet type: " << *(Type *)&data[_PACKETID] << endl;
     194  switch( *(Type *)(data + _PACKETID) )
    195195  {
    196196    case Type::Acknowledgement:
  • code/branches/cpp11_v2/src/libraries/network/packet/Packet.h

    r8327 r11006  
    3838{
    3939
    40 namespace Direction
     40enum class Direction
    4141{
    42   enum Value
    43   {
    44     Incoming,
    45     Outgoing,
    46     Bidirectional
    47   };
    48 }
    49 namespace Type
     42  Incoming,
     43  Outgoing,
     44  Bidirectional
     45};
     46enum class Type : uint8_t
    5047{
    51   enum Value
    52   {
    53     Acknowledgement,
    54     Chat,
    55     ClassID,
    56     DeleteObjects,
    57     FunctionIDs,
    58     FunctionCalls,
    59     Gamestate,
    60     Welcome
    61   };
    62 }
     48  Acknowledgement,
     49  Chat,
     50  ClassID,
     51  DeleteObjects,
     52  FunctionIDs,
     53  FunctionCalls,
     54  Gamestate,
     55  Welcome
     56};
    6357
    6458/**
     
    9892    unsigned int peerID_;
    9993    uint32_t requiredGamestateID_;
    100     Direction::Value packetDirection_;
     94    Direction packetDirection_;
    10195    /** Pointer to the data. Be careful when deleting it because it might
    10296        point to a location that was allocated by ENet.
  • code/branches/cpp11_v2/src/libraries/network/packet/Welcome.cc

    r8858 r11006  
    4141#define PACKET_FLAGS_CLASSID  PacketFlag::Reliable
    4242#define _PACKETID             0
    43 #define _CLIENTID             _PACKETID + sizeof(Type::Value)
     43#define _CLIENTID             _PACKETID + sizeof(Type)
    4444#define _ENDIANTEST           _CLIENTID + sizeof(uint32_t)
    4545
     
    5151  data_=new uint8_t[ getSize() ];
    5252  assert(data_);
    53   *(packet::Type::Value *)(data_ + _PACKETID ) = packet::Type::Welcome;
     53  *(packet::Type *)(data_ + _PACKETID ) = packet::Type::Welcome;
    5454  *(uint32_t *)(data_ + _CLIENTID ) = static_cast<uint32_t>(clientID);
    5555  *(uint32_t *)(data_ + _ENDIANTEST ) = 0xFEDC4321;
     
    7070
    7171unsigned int Welcome::getSize() const{
    72   return sizeof(packet::Type::Value) + 2*sizeof(uint32_t);
     72  return sizeof(packet::Type) + 2*sizeof(uint32_t);
    7373}
    7474
Note: See TracChangeset for help on using the changeset viewer.