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

◆ NetworkAckField() [2/2]

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

Definition at line 78 of file NetworkAckField.cpp.

78  {
79 
80  // Get data //
81  packet >> FirstPacketID;
82 
83  // Empty ack fields //
84  if(FirstPacketID == 0)
85  return;
86 
87  uint8_t tmpsize = 0;
88 
89  packet >> tmpsize;
90 
91  if(!packet)
92  return;
93 
94  // Fill in the acks from the packet //
95  Acks.resize(tmpsize);
96 
97  for(char i = 0; i < tmpsize; i++){
98  packet >> Acks[i];
99  }
100 }
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 102 of file NetworkAckField.cpp.

102  {
103 
104  packet << FirstPacketID;
105 
106  if(FirstPacketID == 0)
107  return;
108 
109  uint8_t tmpsize = static_cast<uint8_t>(Acks.size());
110  packet << tmpsize;
111 
112  // fill in the ack data //
113  for(uint8_t i = 0; i < tmpsize; i++){
114 
115  packet << Acks[i];
116  }
117 }
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 119 of file NetworkAckField.cpp.

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

◆ 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: