StormByte C++ Library: Network module 1.1.0
StormByte-Network is the C++26 networking module of the StormByte suite.
Loading...
Searching...
No Matches
server.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Network.
5 *
6 * StormByte-Network is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte-Network is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte-Network. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
23
24#include <atomic>
25#include <mutex>
26#include <thread>
27#include <unordered_map>
28#include <deque>
29
33namespace StormByte::Network {
34 namespace Connection {
35 class Client;
36 }
37
38 namespace Socket {
39 class Server;
40 }
41
42 namespace Detail {
43 class Session;
44 class WorkerPool;
45 }
46
56 public:
62 Server(const DeserializePacketFunction& deserialize_packet_function, std::shared_ptr<Logger::Log> logger) noexcept;
63
67 Server(const Server& other) = delete;
68
72 Server(Server&& other) noexcept;
73
77 virtual ~Server() noexcept;
78
82 Server& operator=(const Server& other) = delete;
83
87 Server& operator=(Server&& other) noexcept;
88
96 bool Connect(const Connection::Protocol& protocol, const std::string& address, const unsigned short& port) override;
97
101 void Disconnect() noexcept override;
102
107 inline Connection::Status Status() const noexcept override {
108 return m_status.load();
109 }
110
111 protected:
116 void DisconnectClient(const std::string& uuid) noexcept;
117
118 private:
119 std::unique_ptr<Socket::Server> m_socket_server;
120 std::atomic<Connection::Status> m_status;
121 std::thread m_accept_thread;
122 Connection::HandlerType m_wakeup_read;
123 Connection::HandlerType m_wakeup_write;
124 std::unordered_map<std::string, std::shared_ptr<Detail::Session>> m_sessions;
125 std::unique_ptr<Detail::WorkerPool> m_pool;
126 enum class CompletionReason: unsigned short { Success, NullHandler, Error };
127 struct Completion {
128 std::string uuid;
129 PacketPointer packet;
130 CompletionReason reason;
131 };
132 std::deque<Completion> m_completions;
133 std::mutex m_completion_mutex;
134 enum class CommandType: unsigned short { DisconnectClient, DisconnectAll, Stop };
135 struct Command { CommandType type; std::string uuid; };
136 std::deque<Command> m_commands;
137 std::mutex m_command_mutex;
138
142 void AcceptClients() noexcept;
143
148 bool CreateWakeup() noexcept;
149
153 void SignalWakeup() noexcept;
154
158 void CloseWakeup() noexcept;
159
163 void AcceptOneClient() noexcept;
164
169 void ProcessSession(const std::shared_ptr<Detail::Session>& session, bool readable, bool writable) noexcept;
170
172 void PostCompletion(Completion completion) noexcept;
173
175 void DrainCompletions() noexcept;
176
178 void PostCommand(Command command) noexcept;
179
181 void DrainCommands() noexcept;
182
184 void DisconnectClientOnLoop(const std::string& uuid) noexcept;
185
192 virtual PacketPointer ProcessClientPacket(const std::string& client_uuid, PacketPointer packet) noexcept = 0;
193 };
194}
Shared base for Client and Server.
Definition endpoint.hxx:42
Abstract application server.
Definition server.hxx:55
Server(const DeserializePacketFunction &deserialize_packet_function, std::shared_ptr< Logger::Log > logger) noexcept
Construct with a packet factory and a logger.
void DisconnectClient(const std::string &uuid) noexcept
Disconnect a client by UUID.
Server(Server &&other) noexcept
Move constructor.
virtual ~Server() noexcept
Destructor (joins threads, disconnects clients).
Server(const Server &other)=delete
Copy constructor (deleted).
int HandlerType
Native socket handle (POSIX)
Definition typedefs.hxx:49
Network module of the StormByte suite.
Definition client.hxx:30
std::function< PacketPointer(Transport::Packet::OpcodeType, Buffer::Consumer, std::shared_ptr< Logger::Log >)> DeserializePacketFunction
Callback that builds a Packet from opcode + payload consumer.
Definition typedefs.hxx:68
std::shared_ptr< Transport::Packet > PacketPointer
Shared packet.
Definition typedefs.hxx:59
#define STORMBYTE_NETWORK_PUBLIC
Definition visibility.h:28