FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
network_manager.hpp
1/*
2 * Copyright 2025 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_NETWORK_MANAGER_HPP_INCLUDED
18#define _FGE_NETWORK_MANAGER_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_scene.hpp"
22#include "FastEngine/network/C_packet.hpp"
23#include <optional>
24#include <variant>
25
26namespace fge::net
27{
28
33
42FGE_API uint32_t GetSceneChecksum(fge::Scene& scene);
43
51FGE_API bool WritePacketDataToFile(Packet& pck, std::string const& file);
52
56
57namespace rules
58{
59
72template<class TValue>
73class ChainedArguments
74{
75public:
76 constexpr ChainedArguments(Packet const& pck, TValue* existingValue = nullptr);
77 constexpr ChainedArguments(Packet const& pck, Error&& err, TValue* existingValue = nullptr);
78 constexpr ChainedArguments(ChainedArguments const& r) = default;
79 constexpr ChainedArguments(ChainedArguments&& r) noexcept = default;
80
81 constexpr ChainedArguments& operator=(ChainedArguments const& r) = default;
82 constexpr ChainedArguments& operator=(ChainedArguments&& r) noexcept = default;
83
91 [[nodiscard]] constexpr TValue* extract();
98 template<class TPeek>
99 [[nodiscard]] constexpr std::optional<TPeek> peek();
100
101 [[nodiscard]] constexpr operator Packet const&() const;
102 [[nodiscard]] constexpr Packet const& packet() const;
103 [[nodiscard]] constexpr TValue const& value() const;
104 [[nodiscard]] constexpr TValue& value();
105
115 template<class TInvokable>
116 [[nodiscard]] constexpr typename std::invoke_result_t<TInvokable, ChainedArguments<TValue>&>
117 and_then(TInvokable&& f);
131 template<class TInvokable, class TIndex>
132 [[nodiscard]] constexpr ChainedArguments<TValue>&
133 and_for_each(TIndex iStart, TIndex iEnd, TIndex iIncrement, TInvokable&& f);
148 template<class TInvokable, class TIndex>
149 [[nodiscard]] constexpr ChainedArguments<TValue>& and_for_each(TIndex iStart, TIndex iIncrement, TInvokable&& f);
161 template<class TInvokable>
162 [[nodiscard]] constexpr ChainedArguments<TValue>& and_for_each(TInvokable&& f);
173 template<class TInvokable>
174 constexpr std::optional<Error> on_error(TInvokable&& f);
180 [[nodiscard]] constexpr std::optional<Error> end();
188 [[nodiscard]] constexpr std::optional<Error> final();
196 [[nodiscard]] constexpr std::optional<Error> skip() const;
204 [[nodiscard]] constexpr std::optional<Error> stop(char const* error, char const* function) const;
205
216 constexpr ChainedArguments<TValue>& apply(TValue& value);
217 template<class TInvokable>
225 constexpr ChainedArguments<TValue>& apply(TInvokable&& f);
226
233 constexpr ChainedArguments<TValue>& setError(Error&& err);
240 constexpr ChainedArguments<TValue>& invalidate(Error&& err);
241 constexpr ChainedArguments<TValue>& invalidate(char const* error, char const* function);
242
243private:
244 using Value = std::variant<TValue, TValue*>;
245
246 Packet const* g_pck;
247 Value g_value;
248 Error g_error;
249};
250
255
256enum class ROutputs : bool
257{
258 R_NORMAL = false,
259 R_INVERTED = true
260};
261
274template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
275constexpr ChainedArguments<TValue> RRange(TValue const& min, TValue const& max, ChainedArguments<TValue>&& args);
276template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
277constexpr ChainedArguments<TValue>
278RRange(TValue const& min, TValue const& max, Packet const& pck, TValue* existingValue = nullptr)
279{
280 return RRange<TValue, TOutput>(min, max, ChainedArguments<TValue>{pck, existingValue});
281}
282
290template<class TValue>
291constexpr ChainedArguments<TValue> RValid(ChainedArguments<TValue>&& args);
292template<class TValue>
293constexpr ChainedArguments<TValue> RValid(Packet const& pck, TValue* existingValue = nullptr)
294{
295 return RValid<TValue>(ChainedArguments<TValue>{pck, existingValue});
296}
297
307template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
308constexpr ChainedArguments<TValue> RMustEqual(TValue const& a, ChainedArguments<TValue>&& args);
309template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
310constexpr ChainedArguments<TValue> RMustEqual(TValue const& a, Packet const& pck, TValue* existingValue = nullptr)
311{
312 return RMustEqual<TValue, TOutput>(a, ChainedArguments<TValue>{pck, existingValue});
313}
314
324template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
326template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
327constexpr ChainedArguments<TValue> RStrictLess(TValue less, Packet const& pck, TValue* existingValue = nullptr)
328{
329 return RStrictLess<TValue, TOutput>(less, ChainedArguments<TValue>{pck, existingValue});
330}
331
341template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
342constexpr ChainedArguments<TValue> RLess(TValue less, ChainedArguments<TValue>&& args);
343template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
344constexpr ChainedArguments<TValue> RLess(TValue less, Packet const& pck, TValue* existingValue = nullptr)
345{
346 return RLess<TValue, TOutput>(less, ChainedArguments<TValue>{pck, existingValue});
347}
348
365template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
366constexpr ChainedArguments<TValue> RSizeRange(SizeType min, SizeType max, ChainedArguments<TValue>&& args);
367template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
369RSizeRange(SizeType min, SizeType max, Packet const& pck, TValue* existingValue = nullptr)
370{
371 return RSizeRange<TValue, TOutput>(min, max, ChainedArguments<TValue>{pck, existingValue});
372}
373template<class TString>
375RStringRange(SizeType min, SizeType max, Packet const& pck, TString* existingValue = nullptr)
376{
377 return RValid(RSizeRange<TString, ROutputs::R_NORMAL>(min, max, ChainedArguments<TString>{pck, existingValue}));
378}
379
391template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
393template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
394constexpr ChainedArguments<TValue> RSizeMustEqual(SizeType a, Packet const& pck, TValue* existingValue = nullptr)
395{
396 return RSizeMustEqual<TValue, TOutput>(a, ChainedArguments<TValue>{pck, existingValue});
397}
398template<class TString>
399constexpr ChainedArguments<TString> RStringMustEqual(SizeType a, Packet const& pck, TString* existingValue = nullptr)
400{
402}
403
415template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
417template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
418constexpr ChainedArguments<TValue> RMustValidUtf8(Packet const& pck, TValue* existingValue = nullptr)
419{
421}
422
426
427} // namespace rules
428} // namespace fge::net
429
430#include "network_manager.inl"
431
432#endif // _FGE_NETWORK_MANAGER_HPP_INCLUDED
A scene contain a collection of object and handle them.
Definition C_scene.hpp:465
Definition C_packet.hpp:52
This is a wrapper around a Packet and a value for safe extraction.
Definition network_manager.hpp:74
constexpr ChainedArguments< TValue > & setError(Error &&err)
Set the error.
Definition network_manager.inl:241
constexpr std::optional< TPeek > peek()
Peek without changing the read position a copy of value.
Definition network_manager.inl:53
constexpr std::optional< Error > skip() const
Helper to "continue" in a and_for_each() loop without returning an error.
Definition network_manager.inl:199
constexpr ChainedArguments< TValue > & apply(TValue &value)
Apply the extracted value to the provided reference.
Definition network_manager.inl:210
constexpr TValue * extract()
Extract and verify the value from the packet.
Definition network_manager.inl:38
constexpr std::invoke_result_t< TInvokable, ChainedArguments< TValue > & > and_then(TInvokable &&f)
Chain up some code after a successful extraction.
Definition network_manager.inl:96
constexpr ChainedArguments< TValue > & and_for_each(TIndex iStart, TIndex iEnd, TIndex iIncrement, TInvokable &&f)
Chain up some code in a for loop after a successful extraction.
Definition network_manager.inl:107
constexpr std::optional< Error > stop(char const *error, char const *function) const
Helper to "break" in a and_for_each() loop with a custom error.
Definition network_manager.inl:204
constexpr std::optional< Error > on_error(TInvokable &&f)
Chain up some code after an unsuccessful extraction.
Definition network_manager.inl:178
constexpr std::optional< Error > end()
End the chain by doing a last validity check on the packet.
Definition network_manager.inl:188
constexpr ChainedArguments< TValue > & invalidate(Error &&err)
Invalidate the packet and set the error.
Definition network_manager.inl:247
constexpr ChainedArguments< TValue > RRange(TValue const &min, TValue const &max, ChainedArguments< TValue > &&args)
Range rule, check if the value is in the min/max range.
Definition network_manager.inl:262
constexpr ChainedArguments< TValue > RMustValidUtf8(ChainedArguments< TValue > &&args)
Check if the extracted string is a valid UTF8 string.
Definition network_manager.inl:378
constexpr ChainedArguments< TValue > RStrictLess(TValue less, ChainedArguments< TValue > &&args)
Strict less rule, check if the value is strictly lesser than the provided one.
Definition network_manager.inl:310
constexpr ChainedArguments< TValue > RMustEqual(TValue const &a, ChainedArguments< TValue > &&args)
Must equal rule, check if the value is equal to the provided one.
Definition network_manager.inl:293
constexpr ChainedArguments< TValue > RSizeMustEqual(SizeType a, ChainedArguments< TValue > &&args)
Size must equal rule, check if the size is equal to the provided one.
Definition network_manager.inl:361
constexpr ChainedArguments< TValue > RSizeRange(SizeType min, SizeType max, ChainedArguments< TValue > &&args)
Size range rule, check if the size is in the min/max range.
Definition network_manager.inl:344
constexpr ChainedArguments< TValue > RValid(ChainedArguments< TValue > &&args)
Valid rule, check if the value is correctly extracted.
Definition network_manager.inl:279
constexpr ChainedArguments< TValue > RLess(TValue less, ChainedArguments< TValue > &&args)
Less rule, check if the value is lesser than the provided one.
Definition network_manager.inl:327
FGE_API bool WritePacketDataToFile(Packet &pck, std::string const &file)
Utility function to write packet data into a file.
FGE_API uint32_t GetSceneChecksum(fge::Scene &scene)
Get a basic scene checksum.
Definition C_error.hpp:28