ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
event.c File Reference

Go to the source code of this file.

Data Structures

struct  event_command_t
struct  platform_event_loop
struct  platform_event_stream
 The useful part of event write is in request, stream, done, arg: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path. More...
struct  platform_event_listener
 platform event stream is intentionally a small, concrete record. Fields such as listener, owner, tcp, timer give callers the identity and current value they need. It is normally borrowed from, or owned by, the surrounding subsystem—not independently managed. More...
struct  event_write
struct  event_work_t
 event command keeps the values that move through the subsystem: fn, arg. Some members are pointers into caller- or subsystem-owned memory, so lifetime follows the API that populated them rather than the struct copy itself. More...
struct  loop_work_t
 This is the handoff point for event work. Callers fill work, stream, run, done before passing it on; consumers read the values during the operation and return ownership only where the member contract explicitly says they do. More...

Macros

#define EVENT_COMMAND_LIMIT   1024
#define EVENT_WRITE_LIMIT   (16U * 1024U * 1024U)
#define EVENT_WRITE_COUNT_LIMIT   4096
#define EVENT_READ_SIZE   65536

Typedefs

typedef struct event_write event_write_t

Functions

void platform_event_stream_close (platform_event_stream_t *stream, asciichat_error_t reason)
void platform_event_listener_close (platform_event_listener_t *listener)
asciichat_error_t platform_event_loop_create (platform_event_loop_t **out)
asciichat_error_t platform_event_loop_run (platform_event_loop_t *loop)
asciichat_error_t platform_event_loop_post (platform_event_loop_t *loop, platform_event_task_fn fn, void *arg)
void platform_event_loop_stop (platform_event_loop_t *loop)
asciichat_error_t platform_event_loop_destroy (platform_event_loop_t *loop)
const char * platform_event_backend (void)
asciichat_error_t platform_event_loop_tick (platform_event_loop_t *loop, uint64_t interval, platform_event_task_fn fn, void *arg)
asciichat_error_t platform_event_stream_start (platform_event_stream_t *stream, platform_event_read_fn read, platform_event_close_fn close, void *arg)
void platform_event_stream_pause (platform_event_stream_t *stream)
asciichat_error_t platform_event_stream_resume (platform_event_stream_t *stream)
asciichat_error_t platform_event_stream_deadline (platform_event_stream_t *stream, uint64_t timeout_ms)
asciichat_error_t platform_event_stream_adopt (platform_event_loop_t *loop, socket_t *socket, platform_event_stream_t **out)
asciichat_error_t platform_event_listen (platform_event_loop_t *loop, socket_t *socket, platform_event_accept_fn accept, void *arg, platform_event_listener_t **out)
void platform_event_stream_set_zerocopy (platform_event_stream_t *stream, bool enabled)
void platform_event_stream_request_close (platform_event_stream_t *stream, asciichat_error_t reason)
asciichat_error_t platform_event_stream_write (platform_event_stream_t *stream, const socket_buffer_t *buffers, size_t count, platform_event_write_fn done, void *arg)
size_t platform_event_stream_queued_bytes (const platform_event_stream_t *stream)
void platform_event_stream_observe_queue (platform_event_stream_t *stream, platform_event_queue_fn observer, void *context)
asciichat_error_t platform_event_loop_work (platform_event_loop_t *loop, platform_event_task_fn run, platform_event_task_fn done, void *arg)
asciichat_error_t platform_event_stream_work (platform_event_stream_t *stream, platform_event_work_fn run, platform_event_work_done_fn done, void *arg)
asciichat_error_t platform_event_stream_peer (platform_event_stream_t *stream, struct sockaddr_storage *address, socklen_t *length)

Macro Definition Documentation

◆ EVENT_COMMAND_LIMIT

#define EVENT_COMMAND_LIMIT   1024

Definition at line 7 of file event.c.

Referenced by platform_event_loop_post().

◆ EVENT_READ_SIZE

#define EVENT_READ_SIZE   65536

Definition at line 10 of file event.c.

◆ EVENT_WRITE_COUNT_LIMIT

#define EVENT_WRITE_COUNT_LIMIT   4096

Definition at line 9 of file event.c.

Referenced by platform_event_stream_write().

◆ EVENT_WRITE_LIMIT

#define EVENT_WRITE_LIMIT   (16U * 1024U * 1024U)

Definition at line 8 of file event.c.

Referenced by platform_event_stream_write().

Typedef Documentation

◆ event_write_t

typedef struct event_write event_write_t

Function Documentation

◆ platform_event_backend()

const char * platform_event_backend ( void )

Definition at line 288 of file event.c.

288 {
289#ifdef _WIN32
290 return "iocp";
291#elif defined(__linux__)
292 return "epoll";
293#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
294 defined(__NetBSD__)
295 return "kqueue";
296#else
297 return "libuv";
298#endif
299}

◆ platform_event_listen()

asciichat_error_t platform_event_listen ( platform_event_loop_t * loop,
socket_t * socket,
platform_event_accept_fn accept,
void * arg,
platform_event_listener_t ** out )

Sets *socket to INVALID_SOCKET_VALUE when ownership transfers, including if listen subsequently fails. Accepted streams are owned by the loop until their close callback returns. Set callbacks in the accept callback.

Definition at line 476 of file event.c.

480 {
481 if (!loop || !socket || !accept || !out)
482 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid event listener");
483 *out = NULL;
484 platform_event_listener_t *listener =
485 SAFE_CALLOC(1, sizeof(*listener), platform_event_listener_t *);
486 int status = uv_tcp_init(&loop->uv, &listener->tcp);
487 if (status < 0) {
488 SAFE_FREE(listener);
489 return event_error(status, "Initialize listener");
490 }
491 listener->tcp.data = listener;
492 listener->listener = true;
493 listener->accept = accept;
494 listener->arg = arg;
495 status = uv_tcp_open(&listener->tcp, (uv_os_sock_t)*socket);
496 if (status < 0) {
497 uv_close((uv_handle_t *)&listener->tcp, listener_closed);
498 return event_error(status, "Adopt listener");
499 }
500 *socket = INVALID_SOCKET_VALUE;
501 status = uv_listen((uv_stream_t *)&listener->tcp, 128, accepted);
502 if (status < 0) {
503 // Adoption has succeeded: the loop owns the descriptor even if listen
504 // fails.
505 uv_close((uv_handle_t *)&listener->tcp, listener_closed);
506 return event_error(status, "Listen");
507 }
508 *out = listener;
509 return ASCIICHAT_OK;
510}
#define SET_ERRNO(code,...)
struct platform_event_listener platform_event_listener_t
Definition event.h:17
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
static bool size_t gotel_query_t * out
Definition queries.h:10
platform_event_accept_fn accept
Definition event.c:82
uv_loop_t uv
Definition event.c:22

References platform_event_listener::accept, platform_event_listener::arg, ASCIICHAT_OK, ERROR_INVALID_PARAM, platform_event_listener::listener, out, SAFE_CALLOC, SAFE_FREE, SET_ERRNO, platform_event_listener::tcp, and platform_event_loop::uv.

◆ platform_event_listener_close()

void platform_event_listener_close ( platform_event_listener_t * listener)

Definition at line 169 of file event.c.

169 {
170 if (listener && !uv_is_closing((uv_handle_t *)&listener->tcp))
171 uv_close((uv_handle_t *)&listener->tcp, listener_closed);
172}

References platform_event_listener::tcp.

◆ platform_event_loop_create()

asciichat_error_t platform_event_loop_create ( platform_event_loop_t ** out)

Definition at line 210 of file event.c.

210 {
211 if (!out)
212 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing event loop output");
213 *out = NULL;
215 SAFE_CALLOC(1, sizeof(*loop), platform_event_loop_t *);
216 int status = uv_loop_init(&loop->uv);
217 if (status < 0) {
218 SAFE_FREE(loop);
219 return event_error(status, "Initialize event loop");
220 }
221 status = uv_mutex_init(&loop->mutex);
222 if (status < 0) {
223 uv_loop_close(&loop->uv);
224 SAFE_FREE(loop);
225 return event_error(status, "Initialize event queue");
226 }
227 status = uv_async_init(&loop->uv, &loop->wake, wake_loop);
228 if (status < 0) {
229 uv_mutex_destroy(&loop->mutex);
230 uv_loop_close(&loop->uv);
231 SAFE_FREE(loop);
232 return event_error(status, "Initialize event wakeup");
233 }
234 loop->wake.data = loop;
235 loop->uv.data = loop;
236 *out = loop;
237 return ASCIICHAT_OK;
238}
struct platform_event_loop platform_event_loop_t
Definition event.h:15
uv_mutex_t mutex
Definition event.c:29
uv_async_t wake
Definition event.c:23

References ASCIICHAT_OK, ERROR_INVALID_PARAM, platform_event_loop::mutex, out, SAFE_CALLOC, SAFE_FREE, SET_ERRNO, platform_event_loop::uv, and platform_event_loop::wake.

Referenced by acip_async_tcp_client_create().

◆ platform_event_loop_destroy()

asciichat_error_t platform_event_loop_destroy ( platform_event_loop_t * loop)

Definition at line 277 of file event.c.

277 {
278 if (!loop)
279 return ASCIICHAT_OK;
280 int status = uv_loop_close(&loop->uv);
281 if (status < 0)
282 return event_error(status, "Destroy event loop (stop and join first)");
283 uv_mutex_destroy(&loop->mutex);
284 SAFE_FREE(loop);
285 return ASCIICHAT_OK;
286}

References ASCIICHAT_OK, platform_event_loop::mutex, SAFE_FREE, and platform_event_loop::uv.

◆ platform_event_loop_post()

asciichat_error_t platform_event_loop_post ( platform_event_loop_t * loop,
platform_event_task_fn fn,
void * arg )

Definition at line 247 of file event.c.

249 {
250 if (!loop || !fn)
251 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing event command");
252 uv_mutex_lock(&loop->mutex);
253 if (loop->stopping || loop->count == EVENT_COMMAND_LIMIT) {
254 uv_mutex_unlock(&loop->mutex);
255 return SET_ERRNO(ERROR_INVALID_STATE, "Event queue stopped or full");
256 }
257 loop->commands[(loop->head + loop->count) % EVENT_COMMAND_LIMIT] =
258 (event_command_t){fn, arg};
259 ++loop->count;
260 // Keep the wake handle alive until this send has finished.
261 uv_async_send(&loop->wake);
262 uv_mutex_unlock(&loop->mutex);
263 return ASCIICHAT_OK;
264}
#define EVENT_COMMAND_LIMIT
Definition event.c:7
@ ERROR_INVALID_STATE
event_command_t commands[1024]
Definition event.c:30

References ASCIICHAT_OK, platform_event_loop::commands, platform_event_loop::count, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, EVENT_COMMAND_LIMIT, platform_event_loop::head, platform_event_loop::mutex, SET_ERRNO, platform_event_loop::stopping, and platform_event_loop::wake.

◆ platform_event_loop_run()

asciichat_error_t platform_event_loop_run ( platform_event_loop_t * loop)

Definition at line 240 of file event.c.

240 {
241 if (!loop)
242 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing event loop");
243 uv_run(&loop->uv, UV_RUN_DEFAULT);
244 return ASCIICHAT_OK;
245}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, SET_ERRNO, and platform_event_loop::uv.

◆ platform_event_loop_stop()

void platform_event_loop_stop ( platform_event_loop_t * loop)

Definition at line 266 of file event.c.

266 {
267 if (!loop)
268 return;
269 uv_mutex_lock(&loop->mutex);
270 if (!loop->stopping) {
271 loop->stopping = true;
272 uv_async_send(&loop->wake);
273 }
274 uv_mutex_unlock(&loop->mutex);
275}

References platform_event_loop::mutex, platform_event_loop::stopping, and platform_event_loop::wake.

◆ platform_event_loop_tick()

asciichat_error_t platform_event_loop_tick ( platform_event_loop_t * loop,
uint64_t interval,
platform_event_task_fn fn,
void * arg )

Definition at line 306 of file event.c.

309 {
310 if (!loop || !interval || !fn || loop->tick_initialized)
311 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid event loop tick");
312 int status = uv_timer_init(&loop->uv, &loop->tick);
313 if (status < 0)
314 return event_error(status, "Initialize loop timer");
315 loop->tick_initialized = true;
316 loop->tick.data = loop;
317 loop->tick_fn = fn;
318 loop->tick_arg = arg;
319 status = uv_timer_start(&loop->tick, tick_loop, interval, interval);
320 return status < 0 ? event_error(status, "Start loop timer") : ASCIICHAT_OK;
321}
platform_event_task_fn tick_fn
Definition event.c:25
void * tick_arg
Definition event.c:26
bool tick_initialized
Definition event.c:27
uv_timer_t tick
Definition event.c:24

References ASCIICHAT_OK, ERROR_INVALID_PARAM, SET_ERRNO, platform_event_loop::tick, platform_event_loop::tick_arg, platform_event_loop::tick_fn, platform_event_loop::tick_initialized, and platform_event_loop::uv.

Referenced by acip_async_tcp_client_create().

◆ platform_event_loop_work()

asciichat_error_t platform_event_loop_work ( platform_event_loop_t * loop,
platform_event_task_fn run,
platform_event_task_fn done,
void * arg )

Definition at line 816 of file event.c.

819 {
820 if (!loop || !run || !done)
821 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid event worker");
822 loop_work_t *work = SAFE_CALLOC(1, sizeof(*work), loop_work_t *);
823 work->request.data = work;
824 work->work = run;
825 work->done = done;
826 work->arg = arg;
827 int status =
828 uv_queue_work(&loop->uv, &work->request, loop_work_run, loop_work_done);
829 if (status < 0) {
830 SAFE_FREE(work);
831 return event_error(status, "Queue loop work");
832 }
833 return ASCIICHAT_OK;
834}
This is the handoff point for event work. Callers fill work, stream, run, done before passing it on; ...
Definition event.c:794
platform_event_task_fn done
Definition event.c:796
void * arg
Definition event.c:797
uv_work_t request
Definition event.c:795
platform_event_task_fn work
Definition event.c:796

References loop_work_t::arg, ASCIICHAT_OK, loop_work_t::done, ERROR_INVALID_PARAM, loop_work_t::request, SAFE_CALLOC, SAFE_FREE, SET_ERRNO, platform_event_loop::uv, and loop_work_t::work.

◆ platform_event_stream_adopt()

asciichat_error_t platform_event_stream_adopt ( platform_event_loop_t * loop,
socket_t * socket,
platform_event_stream_t ** out )

Adopt an already-connected socket, setting it invalid on ownership transfer.

Definition at line 434 of file event.c.

436 {
437 if (!loop || !socket || !out)
438 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid socket adoption");
439 *out = NULL;
440 platform_event_stream_t *stream = create_stream(&loop->uv);
441 if (!stream)
442 return SET_ERRNO(ERROR_MEMORY, "Cannot initialize event stream");
443 int status = uv_tcp_open(&stream->tcp, (uv_os_sock_t)*socket);
444 if (status < 0) {
446 return event_error(status, "Adopt TCP stream");
447 }
448 *socket = INVALID_SOCKET_VALUE;
449 uv_tcp_nodelay(&stream->tcp, 1);
450 uv_tcp_keepalive(&stream->tcp, 1, 60);
451 *out = stream;
452 return ASCIICHAT_OK;
453}
void platform_event_stream_close(platform_event_stream_t *stream, asciichat_error_t reason)
Definition event.c:123
struct platform_event_stream platform_event_stream_t
Definition event.h:16
@ ERROR_NETWORK
Definition error_codes.h:77
@ ERROR_MEMORY
Definition error_codes.h:55

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_NETWORK, out, platform_event_stream_close(), SET_ERRNO, event_write::stream, platform_event_stream::tcp, and platform_event_loop::uv.

Referenced by acip_async_tcp_client_create().

◆ platform_event_stream_close()

void platform_event_stream_close ( platform_event_stream_t * stream,
asciichat_error_t reason )

Definition at line 123 of file event.c.

124 {
125 if (!stream)
126 return;
127 uv_mutex_lock(&stream->mutex);
128 if (stream->closing) {
129 uv_mutex_unlock(&stream->mutex);
130 return;
131 }
132 stream->closing = true;
133 event_write_t *pending = stream->first;
134 stream->first = stream->last = NULL;
135 uv_mutex_unlock(&stream->mutex);
136 while (pending) {
137 event_write_t *next = pending->next;
138 uv_mutex_lock(&stream->mutex);
139 stream->queued -= pending->size;
140 if (stream->queue_changed)
141 stream->queue_changed(stream->queue_context, stream->queued);
142 --stream->writes;
143 uv_mutex_unlock(&stream->mutex);
144 if (pending->done)
145 pending->done(pending->arg, ERROR_NETWORK);
146 socket_send_buffer_free(&pending->storage);
147 SAFE_FREE(pending);
148 pending = next;
149 }
150 stream->reason = reason;
151 // Submitted uv_write requests complete during TCP close. A raw zerocopy
152 // submission has no libuv request; discard its mapping without recycling it.
153 if (stream->active && stream->active->waiting)
154 write_complete(&stream->active->request, UV_ECANCELED);
155 uv_timer_stop(&stream->output_timer);
156 uv_close((uv_handle_t *)&stream->output_timer, stream_closed);
157 uv_read_stop((uv_stream_t *)&stream->tcp);
158 uv_timer_stop(&stream->timer);
159 uv_close((uv_handle_t *)&stream->timer, stream_closed);
160 uv_close((uv_handle_t *)&stream->tcp, stream_closed);
161 uv_close((uv_handle_t *)&stream->output, stream_closed);
162}
struct event_write event_write_t
socket_send_buffer_t storage
Definition event.c:93
size_t size
Definition event.c:91
uv_write_t request
Definition event.c:87
bool waiting
Definition event.c:96
platform_event_write_fn done
Definition event.c:89
void * arg
Definition event.c:90
struct event_write * next
Definition event.c:92
struct event_write * active
Definition event.c:53
platform_event_queue_fn queue_changed
Definition event.c:61
asciichat_error_t reason
Definition event.c:69
void * queue_context
Definition event.c:62
struct event_write * first
Definition event.c:53
uv_timer_t output_timer
Definition event.c:50
uv_async_t output
Definition event.c:51
uv_mutex_t mutex
Definition event.c:52
uv_timer_t timer
Definition event.c:49
struct event_write * last
Definition event.c:53

References platform_event_stream::active, event_write::arg, platform_event_stream::closing, event_write::done, ERROR_NETWORK, platform_event_stream::first, platform_event_stream::last, platform_event_stream::mutex, event_write::next, platform_event_stream::output, platform_event_stream::output_timer, platform_event_stream::queue_changed, platform_event_stream::queue_context, platform_event_stream::queued, platform_event_stream::reason, event_write::request, SAFE_FREE, event_write::size, event_write::storage, event_write::stream, platform_event_stream::tcp, platform_event_stream::timer, event_write::waiting, and platform_event_stream::writes.

Referenced by acip_async_tcp_accepted_create(), and platform_event_stream_adopt().

◆ platform_event_stream_deadline()

asciichat_error_t platform_event_stream_deadline ( platform_event_stream_t * stream,
uint64_t timeout_ms )

Relative timeout converted to an absolute deadline; zero disables it.

Definition at line 377 of file event.c.

378 {
379 if (!stream || stream->closing)
380 return SET_ERRNO(ERROR_INVALID_STATE, "Stream is closing");
381 uv_timer_stop(&stream->timer);
382 int status = timeout_ms ? uv_timer_start(&stream->timer, deadline_expired,
383 timeout_ms, 0)
384 : 0;
385 return status < 0 ? event_error(status, "Set stream deadline") : ASCIICHAT_OK;
386}

References ASCIICHAT_OK, platform_event_stream::closing, ERROR_INVALID_STATE, SET_ERRNO, event_write::stream, and platform_event_stream::timer.

◆ platform_event_stream_observe_queue()

void platform_event_stream_observe_queue ( platform_event_stream_t * stream,
platform_event_queue_fn observer,
void * context )

Publish queued-byte changes without polling the stream. Configure before producers start. Invoked under the stream lock; only publish atomic state, never call stream/transport APIs. Context remains alive through on_close.

Definition at line 755 of file event.c.

757 {
758 if (!stream)
759 return;
760 uv_mutex_lock(&stream->mutex);
761 stream->queue_changed = observer;
762 stream->queue_context = context;
763 if (observer)
764 observer(context, stream->queued);
765 uv_mutex_unlock(&stream->mutex);
766}

References platform_event_stream::mutex, platform_event_stream::queue_changed, platform_event_stream::queue_context, platform_event_stream::queued, and event_write::stream.

Referenced by acip_async_tcp_accepted_create(), acip_async_tcp_client_create(), acip_async_tcp_transport_create(), and acip_async_tcp_transport_detach().

◆ platform_event_stream_pause()

void platform_event_stream_pause ( platform_event_stream_t * stream)

Definition at line 358 of file event.c.

358 {
359 if (stream && !stream->closing)
360 uv_read_stop((uv_stream_t *)&stream->tcp);
361}

References platform_event_stream::closing, event_write::stream, and platform_event_stream::tcp.

Referenced by platform_event_stream_work().

◆ platform_event_stream_peer()

asciichat_error_t platform_event_stream_peer ( platform_event_stream_t * stream,
struct sockaddr_storage * address,
socklen_t * length )

Definition at line 878 of file event.c.

880 {
881 if (!stream || !address || !length)
882 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid peer address output");
883 int size = (int)*length;
884 int status =
885 uv_tcp_getpeername(&stream->tcp, (struct sockaddr *)address, &size);
886 *length = (socklen_t)size;
887 return status < 0 ? event_error(status, "Get stream peer") : ASCIICHAT_OK;
888}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, SET_ERRNO, and platform_event_stream::tcp.

◆ platform_event_stream_queued_bytes()

size_t platform_event_stream_queued_bytes ( const platform_event_stream_t * stream)

Definition at line 746 of file event.c.

746 {
747 if (!stream)
748 return 0;
749 uv_mutex_lock((uv_mutex_t *)&stream->mutex);
750 size_t bytes = stream->queued;
751 uv_mutex_unlock((uv_mutex_t *)&stream->mutex);
752 return bytes;
753}

References platform_event_stream::mutex, platform_event_stream::queued, and event_write::stream.

◆ platform_event_stream_request_close()

void platform_event_stream_request_close ( platform_event_stream_t * stream,
asciichat_error_t reason )

Reject new writes, drain accepted output, then close; write deadlines still apply.

Definition at line 662 of file event.c.

663 {
664 if (!stream)
665 return;
666 uv_mutex_lock(&stream->mutex);
667 if (!stream->closing && !stream->close_requested) {
668 stream->close_requested = true;
669 stream->requested_reason = reason;
670 uv_async_send(&stream->output);
671 }
672 uv_mutex_unlock(&stream->mutex);
673}
asciichat_error_t requested_reason
Definition event.c:68

References platform_event_stream::close_requested, platform_event_stream::closing, platform_event_stream::mutex, platform_event_stream::output, platform_event_stream::requested_reason, and event_write::stream.

◆ platform_event_stream_resume()

asciichat_error_t platform_event_stream_resume ( platform_event_stream_t * stream)

Definition at line 364 of file event.c.

364 {
365 if (!stream || stream->closing || !stream->read)
366 return SET_ERRNO(ERROR_INVALID_STATE, "Stream cannot resume");
367 int status =
368 uv_read_start((uv_stream_t *)&stream->tcp, allocate_read, read_stream);
369 return status < 0 ? event_error(status, "Start stream read") : ASCIICHAT_OK;
370}
platform_event_read_fn read
Definition event.c:57

References ASCIICHAT_OK, platform_event_stream::closing, ERROR_INVALID_STATE, platform_event_stream::read, SET_ERRNO, event_write::stream, and platform_event_stream::tcp.

Referenced by platform_event_stream_start().

◆ platform_event_stream_set_zerocopy()

void platform_event_stream_set_zerocopy ( platform_event_stream_t * stream,
bool enabled )

Configure copy avoidance before starting producers. The loop always owns I/O.

Definition at line 653 of file event.c.

654 {
655 if (!stream)
656 return;
657 uv_mutex_lock(&stream->mutex);
658 stream->zerocopy = enabled;
659 uv_mutex_unlock(&stream->mutex);
660}
bool enabled
Is filtering active?
Definition grep.c:63

References enabled, platform_event_stream::mutex, event_write::stream, and platform_event_stream::zerocopy.

◆ platform_event_stream_start()

asciichat_error_t platform_event_stream_start ( platform_event_stream_t * stream,
platform_event_read_fn read,
platform_event_close_fn close,
void * arg )

Definition at line 341 of file event.c.

344 {
345 if (!stream || stream->closing || !read)
346 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid stream callbacks");
347 // Retain cleanup callbacks even when configuring or starting reads fails.
348 stream->read = read;
349 stream->close = close;
350 stream->arg = arg;
351 int send_buffer = SOCKET_STREAM_SEND_BUFFER;
352 int status = uv_send_buffer_size((uv_handle_t *)&stream->tcp, &send_buffer);
353 if (status < 0)
354 return event_error(status, "Bound stream send buffer");
355 return platform_event_stream_resume(stream);
356}
asciichat_error_t platform_event_stream_resume(platform_event_stream_t *stream)
Definition event.c:364
platform_event_close_fn close
Definition event.c:58

References platform_event_stream::arg, platform_event_stream::close, platform_event_stream::closing, ERROR_INVALID_PARAM, platform_event_stream_resume(), platform_event_stream::read, SET_ERRNO, event_write::stream, and platform_event_stream::tcp.

Referenced by acip_async_tcp_accepted_create(), and acip_async_tcp_client_create().

◆ platform_event_stream_work()

asciichat_error_t platform_event_stream_work ( platform_event_stream_t * stream,
platform_event_work_fn run,
platform_event_work_done_fn done,
void * arg )

Run blocking application work outside the loop. One job per stream at a time; reads pause until done runs on the loop. Stream lifetime includes this job. write(), request_close(), and queued_bytes() are thread-safe while the caller holds a live stream reference; other stream operations are loop-only.

Definition at line 853 of file event.c.

856 {
857 if (!stream || stream->closing || stream->working || !run || !done)
859 "Stream work already active or closing");
860 event_work_t *work = SAFE_CALLOC(1, sizeof(*work), event_work_t *);
861 work->stream = stream;
862 work->run = run;
863 work->done = done;
864 work->arg = arg;
865 work->work.data = work;
867 int status =
868 uv_queue_work(stream->tcp.loop, &work->work, run_work, finish_work);
869 if (status < 0) {
870 SAFE_FREE(work);
871 return event_error(status, "Queue stream work");
872 }
873 stream->working = true;
874 ++stream->handles;
875 return ASCIICHAT_OK;
876}
void platform_event_stream_pause(platform_event_stream_t *stream)
Definition event.c:358
event command keeps the values that move through the subsystem: fn, arg. Some members are pointers in...
Definition event.c:774
platform_event_work_fn run
Definition event.c:777
platform_event_stream_t * stream
Definition event.c:776
void * arg
Definition event.c:779
uv_work_t work
Definition event.c:775
platform_event_work_done_fn done
Definition event.c:778
unsigned handles
Definition event.c:64

References event_work_t::arg, ASCIICHAT_OK, platform_event_stream::closing, event_work_t::done, ERROR_INVALID_STATE, platform_event_stream::handles, platform_event_stream_pause(), event_work_t::run, SAFE_CALLOC, SAFE_FREE, SET_ERRNO, event_work_t::stream, platform_event_stream::tcp, event_work_t::work, and platform_event_stream::working.

◆ platform_event_stream_write()

asciichat_error_t platform_event_stream_write ( platform_event_stream_t * stream,
const socket_buffer_t * buffers,
size_t count,
platform_event_write_fn done,
void * arg )

Copies input before returning. Completion means local write completion, not peer acknowledgement. Rejected writes never invoke completion. Accepted writes invoke completion exactly once, including during shutdown, before on_close.

Definition at line 675 of file event.c.

679 {
680 if (!stream || !buffers || !count || count > SOCKET_IOV_MAX)
681 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid asynchronous write");
682 size_t size = 0;
683 for (size_t i = 0; i < count; ++i) {
684 if ((!buffers[i].data && buffers[i].len) ||
685 buffers[i].len > EVENT_WRITE_LIMIT - size)
686 return SET_ERRNO(ERROR_NETWORK_SIZE, "Asynchronous write too large");
687 size += buffers[i].len;
688 }
689 uv_mutex_lock(&stream->mutex);
690 if (stream->closing || stream->close_requested || !size ||
691 stream->writes >= EVENT_WRITE_COUNT_LIMIT ||
692 size > EVENT_WRITE_LIMIT - stream->queued) {
693 uv_mutex_unlock(&stream->mutex);
694 return SET_ERRNO(ERROR_NETWORK_SIZE, "Asynchronous output queue full");
695 }
696 bool mapped = stream->zerocopy && size >= SOCKET_ZEROCOPY_THRESHOLD;
697 event_write_t **cached = &stream->recycled;
698 while (*cached && (mapped || (*cached)->storage.capacity < size))
699 cached = &(*cached)->next;
700 event_write_t *write = *cached;
701 socket_send_buffer_t storage = {0};
702 if (write) {
703 *cached = write->next;
704 --stream->recycled_count;
705 storage = write->storage;
706 } else {
707 write = SAFE_MALLOC(sizeof(*write), event_write_t *);
708 }
709 *write = (event_write_t){.stream = stream,
710 .done = done,
711 .arg = arg,
712 .size = size,
713 .storage = storage};
714 if (!write->storage.data &&
715 socket_send_buffer_alloc(size, &write->storage) != ASCIICHAT_OK) {
716 SAFE_FREE(write);
717 uv_mutex_unlock(&stream->mutex);
718 return SET_ERRNO(ERROR_MEMORY,
719 mapped
720 ? "Async copy avoidance exceeds process buffer budget"
721 : "Async output exceeds process buffer budget");
722 }
723 write->request.data = write;
724 size_t offset = 0;
725 for (size_t i = 0; i < count; ++i) {
726 if (buffers[i].len)
727 memcpy((char *)write->storage.data + offset, buffers[i].data,
728 buffers[i].len);
729 offset += buffers[i].len;
730 }
731 stream->queued += size;
732 if (stream->queue_changed)
733 stream->queue_changed(stream->queue_context, stream->queued);
734 ++stream->writes;
735 if (stream->last)
736 stream->last->next = write;
737 else
738 stream->first = write;
739 stream->last = write;
740 uv_async_send(&stream->output);
741 uv_mutex_unlock(&stream->mutex);
742 return ASCIICHAT_OK;
743}
#define EVENT_WRITE_LIMIT
Definition event.c:8
#define EVENT_WRITE_COUNT_LIMIT
Definition event.c:9
#define SAFE_MALLOC(size, cast)
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
size_t recycled_count
Definition event.c:55
struct event_write * recycled
Definition event.c:54

References platform_event_stream::arg, ASCIICHAT_OK, platform_event_stream::close_requested, platform_event_stream::closing, event_write::done, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_NETWORK_SIZE, EVENT_WRITE_COUNT_LIMIT, EVENT_WRITE_LIMIT, platform_event_stream::first, platform_event_stream::last, platform_event_stream::mutex, event_write::next, event_write::offset, platform_event_stream::output, platform_event_stream::queue_changed, platform_event_stream::queue_context, platform_event_stream::queued, platform_event_stream::recycled, platform_event_stream::recycled_count, event_write::request, SAFE_FREE, SAFE_MALLOC, SET_ERRNO, event_write::size, event_write::storage, event_write::stream, platform_event_stream::writes, and platform_event_stream::zerocopy.