FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_protocol.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_PROTOCOL_HPP_INCLUDED
18#define _FGE_C_PROTOCOL_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/network/C_packet.hpp"
22#include <memory>
23#include <optional>
24#include <queue>
25#include <vector>
26
27#define FGE_NET_HEADER_TYPE uint16_t
28
29#define FGE_NET_HEADERID_MAX 0x1FFE
30#define FGE_NET_HEADERID_START 1
31#define FGE_NET_BAD_HEADERID 0
32
33#define FGE_NET_HEADER_DO_NOT_DISCARD_FLAG 0x8000
34#define FGE_NET_HEADER_DO_NOT_REORDER_FLAG 0x4000
35#define FGE_NET_HEADER_LOCAL_REORDERED_FLAG 0x2000
36#define FGE_NET_HEADER_FLAGS_MASK 0xE000
37
38#define FGE_NET_DEFAULT_REALM 0
39#define FGE_NET_PACKET_REORDERER_CACHE_MAX 5
40
41namespace fge::net
42{
43
51class ProtocolPacket : public Packet
52{
53public:
54 using Header = FGE_NET_HEADER_TYPE;
55 using Realm = uint8_t;
56 using CountId = uint16_t;
57 constexpr static std::size_t HeaderSize = sizeof(Header) + sizeof(Realm) + sizeof(CountId);
58 constexpr static std::size_t HeaderIdPosition = 0;
59 constexpr static std::size_t RealmPosition = sizeof(Header);
60 constexpr static std::size_t CountIdPosition = sizeof(Header) + sizeof(Realm);
61
62 inline ProtocolPacket(Header header, Realm realmId, CountId countId);
63 inline ProtocolPacket(ProtocolPacket const& r) = default;
64 inline ProtocolPacket(Packet const& r) :
65 Packet(r)
66 {}
67 inline ProtocolPacket(ProtocolPacket&& r) noexcept = default;
68 inline ProtocolPacket(Packet&& r) noexcept :
69 Packet(std::move(r))
70 {}
71 inline ~ProtocolPacket() override = default;
72
73 [[nodiscard]] inline Packet& packet() noexcept { return *this; }
74 [[nodiscard]] inline Packet const& packet() const noexcept { return *this; }
75
76 [[nodiscard]] inline bool haveCorrectHeaderSize() const;
77 [[nodiscard]] inline std::optional<Header> retrieveHeader() const;
78 [[nodiscard]] inline std::optional<Header> retrieveHeaderId() const;
79 [[nodiscard]] inline std::optional<Realm> retrieveRealm() const;
80 [[nodiscard]] inline std::optional<CountId> retrieveCountId() const;
81
82 inline void setHeader(Header header);
83 inline void setHeaderId(Header headerId);
84 inline void setHeaderFlags(Header headerFlags);
85 inline void addHeaderFlags(Header headerFlags);
86 inline void removeHeaderFlags(Header headerFlags);
87 inline void setRealm(Realm realmId);
88 inline void setCountId(CountId countId);
89};
90
91class FluxPacket;
92using FluxPacketPtr = std::unique_ptr<FluxPacket>;
93
101class FGE_API PacketReorderer
102{
103public:
104 enum class Stats
105 {
106 OLD_REALM,
107 OLD_COUNTID,
108 WAITING_NEXT_REALM,
109 WAITING_NEXT_COUNTID,
110 RETRIEVABLE
111 };
112
113 PacketReorderer() = default;
114 PacketReorderer(PacketReorderer const& r) = delete;
115 PacketReorderer(PacketReorderer&& r) noexcept = default;
116 ~PacketReorderer() = default;
117
118 PacketReorderer& operator=(PacketReorderer const& r) = delete;
119 PacketReorderer& operator=(PacketReorderer&& r) noexcept = default;
120
121 void clear();
122
123 void push(FluxPacketPtr&& fluxPacket);
124 [[nodiscard]] static Stats checkStat(FluxPacketPtr const& fluxPacket,
125 ProtocolPacket::CountId currentCountId,
126 ProtocolPacket::Realm currentRealm);
127 [[nodiscard]] bool isForced() const;
128 [[nodiscard]] std::optional<Stats> checkStat(ProtocolPacket::CountId currentCountId,
129 ProtocolPacket::Realm currentRealm) const;
130 [[nodiscard]] FluxPacketPtr pop();
131
132 [[nodiscard]] bool isEmpty() const;
133
134private:
135 struct FGE_API Data
136 {
137 explicit Data(FluxPacketPtr&& fluxPacket);
138 Data(Data const& r) = delete;
139 Data(Data&& r) noexcept;
140 ~Data();
141
142 Data& operator=(Data const& r) = delete;
143 Data& operator=(Data&& r) noexcept;
144
145 [[nodiscard]] Stats checkStat(ProtocolPacket::CountId currentCountId, ProtocolPacket::Realm currentRealm) const;
146
147 FluxPacketPtr _fluxPacket;
148 ProtocolPacket::CountId _countId;
149 ProtocolPacket::Realm _realm;
150
151 struct Compare
152 {
153 [[nodiscard]] constexpr bool operator()(Data const& l, Data const& r) const
154 {
155 if (l._realm == r._realm)
156 {
157 return l._countId > r._countId;
158 }
159 return l._realm > r._realm;
160 }
161 };
162 };
163
164 std::priority_queue<Data, std::vector<Data>, Data::Compare> g_cache;
165 bool g_forceRetrieve{false};
166};
167
168} // namespace fge::net
169
170#include "C_protocol.inl"
171
172#endif // _FGE_C_PROTOCOL_HPP_INCLUDED
A received packet from a network flux.
Definition C_server.hpp:59
A packet reorderer.
Definition C_protocol.hpp:102
Definition C_packet.hpp:70
A special inheritance of Packet with a predefined communication protocol.
Definition C_protocol.hpp:52
Definition C_protocol.hpp:152