ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
internal.h File Reference

Internal WebSocket implementation types shared between transport.c and server.c. More...

Go to the source code of this file.

Data Structures

struct  websocket_msg_t
 Owned WebSocket frame queued for sending or reassembly. More...
struct  websocket_transport_data_t
 Private state for one libwebsockets-backed ACIP transport. More...

Typedefs

typedef websocket_msg_t websocket_recv_msg_t

Functions

asciichat_error_t websocket_transport_send (acip_transport_t *transport, const void *data, size_t len)
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.

Detailed Description

Internal WebSocket implementation types shared between transport.c and server.c.

Definition in file internal.h.

Typedef Documentation

◆ websocket_recv_msg_t

Definition at line 41 of file internal.h.

Function Documentation

◆ websocket_transport_send()

asciichat_error_t websocket_transport_send ( acip_transport_t * transport,
const void * data,
size_t len )

Queue one packet, preserving its control budget through encryption and ownership transfer.

Definition at line 614 of file websocket/transport.c.

614 {
615 // NOTE: Disabled START_TIMER/STOP_TIMER due to thread safety issues with timer hashtable
616 // causing heap-use-after-free crashes in ASAN. The timer system has race conditions
617 // when accessed from the service thread context. See: line 325 (similar issue).
619
620 // For server-side transports (owns_context=false), the connection is already established.
621 // For client-side transports, packets may be queued before the asynchronous WebSocket
622 // handshake completes. The service thread drains that queue after CLIENT_ESTABLISHED.
623 if (ws_data->owns_context) {
624 // Check connection state without blocking the service thread.
625 mutex_lock(&ws_data->state_mutex);
626 bool connected = ws_data->is_connected;
627 bool connection_failed = ws_data->connection_failed;
628 mutex_unlock(&ws_data->state_mutex);
629
630 if (connection_failed) {
631 log_error("Connection failed - cannot send");
632 return SET_ERRNO(ERROR_NETWORK, "WebSocket connection failed");
633 }
634
635 if (!connected) {
636 log_debug("Connection is opening; queueing packet for delivery after handshake");
637 }
638
639 log_dev_every(1000000, "websocket_send (client): is_connected=%d, wsi=%p, send_len=%zu", connected, (void *)ws_data->wsi, len);
640 } else {
641 log_dev_every(4500 * US_PER_MS_INT, "[WEBSOCKET_SEND_SERVER] Server transport send: wsi=%p, len=%zu (bypassing is_connected check)", (void *)ws_data->wsi, len);
642 }
643
644 // Classify the original packet before encryption hides its type. Keep this
645 // domain through every allocation, including the asynchronous queue owner.
646 buffer_domain_t *domain = NULL;
647 if (data && len >= sizeof(packet_header_t)) {
648 packet_header_t header;
649 memcpy(&header, data, sizeof(header));
650 packet_type_t type = NET_TO_HOST_U16(header.type);
651 if (packet_is_handshake_type(type) || type == PACKET_TYPE_ERROR_MESSAGE)
652 domain = buffer_control_domain();
653 }
654 const void *send_data = NULL;
655 size_t send_len = 0;
656 uint8_t *encrypted_packet = NULL;
657 size_t encrypted_packet_size = 0;
658 asciichat_error_t result = websocket_prepare_payload(transport, domain, data, len, &send_data, &send_len, &encrypted_packet, &encrypted_packet_size);
659 if (result != ASCIICHAT_OK) {
660 return result;
661 }
662
663 result = websocket_queue_payload(ws_data, domain, send_data, send_len);
664 if (encrypted_packet) {
665 buffer_release(encrypted_packet);
666 }
667 return result;
668}
#define SET_ERRNO(code,...)
buffer_domain_t * buffer_control_domain(void)
Definition buffer.c:432
struct buffer_domain buffer_domain_t
Definition buffer.h:28
#define buffer_release(data)
Definition buffer.h:99
#define NET_TO_HOST_U16(val)
Definition endian.h:115
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK
Definition error_codes.h:76
@ ASCIICHAT_OK
Definition error_codes.h:50
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define US_PER_MS_INT
Definition time.h:176
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:388
#define log_dev_every(interval_us, fmt,...)
Rate-limited DEV logging.
Definition log/log.h:740
void * impl_data
Transport-specific state.
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641
Private state for one libwebsockets-backed ACIP transport.
Definition internal.h:51

References ASCIICHAT_OK, buffer_control_domain(), buffer_release, websocket_transport_data_t::connection_failed, ERROR_NETWORK, acip_transport::impl_data, websocket_transport_data_t::is_connected, log_debug, log_dev_every, log_error, NET_TO_HOST_U16, websocket_transport_data_t::owns_context, PACKET_TYPE_ERROR_MESSAGE, SET_ERRNO, websocket_transport_data_t::state_mutex, US_PER_MS_INT, and websocket_transport_data_t::wsi.

◆ websocket_transport_write_pending()

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.

Definition at line 440 of file websocket/transport.c.

440 {
441 if (!ws_data || atomic_load_bool(&ws_data->is_destroying))
442 return;
443 /* lws suppresses writable callbacks until previously buffered output is
444 * consumed. Keep one owner until this completion boundary, or connection
445 * teardown; elapsed service-loop iterations are not a completion signal. */
446 websocket_recv_msg_t msg = {0};
447 mutex_lock(&ws_data->send_mutex);
449 ws_data->inflight_buffer = NULL;
450 ws_data->pending_send_bytes -= ws_data->inflight_bytes;
451 ws_data->inflight_bytes = 0;
452 bool available = ringbuffer_read(ws_data->send_queue, &msg);
453 if (available)
454 ws_data->inflight_buffer = msg.data;
455 ws_data->inflight_bytes = msg.len;
456 mutex_unlock(&ws_data->send_mutex);
457 if (!available)
458 return;
459 int written = lws_write(wsi, msg.data + LWS_PRE, msg.len, LWS_WRITE_BINARY);
460 if (written < 0 || (size_t)written < msg.len) {
461 mutex_lock(&ws_data->state_mutex);
462 ws_data->is_connected = false;
463 ws_data->connection_failed = true;
464 cond_broadcast(&ws_data->state_cond);
465 mutex_unlock(&ws_data->state_mutex);
466 lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_SEND, LWS_TO_KILL_ASYNC);
467 return;
468 }
469 /* Exactly one write per callback. Request a completion callback even if
470 * there is no next message, so the final owner is promptly released. */
471 lws_callback_on_writable(wsi);
472}
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
bool ringbuffer_read(ringbuffer_t *rb, void *data)
Try to read an element from the ring buffer (non-blocking).
Definition ringbuffer.c:92
websocket_msg_t websocket_recv_msg_t
Definition internal.h:41
size_t len
Frame length in bytes.
Definition internal.h:32
uint8_t * data
Frame data (allocated, caller must free).
Definition internal.h:31
mutex_t send_mutex
Protect send_queue operations only.
Definition internal.h:59
mutex_t state_mutex
Protect state changes.
Definition internal.h:62
uint8_t * inflight_buffer
Owned until the next writable callback or teardown.
Definition internal.h:81
ringbuffer_t * send_queue
Send message queue (for server-side transports).
Definition internal.h:56
cond_t state_cond
Signal when is_connected changes (for connection wait).
Definition internal.h:63
bool connection_failed
Connection attempt failed (for client-side detection).
Definition internal.h:61
bool is_connected
Connection state.
Definition internal.h:60
size_t pending_send_bytes
Includes the inflight owner until writable completion.
Definition internal.h:79
atomic_t is_destroying
True when destruction has started.
Definition internal.h:88

References atomic_load_bool(), buffer_release, websocket_transport_data_t::connection_failed, websocket_msg_t::data, websocket_transport_data_t::inflight_buffer, websocket_transport_data_t::inflight_bytes, websocket_transport_data_t::is_connected, websocket_transport_data_t::is_destroying, websocket_msg_t::len, websocket_transport_data_t::pending_send_bytes, ringbuffer_read(), websocket_transport_data_t::send_mutex, websocket_transport_data_t::send_queue, websocket_transport_data_t::state_cond, and websocket_transport_data_t::state_mutex.