feat(lnm): wrote part of context code
ci/woodpecker/push/build Pipeline was successful Details

lnm
Jef Roosens 2023-11-28 10:32:08 +01:00
parent e04f6e170e
commit d119f85260
Signed by: Jef Roosens
GPG Key ID: 02D4C0997E74717B
4 changed files with 94 additions and 2 deletions

View File

@ -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;

View File

@ -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

View File

@ -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;

View File

@ -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);
}