Monorepo for Aesthetic.Computer
aesthetic.computer
1#pragma once
2
3// Minimal TLS WebSocket client for ac-native bare metal
4// Connect + recv run in a background pthread; main thread only polls.
5
6#include <pthread.h>
7
8#define WS_MAX_MESSAGES 16
9#define WS_MAX_MSG_LEN (256 * 1024) // 256KB — chat "connected" history can be large
10
11typedef struct {
12 // --- background thread ---
13 pthread_t thread;
14 int thread_running;
15
16 // pending connect request (written by main, read by thread)
17 char pending_host[256];
18 char pending_path[256];
19 int pending_connect; // 1 = thread should connect
20
21 // connection state (written by thread, read by main)
22 int connected; // 1 = WS handshake complete
23 int connecting; // 1 = currently in progress
24 int error; // 1 = last connect failed
25
26 // outgoing send queue (written by main, drained by thread)
27 char send_buf[16384];
28 int send_pending; // 1 = send_buf has data to send
29
30 // inbound message queue (written by thread, consumed by main each frame)
31 char messages[WS_MAX_MESSAGES][WS_MAX_MSG_LEN];
32 int msg_count; // written by thread
33 int msg_read; // read index advanced by main
34
35 pthread_mutex_t mu;
36
37 // thread-private (only touched by background thread)
38 int fd;
39 void *ssl_ctx;
40 void *ssl;
41 unsigned char frame_buf[512 * 1024]; // 512KB — must fit largest WS frame
42 int frame_len;
43} ACWs;
44
45ACWs *ws_create(void);
46void ws_destroy(ACWs *ws);
47
48// Non-blocking: schedules a connect on the background thread
49void ws_connect(ACWs *ws, const char *url);
50
51// Non-blocking send (queues one message; thread drains it)
52void ws_send(ACWs *ws, const char *text);
53
54// Call each frame from main thread.
55// Swaps the inbound message buffer; returns number of new messages.
56// Messages are in ws->messages[0..return_value-1].
57int ws_poll(ACWs *ws);
58
59// Close and reset (non-blocking; signals thread to stop)
60void ws_close(ACWs *ws);