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 StormByte::Expected<void, ConnectionError> Start(const std::string& host, const unsigned short& port) noexcept;
63
67 virtual void Stop() noexcept;
68
69 protected:
70 std::vector<FutureBufferProcessor> m_input_pipeline;
71 std::vector<FutureBufferProcessor> m_output_pipeline;
72
78 Socket::Client& AddClient(Socket::Client&& client) noexcept;
79
84 void RemoveClient(Socket::Client& client) noexcept;
85
90 void RemoveClientAsync(Socket::Client& client) noexcept;
91
96 void DisconnectClient(Socket::Client& client) noexcept;
97
102 virtual bool Handshake(Socket::Client& client) noexcept;
103
110 virtual ExpectedFutureBuffer ProcessClientMessage(Socket::Client& client, FutureBuffer& message) noexcept = 0;
111
112 private:
113 std::thread m_accept_thread;
114 std::mutex m_clients_mutex;
115 std::vector<Socket::Client> m_clients;
116 std::mutex m_msg_threads_mutex;
117 std::unordered_map<Connection::Handler::Type, std::thread> m_msg_threads;
118
122 void DisconnectAllClients() noexcept;
123
127 void AcceptClients() noexcept;
128
133 void StartClientCommunication(Socket::Client& client) noexcept;
134
139 void HandleClientMessages(Socket::Client& client) noexcept;
140
147 ExpectedVoid SendClientReply(Socket::Client& client, FutureBuffer& message) noexcept;
148
155 ExpectedFutureBuffer ProcessInputMessagePipeline(Socket::Client& client, FutureBuffer& message) noexcept;
156
163 ExpectedFutureBuffer ProcessOutputMessagePipeline(Socket::Client& client, FutureBuffer& message) noexcept;
164 };
165}
The class representing a client.
Definition client.hxx:15
The class representing an error in the connection.
Definition exception.hxx:27
Represents a network endpoint, serving as a base class for both clients and servers.
Definition endpoint.hxx:17
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
std::future< Buffer::Simple > FutureBuffer
The future data type.
Definition typedefs.hxx:26
std::function< StormByte::Expected< FutureBuffer, ConnectionError >(Socket::Client &, FutureBuffer &)> FutureBufferProcessor
The future data function type.
Definition typedefs.hxx:34
StormByte::Expected< FutureBuffer, ConnectionError > ExpectedFutureBuffer
The expected future type.
Definition typedefs.hxx:29
StormByte::Expected< void, ConnectionError > ExpectedVoid
The expected void type.
Definition typedefs.hxx:30