MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ handle_client()

void Lila::Server::handle_client ( int  client_fd)
private

Handles communication with a single client.

Parameters
client_fdClient file descriptor

Definition at line 260 of file Server.cpp.

261{
262 int enable = 1;
263 if (setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char*>(&enable), sizeof(enable)) < 0) {
264 LILA_WARN(Emitter::SERVER, "Failed to set TCP_NODELAY on client fd " + std::to_string(client_fd) + ": " + socket_error_string(socket_errno()));
265 }
266
267 ClientInfo client_info {
268 .fd = client_fd,
269 .session_id = "",
270 .connected_at = std::chrono::system_clock::now()
271 };
272
273 {
274 std::unique_lock lock(m_clients_mutex);
275 m_connected_clients[client_fd] = client_info;
276 }
277
278 if (m_connect_handler) {
279 m_connect_handler(client_info);
280 }
281
282 m_event_bus.publish(StreamEvent { EventType::ClientConnected, client_info });
283
284 LILA_INFO(Emitter::SERVER, "Client connected fd: " + std::to_string(client_fd));
285
286 while (m_running) {
287 auto message = read_message(client_fd);
288
289 if (!message) {
290 if (message.error() != "timeout" && message.error() != "would_block") {
291 break;
292 }
293 std::this_thread::sleep_for(std::chrono::milliseconds(8));
294 continue;
295 }
296
297 if (message->empty())
298 continue;
299
300 if (message->starts_with('@')) {
301 process_control_message(client_fd, message->substr(1));
302 continue;
303 }
304
305 if (m_message_handler) {
306 auto response = m_message_handler(*message);
307 if (response) {
308 if (!send_message(client_fd, *response)) {
309 LILA_WARN(Emitter::SERVER, "Failed to send response to client fd " + std::to_string(client_fd));
310 break;
311 }
312 } else {
313 if (!send_message(client_fd, "{\"status\":\"error\",\"message\":\"" + response.error() + "\"}")) {
314 LILA_WARN(Emitter::SERVER, "Failed to send error response to client fd " + std::to_string(client_fd));
315 break;
316 }
317 }
318 }
319 }
320
321 cleanup_client(client_fd);
322}
#define LILA_WARN(emitter, msg)
#define LILA_INFO(emitter, msg)
void publish(const StreamEvent &event)
Publish an event to all subscribers of its type.
Definition EventBus.cpp:18
std::atomic< bool > m_running
Server running state.
Definition Server.hpp:164
std::expected< std::string, std::string > read_message(int client_fd)
Reads a message from a client.
Definition Server.cpp:351
std::shared_mutex m_clients_mutex
Mutex for client map.
Definition Server.hpp:173
std::unordered_map< int, ClientInfo > m_connected_clients
Map of connected clients.
Definition Server.hpp:174
MessageHandler m_message_handler
Handler for client messages.
Definition Server.hpp:167
bool send_message(int client_fd, std::string_view message)
Sends a message to a client.
Definition Server.cpp:400
EventBus m_event_bus
Event bus for publishing server events.
Definition Server.hpp:171
void process_control_message(int client_fd, std::string_view message)
Processes control messages (e.g., session, ping)
Definition Server.cpp:324
ConnectionHandler m_connect_handler
Handler for client connections.
Definition Server.hpp:168
void cleanup_client(int client_fd)
Cleans up resources for a disconnected client.
Definition Server.cpp:412
static std::string socket_error_string(int code)
Definition Server.cpp:33
static int socket_errno()
Definition Server.cpp:24

References cleanup_client(), Lila::ClientInfo::fd, LILA_INFO, LILA_WARN, m_clients_mutex, m_connect_handler, m_connected_clients, m_event_bus, m_message_handler, m_running, process_control_message(), Lila::EventBus::publish(), read_message(), send_message(), Lila::socket_errno(), and Lila::socket_error_string().

+ Here is the call graph for this function: