FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
network_manager.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_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_client.hpp"
23#include "FastEngine/network/C_packet.hpp"
24#include "FastEngine/object/C_object.hpp"
25#include <optional>
26#include <variant>
27
28namespace fge::net
29{
30
44FGE_API uint32_t GetSceneChecksum(fge::Scene& scene);
45
53FGE_API bool WritePacketDataToFile(fge::net::Packet& pck, std::string const& file);
54
62inline bool CheckSkey(fge::net::Packet& pck, fge::net::Skey skey);
70
75namespace rules
76{
77
90template<class TValue>
92{
93public:
94 constexpr ChainedArguments(fge::net::Packet const& pck, TValue* existingValue = nullptr);
95 constexpr ChainedArguments(fge::net::Packet const& pck, Error&& err, TValue* existingValue = nullptr);
96 constexpr ChainedArguments(ChainedArguments const& r) = default;
97 constexpr ChainedArguments(ChainedArguments&& r) noexcept = default;
98
99 constexpr ChainedArguments& operator=(ChainedArguments const& r) = default;
100 constexpr ChainedArguments& operator=(ChainedArguments&& r) noexcept = default;
101
109 [[nodiscard]] constexpr TValue* extract();
116 template<class TPeek>
117 [[nodiscard]] constexpr std::optional<TPeek> peek();
118
119 [[nodiscard]] constexpr fge::net::Packet const& packet() const;
120 [[nodiscard]] constexpr TValue const& value() const;
121 [[nodiscard]] constexpr TValue& value();
122
132 template<class TInvokable>
133 [[nodiscard]] constexpr typename std::invoke_result_t<TInvokable, ChainedArguments<TValue>&>
134 and_then(TInvokable&& f);
148 template<class TInvokable, class TIndex>
149 [[nodiscard]] constexpr ChainedArguments<TValue>&
150 and_for_each(TIndex iStart, TIndex iEnd, TIndex iIncrement, TInvokable&& f);
165 template<class TInvokable, class TIndex>
166 [[nodiscard]] constexpr ChainedArguments<TValue>& and_for_each(TIndex iStart, TIndex iIncrement, TInvokable&& f);
178 template<class TInvokable>
179 [[nodiscard]] constexpr ChainedArguments<TValue>& and_for_each(TInvokable&& f);
190 template<class TInvokable>
191 constexpr std::optional<Error> on_error(TInvokable&& f);
197 [[nodiscard]] constexpr std::optional<Error> end();
206 [[nodiscard]] constexpr std::optional<Error> end(std::nullopt_t nullopt) const;
213 [[nodiscard]] constexpr std::optional<Error> end(Error&& err) const;
214
225 constexpr ChainedArguments<TValue>& apply(TValue& value);
226 template<class TInvokable>
234 constexpr ChainedArguments<TValue>& apply(TInvokable&& f);
235
245 template<class TNewValue>
246 constexpr ChainedArguments<TNewValue> newChain(TNewValue* existingValue = nullptr);
257 template<class TNewValue>
258 constexpr ChainedArguments<TNewValue> newChain(TNewValue* existingValue = nullptr) const;
259
266 constexpr ChainedArguments<TValue>& setError(Error&& err);
273 constexpr ChainedArguments<TValue>& invalidate(Error&& err);
274
275private:
276 using Value = std::variant<TValue, TValue*>;
277
278 fge::net::Packet const* g_pck;
279 Value g_value;
280 Error g_error;
281};
282
288enum class ROutputs : bool
289{
290 R_NORMAL = false,
291 R_INVERTED = true
292};
293
306template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
307constexpr ChainedArguments<TValue> RRange(TValue const& min, TValue const& max, ChainedArguments<TValue>&& args);
308
316template<class TValue>
317constexpr ChainedArguments<TValue> RValid(ChainedArguments<TValue>&& args);
318
328template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
329constexpr ChainedArguments<TValue> RMustEqual(TValue const& a, ChainedArguments<TValue>&& args);
330
340template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
341constexpr ChainedArguments<TValue> RStrictLess(TValue less, ChainedArguments<TValue>&& args);
342
352template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
353constexpr ChainedArguments<TValue> RLess(TValue less, ChainedArguments<TValue>&& args);
354
371template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
372constexpr ChainedArguments<TValue> RSizeRange(SizeType min, SizeType max, ChainedArguments<TValue>&& args);
373
385template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
386constexpr ChainedArguments<TValue> RSizeMustEqual(fge::net::SizeType a, ChainedArguments<TValue>&& args);
387
399template<class TValue, ROutputs TOutput = ROutputs::R_NORMAL>
400constexpr ChainedArguments<TValue> RMustValidUtf8(ChainedArguments<TValue>&& args);
401
406} // namespace rules
407} // namespace fge::net
408
409#include "network_manager.inl"
410
411#endif // _FGE_NETWORK_MANAGER_HPP_INCLUDED
A scene contain a collection of object and handle them.
Definition C_scene.hpp:450
Definition C_packet.hpp:70
This is a wrapper around a Packet and a value for safe extraction.
Definition network_manager.hpp:92
constexpr ChainedArguments< TValue > & setError(Error &&err)
Set the error.
Definition network_manager.inl:263
constexpr std::optional< TPeek > peek()
Peek without changing the read position a copy of value.
Definition network_manager.inl:73
constexpr ChainedArguments< TNewValue > newChain(TNewValue *existingValue=nullptr)
Create a new chain with a different value type.
Definition network_manager.inl:251
constexpr ChainedArguments< TValue > & apply(TValue &value)
Apply the extracted value to the provided reference.
Definition network_manager.inl:219
constexpr TValue * extract()
Extract and verify the value from the packet.
Definition network_manager.inl:58
constexpr std::invoke_result_t< TInvokable, ChainedArguments< TValue > & > and_then(TInvokable &&f)
Chain up some code after a successful extraction.
Definition network_manager.inl:111
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:122
constexpr std::optional< Error > on_error(TInvokable &&f)
Chain up some code after a unsuccessful extraction.
Definition network_manager.inl:193
constexpr std::optional< Error > end()
End the chain by doing a last validity check on the packet.
Definition network_manager.inl:203
constexpr ChainedArguments< TValue > & invalidate(Error &&err)
Invalidate the packet and set the error.
Definition network_manager.inl:269
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:277
constexpr ChainedArguments< TValue > RMustValidUtf8(ChainedArguments< TValue > &&args)
Check if the extracted string is a valid UTF8 string.
Definition network_manager.inl:393
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:325
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:308
constexpr ChainedArguments< TValue > RSizeMustEqual(fge::net::SizeType a, ChainedArguments< TValue > &&args)
Size must equal rule, check if the size is equal to the provided one.
Definition network_manager.inl:376
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:359
constexpr ChainedArguments< TValue > RValid(ChainedArguments< TValue > &&args)
Valid rule, check if the value is correctly extracted.
Definition network_manager.inl:294
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:342
fge::net::Skey GetSkey(fge::net::Packet &pck)
Shortcut function that will extract the skey.
Definition network_manager.inl:29
bool CheckSkey(fge::net::Packet &pck, fge::net::Skey skey)
Shortcut function that will extract and compare the provided skey.
Definition network_manager.inl:20
FGE_API bool WritePacketDataToFile(fge::net::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.
uint32_t Skey
The session key can be used to identify a client when connecting to a server.
Definition C_client.hpp:47
Definition C_packet.hpp:45