ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/network/websocket/server.c
Go to the documentation of this file.
1
9
10/* Storage capacities in elements; text buffers include the terminating NUL. */
11#define NETWORK_WEBSOCKET_SERVER_WS_TRANSPORT_NAME_BUFFER_SIZE 64
12#define NETWORK_WEBSOCKET_SERVER_PROTO_VER_BUFFER_SIZE 16
13#define NETWORK_WEBSOCKET_SERVER_SSL_ERR_BUFFER_SIZE 256
14#define NETWORK_WEBSOCKET_SERVER_WS_PORT_NAME_BUFFER_SIZE 32
15
18
20#include <ascii-chat/version.h>
22#include <time.h>
23#include <yyjson.h>
25#include <ascii-chat/log/log.h>
27#include <ascii-chat/common.h>
28#include <libplatform/compiler.h>
29#include <libplatform/system.h>
30#include <libplatform/terminal.h>
32#include <ascii-chat/buffer.h>
33#include <libplatform/mutex.h>
34#include <libplatform/cond.h>
39#include <libwebsockets.h>
40#include <string.h>
41#include <errno.h>
42#include <openssl/err.h>
43#ifndef _WIN32
44#include <sys/socket.h>
45#include <netinet/tcp.h>
46#include <netinet/in.h>
47#include <unistd.h>
48#ifndef R_OK
49#define R_OK 4
50#endif
51#endif
52
53// Shared internal types (websocket_recv_msg_t, websocket_transport_data_t)
56
85
86// Forward declaration for websocket_protocols (defined later in file)
87static struct lws_protocols websocket_protocols[];
88
89static void websocket_configure_socket(struct lws *wsi) {
90 // Optimize TCP for high-throughput large message transfer.
91 // TCP delayed ACK (default ~40ms) causes the sender to stall waiting for ACKs,
92 // creating ~30ms gaps between 128KB chunk deliveries for large messages.
93 // TCP_QUICKACK forces immediate ACKs so the sender can push data continuously (Linux only).
94 // TCP_NODELAY disables Nagle's algorithm for the send path.
95#ifdef __linux__
96 {
97 int fd = lws_get_socket_fd(wsi);
98 if (fd >= 0) {
99 int quickack = 1;
100 if (setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &quickack, sizeof(quickack)) < 0) {
101 log_warn("Failed to set TCP_QUICKACK: %s", SAFE_STRERROR(errno));
102 }
103 int nodelay = 1;
104 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) {
105 log_warn("Failed to set TCP_NODELAY: %s", SAFE_STRERROR(errno));
106 }
107 // Do NOT set SO_RCVBUF/SO_SNDBUF manually.
108 // Setting SO_RCVBUF disables TCP autotuning on Linux, which locks the receive buffer
109 // at the rmem_default (212KB). Without manual override, the kernel can autotune up to
110 // tcp_rmem max (typically 6MB), allowing the entire 921KB video frame to fit in one
111 // TCP window without flow control stalls.
112 int actual_rcv = 0, actual_snd = 0;
113 socklen_t optlen = sizeof(int);
114 getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &actual_rcv, &optlen);
115 getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &actual_snd, &optlen);
116 log_debug("[WS_SOCKET] TCP_QUICKACK=1, TCP_NODELAY=1, SO_RCVBUF=%d (autotuned), SO_SNDBUF=%d", actual_rcv, actual_snd);
117 }
118 }
119#else
120 (void)wsi;
121#endif
122}
123
124static asciichat_error_t websocket_establish_connection(struct lws *wsi, websocket_connection_data_t *conn_data) {
125 // New WebSocket connection established
126 uint64_t established_ns = time_get_ns();
127 log_debug("🔵🔵🔵 LWS_CALLBACK_ESTABLISHED FIRED! wsi=%p", (void *)wsi);
128 log_debug("[LWS_CALLBACK_ESTABLISHED] WebSocket client connection established at timestamp=%llu", (unsigned long long)established_ns);
129
130 // Get server instance from protocol user data
131 const struct lws_protocols *protocol = lws_get_protocol(wsi);
132 log_debug("[LWS_CALLBACK_ESTABLISHED] Got protocol: %p", (void *)protocol);
133 if (!protocol || !protocol->user) {
134 log_error("[LWS_CALLBACK_ESTABLISHED] FAILED: Missing protocol user data (protocol=%p, user=%p)", (void *)protocol, protocol ? protocol->user : NULL);
135 return SET_ERRNO(ERROR_INVALID_STATE, "WebSocket protocol user data is missing");
136 }
137 websocket_server_t *server = (websocket_server_t *)protocol->user;
138 log_debug("[LWS_CALLBACK_ESTABLISHED] Got server: %p, handler: %p", (void *)server, (void *)server->handler);
139
140 // Initialize connection data
141 conn_data->server = server;
142 conn_data->transport = NULL;
143 conn_data->handler_started = false;
144 conn_data->pending_send_data = NULL;
145 conn_data->pending_send_len = 0;
146 conn_data->pending_send_offset = 0;
147 conn_data->has_pending_send = false;
150
151 // Get client address
152 char client_ip[IP_ADDRESS_BUFFER_SIZE] = {0};
153 if (!lws_get_peer_simple(wsi, client_ip, sizeof(client_ip)) || client_ip[0] == '\0') {
154 safe_snprintf(client_ip, sizeof(client_ip), "unknown");
155 log_warn("Could not determine WebSocket client IP address");
156 }
157
158 log_info("WebSocket client connected from %s", client_ip);
159 log_debug("[LWS_CALLBACK_ESTABLISHED] Client IP: %s", client_ip);
160
161 websocket_configure_socket(wsi);
162
163 // Create ACIP WebSocket server transport for this connection
164 // Note: We pass NULL for crypto_ctx here - crypto handshake happens at ACIP level
165 log_debug("[LWS_CALLBACK_ESTABLISHED] Creating ACIP WebSocket transport...");
167 snprintf(ws_transport_name, sizeof(ws_transport_name), "server_%p", (void *)wsi);
168 conn_data->transport = acip_websocket_server_transport_create(ws_transport_name, wsi, NULL);
169 if (!conn_data->transport) {
170 log_error("[LWS_CALLBACK_ESTABLISHED] FAILED: acip_websocket_server_transport_create returned NULL");
171 return SET_ERRNO(ERROR_NETWORK, "Failed to create WebSocket server transport");
172 }
173 log_debug("[LWS_CALLBACK_ESTABLISHED] Transport created: %p", (void *)conn_data->transport);
174
175 // Create client context for handler
177 if (!client_ctx) {
178 log_error("Failed to allocate client context");
180 conn_data->transport = NULL;
181 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate WebSocket client context");
182 }
183
184 client_ctx->transport = conn_data->transport;
185 SAFE_STRNCPY(client_ctx->client_ip, client_ip, sizeof(client_ctx->client_ip));
186 client_ctx->client_port = 0; // WebSocket doesn't expose client port easily
187 client_ctx->user_data = server->user_data;
188 // Global SSL initialization does not mean this listener uses TLS. Require
189 // both listener TLS configuration and an SSL connection before skipping
190 // the ACIP handshake.
191 client_ctx->is_secure = server->tls_enabled && lws_is_ssl(wsi) > 0;
192 client_ctx->auth_required = false; // Set by server handler if authentication is required
193
194 // Queue handler to thread pool (no pthread_create from callback context)
195 // The handler_pool was created at server startup with pre-allocated workers
196 log_debug("🔵 ABOUT TO QUEUE HANDLER: handler=%p, ctx=%p", (void *)server->handler, (void *)client_ctx);
197 log_debug("[LWS_CALLBACK_ESTABLISHED] Queueing handler to work pool (handler=%p, ctx=%p)...", (void *)server->handler, (void *)client_ctx);
198
199 if (!server->handler_pool) {
200 log_error("[LWS_CALLBACK_ESTABLISHED] FAILED: handler_pool is NULL");
201 SAFE_FREE(client_ctx);
203 conn_data->transport = NULL;
204 return SET_ERRNO(ERROR_INVALID_STATE, "WebSocket handler pool is missing");
205 }
206
207 // Mark the handler as owning the transport before it becomes visible to a
208 // worker. A peer can close immediately after the upgrade; CLOSED must not
209 // destroy a transport that has already been queued for handling.
210 atomic_store_bool(&((websocket_transport_data_t *)conn_data->transport->impl_data)->server_handler_owned, true);
211 conn_data->handler_started = true;
212 asciichat_error_t queue_result = thread_pool_queue_work("websocket_handler_established", server->handler_pool, server->handler, client_ctx);
213 log_debug("🔵 thread_pool_queue_work returned: %s", queue_result == ASCIICHAT_OK ? "OK" : "ERROR");
214
215 if (queue_result != ASCIICHAT_OK) {
216 log_error("[LWS_CALLBACK_ESTABLISHED] FAILED: thread_pool_queue_work returned error");
217 conn_data->handler_started = false;
218 atomic_store_bool(&((websocket_transport_data_t *)conn_data->transport->impl_data)->server_handler_owned, false);
219 SAFE_FREE(client_ctx);
221 conn_data->transport = NULL;
222 return queue_result;
223 }
224
225 log_debug("[LWS_CALLBACK_ESTABLISHED] Handler work queued successfully");
226 log_debug("★★★ ESTABLISHED CALLBACK SUCCESS - handler work queued! ★★★");
227 return ASCIICHAT_OK;
228}
229
230static void websocket_close_connection(struct lws *wsi, websocket_connection_data_t *conn_data, void *in, size_t len) {
231 // Connection closed
232 uint64_t close_callback_start_ns = time_get_ns();
233 log_debug("★★★ LWS_CALLBACK_CLOSED FIRED - WHY IS CONNECTION CLOSING? ★★★");
234 log_info("WebSocket client disconnected, wsi=%p, handler_started=%d, timestamp=%llu", (void *)wsi, conn_data ? conn_data->handler_started : -1, (unsigned long long)close_callback_start_ns);
235
236 // Try to extract close code from 'in' parameter if available
237 uint16_t close_code = 0;
238 if (in && len >= 2) {
239 close_code = (uint16_t)((uint8_t *)in)[0] << 8 | ((uint8_t *)in)[1];
240 log_debug("[LWS_CALLBACK_CLOSED] Close frame received with code=%u (1000=normal, 1001=going away, 1006=abnormal, "
241 "1009=message too big)",
242 close_code);
243 } else {
244 log_info("[LWS_CALLBACK_CLOSED] No close frame (in=%p, len=%zu) - connection closed without WebSocket close handshake", in, len);
245 }
246
247 // Mark cleanup in progress to prevent race conditions with other threads accessing transport
248 if (conn_data) {
249 conn_data->cleaning_up = true;
250 }
251
252 // Clean up pending send data only if not already freed
253 if (conn_data && conn_data->has_pending_send && conn_data->pending_send_data) {
254 SAFE_FREE(conn_data->pending_send_data);
255 conn_data->pending_send_data = NULL;
256 conn_data->has_pending_send = false;
257 }
258
259 // Close the transport to mark it as disconnected.
260 // This signals the receive thread to exit, allowing clean shutdown.
261 // Guard against use-after-free: check both conn_data and transport are valid
262 websocket_transport_data_t *detached = conn_data && conn_data->transport ? conn_data->transport->impl_data : NULL;
263 acip_transport_t *transport_snapshot = NULL;
264 if (conn_data && conn_data->transport) {
265 transport_snapshot = conn_data->transport;
266 conn_data->transport = NULL; // NULL out immediately to prevent race condition
267
268 log_debug("[LWS_CALLBACK_CLOSED] Closing transport=%p for wsi=%p", (void *)transport_snapshot, (void *)wsi);
269 // The handler may already be destroying the outbound queue. CLOSED owns
270 // only backend notification; its storage stays alive until server_detached.
271 if (transport_snapshot->methods && transport_snapshot->methods->close)
272 transport_snapshot->methods->close(transport_snapshot);
273 }
274
275 if (conn_data && conn_data->handler_started) {
276 // Handler is managed by thread pool - no join needed
277 // The pool worker will finish handling the transport and return to the pool
278 // Transport was already closed above, signaling the handler to exit
279 // DO NOT destroy the transport here - the handler might still be using it!
280 // The handler will free the context, and the transport will be cleaned up
281 // through the client structure (remove_client handles transport cleanup)
282 conn_data->handler_started = false;
283 log_debug("[LWS_CALLBACK_CLOSED] Handler was queued to pool (it will cleanup when done)");
284 log_debug("[LWS_CALLBACK_CLOSED] NOT destroying transport here - handler is still executing");
285 transport_snapshot = NULL; // Don't destroy below
286 }
287
288 if (detached && atomic_load_bool(&detached->server_handler_owned)) {
289 mutex_lock(&detached->state_mutex);
290 detached->context = NULL;
291 detached->wsi = NULL;
292 mutex_unlock(&detached->state_mutex);
293 atomic_store_bool(&detached->server_detached, true);
294 }
295
296 // Destroy the transport only if the handler is NOT running
297 // (Handler manages cleanup when it's running)
298 if (transport_snapshot) {
299 log_debug("[LWS_CALLBACK_CLOSED] Destroying transport=%p", (void *)transport_snapshot);
300 acip_transport_destroy(transport_snapshot);
301 }
302
303 // Ensure transport pointer is NULL
304 if (conn_data) {
305 conn_data->transport = NULL;
306 }
307
308 uint64_t close_callback_end_ns = time_get_ns();
309 char total_duration_str[TIME_DURATION_BUFFER_SIZE];
310 time_pretty(close_callback_end_ns - close_callback_start_ns, -1, total_duration_str, sizeof(total_duration_str));
311 log_debug("[LWS_CALLBACK_CLOSED] Complete cleanup took %s", total_duration_str);
312}
313
314static void websocket_send_queued_messages(struct lws *wsi, websocket_connection_data_t *conn_data) {
315 if (conn_data)
317 if (!conn_data || conn_data->cleaning_up || !conn_data->transport)
318 return;
319 uint64_t started = time_get_ns();
320 websocket_transport_data_t *ws_data = conn_data->transport->impl_data;
322 websocket_callback_timing_record(&g_ws_callback_timing.server_writeable, started, time_get_ns());
323}
324
325static asciichat_error_t websocket_receive_fragment(struct lws *wsi, websocket_connection_data_t *conn_data, void *in, size_t len) {
326 // Received data from client - may be fragmented for large messages
327 log_dev_every(4500 * US_PER_MS_INT, "LWS_CALLBACK_RECEIVE: conn_data=%p, transport=%p, len=%zu", (void *)conn_data, conn_data ? (void *)conn_data->transport : NULL, len);
328
329 if (!conn_data) {
330 log_error("LWS_CALLBACK_RECEIVE: conn_data is NULL! Need to initialize from ESTABLISHED or handle here");
331 return ASCIICHAT_OK;
332 }
333
334 // Check if cleanup is in progress to avoid race condition with remove_client
335 if (conn_data->cleaning_up) {
336 log_debug("LWS_CALLBACK_RECEIVE: Cleanup in progress, discarding received data");
337 return ASCIICHAT_OK;
338 }
339
340 // Snapshot the transport pointer to avoid race condition with cleanup thread
341 acip_transport_t *transport_snapshot = conn_data->transport;
342 log_dev("🔵 [WS_RECEIVE] conn_data=%p transport_snapshot=%p handler_started=%d", (void *)conn_data, (void *)transport_snapshot, conn_data ? conn_data->handler_started : -1);
343 if (!transport_snapshot) {
344 return SET_ERRNO(ERROR_INVALID_STATE, "Transport snapshot is NULL");
345 }
346
347 if (!in || len == 0) {
348 return ASCIICHAT_OK;
349 }
350
351 // Check if cleanup is already in progress to avoid race condition with LWS_CALLBACK_CLOSED
352 if (conn_data && conn_data->cleaning_up) {
353 log_debug("RECEIVE: Cleanup in progress, ignoring fragment");
354 return ASCIICHAT_OK;
355 }
356
357 // Handler thread was spawned in LWS_CALLBACK_ESTABLISHED (or RECEIVE fallback)
358 // (It fires for server-side connections and properly initializes client_info_t)
359
360 // Get transport data using snapshotted pointer
361 websocket_transport_data_t *ws_data = (websocket_transport_data_t *)transport_snapshot->impl_data;
362 if (!ws_data || !ws_data->recv_queue) {
363 log_error("WebSocket transport has no implementation data or recv_queue (ws_data=%p, recv_queue=%p)", (void *)ws_data, ws_data ? (void *)ws_data->recv_queue : NULL);
364 return ASCIICHAT_OK;
365 }
366
367 uint64_t callback_enter_ns = time_get_ns(); // Capture entry time for duration measurement
368 log_debug("[WS_TIMING] callback_enter_ns captured: %llu", (unsigned long long)callback_enter_ns);
369
370 bool is_first = lws_is_first_fragment(wsi);
371 bool is_final = lws_is_final_fragment(wsi);
372 log_debug("[WS_TIMING] is_first=%d is_final=%d, about to increment callback count", is_first, is_final);
373 log_dev("[WS_FRAG_DEBUG] === RECEIVE CALLBACK: is_first=%d is_final=%d len=%zu ===", is_first, is_final, len);
374
375 atomic_fetch_add_u64(&conn_data->receive_callback_count, 1);
376 log_debug("[WS_TIMING] incremented callback count");
377
378 // OPTIMIZATION: Only enable TCP_QUICKACK on first fragment of each message.
379 // Re-enabling on EVERY fragment is too expensive (syscall overhead).
380 // Linux resets TCP_QUICKACK after each ACK, but we only need it for the first chunk.
381#ifdef __linux__
382 if (is_first) {
383 int fd = lws_get_socket_fd(wsi);
384 if (fd >= 0) {
385 int quickack = 1;
386 setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &quickack, sizeof(quickack));
387 }
388 }
389#endif
390
391 if (is_first) {
392 // Reset fragment counters at start of new message
393 atomic_store_u64(&conn_data->writeable_callback_count, 0);
394 atomic_store_u64(&conn_data->receive_callback_count, 1);
395 }
396
397 // Log fragment arrival
398 // Note: Timing calculation with global g_receive_first_fragment_ns is broken for multi-fragment
399 // messages and will be fixed with per-connection tracking
400 {
401 uint64_t frag_num = atomic_load_u64(&conn_data->receive_callback_count);
402 log_dev("[WS_FRAG] Fragment #%llu: %zu bytes (first=%d final=%d)", (unsigned long long)frag_num, len, is_first, is_final);
403 }
404
406 msg.data = buffer_alloc(NULL, len);
407 if (!msg.data) {
408 return SET_ERRNO(ERROR_MEMORY, "Cannot admit WebSocket fragment (%zu bytes)", len);
409 }
410
411 memcpy(msg.data, in, len);
412 msg.len = len;
413 msg.first = is_first;
414 msg.final = is_final;
415
416 mutex_lock(&ws_data->recv_mutex);
417
418 if (!ws_data->recv_queue) {
419 log_warn("recv_queue was cleared during lock wait, dropping fragment");
420 mutex_unlock(&ws_data->recv_mutex);
421 buffer_release(msg.data);
422 return ASCIICHAT_OK;
423 }
424
425 if (is_first) {
426 size_t queue_current_size = ringbuffer_size(ws_data->recv_queue);
427 size_t queue_capacity = ws_data->recv_queue->capacity;
428 size_t queue_free = queue_capacity - queue_current_size;
429 log_dev_every(4500 * US_PER_MS_INT, "[WS_FLOW] Queue: free=%zu/%zu (used=%zu)", queue_free, queue_capacity, queue_current_size);
430 }
431
432 bool success = ringbuffer_write(ws_data->recv_queue, &msg);
433 if (!success) {
434 // Reject the connection rather than losing part of an ordered packet.
435 log_warn("[WS_FLOW] Receive queue FULL - closing connection (len=%zu, first=%d, final=%d)", len, is_first, is_final);
436 buffer_release(msg.data);
437
438 mutex_unlock(&ws_data->recv_mutex);
439 return SET_ERRNO(ERROR_BUFFER, "WebSocket receive queue exhausted");
440 }
441
442 // Signal waiting recv() call that a fragment is available
443 log_dev("[WS_DEBUG] RECEIVE: About to signal recv_cond (queue size=%zu)", ringbuffer_size(ws_data->recv_queue));
444 cond_signal(&ws_data->recv_cond);
445 mutex_unlock(&ws_data->recv_mutex);
446 log_dev("[WS_DEBUG] RECEIVE: Unlocked recv_mutex");
447
448 // Signal handler to process queued fragments via the condition variable
449 // (cond_signal already called above - do not call lws_callback_on_writable here as it
450 // interferes with fragmented frame processing in libwebsockets)
451
452 log_dev("[WS_FRAG] Queued fragment: %zu bytes (first=%d final=%d, total_fragments=%llu)", len, is_first, is_final, (unsigned long long)atomic_load_u64(&conn_data->receive_callback_count));
453
454 // Record timing for this callback
455 websocket_callback_timing_record(&g_ws_callback_timing.receive, callback_enter_ns, websocket_callback_timing_start());
456 return ASCIICHAT_OK;
457}
458
459// Consumes the JSON document and returns whether the HTTP connection stays open.
460static bool websocket_write_json(struct lws *wsi, yyjson_mut_doc *doc, unsigned char *buf, size_t buf_size) {
461 size_t json_len = 0;
462 char *json = yyjson_mut_write(doc, YYJSON_WRITE_PRETTY_TWO_SPACES, &json_len);
463 yyjson_mut_doc_free(doc);
464 if (!json)
465 return false;
466
467 unsigned char *start = buf + LWS_PRE;
468 unsigned char *p = start;
469 unsigned char *end = buf + buf_size;
470
471 if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end) || lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE, (const unsigned char *)"application/json", 16, &p, end) ||
472 lws_add_http_header_content_length(wsi, (lws_filepos_t)json_len, &p, end) || lws_finalize_http_header(wsi, &p, end)) {
473 free(json);
474 return false;
475 }
476
477 memcpy(p, json, json_len);
478 p += json_len;
479 free(json);
480
481 if (lws_write(wsi, start, (size_t)(p - start), LWS_WRITE_HTTP_HEADERS) < 0)
482 return false;
483 if (lws_http_transaction_completed(wsi))
484 return false;
485 return true;
486}
487
488static bool websocket_serve_health(struct lws *wsi) {
489 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
490 yyjson_mut_val *root = yyjson_mut_obj(doc);
491 yyjson_mut_doc_set_root(doc, root);
492 yyjson_mut_obj_add_str(doc, root, "status", "ok");
493 yyjson_mut_obj_add_str(doc, root, "version", ASCII_CHAT_VERSION_FULL);
495 snprintf(proto_ver, sizeof(proto_ver), "%d.%d.%d", PROTOCOL_VERSION_MAJOR, PROTOCOL_VERSION_MINOR, PROTOCOL_VERSION_PATCH);
496 yyjson_mut_obj_add_str(doc, root, "protocol", proto_ver);
497
498 unsigned char buf[LWS_PRE + 1024];
499 return websocket_write_json(wsi, doc, buf, sizeof(buf));
500}
501
502// HTTP helpers return whether libwebsockets should keep the connection open.
503static bool websocket_handle_http(struct lws *wsi, const char *path) {
504 if (path && (strcmp(path, "/health") == 0 || strcmp(path, "health") == 0)) {
505 return websocket_serve_health(wsi);
506 }
507 lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, "Not Found");
508 return !lws_http_transaction_completed(wsi);
509}
510
511static asciichat_error_t websocket_request_writable_callbacks(struct lws *wsi) {
512 if (!wsi) {
513 return SET_ERRNO(ERROR_INVALID_PARAM, "Cannot request WebSocket writable callbacks without a connection");
514 }
515 // Fired on the service thread when lws_cancel_service() is called from another thread.
516 // This is how we safely convert cross-thread send requests into writable callbacks.
517 // lws_callback_on_writable() is only safe from the service thread context.
518 //
519 // lws_cancel_service() wakes the event loop, which fires this callback.
520 // The `wsi` parameter may be the listening socket or any random connection, so we
521 // CANNOT rely on lws_get_protocol(wsi) to get the correct protocol.
522 // Instead, we MUST trigger WRITEABLE on ALL protocols that handle WebSocket frames:
523 // - "http" protocol (browser connects here for initial HTTP upgrade to WebSocket)
524 // - "acip" protocol (for ACIP over WebSocket connections)
525 // Both protocols use the same callback and handle frame transmission.
526 log_debug_every(1 * US_PER_SEC_INT, ">>> LWS_CALLBACK_EVENT_WAIT_CANCELLED triggered - requesting writable callbacks for all protocols");
527
528 struct lws_context *ctx = lws_get_context(wsi);
529 if (!ctx) {
530 return SET_ERRNO(ERROR_INVALID_STATE, "EVENT_WAIT_CANCELLED: Could not get context from wsi");
531 }
532
533 // Trigger WRITEABLE on both protocols
534 for (int i = 0; i < 2; i++) {
535 log_dev_every(1 * US_PER_SEC_INT, ">>> EVENT_WAIT_CANCELLED: Calling lws_callback_on_writable_all_protocol for protocol '%s'", websocket_protocols[i].name);
536 int result = lws_callback_on_writable_all_protocol(ctx, &websocket_protocols[i]);
537 if (result != 0) {
538 return SET_ERRNO(ERROR_NETWORK, "Failed to request WebSocket writable callbacks for protocol '%s': %d", websocket_protocols[i].name, result);
539 }
540 }
541 return ASCIICHAT_OK;
542}
543
550static int websocket_server_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
552 const char *proto_name = lws_get_protocol(wsi) ? lws_get_protocol(wsi)->name : "NULL";
553
554 // LOG EVERY SINGLE CALLBACK WITH PROTOCOL NAME
555 log_dev("🔵 CALLBACK: reason=%d, proto=%s, wsi=%p, len=%zu", reason, proto_name, (void *)wsi, len);
556
557 switch (reason) {
558 case LWS_CALLBACK_ESTABLISHED:
559 return websocket_establish_connection(wsi, conn_data) == ASCIICHAT_OK ? 0 : -1;
560
561 case LWS_CALLBACK_CLOSED:
562 websocket_close_connection(wsi, conn_data, in, len);
563 break;
564
565 case LWS_CALLBACK_SERVER_WRITEABLE:
566 if (conn_data && conn_data->transport && !acip_transport_is_connected(conn_data->transport))
567 return -1;
568 websocket_send_queued_messages(wsi, conn_data);
569 break;
570
571 case LWS_CALLBACK_RECEIVE:
572 return websocket_receive_fragment(wsi, conn_data, in, len) == ASCIICHAT_OK ? 0 : -1;
573
574 case LWS_CALLBACK_HTTP:
575 return websocket_handle_http(wsi, (const char *)in) ? 0 : -1;
576
577 case LWS_CALLBACK_FILTER_HTTP_CONNECTION: {
578 // WebSocket upgrade handshake - allow all connections
579 log_debug("[FILTER_HTTP_CONNECTION] WebSocket upgrade request (allow protocol upgrade)");
580 return 0; // Allow the connection
581 }
582
583 case LWS_CALLBACK_PROTOCOL_INIT: {
584 // Protocol initialization with timing instrumentation
585 uint64_t callback_start_ns = websocket_callback_timing_start();
586 log_debug("[PROTOCOL_INIT] Protocol initialized, proto=%s", proto_name);
587 uint64_t callback_end_ns = websocket_callback_timing_start();
588 websocket_callback_timing_record(&g_ws_callback_timing.protocol_init, callback_start_ns, callback_end_ns);
589 break;
590 }
591
592 case LWS_CALLBACK_PROTOCOL_DESTROY: {
593 // Protocol destruction with timing instrumentation
594 uint64_t callback_start_ns = websocket_callback_timing_start();
595 log_debug("[PROTOCOL_DESTROY] Protocol destroyed, proto=%s", proto_name);
596 uint64_t callback_end_ns = websocket_callback_timing_start();
597 websocket_callback_timing_record(&g_ws_callback_timing.protocol_destroy, callback_start_ns, callback_end_ns);
598 break;
599 }
600
601 case LWS_CALLBACK_EVENT_WAIT_CANCELLED: {
603 asciichat_error_t result = websocket_request_writable_callbacks(wsi);
604 if (result != ASCIICHAT_OK) {
606 if (errno_peek_since(scope, &error)) {
607 log_error("WebSocket writable callback request failed: %s", error.context_message);
608 } else {
609 log_error("WebSocket writable callback request failed: %s", asciichat_error_format(NULL, result));
610 }
611 }
613 return result == ASCIICHAT_OK ? 0 : -1;
614 }
615
616 default:
617 break;
618 }
619
620 return 0;
621}
622
626static struct lws_protocols websocket_protocols[] = {
627 {
628 "http", // Default HTTP protocol (for WebSocket upgrade)
629 websocket_server_callback, // Use same callback for all protocols
630 sizeof(websocket_connection_data_t), // Per-session data size
631 524288, // RX buffer size (512KB for video frames)
632 0, // ID (auto-assigned)
633 NULL, // User pointer (set to server instance)
634 524288 // TX packet size (512KB for video frames)
635 },
636 {
637 "acip", // ACIP WebSocket subprotocol (after HTTP upgrade)
638 websocket_server_callback, // Callback function
639 sizeof(websocket_connection_data_t), // Per-session data size
640 524288, // RX buffer size (512KB for video frames)
641 0, // ID (auto-assigned)
642 NULL, // User pointer (set to server instance)
643 524288 // TX packet size (512KB for video frames)
644 },
645 {NULL, NULL, 0, 0, 0, NULL, 0} // Terminator
646};
647
672static const lws_retry_bo_t keep_alive_policy = {
673 .secs_since_valid_ping = 30, // Send PING after 30s idle
674 .secs_since_valid_hangup = 35, // Hangup if still idle after 35s
675};
676
678 if (!server || !config || !config->client_handler) {
679 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
680 }
681
682 memset(server, 0, sizeof(*server));
683 server->handler = config->client_handler;
684 server->user_data = config->user_data;
685 server->port = config->port;
686 atomic_store_bool(&server->running, true);
687
688 // Store server pointer in protocol user data so callbacks can access it
689 websocket_protocols[0].user = server; // http protocol
690 websocket_protocols[1].user = server; // acip protocol
691
692 // Enable libwebsockets logging through centralized logging system
694
695 // HTTP mount for /health endpoint (required for LWS_CALLBACK_HTTP to fire)
696 static const struct lws_http_mount health_mount = {
697 .mount_next = NULL,
698 .mountpoint = "/",
699 .mountpoint_len = 1,
700 .origin = "http",
701 .def = NULL,
702 .protocol = "http",
703 .origin_protocol = LWSMPRO_CALLBACK,
704 };
705
706 // Configure libwebsockets context
707 struct lws_context_creation_info info = {0};
708 info.port = config->port;
709 info.iface = config->bind_address;
710 info.protocols = websocket_protocols;
711 info.mounts = &health_mount;
712 info.gid = (gid_t)-1; // Cast to avoid undefined behavior with unsigned type
713 info.uid = (uid_t)-1; // Cast to avoid undefined behavior with unsigned type
714 info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; // Initialize SSL/TLS support (required for server binding)
715 info.extensions = NULL; // Disable permessage-deflate - causing connection issues
716 info.retry_and_idle_policy = &keep_alive_policy; // Configure keep-alive to prevent idle disconnects during handshake
717
718 // Configure TLS/WSS support if certificates are provided (check for non-empty strings)
719 if (config->tls_cert_path && config->tls_cert_path[0] != '\0' && config->tls_key_path && config->tls_key_path[0] != '\0') {
720#ifdef LWS_WITH_TLS
721 // Validate certificate and key files are readable
722 if (platform_access(config->tls_cert_path, R_OK) != 0) {
723 return SET_ERRNO(ERROR_NETWORK_BIND, "TLS certificate file not readable: %s (errno=%d)", config->tls_cert_path, errno);
724 }
725 if (platform_access(config->tls_key_path, R_OK) != 0) {
726 return SET_ERRNO(ERROR_NETWORK_BIND, "TLS key file not readable: %s (errno=%d)", config->tls_key_path, errno);
727 }
728
729 info.ssl_cert_filepath = config->tls_cert_path;
730 info.ssl_private_key_filepath = config->tls_key_path;
731 server->tls_enabled = true;
732 log_info("WebSocket server configured for WSS (TLS): cert=%s, key=%s", config->tls_cert_path, config->tls_key_path);
733#else
734 log_warn("WebSocket server: TLS support not compiled in libwebsockets; WSS unavailable");
735#endif
736 } else if ((config->tls_cert_path && config->tls_cert_path[0] != '\0') || (config->tls_key_path && config->tls_key_path[0] != '\0')) {
737 NOTICE(DANGER, "WEBSOCKET TLS DISABLED", "WebSocket server: Both TLS certificate and key must be provided for WSS; using plain WS");
738 }
739
740 // Increase per-thread service buffer to prevent fragmentation of large messages
741 // Default is 4KB, causing large frames to fragment into many chunks
742 // Video frames are 921KB, so increase to 2MB to handle them without fragmentation
743 info.pt_serv_buf_size = 2 * 1024 * 1024; // 2MB per-thread service buffer
744
745 // Disable ALL default timeouts and keep-alive mechanisms
746 // Use only the explicit retry_and_idle_policy we set above (30/35 seconds)
747 info.ka_time = 0; // Disable TCP keep-alive probes (use WebSocket pings instead)
748 info.ka_probes = 0; // Disable TCP probes
749 info.ka_interval = 0; // Disable TCP probe intervals
750 info.keepalive_timeout = 0; // Disable HTTP keep-alive timeout entirely
751
752 // Explicitly set a very large idle timeout to prevent early disconnection
753 // Without this, libwebsockets defaults to 5 seconds for HTTP connections
754 // Set to effectively infinite (100 hours) since we use retry_and_idle_policy instead
755 info.timeout_secs = 360000; // 100 hours - effectively disabled
756
757 // Create libwebsockets context
758 // Log diagnostic info before attempting creation
759 log_debug("lws_create_context: port=%u, protocols=%p, options=0x%x", info.port, (void *)info.protocols, info.options);
760
761 server->context = lws_create_context(&info);
762 if (!server->context) {
763 // Capture any OpenSSL errors that may have occurred during context creation
764 unsigned long ssl_err = 0;
765 char ssl_err_str[NETWORK_WEBSOCKET_SERVER_SSL_ERR_BUFFER_SIZE] = "no OpenSSL errors in queue";
766
767 // Extract all errors from OpenSSL error queue
768 while ((ssl_err = ERR_get_error()) != 0) {
769 ERR_error_string_n(ssl_err, ssl_err_str, sizeof(ssl_err_str));
770 log_error("OpenSSL error: %lu: %s", ssl_err, ssl_err_str);
771 }
772
773 return SET_ERRNO(ERROR_NETWORK_BIND, "Failed to create libwebsockets context (last OpenSSL error: %s)", ssl_err_str);
774 }
775
776 // Each connection owns a handler until disconnect; provide a worker for every participant.
778
779 if (!server->handler_pool) {
780 lws_context_destroy(server->context);
781 server->context = NULL;
782 return SET_ERRNO(ERROR_THREAD, "Failed to create WebSocket handler thread pool");
783 }
784
785 /* Register WebSocket server with named registry */
787 snprintf(ws_port_name, sizeof(ws_port_name), "ws:%d", server->port);
788 NAMED_REGISTER(server, ws_port_name, "websocket_server", "0x%tx", NULL);
789
790 /* Register server's sync primitives with hierarchical naming */
791 NAMED_REGISTER_ATOMIC(&server->running, "is_running", (uintptr_t)(const void *)(server));
792
793 /* Register WebSocket context as child of server */
794 NAMED_REGISTER_WEBSOCKET(server->context, "context", (uintptr_t)(const void *)(server));
795
796 log_info("WebSocket server initialized on port %d with static file serving", config->port);
797 return ASCIICHAT_OK;
798}
799
801 if (!server || !server->context) {
802 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid server");
803 }
804
805 log_info("WebSocket server starting event loop on port %d", server->port);
806
807 // Run libwebsockets event loop
808 uint64_t last_service_ns = 0;
809 int service_call_count = 0;
810 while (atomic_load_bool(&server->running)) {
811 // Service libwebsockets with 16ms timeout to match 60 FPS frame rate.
812 // This provides frequent event processing (~60 callback invocations per second) so that
813 // WebSocket frames queued by the render loop are sent promptly, matching TCP performance.
814 // All client connections share this single server context, so a single lws_service() call
815 // services fragments and events for all clients simultaneously.
816 uint64_t service_start_ns = time_get_ns();
817 if (last_service_ns && service_start_ns - last_service_ns > 30 * US_PER_MS_INT) {
818 // > 30ms gap between service calls
819 double gap_ms = (double)(service_start_ns - last_service_ns) / 1e6;
820 log_debug_every(US_PER_SEC_INT, "[LWS_SERVICE_GAP] %.1fms gap between lws_service calls", gap_ms);
821 }
822 service_call_count++;
823 log_debug_every(500 * US_PER_MS_INT, "[LWS_SERVICE] Call #%d, context=%s", service_call_count, NAMED_DESCRIBE(server->context, "websocket_server"));
824 int result = lws_service(server->context, 1);
825 if (result < 0) {
826 log_error("libwebsockets service error: %d", result);
827 break;
828 }
829 }
830
831 log_info("WebSocket server event loop exited (context will be destroyed by main thread after handler threads complete)");
832 return ASCIICHAT_OK;
833}
834
836 if (server && server->context) {
837 lws_cancel_service(server->context);
838 }
839}
840
842 if (!server) {
843 return;
844 }
845
846 atomic_store_bool(&server->running, false);
847
848 // Cancel any pending libwebsockets service calls to interrupt blocking lws_service()
849 // This prevents the event loop from getting stuck in lws_service()
850 if (server->context) {
851 log_debug("Cancelling libwebsockets service");
852 lws_cancel_service(server->context);
853 }
854
855 // Unregister atomic fields
856 NAMED_UNREGISTER(&server->running);
857
858 if (server->context) {
859 NAMED_UNREGISTER(server->context);
860 lws_context_destroy(server->context);
861 server->context = NULL;
862 }
863 if (server->handler_pool) {
865 server->handler_pool = NULL;
866 }
867
868 log_debug("WebSocket server destroyed");
869}
bool errno_peek_since(asciichat_errno_scope_t scope, asciichat_error_context_t *out)
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
const char * asciichat_error_format(const char *message, asciichat_error_t code)
#define SET_ERRNO(code,...)
@ ASCIICHAT_ERRNO_HANDLED
uint64_t atomic_fetch_add_u64(atomic_t *a, uint64_t delta)
Atomically add to a uint64_t and return the previous value.
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
uint64_t atomic_load_u64(atomic_t *a)
Atomically load a uint64_t value.
void acip_transport_destroy(acip_transport_t *transport)
Destroy transport and free all resources.
void * buffer_alloc(buffer_domain_t *domain, size_t size)
Definition buffer.c:272
#define buffer_release(data)
Definition buffer.h:99
websocket_callback_timing_t g_ws_callback_timing
Global timing tracker - shared across all WebSocket connections.
void websocket_callback_timing_record(websocket_callback_stats_t *stats, uint64_t start_ns, uint64_t end_ns)
Record callback invocation.
WebSocket callback timing instrumentation.
Application limits and constraints.
Named object registry for debugging — log identifiable resource names.
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
int platform_access(const char *pathname, int mode)
Definition filesystem.c:95
unsigned short uint16_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_STRERROR(errnum)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
unsigned char uint8_t
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.
#define NAMED_REGISTER(ptr, name, type, fmt, parent_ptr)
Register any pointer with base name, type, format spec, and location (auto-suffix).
#define NAMED_DESCRIBE(ptr, hint)
Describe a pointer in log format.
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
#define NAMED_REGISTER_WEBSOCKET(websocket, name, parent_ptr)
Register a WebSocket connection with automatic format specifier.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_NETWORK_BIND
Definition error_codes.h:77
@ ERROR_NETWORK
Definition error_codes.h:76
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER
@ ERROR_THREAD
#define MAX_SESSION_PARTICIPANTS
#define IP_ADDRESS_BUFFER_SIZE
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:575
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
int time_pretty(uint64_t nanoseconds, int decimals, char *buffer, size_t buffer_size)
Format nanoseconds as a compact duration with configurable sub-minute precision.
Definition time.c:368
#define TIME_DURATION_BUFFER_SIZE
Definition time.h:40
#define US_PER_MS_INT
Definition time.h:176
#define US_PER_SEC_INT
Definition time.h:177
#define PROTOCOL_VERSION_MAJOR
Major protocol version number.
#define PROTOCOL_VERSION_MINOR
Minor protocol version number.
#define PROTOCOL_VERSION_PATCH
Patch protocol version number.
size_t ringbuffer_size(const ringbuffer_t *rb)
Get current number of elements in the buffer.
Definition ringbuffer.c:131
bool ringbuffer_write(ringbuffer_t *rb, const void *data)
Try to write an element to the ring buffer (non-blocking).
Definition ringbuffer.c:70
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Transport abstraction layer for ACIP protocol.
WebSocket server for accepting browser client connections.
struct websocket_server websocket_server_t
Internal WebSocket implementation types shared between transport.c and server.c.
websocket_msg_t websocket_recv_msg_t
Definition internal.h:41
asciichat_error_t websocket_server_init(websocket_server_t *server, const websocket_server_config_t *config)
Initialize WebSocket server.
#define NETWORK_WEBSOCKET_SERVER_WS_PORT_NAME_BUFFER_SIZE
asciichat_error_t websocket_server_run(websocket_server_t *server)
Run WebSocket server event loop.
void websocket_server_destroy(websocket_server_t *server)
Destroy WebSocket server and free resources.
#define NETWORK_WEBSOCKET_SERVER_WS_TRANSPORT_NAME_BUFFER_SIZE
void websocket_server_cancel_service(websocket_server_t *server)
Wake the WebSocket server event loop immediately.
#define NETWORK_WEBSOCKET_SERVER_PROTO_VER_BUFFER_SIZE
#define NETWORK_WEBSOCKET_SERVER_SSL_ERR_BUFFER_SIZE
📝 Logging API with multiple log levels and terminal output control
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
Definition log/log.h:743
#define log_dev_every(interval_us, fmt,...)
Rate-limited DEV logging.
Definition log/log.h:740
#define NOTICE(severity, title,...)
Definition notice.h:56
Protocol version, features, compression, and frame flag constants.
Lock-Free Ring Buffer and Frame Buffer Management.
🔤 String Manipulation and Shell Escaping Utilities
void * impl_data
Transport-specific state.
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
size_t capacity
Number of elements that can be stored.
Definition ringbuffer.h:100
Listener settings and callbacks used to create a WebSocket server.
acip_transport_t * transport
ACIP transport for this client.
char client_ip[64]
Client IP address (for logging/rate limiting).
bool auth_required
True if client authentication is required.
void * user_data
User-provided data from server config.
bool is_secure
True if connection is using TLS (wss://).
Libwebsockets callback state for one ACIP WebSocket connection.
bool cleaning_up
True if cleanup is in progress (prevents race with remove_client).
bool handler_started
True if handler thread was started.
acip_transport_t * transport
ACIP transport for this connection.
uint8_t * pending_send_data
Current message being sent.
atomic_t writeable_callback_count
WRITEABLE callbacks for this connection.
bool has_pending_send
True if there's an in-progress message.
size_t pending_send_len
Total length of message being sent.
atomic_t receive_callback_count
RECEIVE callbacks for this connection.
websocket_server_t * server
Back-reference to server.
platform_thread_t handler_thread
Client handler thread.
size_t pending_send_offset
Current offset in message (bytes already sent).
size_t len
Frame length in bytes.
Definition internal.h:32
uint8_t first
1 if first frame of message (or complete message)
Definition internal.h:33
uint8_t final
1 if final frame of message (or complete message)
Definition internal.h:34
uint8_t * data
Frame data (allocated, caller must free).
Definition internal.h:31
Listener settings and callbacks used to create a WebSocket server.
websocket_client_handler_fn client_handler
Handler for new connections.
const char * bind_address
Numeric local address to listen on (never a wildcard by default).
const char * tls_cert_path
TLS certificate file path (optional, for wss://).
const char * tls_key_path
TLS private key file path (optional, for wss://).
websocket_client_handler_fn handler
Client handler function.
struct thread_pool * handler_pool
Thread pool for queueing handler work.
struct lws_context * context
libwebsockets context
bool tls_enabled
True when this listener is configured with TLS credentials.
Private state for one libwebsockets-backed ACIP transport.
Definition internal.h:51
struct lws_context * context
libwebsockets context (may be owned or borrowed)
Definition internal.h:53
cond_t recv_cond
Signal when recv_queue messages arrive.
Definition internal.h:58
mutex_t state_mutex
Protect state changes.
Definition internal.h:62
struct lws * wsi
libwebsockets instance (owned)
Definition internal.h:52
ringbuffer_t * recv_queue
Receive message queue.
Definition internal.h:55
mutex_t recv_mutex
Protect recv_queue operations only.
Definition internal.h:57
void thread_pool_destroy(thread_pool_t *pool)
Destroy a thread pool.
asciichat_error_t thread_pool_queue_work(const char *name, thread_pool_t *pool, void *(*work_func)(void *), void *work_arg)
Queue a named work item to a work queue pool.
thread_pool_t * thread_pool_create_with_workers(const char *pool_name, size_t num_workers)
Create a new thread pool in work queue mode.
⏱️ High-precision timing utilities using sokol_time.h and uthash
void websocket_transport_write_pending(struct lws *wsi, websocket_transport_data_t *ws_data)
Drain client messages from the send queue on the service thread.
acip_transport_t * acip_websocket_server_transport_create(const char *name, struct lws *wsi, crypto_context_t *crypto_ctx)
Create WebSocket server transport from existing connection.
void lws_log_init_server(void)
Initialize libwebsockets logging for server mode.
Definition websocket.c:87
Libwebsockets logging integration.