FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_clientList.hpp
1/*
2 * Copyright 2025 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 "FastEngine/network/C_netCommand.hpp"
25#include <deque>
26#include <memory>
27#include <mutex>
28#include <unordered_map>
29
30namespace fge::net
31{
32
33class SocketUdp;
34
35using ClientSharedPtr = std::shared_ptr<Client>;
36
43{
44 enum Events : uint8_t
45 {
46 CLEVT_DELCLIENT = 0,
47 CLEVT_NEWCLIENT
48 };
49
50 Events _event;
51 Identity _id;
52};
53
59class FGE_API ClientList
60{
61public:
62 struct Data
63 {
64 inline explicit Data(ClientSharedPtr client) :
65 _client(std::move(client))
66 {}
67
68 ClientSharedPtr _client;
69 PacketDefragmentation _defragmentation;
70 CommandQueue _commands;
71
72 std::future<uint16_t> _mtuFuture;
73 };
74
75 using DataList = std::unordered_map<Identity, Data, IdentityHash>;
76 using EventList = std::deque<ClientListEvent>;
77
78 ClientList() = default;
79 ~ClientList() = default;
80
84 void clear();
85
95 void sendToAll(SocketUdp& socket, Packet& pck) const;
101 void sendToAll(TransmitPacketPtr const& pck) const;
102
109 void add(Identity const& id, ClientSharedPtr const& newClient);
115 void remove(Identity const& id);
126 DataList::iterator remove(DataList::const_iterator itPos, AccessLock<std::recursive_mutex> const& lock);
127
134 ClientSharedPtr get(Identity const& id) const;
135 Data const* getData(Identity const& id) const;
136 Data* getData(Identity const& id);
137
148
161 DataList::iterator begin(AccessLock<std::recursive_mutex> const& lock);
162 DataList::const_iterator begin(AccessLock<std::recursive_mutex> const& lock) const;
163 DataList::iterator end(AccessLock<std::recursive_mutex> const& lock);
164 DataList::const_iterator end(AccessLock<std::recursive_mutex> const& lock) const;
165
171 std::size_t getSize() const;
172
180 void watchEvent(bool on);
186 bool isWatchingEvent() const;
187
199 ClientListEvent const& getClientEvent(std::size_t index) const;
205 std::size_t getClientEventSize() const;
213
214private:
215 DataList g_data;
216 EventList g_events;
217 mutable std::recursive_mutex g_mutex;
218 bool g_enableClientEventsFlag = false;
219};
220
221} // namespace fge::net
222
223
224#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 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.
void pushClientEvent(ClientListEvent const &evt)
Manually push a client event.
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.
ClientListEvent const & getClientEvent(std::size_t index) const
Get the client event with its index.
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.
std::size_t getClientEventSize() const
Get the number of client events.
Definition C_protocol.hpp:287
Definition C_packet.hpp:52
A wrapper for UDP sockets inheriting from Socket.
Definition C_socket.hpp:348
Represents an event on the client list (client added, client removed, ...)
Definition C_clientList.hpp:43
Definition C_clientList.hpp:63
A class to represent a client or server identity with an IP address and a port.
Definition C_identity.hpp:31