MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Server.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <asio.hpp>
4#include <expected>
5
6#include "EventBus.hpp"
7
8namespace Lila {
9
10class ClientSession;
11
12/**
13 * @class Server
14 * @brief TCP server for interactive live coding sessions in MayaFlux
15 *
16 * Async TCP server built on asio. Accepts newline-delimited messages,
17 * dispatches them to a message handler, and returns the response.
18 * Control messages prefixed with '@' are handled internally.
19 */
20class LILA_API Server {
21public:
22 /// @brief Handler for processing incoming client messages
23 using MessageHandler = std::function<std::expected<std::string, std::string>(std::string_view)>;
24
25 /// @brief Handler for client connection/disconnection events
26 using ConnectionHandler = std::function<void(const ClientInfo&)>;
27
28 /// @brief Handler for server start events
29 using StartHandler = std::function<void()>;
30
31 explicit Server(int port = 9090);
33
34 Server(const Server&) = delete;
35 Server& operator=(const Server&) = delete;
36
37 [[nodiscard]] bool start() noexcept;
38 void stop() noexcept;
39 [[nodiscard]] bool is_running() const { return m_running.load(std::memory_order_acquire); }
40
41 void set_message_handler(MessageHandler handler) { m_message_handler = std::move(handler); }
42 void on_client_connected(ConnectionHandler handler) { m_connect_handler = std::move(handler); }
43 void on_client_disconnected(ConnectionHandler handler) { m_disconnect_handler = std::move(handler); }
44 void on_server_started(StartHandler handler) { m_start_handler = std::move(handler); }
45
46 EventBus& event_bus() { return m_event_bus; }
47 const EventBus& event_bus() const noexcept { return m_event_bus; }
48
49 void broadcast_event(const StreamEvent& event,
50 std::optional<std::string_view> target_session = std::nullopt);
51 void broadcast_to_all(std::string_view message);
52
53 void set_client_session(int client_id, std::string session_id);
54 [[nodiscard]] std::optional<std::string> get_client_session(int client_id) const;
55 [[nodiscard]] std::vector<ClientInfo> get_connected_clients() const;
56
57private:
59 void register_session(const std::shared_ptr<ClientSession>& session);
60 void remove_session(int client_id);
61 void process_control_message(int client_id, std::string_view message);
62
63 friend class ClientSession;
64
65 int m_port;
66 std::atomic<bool> m_running { false };
67 int m_next_client_id { 1 };
68
69 asio::io_context m_io_context;
70 std::unique_ptr<asio::ip::tcp::acceptor> m_acceptor;
71 std::thread m_io_thread;
72
78
79 mutable std::shared_mutex m_clients_mutex;
80 std::unordered_map<int, std::shared_ptr<ClientSession>> m_sessions;
81};
82
83} // namespace Lila
Thread-safe event subscription and publishing system for Lila.
Definition EventBus.hpp:180
std::thread m_io_thread
Definition Server.hpp:71
Server(const Server &)=delete
void broadcast_to_all(std::string_view message)
ConnectionHandler m_disconnect_handler
Definition Server.hpp:75
void process_control_message(int client_id, std::string_view message)
asio::io_context m_io_context
Definition Server.hpp:69
std::shared_mutex m_clients_mutex
Definition Server.hpp:79
void remove_session(int client_id)
std::unique_ptr< asio::ip::tcp::acceptor > m_acceptor
Definition Server.hpp:70
void set_message_handler(MessageHandler handler)
Definition Server.hpp:41
bool start() noexcept
void set_client_session(int client_id, std::string session_id)
std::optional< std::string > get_client_session(int client_id) const
std::function< void(const ClientInfo &)> ConnectionHandler
Handler for client connection/disconnection events.
Definition Server.hpp:26
void start_accept()
std::unordered_map< int, std::shared_ptr< ClientSession > > m_sessions
Definition Server.hpp:80
void on_client_disconnected(ConnectionHandler handler)
Definition Server.hpp:43
void register_session(const std::shared_ptr< ClientSession > &session)
Server & operator=(const Server &)=delete
Server(int port=9090)
EventBus & event_bus()
Definition Server.hpp:46
MessageHandler m_message_handler
Definition Server.hpp:73
void on_client_connected(ConnectionHandler handler)
Definition Server.hpp:42
StartHandler m_start_handler
Definition Server.hpp:76
void broadcast_event(const StreamEvent &event, std::optional< std::string_view > target_session=std::nullopt)
EventBus m_event_bus
Definition Server.hpp:77
ConnectionHandler m_connect_handler
Definition Server.hpp:74
std::function< void()> StartHandler
Handler for server start events.
Definition Server.hpp:29
std::vector< ClientInfo > get_connected_clients() const
void on_server_started(StartHandler handler)
Definition Server.hpp:44
std::function< std::expected< std::string, std::string >(std::string_view)> MessageHandler
Handler for processing incoming client messages.
Definition Server.hpp:23
const EventBus & event_bus() const noexcept
Definition Server.hpp:47
TCP server for interactive live coding sessions in MayaFlux.
Definition Server.hpp:20
Information about a connected client.
Definition EventBus.hpp:58
Represents a published event in the system.
Definition EventBus.hpp:121