http-server.c

Basic HTTP server and router in C
git clone git://git.finwo.net/lib/http-server.c
Log | Files | Refs | README

commit 71c7f3ff5fc67cb605db69b8d3c579dc2e6e52ce
parent a60fc09b887c26afb24946c1ec5c8837407e8c52
Author: Yersa Nordman <yersa@finwo.nl>
Date:   Tue, 24 Oct 2023 22:07:01 +0200

Export tick event handler registration

Diffstat:
Mexample.c | 12+++++++++++-
Msrc/http-server.c | 10+++++++++-
Msrc/http-server.h | 1+
3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/example.c b/example.c @@ -11,6 +11,15 @@ void onServing(char *addr, uint16_t port, void *udata) { printf("Serving at %s:%d\n", addr, port); } +int ticksHad = 0; +void onTick(void *udata) { + printf("Tick %d\n", ticksHad); + if (++ticksHad >= 10) { + printf("10 seconds have passed\n"); + ticksHad = 0; + } +} + void route_get_hello(struct http_server_reqdata *reqdata) { http_parser_header_set(reqdata->reqres->response, "Content-Type", "text/plain"); reqdata->reqres->response->body = calloc(1, sizeof(struct buf)); @@ -35,7 +44,8 @@ int main() { struct http_server_events evs = { .serving = onServing, .close = NULL, - .notFound = route_404 + .notFound = route_404, + .tick = onTick, }; http_server_route("GET", "/hello", route_get_hello); diff --git a/src/http-server.c b/src/http-server.c @@ -31,6 +31,14 @@ void _hs_onServing(struct fnet_ev *ev) { } } +void _hs_onTick(struct fnet_ev *ev) { + struct fnet_udata *ludata = ev->udata; + + if (ludata->evs && ludata->evs->tick) { + ludata->evs->tick(ludata->cudata); + } +} + static void _hs_onRequest(struct http_parser_event *ev) { struct http_server_reqdata *reqdata = ev->udata; struct hs_route *route = registered_routes; @@ -127,7 +135,7 @@ void http_server_main(const struct http_server_opts *opts) { .onListen = _hs_onServing, .onConnect = _hs_onConnect, .onData = NULL, - .onTick = NULL, + .onTick = _hs_onTick, .onClose = NULL, .udata = ludata, }))) { diff --git a/src/http-server.h b/src/http-server.h @@ -22,6 +22,7 @@ struct http_server_events { void (*serving)(char *addrs, uint16_t port, void *udata); void (*close)(struct http_server_reqdata *reqdata); void (*notFound)(struct http_server_reqdata *reqdata); + void (*tick)(void *udata); }; void http_server_main(const struct http_server_opts *opts);