36 return FGE_NET_BAD_SKEY;
43constexpr ChainedArguments<TValue>::ChainedArguments(
fge::net::Packet const& pck, TValue* existingValue) :
45 g_value(existingValue == nullptr ? Value{std::in_place_type<TValue>} : Value{existingValue}),
46 g_error(pck.isValid() ? Error{}
47 : Error{Error::Types::ERR_ALREADY_INVALID, pck.getReadPos(),
"already invalid packet",
51constexpr ChainedArguments<TValue>::ChainedArguments(
fge::net::Packet const& pck, Error&& err, TValue* existingValue) :
53 g_value(existingValue == nullptr ? Value{std::in_place_type<TValue>} : Value{existingValue}),
54 g_error(std::move(err))
60 auto* value = std::holds_alternative<TValue>(this->g_value) ? &std::get<TValue>(this->g_value)
61 : std::get<TValue*>(this->g_value);
62 *this->g_pck >> *value;
63 if (this->g_pck->isValid())
68 net::Error{net::Error::Types::ERR_EXTRACT, this->g_pck->getReadPos(),
"extraction failed", __func__};
75 auto startReadPos = this->g_pck->getReadPos();
78 *this->g_pck >> value;
80 if (this->g_pck->isValid())
82 this->g_pck->setReadPos(startReadPos);
86 this->g_error =
net::Error{net::Error::Types::ERR_EXTRACT, this->g_pck->getReadPos(),
"peek failed", __func__};
96constexpr TValue
const& ChainedArguments<TValue>::value()
const
98 return std::holds_alternative<TValue>(this->g_value) ? std::get<TValue>(this->g_value)
99 : *std::get<TValue*>(this->g_value);
101template<
class TValue>
102constexpr TValue& ChainedArguments<TValue>::value()
104 return std::holds_alternative<TValue>(this->g_value) ? std::get<TValue>(this->g_value)
105 : *std::get<TValue*>(this->g_value);
108template<
class TValue>
109template<
class TInvokable>
110constexpr typename std::invoke_result_t<TInvokable, ChainedArguments<TValue>&>
113 if (this->g_pck->isValid())
115 return std::invoke(std::forward<TInvokable>(f), *
this);
117 return {*this->g_pck, std::move(this->g_error),
nullptr};
119template<
class TValue>
120template<
class TInvokable,
class TIndex>
124 if (!this->g_pck->isValid())
129 for (iStart; iStart != iEnd; iStart += iIncrement)
131 std::optional<Error> err =
136 this->invalidate(std::move(err.value()));
142template<
class TValue>
143template<
class TInvokable,
class TIndex>
147 if (!this->g_pck->isValid())
152 auto& value = this->value();
154 for (; iStart != value; iStart += iIncrement)
156 std::optional<Error> err =
161 this->invalidate(std::move(err.value()));
167template<
class TValue>
168template<
class TInvokable>
171 if (!this->g_pck->isValid())
176 auto& value = this->value();
178 for (TValue iIndex = 0; iIndex != value; ++iIndex)
180 std::optional<Error> err =
185 this->invalidate(std::move(err.value()));
191template<
class TValue>
192template<
class TInvokable>
195 if (!this->g_pck->isValid())
197 std::invoke(std::forward<TInvokable>(f), *
this);
198 return this->g_error;
202template<
class TValue>
205 return this->g_pck->isValid() ? std::nullopt : std::optional<Error>{std::move(this->g_error)};
207template<
class TValue>
210 return std::optional<Error>{nullopt};
212template<
class TValue>
215 return std::optional<Error>{std::move(err)};
218template<
class TValue>
221 if (this->g_pck->isValid())
223 auto* extractedValue = std::holds_alternative<TValue>(this->g_value) ? &std::get<TValue>(this->g_value)
224 : std::get<TValue*>(this->g_value);
225 if constexpr (std::is_move_assignable_v<TValue>)
227 value = std::move(*extractedValue);
231 value = *extractedValue;
236template<
class TValue>
237template<
class TInvokable>
240 if (this->g_pck->isValid())
242 auto* extractedValue = std::holds_alternative<TValue>(this->g_value) ? &std::get<TValue>(this->g_value)
243 : std::get<TValue*>(this->g_value);
244 std::invoke(std::forward<TInvokable>(f),
const_cast<TValue&
>(*extractedValue));
249template<
class TValue>
250template<
class TNewValue>
255template<
class TValue>
256template<
class TNewValue>
262template<
class TValue>
265 this->g_error = std::move(err);
268template<
class TValue>
272 this->g_error = std::move(err);
276template<
class TValue, ROutputs TOutput>
279 if (args.packet().isValid())
281 auto* val = args.extract();
284 if (!((*val >= min && *val <= max) ^
static_cast<bool>(TOutput)))
286 args.invalidate(
Error{Error::Types::ERR_RULE, args.packet().getReadPos(),
"rule failed", __func__});
293template<
class TValue>
296 if (args.packet().isValid())
298 auto* val = args.extract();
301 args.invalidate(
Error{Error::Types::ERR_RULE, args.packet().getReadPos(),
"rule failed", __func__});
307template<
class TValue, ROutputs TOutput>
310 if (args.packet().isValid())
312 auto* val = args.extract();
315 if (!((*val == a) ^
static_cast<bool>(TOutput)))
317 args.invalidate(
Error{Error::Types::ERR_RULE, args.packet().getReadPos(),
"rule failed", __func__});
324template<
class TValue, ROutputs TOutput>
327 if (args.packet().isValid())
329 auto* val = args.extract();
332 if (!((*val < less) ^
static_cast<bool>(TOutput)))
334 args.invalidate(
Error{Error::Types::ERR_RULE, args.packet().getReadPos(),
"rule failed", __func__});
341template<
class TValue, ROutputs TOutput>
344 if (args.packet().isValid())
346 auto* val = args.extract();
349 if (!((*val <= less) ^
static_cast<bool>(TOutput)))
351 args.invalidate(
Error{Error::Types::ERR_RULE, args.packet().getReadPos(),
"rule failed", __func__});
358template<
class TValue, ROutputs TOutput>
361 if (args.packet().isValid())
363 auto size = args.template peek<fge::net::SizeType>();
366 if (!((size >= min && size <= max) ^
static_cast<bool>(TOutput)))
368 args.invalidate(
Error{Error::Types::ERR_RULE, args.packet().getReadPos(),
"rule failed", __func__});
375template<
class TValue, ROutputs TOutput>
378 if (args.packet().isValid())
380 auto size = args.template peek<fge::net::SizeType>();
383 if (!((size == a) ^
static_cast<bool>(TOutput)))
385 args.invalidate(
Error{Error::Types::ERR_RULE, args.packet().getReadPos(),
"rule failed", __func__});
392template<
class TValue, ROutputs TOutput>
395 if (args.packet().isValid())
397 auto* val = args.extract();
402 args.invalidate(
Error{Error::Types::ERR_RULE, args.packet().getReadPos(),
"rule failed", __func__});
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 > & 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
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