Handles communication with a single client.
261{
262 int enable = 1;
263 if (setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char*>(&enable), sizeof(enable)) < 0) {
265 }
266
267 ClientInfo client_info {
268 .fd = client_fd,
269 .session_id = "",
270 .connected_at = std::chrono::system_clock::now()
271 };
272
273 {
276 }
277
280 }
281
283
284 LILA_INFO(Emitter::SERVER,
"Client connected fd: " + std::to_string(client_fd));
285
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('@')) {
302 continue;
303 }
304
307 if (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
322}
void publish(const StreamEvent &event)
Publish an event to all subscribers of its type.
std::atomic< bool > m_running
Server running state.
std::expected< std::string, std::string > read_message(int client_fd)
Reads a message from a client.
std::shared_mutex m_clients_mutex
Mutex for client map.
std::unordered_map< int, ClientInfo > m_connected_clients
Map of connected clients.
MessageHandler m_message_handler
Handler for client messages.
bool send_message(int client_fd, std::string_view message)
Sends a message to a client.
EventBus m_event_bus
Event bus for publishing server events.
void process_control_message(int client_fd, std::string_view message)
Processes control messages (e.g., session, ping)
ConnectionHandler m_connect_handler
Handler for client connections.
void cleanup_client(int client_fd)
Cleans up resources for a disconnected client.
static std::string socket_error_string(int code)
static int socket_errno()