FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_networkType.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_NETWORKTYPE_HPP_INCLUDED
18#define _FGE_C_NETWORKTYPE_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "C_identity.hpp"
22#include "C_packet.hpp"
23#include "FastEngine/C_callback.hpp"
24#include "FastEngine/C_dataAccessor.hpp"
25#include "FastEngine/C_propertyList.hpp"
26#include <memory>
27#include <string>
28#include <unordered_map>
29#include <vector>
30
31#define FGE_NET_WAITING_UPDATE_DELAY std::chrono::milliseconds(800)
32
33namespace fge
34{
35
36class Scene;
37class TagList;
38
39namespace net
40{
41
42enum PerClientConfigs : uint8_t
43{
44 CLIENTCONFIG_MODIFIED_FLAG = 1 << 0,
45 CLIENTCONFIG_REQUIRE_EXPLICIT_UPDATE_FLAG = 1 << 1
46};
47
49{
50 std::underlying_type_t<PerClientConfigs> _config{0};
51 void* _customData{nullptr};
52};
53
54using NetworkPerClientModificationTable =
55 std::unordered_map<fge::net::Identity, fge::net::PerClientConfig, fge::net::IdentityHash>;
56
57class ClientList;
58
69class FGE_API NetworkTypeBase
70{
71protected:
72 NetworkTypeBase() = default;
73
74public:
75 virtual ~NetworkTypeBase() = default;
76
82 virtual void const* getSource() const = 0;
83
90 virtual bool applyData(fge::net::Packet const& pck) = 0;
97 virtual void packData(fge::net::Packet& pck, fge::net::Identity const& id) = 0;
103 virtual void packData(fge::net::Packet& pck) = 0;
104
117 virtual bool clientsCheckup(fge::net::ClientList const& clients, bool force);
118
125 virtual bool checkClient(fge::net::Identity const& id) const;
131 virtual void forceCheckClient(fge::net::Identity const& id);
137 virtual void forceUncheckClient(fge::net::Identity const& id);
146
152 virtual bool check() const = 0;
156 virtual void forceCheck() = 0;
160 virtual void forceUncheck() = 0;
166 [[nodiscard]] bool isForced() const;
167
168 void clearExplicitUpdateFlag();
173 bool isNeedingExplicitUpdate() const;
174
175 void clearWaitingUpdateFlag();
187 bool isWaitingUpdate() const;
188
189 [[nodiscard]] std::chrono::microseconds getLastUpdateTime() const;
190 void setLastUpdateTime();
191
196
197protected:
198 virtual void createClientCustomData([[maybe_unused]] void*& ptr) const {}
199 virtual void destroyClientCustomData([[maybe_unused]] void*& ptr) const {}
200 virtual void applyClientCustomData([[maybe_unused]] void*& ptr) const {}
201
202 fge::net::NetworkPerClientModificationTable _g_tableId;
203 bool _g_needExplicitUpdate{false};
204 bool _g_waitingUpdate{false};
205 bool _g_force{false};
206 std::chrono::microseconds _g_lastUpdateTime{0};
207};
208
216template<class T>
218{
219public:
221 ~NetworkType() override = default;
222
223 void const* getSource() const override;
224
225 bool applyData(fge::net::Packet const& pck) override;
226 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
227 void packData(fge::net::Packet& pck) override;
228
229 bool check() const override;
230 void forceCheck() override;
231 void forceUncheck() override;
232
233private:
234 T g_typeCopy;
235 fge::DataAccessor<T> g_typeSource;
236};
237
243class FGE_API NetworkTypeScene : public NetworkTypeBase
244{
245public:
247 ~NetworkTypeScene() override = default;
248
249 void const* getSource() const override;
250
251 bool applyData(fge::net::Packet const& pck) override;
252 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
253 void packData(fge::net::Packet& pck) override;
254
255 bool clientsCheckup(fge::net::ClientList const& clients, bool force) override;
256
257 bool checkClient(fge::net::Identity const& id) const override;
258 void forceCheckClient(fge::net::Identity const& id) override;
259 void forceUncheckClient(fge::net::Identity const& id) override;
260
261 bool check() const override;
262 void forceCheck() override;
263 void forceUncheck() override;
264
265private:
266 fge::Scene* g_typeSource;
267};
268
274class FGE_API NetworkTypeTag : public NetworkTypeBase
275{
276public:
277 NetworkTypeTag(fge::TagList* source, std::string tag);
278 ~NetworkTypeTag() override = default;
279
280 void const* getSource() const override;
281
282 bool applyData(fge::net::Packet const& pck) override;
283 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
284 void packData(fge::net::Packet& pck) override;
285
286 bool check() const override;
287 void forceCheck() override;
288 void forceUncheck() override;
289
290private:
291 fge::TagList* g_typeSource;
292 std::string g_tag;
293};
294
302{
303public:
305 ~NetworkTypeSmoothVec2Float() override = default;
306
307 void const* getSource() const override;
308
309 bool applyData(fge::net::Packet const& pck) override;
310 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
311 void packData(fge::net::Packet& pck) override;
312
313 bool check() const override;
314 void forceCheck() override;
315 void forceUncheck() override;
316
317 fge::Vector2f const& getCache() const;
318 void setErrorRange(float range);
319 float getErrorRange() const;
320
321private:
322 fge::Vector2f g_typeCopy;
324 float g_errorRange;
325};
332{
333public:
334 NetworkTypeSmoothFloat(fge::DataAccessor<float> source, float errorRange);
335 ~NetworkTypeSmoothFloat() override = default;
336
337 void const* getSource() const override;
338
339 bool applyData(fge::net::Packet const& pck) override;
340 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
341 void packData(fge::net::Packet& pck) override;
342
343 bool check() const override;
344 void forceCheck() override;
345 void forceUncheck() override;
346
347 float getCache() const;
348 void setErrorRange(float range);
349 float getErrorRange() const;
350
351private:
352 float g_typeCopy;
353 fge::DataAccessor<float> g_typeSource;
354 float g_errorRange;
355};
356
364template<class T>
366{
367public:
369 ~NetworkTypeProperty() override = default;
370
371 void const* getSource() const override;
372
373 bool applyData(fge::net::Packet const& pck) override;
374 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
375 void packData(fge::net::Packet& pck) override;
376
377 bool check() const override;
378 void forceCheck() override;
379 void forceUncheck() override;
380
381private:
382 fge::Property* g_typeSource;
383};
384
392template<class T>
394{
395public:
396 NetworkTypePropertyList(fge::PropertyList* source, std::string const& vname);
397 ~NetworkTypePropertyList() override = default;
398
399 void const* getSource() const override;
400
401 bool applyData(fge::net::Packet const& pck) override;
402 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
403 void packData(fge::net::Packet& pck) override;
404
405 bool check() const override;
406 void forceCheck() override;
407 void forceUncheck() override;
408
409 std::string const& getValueName() const;
410
411private:
412 fge::PropertyList* g_typeSource;
413 std::string g_vname;
414};
415
423template<class T>
425{
426public:
427 NetworkTypeManual(T* source);
428 ~NetworkTypeManual() override = default;
429
430 void const* getSource() const override;
431
432 bool applyData(fge::net::Packet const& pck) override;
433 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
434 void packData(fge::net::Packet& pck) override;
435
436 bool check() const override;
437 void forceCheck() override;
438 void forceUncheck() override;
439
440 void trigger();
441
442private:
443 T* g_typeSource;
444 bool g_trigger;
445};
446
447template<class T>
448class RecordedVector;
449
450template<class T>
451Packet& operator<<(Packet& pck, RecordedVector<T> const& vec);
452template<class T>
453Packet const& operator>>(Packet const& pck, RecordedVector<T>& vec);
454
455enum class RecordedEventTypes : uint8_t
456{
457 ADD,
458 REMOVE,
459 REMOVE_ALL,
460 MODIFY
461};
463{
464 RecordedEventTypes _type;
465 SizeType _index;
466};
467
468inline Packet& operator<<(Packet& pck, RecordedEvent const& event);
469inline Packet const& operator>>(Packet const& pck, RecordedEvent& event);
470
471template<class T>
473{
474public:
475 using const_iterator = typename std::vector<T>::const_iterator;
476 using iterator = typename std::vector<T>::iterator;
477 using const_reverse_iterator = typename std::vector<T>::const_reverse_iterator;
478 using const_reference = typename std::vector<T>::const_reference;
479 using reference = typename std::vector<T>::reference;
480
481 using EventQueue = std::vector<RecordedEvent>;
482
483 RecordedVector() = default;
484 ~RecordedVector() = default;
485
486 [[nodiscard]] const_reference at(SizeType index) const;
487 [[nodiscard]] const_reference operator[](SizeType index) const;
488 [[nodiscard]] const_reference front() const;
489 [[nodiscard]] const_reference back() const;
490 [[nodiscard]] T const* data() const;
491
492 [[nodiscard]] const_iterator begin() const;
493 [[nodiscard]] const_iterator end() const;
494 [[nodiscard]] const_iterator cbegin() const;
495 [[nodiscard]] const_iterator cend() const;
496 [[nodiscard]] const_reverse_iterator rbegin() const;
497 [[nodiscard]] const_reverse_iterator rend() const;
498 [[nodiscard]] const_reverse_iterator crbegin() const;
499 [[nodiscard]] const_reverse_iterator crend() const;
500
501 [[nodiscard]] SizeType size() const;
502 [[nodiscard]] bool empty() const;
503
504 void reserve(SizeType n);
505
506 void clear();
507 iterator insert(const_iterator pos, T const& value);
508 iterator insert(const_iterator pos, T&& value);
509 template<class... TArgs>
510 iterator emplace(const_iterator pos, TArgs&&... value);
511 template<class TArg>
512 void push_back(TArg&& arg);
513 template<class... TArgs>
514 reference emplace_back(TArgs&&... arg);
515 const_iterator erase(const_iterator pos);
516 void pop_back();
517
518 [[nodiscard]] reference modify(SizeType index);
519 [[nodiscard]] reference modify(const_iterator pos);
520
521 void clearEvents();
522 [[nodiscard]] SizeType eventsSize() const;
523 [[nodiscard]] EventQueue const& getEventQueue() const;
524 [[nodiscard]] bool isRegisteringEvents() const;
525 void registerEvents(bool enable);
526
527private:
528 void pushEvent(RecordedEvent event);
529
530 std::vector<T> g_container;
531 EventQueue g_events;
532#ifdef FGE_DEF_SERVER
533 bool g_registerEvents{true};
534#else
535 bool g_registerEvents{false};
536#endif
537
538 friend Packet& operator<< <T>(Packet& pck, RecordedVector const& vec);
539 friend Packet const& operator>> <T>(Packet const& pck, RecordedVector& vec);
540};
541
547template<class T>
549{
550public:
552 ~NetworkTypeVector() override;
553
554 void const* getSource() const override;
555
556 bool applyData(fge::net::Packet const& pck) override;
557 void packData(fge::net::Packet& pck, fge::net::Identity const& id) override;
558 void packData(fge::net::Packet& pck) override;
559
560 void forceCheckClient(fge::net::Identity const& id) override;
561 void forceUncheckClient(fge::net::Identity const& id) override;
562
563 bool check() const override;
564 void forceCheck() override;
565 void forceUncheck() override;
566
567private:
568 void createClientCustomData(void*& ptr) const override;
569 void destroyClientCustomData(void*& ptr) const override;
570 void applyClientCustomData(void*& ptr) const override;
571
572 enum class PackTypes : uint8_t
573 {
574 FULL,
575 PARTIAL
576 };
577
578 RecordedVector<T>* g_typeSource;
579};
580
590{
591public:
592 NetworkTypeHandler() = default;
593 ~NetworkTypeHandler() = default;
594
595 //Copy function that does nothing
596 NetworkTypeHandler([[maybe_unused]] NetworkTypeHandler const& n) {}
597 NetworkTypeHandler& operator=([[maybe_unused]] NetworkTypeHandler const& n) { return *this; }
598
599 void clear();
600
601 void clientsCheckup(fge::net::ClientList const& clients, bool force = false) const;
602 void forceCheckClient(fge::net::Identity const& id) const;
603 void forceUncheckClient(fge::net::Identity const& id) const;
604
605 fge::net::NetworkTypeBase* push(std::unique_ptr<fge::net::NetworkTypeBase>&& newNet);
606 template<class T, class... TArgs>
607 T* push(TArgs&&... args)
608 {
609 static_assert(std::is_base_of_v<fge::net::NetworkTypeBase, T>, "T must inherit from fge::net::NetworkTypeBase");
610 return static_cast<T*>(this->push(std::make_unique<T>(std::forward<TArgs>(args)...)));
611 }
612 template<class T, class... TArgs>
613 fge::net::NetworkType<T>* pushTrivial(TArgs&&... args)
614 {
615 return static_cast<fge::net::NetworkType<T>*>(this->push(
616 std::make_unique<fge::net::NetworkType<T>>(fge::DataAccessor<T>{std::forward<TArgs>(args)...})));
617 }
618
619 std::size_t packNeededUpdate(fge::net::Packet& pck) const;
620 void unpackNeededUpdate(fge::net::Packet const& pck, fge::net::Identity const& id) const;
621
622 [[nodiscard]] inline std::size_t size() const { return this->g_data.size(); }
623 [[nodiscard]] inline fge::net::NetworkTypeBase* get(std::size_t index) const { return this->g_data[index].get(); }
624 template<class T>
625 [[nodiscard]] inline T* get(std::size_t index) const
626 {
627 static_assert(std::is_base_of_v<fge::net::NetworkTypeBase, T>, "T must inherit from fge::net::NetworkTypeBase");
628 return static_cast<T*>(this->g_data[index].get());
629 }
630 [[nodiscard]] inline fge::net::NetworkTypeBase* operator[](std::size_t index) const
631 {
632 return this->g_data[index].get();
633 }
634
635private:
636 std::vector<std::unique_ptr<fge::net::NetworkTypeBase>> g_data;
637};
638
639} // namespace net
640} // namespace fge
641
642#include "C_networkType.inl"
643
644#endif // _FGE_C_NETWORKTYPE_HPP_INCLUDED
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:189
A class that map a string to a Property.
Definition C_propertyList.hpp:35
A class that can store any type of data.
Definition C_property.hpp:54
A scene contain a collection of object and handle them.
Definition C_scene.hpp:450
Definition C_tagList.hpp:28
A list of clients used by a server.
Definition C_clientList.hpp:57
Base class for a network type.
Definition C_networkType.hpp:70
void waitingUpdate()
Tell that this network type is waiting for an update.
virtual bool check() const =0
Check if the value have been modified.
virtual bool clientsCheckup(fge::net::ClientList const &clients, bool force)
Do a clients checkup with the specified client list.
virtual void requireExplicitUpdateClient(fge::net::Identity const &id)
Ask for an explicit update of the value for the specified client identity.
virtual void forceCheckClient(fge::net::Identity const &id)
Force the modification flag to be set for the specified client identity.
virtual bool checkClient(fge::net::Identity const &id) const
Check if the modification flag is set for the specified client identity.
virtual void forceUncheck()=0
Remove the forced modification of the value.
virtual void packData(fge::net::Packet &pck)=0
Pack the data without any client identity.
bool isForced() const
Check if the value is forced to be modified.
virtual void const * getSource() const =0
Get the source pointer that have been used to create this network type.
virtual void packData(fge::net::Packet &pck, fge::net::Identity const &id)=0
Pack the data into a packet and reset the modification flag of the identity.
virtual void forceUncheckClient(fge::net::Identity const &id)
Reset the modification flag for the specified client identity.
void needExplicitUpdate()
Tell that this network type need an explicit update from the server.
fge::CallbackHandler _onApplied
Callback called when the value have been applied.
Definition C_networkType.hpp:195
virtual void forceCheck()=0
Force the value to be modified (even if it is not)
virtual bool applyData(fge::net::Packet const &pck)=0
Apply the data packed by the same network type from a server.
A regroupment of network types.
Definition C_networkType.hpp:590
The network type for a trivial type but triggered manually.
Definition C_networkType.hpp:425
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:215
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:254
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:233
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:259
NetworkTypeManual(T *source)
NetworkTypeManual.
Definition C_networkType.inl:209
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:221
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:249
The network type for a property inside a list.
Definition C_networkType.hpp:394
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:147
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:186
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:196
NetworkTypePropertyList(fge::PropertyList *source, std::string const &vname)
NetworkTypePropertyList.
Definition C_networkType.inl:138
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:153
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:191
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:165
The network type for a property.
Definition C_networkType.hpp:366
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:131
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:126
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:94
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:104
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:88
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:121
NetworkTypeProperty(fge::Property *source)
NetworkTypeProperty.
Definition C_networkType.inl:81
The network type for a scene.
Definition C_networkType.hpp:244
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
void const * getSource() const override
Get the source pointer that have been used to create this network type.
void packData(fge::net::Packet &pck) override
Pack the data without any client identity.
bool check() const override
Check if the value have been modified.
void forceCheck() override
Force the value to be modified (even if it is not)
void forceCheckClient(fge::net::Identity const &id) override
Force the modification flag to be set for the specified client identity.
void forceUncheckClient(fge::net::Identity const &id) override
Reset the modification flag for the specified client identity.
bool checkClient(fge::net::Identity const &id) const override
Check if the modification flag is set for the specified client identity.
bool clientsCheckup(fge::net::ClientList const &clients, bool force) override
Do a clients checkup with the specified client list.
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
void forceUncheck() override
Remove the forced modification of the value.
The network type for a float that wait for the error threshold in order to set the value.
Definition C_networkType.hpp:332
void forceUncheck() override
Remove the forced modification of the value.
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
bool check() const override
Check if the value have been modified.
void forceCheck() override
Force the value to be modified (even if it is not)
void packData(fge::net::Packet &pck) override
Pack the data without any client identity.
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
void const * getSource() const override
Get the source pointer that have been used to create this network type.
The network type for a vector2 float that wait for the error threshold in order to set the value (use...
Definition C_networkType.hpp:302
bool check() const override
Check if the value have been modified.
void const * getSource() const override
Get the source pointer that have been used to create this network type.
void forceCheck() override
Force the value to be modified (even if it is not)
void forceUncheck() override
Remove the forced modification of the value.
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
void packData(fge::net::Packet &pck) override
Pack the data without any client identity.
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
The network type for a tag.
Definition C_networkType.hpp:275
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
void const * getSource() const override
Get the source pointer that have been used to create this network type.
void forceUncheck() override
Remove the forced modification of the value.
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
void packData(fge::net::Packet &pck) override
Pack the data without any client identity.
void forceCheck() override
Force the value to be modified (even if it is not)
bool check() const override
Check if the value have been modified.
The network type for a vector.
Definition C_networkType.hpp:549
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:654
void forceUncheckClient(fge::net::Identity const &id) override
Reset the modification flag for the specified client identity.
Definition C_networkType.inl:633
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:659
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:499
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:505
NetworkTypeVector(RecordedVector< T > *source)
NetworkTypeVector.
Definition C_networkType.inl:486
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:571
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:645
void forceCheckClient(fge::net::Identity const &id) override
Force the modification flag to be set for the specified client identity.
Definition C_networkType.inl:622
The default network type for most trivial types.
Definition C_networkType.hpp:218
NetworkType(fge::DataAccessor< T > source)
NetworkType.
Definition C_networkType.inl:23
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:67
void packData(fge::net::Packet &pck, fge::net::Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:47
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:72
bool applyData(fge::net::Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:34
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:29
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:62
Definition C_packet.hpp:70
Definition C_networkType.hpp:473
Definition C_dataAccessor.hpp:27
A class to represent a client or server identity with an IP address and a port.
Definition C_identity.hpp:31
Definition C_networkType.hpp:49
Definition C_networkType.hpp:463