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
Go to the documentation of this file.
1#include <ascii-chat/buffer.h>
2#include <ascii-chat/common.h>
4#include <string.h>
5#include <uv.h>
6
7#define EVENT_COMMAND_LIMIT 1024
8#define EVENT_WRITE_LIMIT (16U * 1024U * 1024U)
9#define EVENT_WRITE_COUNT_LIMIT 4096
10#define EVENT_READ_SIZE 65536
11
12typedef struct {
14 void *arg;
18
34
72
85
98
99static asciichat_error_t event_error(int status, const char *operation) {
100 return SET_ERRNO(ERROR_NETWORK, "%s: %s", operation, uv_strerror(status));
101}
102
103static void flush_output(uv_async_t *output);
104static void write_complete(uv_write_t *request, int status);
105
106static void stream_closed(uv_handle_t *handle) {
107 platform_event_stream_t *stream = handle->data;
108 if (--stream->handles)
109 return;
110 if (stream->close)
112 while (stream->recycled) {
113 event_write_t *write = stream->recycled;
114 stream->recycled = write->next;
115 socket_send_buffer_free(&write->storage);
116 SAFE_FREE(write);
117 }
118 uv_mutex_destroy(&stream->mutex);
119 --stream->owner->streams;
121}
122
124 asciichat_error_t reason) {
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;
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.
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}
163
164static void listener_closed(uv_handle_t *handle) {
165 platform_event_listener_t *listener = handle->data;
166 SAFE_FREE(listener);
167}
168
170 if (listener && !uv_is_closing((uv_handle_t *)&listener->tcp))
171 uv_close((uv_handle_t *)&listener->tcp, listener_closed);
172}
173
174static void stop_handle(uv_handle_t *handle, void *arg) {
175 platform_event_loop_t *loop = arg;
176 if (uv_is_closing(handle))
177 return;
178 if (handle->type == UV_TCP) {
179 if (*(bool *)handle->data)
180 platform_event_listener_close(handle->data);
181 else
183 } else if (handle == (uv_handle_t *)&loop->wake ||
184 handle == (uv_handle_t *)&loop->tick) {
185 uv_close(handle, NULL);
186 }
187}
188
189static void wake_loop(uv_async_t *wake) {
190 platform_event_loop_t *loop = wake->data;
191 // Bound each batch so a producer cannot starve socket events and deadlines.
192 for (size_t i = 0; i < EVENT_COMMAND_LIMIT; ++i) {
193 uv_mutex_lock(&loop->mutex);
194 if (!loop->count) {
195 bool stopping = loop->stopping;
196 uv_mutex_unlock(&loop->mutex);
197 if (stopping)
198 uv_walk(&loop->uv, stop_handle, loop);
199 return;
200 }
201 event_command_t command = loop->commands[loop->head];
202 loop->head = (loop->head + 1) % EVENT_COMMAND_LIMIT;
203 --loop->count;
204 uv_mutex_unlock(&loop->mutex);
205 command.fn(command.arg);
206 }
207 uv_async_send(wake);
208}
209
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}
239
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}
246
249 void *arg) {
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}
265
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}
276
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}
287
288const char *platform_event_backend(void) {
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}
300
301static void tick_loop(uv_timer_t *timer) {
302 platform_event_loop_t *loop = timer->data;
303 loop->tick_fn(loop->tick_arg);
304}
305
307 uint64_t interval,
309 void *arg) {
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}
322
323static void allocate_read(uv_handle_t *handle, size_t suggested,
324 uv_buf_t *buffer) {
325 (void)suggested;
326 platform_event_stream_t *stream = handle->data;
327 *buffer = uv_buf_init(stream->input, sizeof(stream->input));
328}
329
330static void read_stream(uv_stream_t *handle, ssize_t count,
331 const uv_buf_t *buffer) {
332 platform_event_stream_t *stream = handle->data;
333 if (count < 0) {
335 count == UV_EOF ? ASCIICHAT_OK : ERROR_NETWORK);
336 } else if (count && !stream->closing && stream->read) {
337 stream->read(stream, buffer->base, (size_t)count, stream->arg);
338 }
339}
340
344 void *arg) {
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");
356}
357
359 if (stream && !stream->closing)
360 uv_read_stop((uv_stream_t *)&stream->tcp);
361}
362
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}
371
372static void deadline_expired(uv_timer_t *timer) {
374}
375
378 uint64_t timeout_ms) {
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}
387
388static platform_event_stream_t *create_stream(uv_loop_t *loop) {
391 int status = uv_mutex_init(&stream->mutex);
392 if (status < 0) {
394 return NULL;
395 }
396 status = uv_tcp_init(loop, &stream->tcp);
397 if (status < 0) {
398 uv_mutex_destroy(&stream->mutex);
400 return NULL;
401 }
402 stream->tcp.data = stream;
403 stream->owner = loop->data;
404 stream->zerocopy = true;
405 ++stream->owner->streams;
406 stream->handles = 1;
407 status = uv_timer_init(loop, &stream->timer);
408 if (status < 0) {
409 uv_close((uv_handle_t *)&stream->tcp, stream_closed);
410 return NULL;
411 }
412 stream->timer.data = stream;
413 ++stream->handles;
414 status = uv_timer_init(loop, &stream->output_timer);
415 if (status < 0) {
416 uv_close((uv_handle_t *)&stream->timer, stream_closed);
417 uv_close((uv_handle_t *)&stream->tcp, stream_closed);
418 return NULL;
419 }
420 stream->output_timer.data = stream;
421 ++stream->handles;
422 status = uv_async_init(loop, &stream->output, flush_output);
423 if (status < 0) {
424 uv_close((uv_handle_t *)&stream->output_timer, stream_closed);
425 uv_close((uv_handle_t *)&stream->timer, stream_closed);
426 uv_close((uv_handle_t *)&stream->tcp, stream_closed);
427 return NULL;
428 }
429 stream->output.data = stream;
430 ++stream->handles;
431 return stream;
432}
433
435 socket_t *socket,
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}
454
455static void accepted(uv_stream_t *handle, int status) {
456 if (status < 0)
457 return;
458 platform_event_listener_t *listener = handle->data;
459 platform_event_stream_t *stream = create_stream(handle->loop);
460 if (!stream)
461 return;
462 status = uv_accept(handle, (uv_stream_t *)&stream->tcp);
463 if (status < 0) {
465 return;
466 }
467 uv_tcp_nodelay(&stream->tcp, 1);
468 uv_tcp_keepalive(&stream->tcp, 1, 60);
469 if (stream->owner->streams > 512) {
471 return;
472 }
473 listener->accept(stream, listener->arg);
474}
475
477 socket_t *socket,
479 void *arg,
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}
511
512static void write_complete(uv_write_t *request, int status) {
513 event_write_t *write = request->data;
516 void *context = write->arg;
517 uv_timer_stop(&stream->output_timer);
518 stream->active = NULL;
519 uv_mutex_lock(&stream->mutex);
520 stream->queued -= write->size;
523 --stream->writes;
524 bool recycle = !stream->closing && stream->recycled_count < 8 &&
525 !write->storage.mapped &&
526 write->storage.capacity <= 256 * 1024;
527 if (recycle) {
528 write->next = stream->recycled;
529 stream->recycled = write;
531 }
532 uv_mutex_unlock(&stream->mutex);
533 if (done)
534 done(context, status < 0 ? ERROR_NETWORK : ASCIICHAT_OK);
535 if (!recycle) {
536 socket_send_buffer_free(&write->storage);
537 SAFE_FREE(write);
538 }
539 if (status < 0)
541 else if (!stream->closing)
542 uv_async_send(&stream->output);
543}
544
545static socket_t stream_socket(platform_event_stream_t *stream) {
546 uv_os_fd_t descriptor;
547 if (uv_fileno((uv_handle_t *)&stream->tcp, &descriptor) < 0)
548 return INVALID_SOCKET_VALUE;
549 return (socket_t)(uintptr_t)descriptor;
550}
551
552static void submit_output(platform_event_stream_t *stream) {
553 event_write_t *write = stream->active;
554 uv_mutex_lock(&stream->mutex);
555 bool zerocopy = stream->zerocopy;
556 uv_mutex_unlock(&stream->mutex);
557 if (zerocopy && write->storage.mapped &&
558 write->size - write->offset >= SOCKET_ZEROCOPY_THRESHOLD) {
559 size_t sent = 0;
560 bool supported;
561 asciichat_error_t result = socket_zerocopy_try(
562 stream_socket(stream), &write->storage, write->offset,
563 write->size - write->offset, &sent, &supported);
564 if (result != ASCIICHAT_OK) {
565 write_complete(&write->request, UV_EIO);
566 return;
567 }
568 if (!supported) {
569 uv_mutex_lock(&stream->mutex);
570 stream->zerocopy = false;
571 uv_mutex_unlock(&stream->mutex);
572 }
573 if (sent) {
574 write->offset += sent;
575 write->waiting = true;
576 return;
577 }
578 }
579 // No kernel-pinned submission is outstanding here. libuv handles readiness,
580 // partial writes, and completion on every platform for the remaining bytes.
581 uv_buf_t buffer = uv_buf_init((char *)write->storage.data + write->offset,
582 (unsigned int)(write->size - write->offset));
583 int status = uv_write(&write->request, (uv_stream_t *)&stream->tcp, &buffer,
584 1, write_complete);
585 if (status < 0)
586 write_complete(&write->request, status);
587}
588
589static void output_progress(uv_timer_t *timer) {
590 platform_event_stream_t *stream = timer->data;
591 event_write_t *write = stream->active;
592 if (!write || stream->closing)
593 return;
594 if (uv_now(stream->tcp.loop) - write->started >= 30000) {
596 return;
597 }
598 if (!write->waiting)
599 return;
600 bool completed, copied;
601 asciichat_error_t result =
602 socket_zerocopy_poll(stream_socket(stream), &completed, &copied);
603 if (result != ASCIICHAT_OK) {
605 return;
606 }
607 if (!completed)
608 return;
609 write->waiting = false;
610 if (copied) {
611 uv_mutex_lock(&stream->mutex);
612 stream->zerocopy = false;
613 uv_mutex_unlock(&stream->mutex);
614 }
615 if (write->offset == write->size)
616 write_complete(&write->request, 0);
617 else
618 submit_output(stream);
619}
620
621static void flush_output(uv_async_t *output) {
622 platform_event_stream_t *stream = output->data;
623 uv_mutex_lock(&stream->mutex);
626 uv_mutex_unlock(&stream->mutex);
628 return;
629 }
630 if (stream->closing || stream->active || !stream->first) {
631 uv_mutex_unlock(&stream->mutex);
632 return;
633 }
634 event_write_t *write = stream->first;
635 stream->first = write->next;
636 if (!stream->first)
637 stream->last = NULL;
638 stream->active = write;
639 uv_mutex_unlock(&stream->mutex);
640 write->started = uv_now(stream->tcp.loop);
641 // Only copy-avoidance submissions need completion probing. Ordinary writes
642 // use this timer solely for their deadline.
643 uint64_t interval = write->storage.mapped ? 5 : 30000;
644 int status = uv_timer_start(&stream->output_timer, output_progress, interval,
645 interval);
646 if (status < 0) {
647 write_complete(&write->request, status);
648 return;
649 }
650 submit_output(stream);
651}
652
654 bool enabled) {
655 if (!stream)
656 return;
657 uv_mutex_lock(&stream->mutex);
659 uv_mutex_unlock(&stream->mutex);
660}
661
663 asciichat_error_t reason) {
664 if (!stream)
665 return;
666 uv_mutex_lock(&stream->mutex);
668 stream->close_requested = true;
669 stream->requested_reason = reason;
670 uv_async_send(&stream->output);
671 }
672 uv_mutex_unlock(&stream->mutex);
673}
674
676 const socket_buffer_t *buffers,
677 size_t count,
679 void *arg) {
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);
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;
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;
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}
744
745size_t
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}
754
757 void *context) {
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}
767
787
794typedef struct {
795 uv_work_t request;
797 void *arg;
805
806static void loop_work_run(uv_work_t *request) {
807 loop_work_t *work = request->data;
808 work->work(work->arg);
809}
810static void loop_work_done(uv_work_t *request, int status) {
811 (void)status;
812 loop_work_t *work = request->data;
813 work->done(work->arg);
814 SAFE_FREE(work);
815}
819 void *arg) {
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}
835
836static void run_work(uv_work_t *request) {
837 event_work_t *work = request->data;
838 work->run(work->arg);
839}
840
841static void finish_work(uv_work_t *request, int status) {
842 (void)status;
843 event_work_t *work = request->data;
844 platform_event_stream_t *stream = work->stream;
845 stream->working = false;
846 work->done(stream, work->arg, stream->closing);
847 SAFE_FREE(work);
848 // The extra reference keeps application state alive through worker
849 // completion.
850 stream_closed((uv_handle_t *)&stream->tcp);
851}
852
856 void *arg) {
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}
877
879 struct sockaddr_storage *address,
880 socklen_t *length) {
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}
#define SET_ERRNO(code,...)
asciichat_error_t platform_event_stream_resume(platform_event_stream_t *stream)
Definition event.c:364
size_t platform_event_stream_queued_bytes(const platform_event_stream_t *stream)
Definition event.c:746
asciichat_error_t platform_event_loop_destroy(platform_event_loop_t *loop)
Definition event.c:277
asciichat_error_t platform_event_loop_post(platform_event_loop_t *loop, platform_event_task_fn fn, void *arg)
Definition event.c:247
struct event_write event_write_t
void platform_event_stream_close(platform_event_stream_t *stream, asciichat_error_t reason)
Definition event.c:123
void platform_event_stream_request_close(platform_event_stream_t *stream, asciichat_error_t reason)
Definition event.c:662
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)
Definition event.c:675
#define EVENT_COMMAND_LIMIT
Definition event.c:7
asciichat_error_t platform_event_stream_peer(platform_event_stream_t *stream, struct sockaddr_storage *address, socklen_t *length)
Definition event.c:878
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)
Definition event.c:476
asciichat_error_t platform_event_stream_adopt(platform_event_loop_t *loop, socket_t *socket, platform_event_stream_t **out)
Definition event.c:434
#define EVENT_READ_SIZE
Definition event.c:10
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 event.c:341
void platform_event_stream_set_zerocopy(platform_event_stream_t *stream, bool enabled)
Definition event.c:653
void platform_event_loop_stop(platform_event_loop_t *loop)
Definition event.c:266
#define EVENT_WRITE_LIMIT
Definition event.c:8
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)
Definition event.c:853
asciichat_error_t platform_event_loop_run(platform_event_loop_t *loop)
Definition event.c:240
asciichat_error_t platform_event_loop_create(platform_event_loop_t **out)
Definition event.c:210
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 event.c:816
void platform_event_stream_observe_queue(platform_event_stream_t *stream, platform_event_queue_fn observer, void *context)
Definition event.c:755
asciichat_error_t platform_event_loop_tick(platform_event_loop_t *loop, uint64_t interval, platform_event_task_fn fn, void *arg)
Definition event.c:306
void platform_event_listener_close(platform_event_listener_t *listener)
Definition event.c:169
const char * platform_event_backend(void)
Definition event.c:288
void platform_event_stream_pause(platform_event_stream_t *stream)
Definition event.c:358
#define EVENT_WRITE_COUNT_LIMIT
Definition event.c:9
asciichat_error_t platform_event_stream_deadline(platform_event_stream_t *stream, uint64_t timeout_ms)
Definition event.c:377
struct platform_event_listener platform_event_listener_t
Definition event.h:17
struct platform_event_loop platform_event_loop_t
Definition event.h:15
void(* platform_event_work_fn)(void *)
Definition event.h:25
void(* platform_event_accept_fn)(platform_event_stream_t *, void *)
Definition event.h:24
void(* platform_event_close_fn)(void *, asciichat_error_t)
Definition event.h:21
struct platform_event_stream platform_event_stream_t
Definition event.h:16
void(* platform_event_work_done_fn)(platform_event_stream_t *, void *, bool closing)
Definition event.h:26
void(* platform_event_task_fn)(void *arg)
Definition event.h:18
void(* platform_event_queue_fn)(void *, size_t)
Definition event.h:23
void(* platform_event_write_fn)(void *, asciichat_error_t)
Definition event.h:22
void(* platform_event_read_fn)(platform_event_stream_t *, const void *, size_t, void *)
Definition event.h:19
bool enabled
Is filtering active?
Definition grep.c:63
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_NETWORK
Definition error_codes.h:77
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ ERROR_INVALID_PARAM
@ ERROR_NETWORK_TIMEOUT
Definition error_codes.h:80
int socket_t
⚙️ Common definitions, error codes, macros, and types shared throughout the application
static bool size_t gotel_query_t * out
Definition queries.h:10
🔤 String Manipulation and Shell Escaping Utilities
void * arg
Definition event.c:14
platform_event_task_fn fn
Definition event.c:13
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
socket_send_buffer_t storage
Definition event.c:93
size_t size
Definition event.c:91
uv_write_t request
Definition event.c:87
size_t offset
Definition event.c:94
bool waiting
Definition event.c:96
platform_event_write_fn done
Definition event.c:89
void * arg
Definition event.c:90
uint64_t started
Definition event.c:95
struct event_write * next
Definition event.c:92
platform_event_stream_t * stream
Definition event.c:88
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
platform event stream is intentionally a small, concrete record. Fields such as listener,...
Definition event.c:79
platform_event_accept_fn accept
Definition event.c:82
event_command_t commands[1024]
Definition event.c:30
uv_loop_t uv
Definition event.c:22
uv_mutex_t mutex
Definition event.c:29
uv_async_t wake
Definition event.c:23
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
The useful part of event write is in request, stream, done, arg: together they describe what is pendi...
Definition event.c:45
size_t recycled_count
Definition event.c:55
struct event_write * recycled
Definition event.c:54
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
char input[65536]
Definition event.c:70
void * queue_context
Definition event.c:62
struct event_write * first
Definition event.c:53
platform_event_close_fn close
Definition event.c:58
unsigned handles
Definition event.c:64
platform_event_loop_t * owner
Definition event.c:47
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
platform_event_read_fn read
Definition event.c:57
asciichat_error_t requested_reason
Definition event.c:68
uv_timer_t timer
Definition event.c:49
struct event_write * last
Definition event.c:53