From d119f85260d19266d87d7227ee056b6acd125fef Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 28 Nov 2023 10:32:08 +0100 Subject: [PATCH] feat(lnm): wrote part of context code --- lnm/include/lnm/http/loop.h | 15 ++++++- lnm/src/_include/lnm/http/loop_internal.h | 23 +++++++++++ lnm/src/http/lnm_http_loop.c | 9 ++++- lnm/src/http/loop_ctx.c | 49 +++++++++++++++++++++++ 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/lnm/include/lnm/http/loop.h b/lnm/include/lnm/http/loop.h index 2a253d3..550d046 100644 --- a/lnm/include/lnm/http/loop.h +++ b/lnm/include/lnm/http/loop.h @@ -18,16 +18,26 @@ typedef enum lnm_step_err { lnm_step_err_done = 0, lnm_step_err_io_needed, lnm_step_err_close, + lnm_step_err_res, } lnm_step_err; typedef lnm_step_err (*lnm_http_step_fn)(lnm_http_conn *conn); +typedef lnm_err (*lnm_http_ctx_init_fn)(void **c_ctx, void *gctx); + +typedef void (*lnm_http_ctx_reset_fn)(void *c_ctx); + +typedef void (*lnm_http_ctx_free_fn)(void *c_ctx); + /** * Initialize a new `lnm_http_loop`. * * @param out where to store pointer to new `lnm_http_loop` */ -lnm_err lnm_http_loop_init(lnm_http_loop **out); +lnm_err lnm_http_loop_init(lnm_http_loop **out, void *c_gctx, + lnm_http_ctx_init_fn ctx_init, + lnm_http_ctx_reset_fn ctx_reset, + lnm_http_ctx_free_fn ctx_free); /** * Append the given step fn to the step. @@ -82,6 +92,9 @@ typedef struct lnm_http_loop_gctx { lnm_http_route **arr; size_t len; } routes; + lnm_http_ctx_init_fn ctx_init; + lnm_http_ctx_reset_fn ctx_reset; + lnm_http_ctx_free_fn ctx_free; void *c; } lnm_http_loop_gctx; diff --git a/lnm/src/_include/lnm/http/loop_internal.h b/lnm/src/_include/lnm/http/loop_internal.h index 7410ade..c6bc128 100644 --- a/lnm/src/_include/lnm/http/loop_internal.h +++ b/lnm/src/_include/lnm/http/loop_internal.h @@ -40,4 +40,27 @@ lnm_err lnm_http_route_init(lnm_http_route **out); */ lnm_err lnm_http_step_init(lnm_http_step **out, lnm_http_step_fn fn); +/** + * Initialize a new global context object. + * + * @param out where to store pointer to new `lnm_http_loop_gctx` + */ +lnm_err lnm_http_loop_gctx_init(lnm_http_loop_gctx **out, void *c_gctx, + lnm_http_ctx_init_fn ctx_init, + lnm_http_ctx_reset_fn ctx_reset, + lnm_http_ctx_free_fn ctx_free); + +/** + * Initialize a new context. + * + * @param out where to store pointer to new object + * @param gctx global context for the loop + */ +lnm_err lnm_http_loop_ctx_init(lnm_http_loop_ctx **out, + lnm_http_loop_gctx *gctx); + +void lnm_http_loop_ctx_reset(lnm_http_loop_ctx *ctx); + +void lnm_http_loop_ctx_free(lnm_http_loop_ctx *ctx); + #endif diff --git a/lnm/src/http/lnm_http_loop.c b/lnm/src/http/lnm_http_loop.c index f082fe7..024da9d 100644 --- a/lnm/src/http/lnm_http_loop.c +++ b/lnm/src/http/lnm_http_loop.c @@ -5,13 +5,20 @@ #include "lnm/http/loop_internal.h" #include "lnm/loop_internal.h" -lnm_err lnm_http_loop_init(lnm_http_loop **out) { +lnm_err lnm_http_loop_init(lnm_http_loop **out, void *c_gctx, + lnm_http_ctx_init_fn ctx_init, + lnm_http_ctx_reset_fn ctx_reset, + lnm_http_ctx_free_fn ctx_free) { lnm_http_loop *hl = calloc(1, sizeof(lnm_http_loop)); if (hl == NULL) { return lnm_err_failed_alloc; } + LNM_RES2(lnm_http_loop_gctx_init((lnm_http_loop_gctx **)&hl->gctx, c_gctx, + ctx_init, ctx_reset, ctx_free), + free(hl)); + *out = hl; return lnm_err_ok; diff --git a/lnm/src/http/loop_ctx.c b/lnm/src/http/loop_ctx.c index e69de29..64ac8eb 100644 --- a/lnm/src/http/loop_ctx.c +++ b/lnm/src/http/loop_ctx.c @@ -0,0 +1,49 @@ +#include "lnm/http/loop_internal.h" + +lnm_err lnm_http_loop_gctx_init(lnm_http_loop_gctx **out, void *c_gctx, + lnm_http_ctx_init_fn ctx_init, + lnm_http_ctx_reset_fn ctx_reset, + lnm_http_ctx_free_fn ctx_free) { + lnm_http_loop_gctx *gctx = calloc(1, sizeof(lnm_http_loop_gctx)); + + if (gctx == NULL) { + return lnm_err_failed_alloc; + } + + gctx->c = c_gctx; + gctx->ctx_init = ctx_init; + gctx->ctx_reset = ctx_reset; + gctx->ctx_free = ctx_free; + + *out = gctx; + + return lnm_err_ok; +} + +lnm_err lnm_http_loop_ctx_init(lnm_http_loop_ctx **out, + lnm_http_loop_gctx *gctx) { + lnm_http_loop_ctx *ctx = calloc(1, sizeof(lnm_http_loop_ctx)); + + if (ctx == NULL) { + return lnm_err_failed_alloc; + } + + LNM_RES2(gctx->ctx_init(&ctx->c, gctx), free(ctx)); + + ctx->g = gctx; + *out = ctx; + + return lnm_err_ok; +} + +void lnm_http_loop_ctx_reset(lnm_http_loop_ctx *ctx) { + ctx->g->ctx_reset(ctx->c); + // TODO actual reset stuff +} + +void lnm_http_loop_ctx_free(lnm_http_loop_ctx *ctx) { + ctx->g->ctx_free(ctx->c); + // TODO actual free stuff + + free(ctx); +}