StormByte-Network 4.0.0
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 original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Network source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, and in particular the bundled StormByte Buffer tree), which
23 * remains under its own license.
24 *
25 * Neither license grants any patent rights. Any patent licenses required
26 * to use this software or third-party components must be obtained separately
27 * from the patent holders.
28 *
29 * StormByte-Network is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * version 3 along with StormByte-Network. If not, see
36 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 *
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
39 */
40
41#pragma once
42
43#include <StormByte/crypto/tls/session.hxx>
50#include <StormByte/safe/atomic.hxx>
51#include <StormByte/safe/deque.hxx>
52#include <StormByte/safe/map.hxx>
53#include <StormByte/safe/mutex.hxx>
54#include <StormByte/safe/optional.hxx>
55#include <StormByte/safe/pair.hxx>
56#include <StormByte/safe/pointers.hxx>
57#include <StormByte/safe/string.hxx>
58#include <StormByte/safe/thread.hxx>
59#include <StormByte/safe/vector.hxx>
60#include <StormByte/size.hxx>
61
62#include <cstdint>
63#include <string_view>
64
69namespace StormByte {
74 namespace Network {
79 namespace Connection {
84 class Client;
85 }
86
91 namespace Socket {
96 class Server;
97 }
98
103 namespace Detail {
107 class TLSConfiguration;
112 class Session;
113
118 class WorkerPool;
119
124 namespace RemoteFile {
129 class Host;
130
135 class MountRegistry;
136 }
137 }
138
149 public:
156
161 Server(const Server& other) = delete;
162
168 Server(Server&& other) noexcept;
169
173 virtual ~Server() noexcept;
174
180 Server& operator=(const Server& other) = delete;
181
188 Server& operator=(Server&& other) noexcept;
189
197 bool Connect(const Connection::Protocol& protocol, std::string_view address, const unsigned short& port) override;
198
209 bool ConfigureTLS(const Crypto::TLS::ServerConfiguration& configuration,
210 bool refresh_validation_time = true) noexcept;
211
216 bool DisableTLS() noexcept;
217
221 void Disconnect() noexcept override;
222
227 Connection::Status Status() const noexcept override;
228
233 StormByte::Safe::Shared<ServerTelemetry> Telemetry() const noexcept;
234
242 bool Announce(const Connection::Protocol& protocol, unsigned short discovery_port) noexcept;
243
247 void StopAnnounce() noexcept;
248
249
250 protected:
261 virtual bool AllowIncomingOpcode(std::string_view uuid, Transport::Packet::OpcodeType opcode) const noexcept;
262
270 virtual bool AllowOutgoingOpcode(std::string_view uuid, Transport::Packet::OpcodeType opcode) const noexcept;
271
279 RemoteFileMount MountRemoteFileReader(std::string_view client_uuid,
280 std::string_view path, std::uint16_t maximum_timeout_seconds = 30) noexcept;
281
289 RemoteFileMount MountRemoteFileWriter(std::string_view client_uuid,
290 std::string_view path, std::uint16_t maximum_timeout_seconds = 30) noexcept;
291
296 void DisconnectClient(std::string_view uuid) noexcept;
297
304 Safe::Optional<PeerAddress> GetPeerAddress(std::string_view uuid);
305
313 void DisconnectClientAfterReply(std::string_view uuid) noexcept;
314
325 virtual bool OnClientConnected(std::string_view uuid) noexcept;
326
336 virtual void OnClientDisconnected(std::string_view uuid) noexcept;
337
343 virtual StormByte::Safe::Binary OnDiscoveryQuery(const StormByte::Safe::Binary& query) const noexcept;
344
345
346 private:
347 Safe::Shared<const Detail::TLSConfiguration> m_tls_configuration;
351 using CompletionReason = Detail::CompletionReason;
352
356 using Completion = Detail::Completion;
357
361 using MountedRemoteFile = Detail::MountedRemoteFile;
362
363 static constexpr Size MAX_REMOTE_FILE_CHANNELS{128};
364
368 using CommandType = Detail::CommandType;
369
373 using Command = Detail::Command;
374
378 using WakeupChannel = Detail::WakeupChannel;
379
380 Safe::Unique<Socket::Server> m_socket_server;
382 Safe::Thread m_accept_thread;
387 Safe::Deque<Completion> m_completions;
388 Connection::Protocol m_protocol{Connection::Protocol::IPv4};
389 Safe::String m_bind_address;
390 Safe::Vector<MountedRemoteFile> m_remote_files;
391 Safe::Map<Safe::String, Detail::RemotePlaneRegistration> m_remote_planes;
392 Safe::Shared<Detail::RemoteFile::MountRegistry> m_remote_file_registry;
393 Safe::Mutex m_remote_file_mutex;
394 Safe::Mutex m_completion_mutex;
395 Safe::Deque<Command> m_commands;
396 Safe::Mutex m_command_mutex;
397 Safe::Shared<ServerTelemetry> m_telemetry;
398 Safe::Thread m_discovery_thread;
399 Safe::Atomic<bool> m_discovery_stop{false};
400
407 static Server&& StopForMove(Server& other) noexcept;
408
416 void MoveStoppedState(Server& other) noexcept;
417
421 void AcceptClients() noexcept;
422
427 bool CreateWakeup() noexcept;
428
432 void SignalWakeup() noexcept;
433
437 void CloseWakeup() noexcept;
438
442 void AcceptOneClient() noexcept;
443
450 void ProcessSession(const Safe::Shared<Detail::Session>& session, bool readable, bool writable) noexcept;
451
456 void PostCompletion(Completion completion) noexcept;
457
461 void DrainCompletions() noexcept;
462
467 void PostCommand(Command command) noexcept;
468
472 void DrainCommands() noexcept;
473
478 void DisconnectClientOnLoop(std::string_view uuid) noexcept;
479
483 void RevokeAllRemoteFiles() noexcept;
484
491 void ProcessRemotePlane(const Safe::Shared<Detail::RemoteFile::Host>& host,
492 bool readable, bool writable) noexcept;
493
500 virtual PacketPointer ProcessClientPacket(std::string_view client_uuid, PacketPointer packet) noexcept = 0;
501 };
502 }
503}
504
STORMBYTE_DECLARE_MAYBE_SAFE(StormByte::Buffer::Bridge)
Forward declaration of a Network-owned peer data plane.
Forward declaration of the remote-file mount registry.
Forward declaration of the listening socket implementation.
Forward declaration of a client parser session.
Caller-owned callback that builds a packet from opcode and payload.
Definition typedefs.hxx:143
Endpoint(DeserializePacketFunction deserialize_packet_function, StormByte::Safe::Shared< Logger::Log > logger) noexcept
Construct with a packet factory and a logger.
Public, validated descriptor for attaching to one remote file channel.
Definition remote_file_mount.hxx:69
Server-wide cumulative telemetry aggregated across all sessions.
Definition server_telemetry.hxx:71
void DisconnectClient(std::string_view uuid) noexcept
Disconnect a client by UUID.
virtual StormByte::Safe::Binary OnDiscoveryQuery(const StormByte::Safe::Binary &query) const noexcept
Decide whether to answer a discovery query.
virtual void OnClientDisconnected(std::string_view uuid) noexcept
Release derived state when an application session is removed.
Connection::Status Status() const noexcept override
Listener / server status.
bool DisableTLS() noexcept
Restore plaintext while the server is stopped.
virtual bool AllowOutgoingOpcode(std::string_view uuid, Transport::Packet::OpcodeType opcode) const noexcept
Admit a response opcode before output transformation.
Server(Server &&other) noexcept
Stop the source before transferring its inactive state.
Safe::Optional< PeerAddress > GetPeerAddress(std::string_view uuid)
Get the remote address for an active client session.
virtual ~Server() noexcept
Destructor (joins threads, disconnects clients).
bool Announce(const Connection::Protocol &protocol, unsigned short discovery_port) noexcept
Start listening for discovery queries on discovery_port.
Server(DeserializePacketFunction deserialize_packet_function, StormByte::Safe::Shared< Logger::Log > logger)
Construct with a packet factory and a logger.
StormByte::Safe::Shared< ServerTelemetry > Telemetry() const noexcept
Shared live telemetry aggregated across this Server's sessions.
RemoteFileMount MountRemoteFileWriter(std::string_view client_uuid, std::string_view path, std::uint16_t maximum_timeout_seconds=30) noexcept
Mount a server-owned file for exclusive writing after application authorization.
RemoteFileMount MountRemoteFileReader(std::string_view client_uuid, std::string_view path, std::uint16_t maximum_timeout_seconds=30) noexcept
Mount a server-owned file for an already authorized client session.
void StopAnnounce() noexcept
Stop the discovery listener.
Server(const Server &other)=delete
Copy constructor (deleted).
virtual bool AllowIncomingOpcode(std::string_view uuid, Transport::Packet::OpcodeType opcode) const noexcept
Admit an application opcode before its pipeline and factory run.
void Disconnect() noexcept override
Stop accept, disconnect clients and close the listener.
bool Connect(const Connection::Protocol &protocol, std::string_view address, const unsigned short &port) override
Bind, listen and start the accept thread.
void DisconnectClientAfterReply(std::string_view uuid) noexcept
Send the current handler's reply, then close and discard pending input.
bool ConfigureTLS(const Crypto::TLS::ServerConfiguration &configuration, bool refresh_validation_time=true) noexcept
Require TLS for new application and remote-file connections.
virtual bool OnClientConnected(std::string_view uuid) noexcept
Admit and initialize a newly registered application session.
Network-owned native wakeup handles, defined out-of-line.
Forward declaration of the packet worker pool.
Connection namespace.
@ Disconnected
Closed / idle.
Definition status.hxx:55
CommandType
Event-loop action requested by a worker or callback.
Definition server_state.hxx:196
Private native transport implementation.
Transport namespace.
Network module of the StormByte suite.
Root namespace of the StormByte suite.
STL namespace.
Event-loop command and optional target client UUID.
Definition server_state.hxx:209
Numeric IP address and source port of a connected peer.
Definition peer_address.hxx:51
#define STORMBYTE_NETWORK_PUBLIC
Definition visibility.h:51