MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Lila::Server Class Reference

TCP server for interactive live coding sessions in MayaFlux. More...

#include <Server.hpp>

+ Collaboration diagram for Lila::Server:

Public Types

using MessageHandler = std::function< std::expected< std::string, std::string >(std::string_view)>
 Handler for processing incoming client messages.
 
using ConnectionHandler = std::function< void(const ClientInfo &)>
 Handler for client connection/disconnection events.
 
using StartHandler = std::function< void()>
 Handler for server start events.
 

Public Member Functions

 Server (int port=9090)
 Constructs a Server instance.
 
 ~Server ()
 Destructor; stops the server if running.
 
bool start () noexcept
 Starts the server and begins accepting clients.
 
void stop () noexcept
 Stops the server and disconnects all clients.
 
bool is_running () const
 Checks if the server is currently running.
 
void set_message_handler (MessageHandler handler)
 Sets the handler for incoming client messages.
 
void on_client_connected (ConnectionHandler handler)
 Registers a handler for client connection events.
 
void on_client_disconnected (ConnectionHandler handler)
 Registers a handler for client disconnection events.
 
void on_server_started (StartHandler handler)
 Registers a handler for server start events.
 
EventBusevent_bus ()
 Accesses the event bus for subscribing to server events.
 
const EventBusevent_bus () const noexcept
 Const access to the event bus.
 
void broadcast_event (const StreamEvent &event, std::optional< std::string_view > target_session=std::nullopt)
 Broadcasts an event to connected clients.
 
void broadcast_to_all (std::string_view message)
 Broadcasts a raw message to all connected clients.
 
void set_client_session (int client_fd, std::string session_id)
 Sets the session ID for a client.
 
std::optional< std::string > get_client_session (int client_fd) const
 Gets the session ID for a client.
 
std::vector< ClientInfoget_connected_clients () const
 Gets a list of all currently connected clients.
 

Private Member Functions

void server_loop (const ServerThread::StopToken &stop_token)
 Main server loop; accepts and manages clients.
 
void handle_client (int client_fd)
 Handles communication with a single client.
 
std::expected< std::string, std::string > read_message (int client_fd)
 Reads a message from a client.
 
bool send_message (int client_fd, std::string_view message)
 Sends a message to a client.
 
void process_control_message (int client_fd, std::string_view message)
 Processes control messages (e.g., session, ping)
 
void cleanup_client (int client_fd)
 Cleans up resources for a disconnected client.
 

Private Attributes

int m_port
 TCP port to listen on.
 
int m_server_fd { -1 }
 Server socket file descriptor.
 
std::atomic< bool > m_running { false }
 Server running state.
 
ServerThread m_server_thread
 Server thread.
 
MessageHandler m_message_handler
 Handler for client messages.
 
ConnectionHandler m_connect_handler
 Handler for client connections.
 
ConnectionHandler m_disconnect_handler
 Handler for client disconnections.
 
StartHandler m_start_handler
 Handler for server start.
 
EventBus m_event_bus
 Event bus for publishing server events.
 
std::shared_mutex m_clients_mutex
 Mutex for client map.
 
std::unordered_map< int, ClientInfom_connected_clients
 Map of connected clients.
 

Detailed Description

TCP server for interactive live coding sessions in MayaFlux.

The Server class provides the underlying network infrastructure for Lila's live coding capabilities. It manages client connections, message handling, session management, and event broadcasting. While end users interact with MayaFlux through higher-level interfaces, Server enables real-time code evaluation and communication between clients and the interpreter.

Core Responsibilities

  • Accepts TCP client connections and manages their lifecycle
  • Receives code snippets or control messages from clients
  • Dispatches messages to the interpreter and returns results
  • Tracks client sessions and broadcasts events (e.g., connection, evaluation, errors)
  • Provides hooks for connection, disconnection, and server lifecycle events

Usage Flow

  1. Construct a Server instance with a desired port (default: 9090)
  2. Set message and connection handlers as needed
  3. Call start() to begin listening for clients
  4. Use stop() to gracefully shut down the server
  5. Use event_bus() to subscribe to server events for monitoring or integration

The Server is designed for internal use by Lila and is not intended for direct user interaction.

Definition at line 41 of file Server.hpp.


The documentation for this class was generated from the following files: