17#ifndef _FGE_C_NETWORKTYPE_HPP_INCLUDED
18#define _FGE_C_NETWORKTYPE_HPP_INCLUDED
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_flag.hpp"
26#include "FastEngine/C_propertyList.hpp"
30#include <unordered_map>
33#define FGE_NET_WAITING_UPDATE_DELAY std::chrono::milliseconds(800)
44enum PerClientConfigs : uint32_t
46 CLIENTCONFIG_MODIFIED_FLAG = 1 << 0,
47 CLIENTCONFIG_REQUIRE_EXPLICIT_UPDATE_FLAG = 1 << 1,
49 CLIENTCONFIG_CUSTOM_FLAG_START = 1 << 2,
51 CLIENTCONFIG_DEFAULT = 0
56 inline constexpr PerClientData() =
default;
57 inline explicit constexpr PerClientData(fge::EnumFlags_t<PerClientConfigs> config) :
62 std::shared_ptr<void> _data{
nullptr};
67class FGE_API PerClientSyncContext
70 using SyncTable = std::unordered_map<Identity, PerClientData, IdentityHash>;
72 PerClientSyncContext() =
default;
73 PerClientSyncContext(PerClientSyncContext
const&) =
delete;
74 PerClientSyncContext(PerClientSyncContext&&)
noexcept =
default;
75 virtual ~PerClientSyncContext() =
default;
77 PerClientSyncContext& operator=(PerClientSyncContext
const&) =
delete;
78 PerClientSyncContext& operator=(PerClientSyncContext&&)
noexcept =
default;
83 fge::EnumFlags_t<PerClientConfigs> config = CLIENTCONFIG_DEFAULT);
85 void setModificationFlag();
86 bool setModificationFlag(
Identity const& client);
87 bool clearModificationFlag(
Identity const& client);
88 [[nodiscard]]
bool isModified(
Identity const& client)
const;
90 void setRequireExplicitUpdateFlag(
Identity const& client);
91 [[nodiscard]]
bool isRequiringExplicitUpdate(
Identity const& client)
const;
93 PerClientData& newClient(
Identity const& client, fge::EnumFlags_t<PerClientConfigs> config = CLIENTCONFIG_DEFAULT);
94 void delClient(
Identity const& client);
96 [[nodiscard]]
bool hasClient(
Identity const& client)
const;
101 [[nodiscard]] SyncTable::const_iterator begin()
const;
102 [[nodiscard]] SyncTable::iterator begin();
103 [[nodiscard]] SyncTable::const_iterator end()
const;
104 [[nodiscard]] SyncTable::iterator end();
107 virtual void createClientData([[maybe_unused]] std::shared_ptr<void>& ptr)
const {}
108 virtual void applyClientData([[maybe_unused]] std::shared_ptr<void>& ptr)
const {}
111 SyncTable g_syncTable;
124class FGE_API NetworkTypeBase :
protected PerClientSyncContext
127 NetworkTypeBase() =
default;
130 ~NetworkTypeBase()
override =
default;
223 void clearExplicitUpdateFlag();
228 bool isNeedingExplicitUpdate()
const;
230 void clearWaitingUpdateFlag();
242 bool isWaitingUpdate()
const;
244 [[nodiscard]] std::chrono::microseconds getLastUpdateTime()
const;
245 void setLastUpdateTime();
253 bool _g_needExplicitUpdate{
false};
254 bool _g_waitingUpdate{
false};
255 bool _g_force{
false};
256 std::chrono::microseconds _g_lastUpdateTime{0};
279 bool check()
const override;
293class FGE_API NetworkTypeScene :
public NetworkTypeBase
297 ~NetworkTypeScene()
override =
default;
324class FGE_API NetworkTypeTag :
public NetworkTypeBase
328 ~NetworkTypeTag()
override =
default;
351class FGE_API NetworkTypeSmoothVec2Float :
public NetworkTypeBase
355 ~NetworkTypeSmoothVec2Float()
override =
default;
367 fge::Vector2f
const& getCache()
const;
368 void setErrorRange(
float range);
369 float getErrorRange()
const;
372 fge::Vector2f g_typeCopy;
381class FGE_API NetworkTypeSmoothFloat :
public NetworkTypeBase
385 ~NetworkTypeSmoothFloat()
override =
default;
397 float getCache()
const;
398 void setErrorRange(
float range);
399 float getErrorRange()
const;
427 bool check()
const override;
455 bool check()
const override;
459 std::string
const& getValueName()
const;
486 bool check()
const override;
505enum class RecordedEventTypes : uint8_t
514 RecordedEventTypes _type;
525 using const_iterator =
typename std::vector<T>::const_iterator;
526 using iterator =
typename std::vector<T>::iterator;
527 using const_reverse_iterator =
typename std::vector<T>::const_reverse_iterator;
528 using const_reference =
typename std::vector<T>::const_reference;
529 using reference =
typename std::vector<T>::reference;
531 using EventQueue = std::vector<RecordedEvent>;
533 RecordedVector() =
default;
534 ~RecordedVector() =
default;
536 [[nodiscard]] const_reference at(SizeType index)
const;
537 [[nodiscard]] const_reference operator[](SizeType index)
const;
538 [[nodiscard]] const_reference front()
const;
539 [[nodiscard]] const_reference back()
const;
540 [[nodiscard]] T
const* data()
const;
542 [[nodiscard]] const_iterator begin()
const;
543 [[nodiscard]] const_iterator end()
const;
544 [[nodiscard]] const_iterator cbegin()
const;
545 [[nodiscard]] const_iterator cend()
const;
546 [[nodiscard]] const_reverse_iterator rbegin()
const;
547 [[nodiscard]] const_reverse_iterator rend()
const;
548 [[nodiscard]] const_reverse_iterator crbegin()
const;
549 [[nodiscard]] const_reverse_iterator crend()
const;
551 [[nodiscard]] SizeType size()
const;
552 [[nodiscard]]
bool empty()
const;
554 void reserve(SizeType n);
557 iterator insert(const_iterator pos, T
const& value);
558 iterator insert(const_iterator pos, T&& value);
559 template<
class... TArgs>
560 iterator emplace(const_iterator pos, TArgs&&... value);
562 void push_back(TArg&& arg);
563 template<
class... TArgs>
564 reference emplace_back(TArgs&&... arg);
565 const_iterator erase(const_iterator pos);
568 [[nodiscard]] reference modify(SizeType index);
569 [[nodiscard]] reference modify(const_iterator pos);
572 [[nodiscard]] SizeType eventsSize()
const;
573 [[nodiscard]] EventQueue
const& getEventQueue()
const;
574 [[nodiscard]]
bool isRegisteringEvents()
const;
575 void registerEvents(
bool enable);
580 std::vector<T> g_container;
583 bool g_registerEvents{
true};
585 bool g_registerEvents{
false};
588 friend Packet& operator<< <T>(
Packet& pck, RecordedVector
const& vec);
589 friend Packet const&
operator>> <T>(
Packet const& pck, RecordedVector& vec);
598class NetworkTypeVector :
public NetworkTypeBase
602 ~NetworkTypeVector()
override =
default;
613 bool check()
const override;
618 void createClientData(std::shared_ptr<void>& ptr)
const override;
619 void applyClientData(std::shared_ptr<void>& ptr)
const override;
623 inline void operator()(
void* ptr)
const {
delete static_cast<typename RecordedVector<T>::EventQueue*
>(ptr); }
626 enum class PackTypes : uint8_t
640template<
class TEnum,
class TData =
void>
641class NetworkTypeEvents :
public NetworkTypeBase
644 using Event = std::conditional_t<std::is_void_v<TData>, TEnum, std::pair<TEnum, TData>>;
646 NetworkTypeEvents() =
default;
647 ~NetworkTypeEvents()
override =
default;
658 bool check()
const override;
662 void pushEvent(Event
const& event);
663 void pushEventIgnore(Event
const& event,
Identity const& ignoreId);
668 void createClientData(std::shared_ptr<void>& ptr)
const override;
669 void applyClientData(std::shared_ptr<void>& ptr)
const override;
671 using EventQueue = std::deque<Event>;
675 inline void operator()(
void* ptr)
const {
delete static_cast<EventQueue*
>(ptr); }
678 bool g_modified{
false};
689class FGE_API NetworkTypeHandler
692 NetworkTypeHandler() =
default;
693 ~NetworkTypeHandler() =
default;
696 NetworkTypeHandler([[maybe_unused]] NetworkTypeHandler
const& n) {}
697 NetworkTypeHandler& operator=([[maybe_unused]] NetworkTypeHandler
const& n) {
return *
this; }
701 void clientsCheckup(
ClientList const& clients,
bool force =
false)
const;
702 void forceCheckClient(
Identity const&
id)
const;
703 void forceUncheckClient(
Identity const&
id)
const;
706 template<
class T,
class... TArgs>
707 T* push(TArgs&&... args)
709 static_assert(std::is_base_of_v<NetworkTypeBase, T>,
"T must inherit from NetworkTypeBase");
710 return static_cast<T*
>(this->push(std::make_unique<T>(std::forward<TArgs>(args)...)));
712 template<
class T,
class... TArgs>
719 std::size_t packNeededUpdate(
Packet& pck)
const;
720 void unpackNeededUpdate(
Packet const& pck,
Identity const&
id)
const;
722 [[nodiscard]]
inline std::size_t size()
const {
return this->g_data.size(); }
723 [[nodiscard]]
inline NetworkTypeBase* get(std::size_t index)
const {
return this->g_data[index].get(); }
725 [[nodiscard]]
inline T* get(std::size_t index)
const
727 static_assert(std::is_base_of_v<NetworkTypeBase, T>,
"T must inherit from NetworkTypeBase");
728 return static_cast<T*
>(this->g_data[index].get());
730 [[nodiscard]]
inline NetworkTypeBase* operator[](std::size_t index)
const {
return this->g_data[index].get(); }
732 void ignoreClient(
Identity const&
id);
733 void unignoreClient(
Identity const&
id);
734 [[nodiscard]]
bool isIgnored(
Identity const&
id)
const;
735 void clearIgnoredClients();
738 std::vector<std::unique_ptr<NetworkTypeBase>> g_data;
739 std::unordered_set<Identity, IdentityHash> g_ignoredClients;
745#include "C_networkType.inl"
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:189
A class to handle "flags" for an enum type.
Definition C_flag.hpp:104
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:465
Definition C_tagList.hpp:28
A list of clients used by a server.
Definition C_clientList.hpp:60
Base class for a network type.
Definition C_networkType.hpp:125
virtual bool checkClient(Identity const &id) const
Check if the modification flag is set for the specified client identity.
void waitingUpdate()
Tell that this network type is waiting for an update.
virtual bool clientsCheckup(ClientList const &clients, bool force)
Do a clients checkup with the specified client list.
virtual bool check() const =0
Check if the value have been modified.
virtual void packData(Packet &pck, Identity const &id)=0
Pack the data into a packet and reset the modification flag of the identity.
virtual void forceUncheck()=0
Remove the forced modification of the value.
virtual void forceUncheckClient(Identity const &id)
Reset the modification flag for the specified client identity.
virtual bool applyData(Packet const &pck)=0
Apply the data packed by the same network type from a server.
virtual void requireExplicitUpdateClient(Identity const &id)
Ask for an explicit update of the value for the specified 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.
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:250
virtual void forceCheck()=0
Force the value to be modified (even if it is not)
virtual void packData(Packet &pck)=0
Pack the data without any client identity.
virtual void forceCheckClient(Identity const &id)
Force the modification flag to be set for the specified client identity.
void packData(Packet &pck, Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:713
bool applyData(Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:681
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:775
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:770
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:675
void forceUncheckClient(Identity const &id) override
Reset the modification flag for the specified client identity.
Definition C_networkType.inl:758
void forceCheckClient(Identity const &id) override
Force the modification flag to be set for the specified client identity.
Definition C_networkType.inl:749
fge::CallbackHandler< Event > _onEvent
Callback called when an event is received.
Definition C_networkType.hpp:665
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:780
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:206
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:243
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:248
NetworkTypeManual(T *source)
NetworkTypeManual.
Definition C_networkType.inl:200
bool applyData(Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:212
void packData(Packet &pck, Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:224
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:238
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:142
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:177
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:187
NetworkTypePropertyList(fge::PropertyList *source, std::string const &vname)
NetworkTypePropertyList.
Definition C_networkType.inl:133
bool applyData(Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:148
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:182
void packData(Packet &pck, Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:160
void packData(Packet &pck, Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:102
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:126
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:121
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:86
bool applyData(Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:92
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:116
NetworkTypeProperty(fge::Property *source)
NetworkTypeProperty.
Definition C_networkType.inl:79
void forceUncheckClient(Identity const &id) override
Reset the modification flag for the specified client identity.
void packData(Packet &pck, Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
void forceCheckClient(Identity const &id) override
Force the modification flag to be set for the specified client identity.
void const * getSource() const override
Get the source pointer that have been used to create this network type.
bool clientsCheckup(ClientList const &clients, bool force) override
Do a clients checkup with the specified client list.
void packData(Packet &pck) override
Pack the data without any client identity.
bool check() const override
Check if the value have been modified.
bool applyData(Packet const &pck) override
Apply the data packed by the same network type from a server.
bool checkClient(Identity const &id) const override
Check if the modification flag is set for the specified client identity.
void forceCheck() override
Force the value to be modified (even if it is not)
void forceUncheck() override
Remove the forced modification of the value.
void forceUncheck() override
Remove the forced modification of the value.
bool applyData(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 packData(Packet &pck, Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
void forceCheck() override
Force the value to be modified (even if it is not)
void packData(Packet &pck) override
Pack the data without any client identity.
void const * getSource() const override
Get the source pointer that have been used to create this network type.
bool applyData(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 const * getSource() const override
Get the source pointer that have been used to create this network type.
void packData(Packet &pck, Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
void packData(Packet &pck) override
Pack the data without any client identity.
void forceCheck() override
Force the value to be modified (even if it is not)
void forceUncheck() override
Remove the forced modification of the value.
void packData(Packet &pck, 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(Packet const &pck) override
Apply the data packed by the same network type from a server.
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.
void packData(Packet &pck) override
Pack the data without any client identity.
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:638
void packData(Packet &pck, Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:552
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:643
void const * getSource() const override
Get the source pointer that have been used to create this network type.
Definition C_networkType.inl:480
bool applyData(Packet const &pck) override
Apply the data packed by the same network type from a server.
Definition C_networkType.inl:486
void forceCheckClient(Identity const &id) override
Force the modification flag to be set for the specified client identity.
Definition C_networkType.inl:606
bool check() const override
Check if the value have been modified.
Definition C_networkType.inl:629
void forceUncheckClient(Identity const &id) override
Reset the modification flag for the specified client identity.
Definition C_networkType.inl:617
The default network type for most trivial types.
Definition C_networkType.hpp:268
NetworkType(fge::DataAccessor< T > source)
NetworkType.
Definition C_networkType.inl:23
void packData(Packet &pck, Identity const &id) override
Pack the data into a packet and reset the modification flag of the identity.
Definition C_networkType.inl:47
void forceCheck() override
Force the value to be modified (even if it is not)
Definition C_networkType.inl:65
void forceUncheck() override
Remove the forced modification of the value.
Definition C_networkType.inl:70
bool applyData(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:60
Definition C_packet.hpp:52
Definition C_networkType.hpp:523
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:55
Definition C_networkType.hpp:513