fnet.c

Simple C networking library
git clone git://git.finwo.net/lib/fnet.c
Log | Files | Refs | README

commit 84670b825e2e6847cc58ff6c595b30e3eb35ca2f
parent a94234c72fa7968ef51d960d2f35980edb226340
Author: Yersa Nordman <yersa@finwo.nl>
Date:   Fri,  4 Aug 2023 20:20:49 +0200

Added onListen callback

Diffstat:
Msrc/fnet.c | 11+++++++++++
Msrc/fnet.h | 11+++++++----
2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/fnet.c b/src/fnet.c @@ -82,6 +82,7 @@ struct fnet_internal_t * _fnet_init(const struct fnet_options_t *options) { conn->ext.status = FNET_STATUS_INITIALIZING; conn->ext.udata = options->udata; conn->flags = options->flags; + conn->ext.onListen = options->onListen; conn->ext.onConnect = options->onConnect; conn->ext.onData = options->onData; conn->ext.onTick = options->onTick; @@ -232,6 +233,15 @@ struct fnet_t * fnet_listen(const char *address, uint16_t port, const struct fne } } + if (conn->ext.onListen) { + conn->ext.onConnect(&((struct fnet_ev){ + .connection = (struct fnet_t *)conn, + .type = FNET_EVENT_LISTEN, + .buffer = NULL, + .udata = conn->ext.udata, + })); + } + freeaddrinfo(addrs); conn->ext.status = FNET_STATUS_LISTENING; return (struct fnet_t *)conn; @@ -455,6 +465,7 @@ FNET_RETURNCODE fnet_process(const struct fnet_t *connection) { nconn = _fnet_init(&((struct fnet_options_t){ .proto = conn->ext.proto, .flags = conn->flags & (~FNET_FLAG_RECONNECT), + .onListen = NULL, .onConnect = NULL, .onData = NULL, .onTick = NULL, diff --git a/src/fnet.h b/src/fnet.h @@ -34,10 +34,11 @@ extern "C" { #define FNET_STATUS_CLOSED 32 #define FNET_EVENT int -#define FNET_EVENT_CONNECT 1 -#define FNET_EVENT_DATA 2 -#define FNET_EVENT_TICK 3 -#define FNET_EVENT_CLOSE 4 +#define FNET_EVENT_LISTEN 1 +#define FNET_EVENT_CONNECT 2 +#define FNET_EVENT_DATA 3 +#define FNET_EVENT_TICK 4 +#define FNET_EVENT_CLOSE 5 #define FNET_CALLBACK(NAME) void (*(NAME))(struct fnet_ev *event) @@ -51,6 +52,7 @@ struct fnet_ev { struct fnet_t { FNET_PROTOCOL proto; FNET_STATUS status; + FNET_CALLBACK(onListen); FNET_CALLBACK(onConnect); FNET_CALLBACK(onData); FNET_CALLBACK(onTick); @@ -61,6 +63,7 @@ struct fnet_t { struct fnet_options_t { FNET_PROTOCOL proto; FNET_FLAG flags; + FNET_CALLBACK(onListen); FNET_CALLBACK(onConnect); FNET_CALLBACK(onData); FNET_CALLBACK(onTick);