FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_clientList.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_CLIENTLIST_HPP_INCLUDED
18#define _FGE_C_CLIENTLIST_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "C_client.hpp"
22#include <deque>
23#include <memory>
24#include <mutex>
25#include <unordered_map>
26
27namespace fge::net
28{
29
30class SocketUdp;
31
32using ClientSharedPtr = std::shared_ptr<fge::net::Client>;
33
40{
41 enum Events : uint8_t
42 {
43 CLEVT_DELCLIENT = 0,
44 CLEVT_NEWCLIENT
45 };
46
47 fge::net::ClientListEvent::Events _event;
49};
50
56class FGE_API ClientList
57{
58public:
59 using ClientListData = std::unordered_map<fge::net::Identity, fge::net::ClientSharedPtr, fge::net::IdentityHash>;
60 using ClientEventList = std::deque<fge::net::ClientListEvent>;
61
62 ClientList() = default;
63 ~ClientList() = default;
64
68 void clear();
69
85 void sendToAll(fge::net::TransmissionPacketPtr const& pck);
86
93 void add(fge::net::Identity const& id, fge::net::ClientSharedPtr const& newClient);
99 void remove(fge::net::Identity const& id);
110 fge::net::ClientList::ClientListData::iterator remove(fge::net::ClientList::ClientListData::const_iterator itPos,
111 std::unique_lock<std::recursive_mutex> const& lock);
112
119 fge::net::ClientSharedPtr get(fge::net::Identity const& id) const;
120
130 std::unique_lock<std::recursive_mutex> acquireLock() const;
131
144 fge::net::ClientList::ClientListData::iterator begin(std::unique_lock<std::recursive_mutex> const& lock);
145 fge::net::ClientList::ClientListData::const_iterator
146 begin(std::unique_lock<std::recursive_mutex> const& lock) const;
147 fge::net::ClientList::ClientListData::iterator end(std::unique_lock<std::recursive_mutex> const& lock);
148 fge::net::ClientList::ClientListData::const_iterator end(std::unique_lock<std::recursive_mutex> const& lock) const;
149
155 std::size_t getSize() const;
156
164 void watchEvent(bool on);
170 bool isWatchingEvent() const;
171
183 fge::net::ClientListEvent const& getClientEvent(std::size_t index) const;
189 std::size_t getClientEventSize() const;
197
198private:
199 fge::net::ClientList::ClientListData g_data;
200 fge::net::ClientList::ClientEventList g_events;
201 mutable std::recursive_mutex g_mutex;
202 bool g_enableClientEventsFlag = false;
203};
204
205} // namespace fge::net
206
207
208#endif // _FGE_C_CLIENTLIST_HPP_INCLUDED
A list of clients used by a server.
Definition C_clientList.hpp:57
void remove(fge::net::Identity const &id)
Remove a client from the list.
void clear()
Clear the client list and the event list.
bool isWatchingEvent() const
Check if the gathering of client events is enabled.
void clearClientEvent()
Clear the client event list.
fge::net::ClientSharedPtr get(fge::net::Identity const &id) const
Get a client from the list.
void sendToAll(fge::net::TransmissionPacketPtr const &pck)
Push a packet to every clients in the list.
void sendToAll(fge::net::SocketUdp &socket, fge::net::Packet &pck)
Directly send a packet to every clients in the list.
void watchEvent(bool on)
Enable or disable the gathering of client events.
fge::net::ClientListEvent const & getClientEvent(std::size_t index) const
Get the client event with its index.
void add(fge::net::Identity const &id, fge::net::ClientSharedPtr const &newClient)
Add a client to the list.
fge::net::ClientList::ClientListData::iterator begin(std::unique_lock< std::recursive_mutex > const &lock)
Get the begin iterator of the ClientList.
fge::net::ClientList::ClientListData::iterator remove(fge::net::ClientList::ClientListData::const_iterator itPos, std::unique_lock< std::recursive_mutex > const &lock)
Remove a client from the list.
std::unique_lock< std::recursive_mutex > acquireLock() const
Acquire a unique lock, with the ClientList mutex.
void pushClientEvent(fge::net::ClientListEvent const &evt)
Manually push a client event.
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_packet.hpp:70
A wrapper for UDP sockets inheriting from Socket.
Definition C_socket.hpp:267
Represents an event on the client list (client added, client removed, ...)
Definition C_clientList.hpp:40
A class to represent a client or server identity with an IP address and a port.
Definition C_identity.hpp:31