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/packet
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • 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.