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#include <StormByte/network/typedefs.hxx>
5
6#include <mutex>
7#include <thread>
8#include <unordered_map>
9#include <vector>
10
11namespace StormByte::Network {
16 class STORMBYTE_NETWORK_PUBLIC Server: public EndPoint {
17 public:
23 Server(Connection::Protocol protocol, std::shared_ptr<Connection::Handler> handler, std::shared_ptr<Logger> logger) noexcept;
24
29 Server(const Server& other) = delete;
30
35 Server(Server&& other) noexcept = default;
36
40 virtual ~Server() noexcept override;
41
47 Server& operator=(const Server& other) = delete;
48
54 Server& operator=(Server&& other) noexcept = default;
55
62 virtual ExpectedVoid Connect(const std::string& host, const unsigned short& port) noexcept override;
63
67 virtual void Disconnect() noexcept override;
68
69 protected:
75 Socket::Client& AddClient(Socket::Client&& client) noexcept;
76
81 void RemoveClient(Socket::Client& client) noexcept;
82
87 void RemoveClientAsync(Socket::Client& client) noexcept;
88
93 void DisconnectClient(Socket::Client& client) noexcept;
94
105 virtual Expected<void, PacketError> ProcessClientPacket(Socket::Client& client, const Packet& packet) noexcept = 0;
106
107 private:
108 std::thread m_accept_thread;
109 std::mutex m_clients_mutex;
110 std::vector<Socket::Client> m_clients;
111 std::mutex m_msg_threads_mutex;
112 std::unordered_map<Connection::Handler::Type, std::thread> m_msg_threads;
113
117 void DisconnectAllClients() noexcept;
118
122 void AcceptClients() noexcept;
123
128 void StartClientCommunication(Socket::Client& client) noexcept;
129
134 void HandleClientMessages(Socket::Client& client) noexcept;
135 };
136}
The class representing a client.
Definition client.hxx:15
Represents a network endpoint, serving as a base class for both clients and servers.
Definition endpoint.hxx:20
The class representing an error in the packet.
Definition exception.hxx:51
Represents a network packet.
Definition packet.hxx:21
A server class.
Definition server.hxx:16
Server(Server &&other) noexcept=default
Move constructor.
virtual ~Server() noexcept override
Destructor.
Server(Connection::Protocol protocol, std::shared_ptr< Connection::Handler > handler, std::shared_ptr< Logger > logger) noexcept
Constructor.
Server(const Server &other)=delete
Copy constructor (deleted).
The namespace containing connection items.
Contains all the network-related classes and utilities.
Definition client.hxx:10
StormByte::Expected< void, ConnectionError > ExpectedVoid
The expected void type.
Definition typedefs.hxx:32