Leviathan  0.8.0.0
Leviathan game engine
Leviathan::NetworkAckField Class Reference

#include <NetworkAckField.h>

Public Types

using PacketReceiveStatus = std::map< uint32_t, RECEIVED_STATE >
 

Public Member Functions

DLLEXPORT NetworkAckField (uint32_t firstpacketid, uint8_t maxacks, PacketReceiveStatus &copyfrom)
 Copies acts from copyfrom starting with the number firstpacketid. More...
 
DLLEXPORT NetworkAckField (sf::Packet &packet)
 
DLLEXPORT void AddDataToPacket (sf::Packet &packet) const
 
bool IsAckSet (uint8_t ackindex) const
 
DLLEXPORT void InvokeForEachAck (std::function< void(uint32_t)> func) const
 Calls func with each set ack. More...
 

Public Attributes

uint32_t FirstPacketID
 
std::vector< uint8_t > Acks
 

Detailed Description

Definition at line 39 of file NetworkAckField.h.

Member Typedef Documentation

◆ PacketReceiveStatus

Definition at line 42 of file NetworkAckField.h.

Constructor & Destructor Documentation

◆ NetworkAckField() [1/2]

DLLEXPORT Leviathan::NetworkAckField::NetworkAckField ( uint32_t  firstpacketid,
uint8_t  maxacks,
PacketReceiveStatus copyfrom 
)

Copies acts from copyfrom starting with the number firstpacketid.

Marks them as RECEIVED_STATE::AcksSent

Definition at line 9 of file NetworkAckField.cpp.

10  :
11  FirstPacketID(firstpacketid)
12 {
13  // Id is 0 nothing should be copied //
14  if(FirstPacketID == 0)
15  return;
16 
17  // Before we reach the first packet id we will want to skip stuff //
18  bool foundstart = false;
19 
20  for(auto iter = copyfrom.begin(); iter != copyfrom.end(); ++iter) {
21 
22  // Skip current if not received //
23  if(iter->second == RECEIVED_STATE::NotReceived)
24  continue;
25 
26  if(!foundstart) {
27 
28  if(iter->first >= FirstPacketID) {
29 
30 
31  // Adjust the starting index, if it is an exact match for firstpacketid
32  // this should be 0
33  auto startindex = iter->first - FirstPacketID;
34 
35  // Check that the index is within the size of the field, otherwise fail
36  if(startindex >= maxacks)
37  break;
38 
39  foundstart = true;
40 
41  } else {
42 
43  continue;
44  }
45  }
46 
47  size_t currentindex = iter->first - FirstPacketID;
48 
49  if(currentindex >= maxacks)
50  break;
51 
52  // Copy current ack //
53  const auto vecelement = currentindex / 8;
54 
55  while(Acks.size() <= vecelement) {
56 
57  Acks.push_back(0);
58  }
59 
60  Acks[vecelement] |= (1 << (currentindex % 8));
61 
62  // Stop copying once enough acks have been set. The loop can end before this, but
63  // that just leaves the rest of the acks as 0
64  if(currentindex + 1 >= maxacks)
65  break;
66  }
67 
68  if(!foundstart) {
69 
70  // No acks to send //
71  FirstPacketID = 0;
72  return;
73  }
74 }
std::vector< uint8_t > Acks
Packet hasn't been received.

◆ NetworkAckField() [2/2]

DLLEXPORT NetworkAckField::NetworkAckField ( sf::Packet &  packet)

Definition at line 76 of file NetworkAckField.cpp.

77 {
78  // Get data //
79  packet >> FirstPacketID;
80 
81  // Empty ack fields //
82  if(FirstPacketID == 0)
83  return;
84 
85  uint8_t tmpsize = 0;
86 
87  packet >> tmpsize;
88 
89  if(!packet)
90  return;
91 
92  // Fill in the acks from the packet //
93  Acks.resize(tmpsize);
94 
95  for(char i = 0; i < tmpsize; i++) {
96  packet >> Acks[i];
97  }
98 }
std::vector< uint8_t > Acks
unsigned char uint8_t
Definition: core.h:38

Member Function Documentation

◆ AddDataToPacket()

DLLEXPORT void NetworkAckField::AddDataToPacket ( sf::Packet &  packet) const

Definition at line 100 of file NetworkAckField.cpp.

101 {
102  packet << FirstPacketID;
103 
104  if(FirstPacketID == 0)
105  return;
106 
107  uint8_t tmpsize = static_cast<uint8_t>(Acks.size());
108  packet << tmpsize;
109 
110  // fill in the ack data //
111  for(uint8_t i = 0; i < tmpsize; i++) {
112 
113  packet << Acks[i];
114  }
115 }
std::vector< uint8_t > Acks
unsigned char uint8_t
Definition: core.h:38

◆ InvokeForEachAck()

DLLEXPORT void NetworkAckField::InvokeForEachAck ( std::function< void(uint32_t)>  func) const

Calls func with each set ack.

Definition at line 117 of file NetworkAckField.cpp.

118 {
119  const auto size = static_cast<uint32_t>(Acks.size());
120  for(uint32_t i = 0; i < size; ++i) {
121 
122  for(uint8_t bit = 0; bit < 8; ++bit) {
123 
124  const auto id = (i * 8) + bit + FirstPacketID;
125 
126  if(Acks[i] & (1 << bit))
127  func(id);
128  }
129  }
130 }
std::vector< uint8_t > Acks
unsigned char uint8_t
Definition: core.h:38
unsigned int uint32_t
Definition: core.h:40

◆ IsAckSet()

bool Leviathan::NetworkAckField::IsAckSet ( uint8_t  ackindex) const
inline
Returns
True if an ack number FirstPacketID + ackindex is set

Definition at line 56 of file NetworkAckField.h.

56  {
57 
58  // We can use division to find out which vector element is wanted //
59  size_t vecelement = ackindex / 8;
60 
61  if (vecelement >= Acks.size())
62  return false;
63 
64  return (Acks[vecelement] & (1 << (ackindex % 8))) != 0;
65  }
std::vector< uint8_t > Acks

Member Data Documentation

◆ Acks

std::vector<uint8_t> Leviathan::NetworkAckField::Acks

Definition at line 72 of file NetworkAckField.h.

◆ FirstPacketID

uint32_t Leviathan::NetworkAckField::FirstPacketID

Definition at line 71 of file NetworkAckField.h.


The documentation for this class was generated from the following files: