FastEngine 0.9.5
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_clientList.hpp
1/*
2 * Copyright 2026 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_CLIENTLIST_HPP_INCLUDED
18#define _FGE_C_CLIENTLIST_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "C_client.hpp"
22#include "C_protocol.hpp"
23#include "FastEngine/C_accessLock.hpp"
24#include <deque>
25#include <memory>
26#include <mutex>
27#include <unordered_map>
28
29namespace fge::net
30{
31
32class SocketUdp;
33
34using ClientSharedPtr = std::shared_ptr<Client>;
35
41class FGE_API ClientList
42{
43public:
49 struct Event
50 {
51 enum class Types : uint8_t
52 {
53 EVT_DELCLIENT = 0,
54 EVT_NEWCLIENT
55 };
56 using Types_t = std::underlying_type_t<Types>;
57
58 inline Event(Types eventType, Identity const& clientId) :
59 _event(eventType),
60 _id(clientId)
61 {}
62
63 Types _event;
64 Identity _id;
65 };
66
67 using DataList = std::unordered_map<Identity, ClientSharedPtr, IdentityHash>;
68 using EventList = std::deque<Event>;
69
70 ClientList() = default;
71 ClientList(ClientList const& r);
72 ClientList(ClientList&& r) noexcept;
73 ~ClientList() = default;
74
75 ClientList& operator=(ClientList const& r);
76 ClientList& operator=(ClientList&& r) noexcept;
77
81 void clear();
82
92 void sendToAll(SocketUdp& socket, Packet& pck) const;
98 void sendToAll(TransmitPacketPtr const& pck) const;
99
100 bool moveTo(ClientList& targetList, Identity const& id);
107 void add(Identity const& id, ClientSharedPtr const& newClient);
113 void remove(Identity const& id);
124 DataList::iterator remove(DataList::const_iterator itPos, AccessLock<std::recursive_mutex> const& lock);
125
132 ClientSharedPtr get(Identity const& id) const;
133
144
157 DataList::iterator begin(AccessLock<std::recursive_mutex> const& lock);
158 DataList::const_iterator begin(AccessLock<std::recursive_mutex> const& lock) const;
159 DataList::iterator end(AccessLock<std::recursive_mutex> const& lock);
160 DataList::const_iterator end(AccessLock<std::recursive_mutex> const& lock) const;
161
167 std::size_t getSize() const;
168
176 void watchEvent(bool on);
182 bool isWatchingEvent() const;
183
189 void pushClientEvent(Event const& evt);
195 Event const& getClientEvent(std::size_t index) const;
201 std::size_t getClientEventSize() const;
209
210private:
211 DataList g_data;
212 EventList g_events;
213 mutable std::recursive_mutex g_mutex;
214 bool g_enableClientEventsFlag = false;
215};
216
217} // namespace fge::net
218
219
220#endif // _FGE_C_CLIENTLIST_HPP_INCLUDED
Class that lock a mutex and unlock it only when the object is destroyed.
Definition C_accessLock.hpp:38
void pushClientEvent(Event const &evt)
Manually push a client event.
void clear()
Clear the client list and the event list.
bool isWatchingEvent() const
Check if the gathering of client events is enabled.
void remove(Identity const &id)
Remove a client from the list.
AccessLock< std::recursive_mutex > acquireLock() const
Acquire a unique lock, with the ClientList mutex.
void clearClientEvent()
Clear the client event list.
void sendToAll(SocketUdp &socket, Packet &pck) const
Directly send a packet to every client in the list.
void watchEvent(bool on)
Enable or disable the gathering of client events.
DataList::iterator remove(DataList::const_iterator itPos, AccessLock< std::recursive_mutex > const &lock)
Remove a client from the list.
DataList::iterator begin(AccessLock< std::recursive_mutex > const &lock)
Get the begin iterator of the ClientList.
ClientSharedPtr get(Identity const &id) const
Get a client from the list.
void sendToAll(TransmitPacketPtr const &pck) const
Push a packet to every client in the list.
void add(Identity const &id, ClientSharedPtr const &newClient)
Add a client to the list.
std::size_t getSize() const
Get the number of clients in the list.
Event const & getClientEvent(std::size_t index) const
Get the client event with its index.
std::size_t getClientEventSize() const
Get the number of client events.
Definition C_packet.hpp:52
A wrapper for UDP sockets inheriting from Socket.
Definition C_socket.hpp:355
Represents an event on the client list (client added, client removed, ...).
Definition C_clientList.hpp:50
A class to represent a client or server identity with an IP address and a port.
Definition C_identity.hpp:31