From 80c0394ec0d51b179658239586494a1a7a5806d7 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Fri, 5 Dec 2025 12:57:58 -0500 Subject: [PATCH] core: Miniscule incremental progress on websocket message handling in C. --- src/httpd.app.c | 33 +++++++++++++++++++++++++++++++-- src/task.c | 2 -- src/taskstub.js.h | 7 +++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/httpd.app.c b/src/httpd.app.c index b8e24c87..5504f66e 100644 --- a/src/httpd.app.c +++ b/src/httpd.app.c @@ -7,6 +7,7 @@ #include "ssb.db.h" #include "ssb.h" #include "task.h" +#include "taskstub.js.h" #include "util.js.h" #include "picohttpparser.h" @@ -219,6 +220,8 @@ typedef struct _app_t tf_http_request_t* request; const char* settings; JSValue credentials; + + tf_taskstub_t* taskstub; } app_t; static void _httpd_auth_query_work(tf_ssb_t* ssb, void* user_data) @@ -230,11 +233,37 @@ static void _httpd_auth_query_work(tf_ssb_t* ssb, void* user_data) static void _httpd_app_on_message(tf_http_request_t* request, int op_code, const void* data, size_t size) { tf_printf("REQUEST MESSAGE %.*s\n", (int)size, (const char*)data); + app_t* work = request->user_data; + JSContext* context = request->context; + switch (op_code) + { + case 0x1: + case 0x2: + break; + /* CLOSE */ + case 0x8: + if (work->taskstub) + { + JSValue result = tf_taskstub_kill(work->taskstub); + tf_util_report_error(context, result); + JS_FreeValue(context, result); + work->taskstub = NULL; + } + break; + /* PONG */ + case 0xa: + break; + } } static void _httpd_app_on_close(tf_http_request_t* request) { tf_printf("REQUEST CLOSE\n"); + JSContext* context = request->context; + app_t* work = request->user_data; + JS_FreeValue(context, work->credentials); + tf_free(work); + tf_http_request_unref(request); } @@ -323,12 +352,12 @@ static void _httpd_auth_query_after_work(tf_ssb_t* ssb, int status, void* user_d /* What now? */ tf_free((void*)cookie); JS_FreeCString(context, name_string); - JS_FreeValue(context, work->credentials); - tf_free(work); // tf_http_request_unref(request); request->on_message = _httpd_app_on_message; request->on_close = _httpd_app_on_close; + request->context = context; + request->user_data = work; } static void _tf_httpd_endpoint_app_socket_c(tf_http_request_t* request) diff --git a/src/task.c b/src/task.c index 9806bb0b..6381d416 100644 --- a/src/task.c +++ b/src/task.c @@ -1761,8 +1761,6 @@ static void _tf_task_on_handle_close(uv_handle_t* handle) handle->data = NULL; } -JSValue tf_taskstub_kill(tf_taskstub_t* stub); - void tf_task_destroy(tf_task_t* task) { if (!task->_shutting_down) diff --git a/src/taskstub.js.h b/src/taskstub.js.h index 63c83c1d..91a73823 100644 --- a/src/taskstub.js.h +++ b/src/taskstub.js.h @@ -82,4 +82,11 @@ void tf_taskstub_on_error(tf_taskstub_t* stub, JSValue error); */ void tf_taskstub_on_print(tf_taskstub_t* stub, JSValue arguments); +/** +** Terminate a task stub. +** @param stub The task stub to kill. +** @return A promise if it is happening asynchronously. +*/ +JSValue tf_taskstub_kill(tf_taskstub_t* stub); + /** @} */