StormByte C++ Library: Network module 0.0.9999
StormByte-Network is a StormByte library module for handling and create network connections
Loading...
Searching...
No Matches
server.hxx
1#pragma once
2
3#include <StormByte/network/endpoint.hxx>
4
5#include <atomic>
6#include <mutex>
7#include <thread>
8#include <unordered_map>
9
15namespace StormByte::Network {
16 namespace Connection {
17 class Client;
18 }
19
20 namespace Socket {
21 class Server;
22 }
23
49 class STORMBYTE_NETWORK_PUBLIC Server: private Endpoint {
50 public:
58 Server(const DeserializePacketFunction& deserialize_packet_function, std::shared_ptr<Logger::Log> logger) noexcept;
59
63 Server(const Server& other) = delete;
64
68 Server(Server&& other) noexcept = default;
69
77 virtual ~Server() noexcept;
78
82 Server& operator=(const Server& other) = delete;
83
87 Server& operator=(Server&& other) noexcept = default;
88
97 bool Connect(const Connection::Protocol& protocol, const std::string& address, const unsigned short& port) override;
98
102 void Disconnect() noexcept override;
103
109 inline Connection::Status Status() const noexcept override {
110 return m_status.load();
111 }
112
113 protected:
119 void DisconnectClient(const std::string& uuid) noexcept;
120
121 private:
122 std::unique_ptr<Socket::Server> m_socket_server;
123 std::atomic<Connection::Status> m_status;
124 std::thread m_accept_thread;
125 std::unordered_map<std::string, std::shared_ptr<Connection::Client>> m_clients;
126 std::unordered_map<std::string, std::thread> m_handle_msg_threads;
127 std::mutex m_mutex;
128
132 void AcceptClients() noexcept;
133
139 void HandleClientCommunication(const std::string& client_uuid) noexcept;
140
152 virtual PacketPointer ProcessClientPacket(const std::string& client_uuid, PacketPointer packet) noexcept = 0;
153 };
154}
Abstract base for network endpoints (clients/servers).
Definition endpoint.hxx:26
Abstract base for application-specific servers.
Definition server.hxx:49
Server(const DeserializePacketFunction &deserialize_packet_function, std::shared_ptr< Logger::Log > logger) noexcept
Constructs a Server instance.
Server(Server &&other) noexcept=default
Move constructor.
void DisconnectClient(const std::string &uuid) noexcept
Disconnects a client by UUID.
virtual ~Server() noexcept
Virtual destructor.
Server(const Server &other)=delete
Copy constructor (deleted).
The namespace containing connection items.
Network-related classes used by the StormByte networking subsystem.
std::function< PacketPointer(Transport::Packet::OpcodeType, Buffer::Consumer, std::shared_ptr< Logger::Log >)> DeserializePacketFunction
The deserialize packet function type.
Definition typedefs.hxx:43
std::shared_ptr< Transport::Packet > PacketPointer
The packet pointer type.
Definition typedefs.hxx:38