19inline ProtocolPacket::ProtocolPacket(
Packet const& pck,
21 std::size_t fluxIndex,
22 std::size_t fluxLifetime) :
27 g_fluxIndex(fluxIndex),
28 g_fluxLifetime(fluxLifetime)
30inline ProtocolPacket::ProtocolPacket(Packet&& pck,
32 std::size_t fluxIndex,
33 std::size_t fluxLifetime) :
34 Packet(std::move(pck)),
38 g_fluxIndex(fluxIndex),
39 g_fluxLifetime(fluxLifetime)
41inline ProtocolPacket::ProtocolPacket(IdType header, RealmType realmId, CounterType countId, CounterType lastCountId)
43 this->operator<<(header) << realmId << countId << lastCountId;
46inline ProtocolPacket::ProtocolPacket(Packet
const& r) :
49inline ProtocolPacket::ProtocolPacket(Packet&& r) noexcept :
53inline ProtocolPacket::ProtocolPacket(ProtocolPacket
const& r) :
55 std::enable_shared_from_this<ProtocolPacket>(r),
57 g_identity(r.g_identity),
58 g_timestamp(r.g_timestamp),
60 g_fluxIndex(r.g_fluxIndex),
61 g_fluxLifetime(r.g_fluxLifetime),
63 g_markedForEncryption(r.g_markedForEncryption),
64 g_markedAsLocallyReordered(r.g_markedAsLocallyReordered),
65 g_markedAsCached(r.g_markedAsCached),
67 g_options(r.g_options)
69inline ProtocolPacket::ProtocolPacket(ProtocolPacket&& r) noexcept :
71 std::enable_shared_from_this<ProtocolPacket>(std::move(r)),
73 g_identity(r.g_identity),
74 g_timestamp(r.g_timestamp),
76 g_fluxIndex(r.g_fluxIndex),
77 g_fluxLifetime(r.g_fluxLifetime),
79 g_markedForEncryption(r.g_markedForEncryption),
80 g_markedAsLocallyReordered(r.g_markedAsLocallyReordered),
81 g_markedAsCached(r.g_markedAsCached),
83 g_options(std::move(r.g_options))
86inline bool ProtocolPacket::haveCorrectHeader()
const
88 if (this->haveCorrectHeaderSize())
91 this->unpack(IdPosition, &headerId,
sizeof(IdType));
93 if ((headerId & ~FGE_NET_HEADER_FLAGS_MASK) == FGE_NET_BAD_ID)
101inline bool ProtocolPacket::haveCorrectHeaderSize()
const
103 return this->getDataSize() >= HeaderSize;
105inline std::optional<ProtocolPacket::IdType> ProtocolPacket::retrieveHeaderId()
const
107 if (this->haveCorrectHeaderSize())
110 this->unpack(IdPosition, &headerId,
sizeof(IdType));
111 return headerId & ~FGE_NET_HEADER_FLAGS_MASK;
115inline std::optional<ProtocolPacket::IdType> ProtocolPacket::retrieveFlags()
const
117 if (this->haveCorrectHeaderSize())
120 this->unpack(IdPosition, &headerFlags,
sizeof(IdType));
121 return headerFlags & FGE_NET_HEADER_FLAGS_MASK;
125inline std::optional<ProtocolPacket::IdType> ProtocolPacket::retrieveFullHeaderId()
const
127 if (this->haveCorrectHeaderSize())
130 this->unpack(IdPosition, &headerId,
sizeof(IdType));
135inline std::optional<ProtocolPacket::RealmType> ProtocolPacket::retrieveRealm()
const
137 if (this->haveCorrectHeaderSize())
140 this->unpack(RealmPosition, &realm,
sizeof(RealmType));
145inline std::optional<ProtocolPacket::CounterType> ProtocolPacket::retrieveCounter()
const
147 if (this->haveCorrectHeaderSize())
150 this->unpack(CounterPosition, &counter,
sizeof(CounterType));
155inline std::optional<ProtocolPacket::CounterType> ProtocolPacket::retrieveLastCounter()
const
157 if (this->haveCorrectHeaderSize())
160 this->unpack(LastCounterPosition, &counter,
sizeof(CounterType));
165inline std::optional<ProtocolPacket::Header> ProtocolPacket::retrieveHeader()
const
167 if (this->haveCorrectHeaderSize())
170 this->unpack(IdPosition, &header._id,
sizeof(IdType));
171 this->unpack(RealmPosition, &header._realm,
sizeof(RealmType));
172 this->unpack(CounterPosition, &header._counter,
sizeof(CounterType));
173 this->unpack(LastCounterPosition, &header._lastCounter,
sizeof(CounterType));
178inline bool ProtocolPacket::isFragmented()
const
180 return this->retrieveHeaderId().value_or(FGE_NET_BAD_ID) == NET_INTERNAL_ID_FRAGMENTED_PACKET;
183inline ProtocolPacket& ProtocolPacket::setHeader(Header
const& header)
185 if (!this->haveCorrectHeaderSize())
187 this->append(HeaderSize - this->getDataSize());
190 this->pack(IdPosition, &header._id,
sizeof(IdType));
191 this->pack(RealmPosition, &header._realm,
sizeof(RealmType));
192 this->pack(CounterPosition, &header._counter,
sizeof(CounterType));
193 this->pack(LastCounterPosition, &header._lastCounter,
sizeof(CounterType));
196inline ProtocolPacket& ProtocolPacket::setHeaderId(IdType
id)
198 if (this->haveCorrectHeaderSize())
201 this->unpack(IdPosition, &headerFlags,
sizeof(IdType));
202 id = (headerFlags & FGE_NET_HEADER_FLAGS_MASK) | (
id & ~FGE_NET_HEADER_FLAGS_MASK);
203 this->pack(IdPosition, &
id,
sizeof(IdType));
208inline ProtocolPacket& ProtocolPacket::setFlags(IdType flags)
210 if (this->haveCorrectHeaderSize())
213 this->unpack(IdPosition, &headerId,
sizeof(IdType));
214 headerId = (headerId & ~FGE_NET_HEADER_FLAGS_MASK) | (flags & FGE_NET_HEADER_FLAGS_MASK);
215 this->pack(IdPosition, &headerId,
sizeof(IdType));
219inline ProtocolPacket& ProtocolPacket::addFlags(IdType flags)
221 if (this->haveCorrectHeaderSize())
224 this->unpack(IdPosition, &headerId,
sizeof(IdType));
225 headerId |= flags & FGE_NET_HEADER_FLAGS_MASK;
226 this->pack(IdPosition, &headerId,
sizeof(IdType));
230inline ProtocolPacket& ProtocolPacket::removeFlags(IdType flags)
232 if (this->haveCorrectHeaderSize())
235 this->unpack(IdPosition, &headerId,
sizeof(IdType));
236 headerId &= ~(flags & FGE_NET_HEADER_FLAGS_MASK);
237 this->pack(IdPosition, &headerId,
sizeof(IdType));
241inline bool ProtocolPacket::checkFlags(IdType flags)
const
243 if (this->haveCorrectHeaderSize())
246 this->unpack(IdPosition, &headerId,
sizeof(IdType));
247 return ((headerId & FGE_NET_HEADER_FLAGS_MASK) & (flags & FGE_NET_HEADER_FLAGS_MASK)) > 0;
251inline ProtocolPacket& ProtocolPacket::doNotDiscard()
253 return this->addFlags(FGE_NET_HEADER_DO_NOT_DISCARD_FLAG);
255inline ProtocolPacket& ProtocolPacket::doNotReorder()
257 return this->addFlags(FGE_NET_HEADER_DO_NOT_REORDER_FLAG);
259inline ProtocolPacket& ProtocolPacket::doNotFragment()
261 return this->addFlags(FGE_NET_HEADER_DO_NOT_FRAGMENT_FLAG);
264inline ProtocolPacket& ProtocolPacket::setRealm(RealmType realm)
266 this->pack(RealmPosition, &realm,
sizeof(RealmType));
269inline ProtocolPacket& ProtocolPacket::setCounter(CounterType counter)
271 this->pack(CounterPosition, &counter,
sizeof(CounterType));
274inline ProtocolPacket& ProtocolPacket::setLastReorderedPacketCounter(CounterType counter)
276 this->pack(LastCounterPosition, &counter,
sizeof(CounterType));
280inline void ProtocolPacket::setTimestamp(Timestamp timestamp)
282 this->g_timestamp = timestamp;
284inline Timestamp ProtocolPacket::getTimeStamp()
const
286 return this->g_timestamp;
288inline Identity
const& ProtocolPacket::getIdentity()
const
290 return this->g_identity;
293inline std::vector<ProtocolPacket::Option>
const& ProtocolPacket::options()
const
295 return this->g_options;
297inline std::vector<ProtocolPacket::Option>& ProtocolPacket::options()
299 return this->g_options;
302inline void ProtocolPacket::markForEncryption()
304 this->g_markedForEncryption =
true;
306inline void ProtocolPacket::unmarkForEncryption()
308 this->g_markedForEncryption =
false;
310inline bool ProtocolPacket::isMarkedForEncryption()
const
312 return this->g_markedForEncryption;
315inline void ProtocolPacket::markAsLocallyReordered()
317 this->g_markedAsLocallyReordered =
true;
319inline void ProtocolPacket::unmarkAsLocallyReordered()
321 this->g_markedAsLocallyReordered =
false;
323inline bool ProtocolPacket::isMarkedAsLocallyReordered()
const
325 return this->g_markedAsLocallyReordered;
328inline void ProtocolPacket::markAsCached()
330 this->g_markedAsCached =
true;
332inline void ProtocolPacket::unmarkAsCached()
334 this->g_markedAsCached =
false;
336inline bool ProtocolPacket::isMarkedAsCached()
const
338 return this->g_markedAsCached;
343 if (++this->g_fluxLifetime >= fluxSize)
347 this->g_fluxIndex = (this->g_fluxIndex + 1) % fluxSize;
350inline std::size_t ProtocolPacket::getFluxIndex()
const
352 return this->g_fluxIndex;
354inline std::size_t ProtocolPacket::bumpFluxIndex(std::size_t fluxSize)
356 this->g_fluxIndex = (this->g_fluxIndex + 1) % fluxSize;
357 return this->g_fluxIndex;
Definition C_packet.hpp:52
bool checkFluxLifetime(std::size_t fluxSize)
Check if the flux lifetime is reached.
Definition C_protocol.inl:341
uint16_t Timestamp
An timestamp represent modulated current time in milliseconds.
Definition C_client.hpp:62
A class to represent a client or server identity with an IP address and a port.
Definition C_identity.hpp:31