fnet.c

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

commit b7fdc6ac99c3071d071984c3d40a0798b13ecf12
parent fe62b8a97539316c459d0726383660b2e351ddb9
Author: Yersa Nordman <yersa@finwo.nl>
Date:   Wed, 26 Jul 2023 22:38:03 +0200

Initialized returncode concept

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

diff --git a/src/fnet.c b/src/fnet.c @@ -9,6 +9,8 @@ extern "C" { #include "fnet.h" +#define FNET_SOCKET int + struct fnet_internal_t { struct fnet_t ext; // KEEP AT TOP, allows casting between fnet_internal_t* and fnet_t* FNET_SOCKET sock; @@ -63,70 +65,92 @@ struct fnet_t * fnet_connect(const char *address, uint16_t port, struct fnet_con return NULL; } + // Check if we support the protocol + switch(options->proto) { + case FNET_PROTO_TCP: + // Intentionally empty + break; + default: + fprintf(stderr, "fnet_connect: unknown protocol\n"); + return NULL; + } return NULL; } -void fnet_process(struct fnet_t *connection) { +FNET_RETURNCODE fnet_process(struct fnet_t *connection) { struct fnet_internal_t *conn = (struct fnet_internal_t *)connection; // Checking arguments are given if (!conn) { fprintf(stderr, "fnet_process: connection argument is required\n"); - return NULL; + return FNET_RETURNCODE_MISSING_ARGUMENT: } + return FNET_RETURNCODE_OK; } -void fnet_write(struct fnet_t *connection, struct buf *buf) { +FNET_RETURNCODE fnet_write(struct fnet_t *connection, struct buf *buf) { struct fnet_internal_t *conn = (struct fnet_internal_t *)connection; // Checking arguments are given if (!conn) { fprintf(stderr, "fnet_write: connection argument is required\n"); - return NULL; + return FNET_RETURNCODE_MISSING_ARGUMENT; } if (!buf) { fprintf(stderr, "fnet_write: buf argument is required\n"); - return NULL; + return FNET_RETURNCODE_MISSING_ARGUMENT; } + return FNET_RETURNCODE_OK; } -void fnet_close(struct fnet_t *connection) { +FNET_RETURNCODE fnet_close(struct fnet_t *connection) { struct fnet_internal_t *conn = (struct fnet_internal_t *)connection; // Checking arguments are given if (!conn) { fprintf(stderr, "fnet_close: connection argument is required\n"); - return NULL; + return FNET_RETURNCODE_MISSING_ARGUMENT; } + + return FNET_RETURNCODE_OK; } -void fnet_free(struct fnet_t *connection) { +FNET_RETURNCODE fnet_free(struct fnet_t *connection) { struct fnet_internal_t *conn = (struct fnet_internal_t *)connection; // Checking arguments are given if (!conn) { fprintf(stderr, "fnet_free: connection argument is required\n"); - return NULL; + return FNET_RETURNCODE_MISSING_ARGUMENT; } free(conn); + + return FNET_RETURNCODE_OK; } -void fnet_step() { +FNET_RETURNCODE fnet_step() { // TODO: process all open fnet instances + return FNET_RETURNCODE_OK; } -void fnet_main() { +FNET_RETURNCODE fnet_main() { + FNET_RETURNCODE ret; while(1) { // TODO: handle kill signal? - fnet_step(); + // TODO: dynamic sleep to have 10ms - 100ms tick? + ret = fnet_step(); + if (ret) return ret; } + + // TODO: is this really ok? + return FNET_RETURNCODE_OK; } diff --git a/src/fnet.h b/src/fnet.h @@ -9,14 +9,16 @@ extern "C" { #include "tidwall/buf.h" -#define FNET_SOCKET int - #define FNET_FLAG uint8_t #define FNET_FLAG_RECONNECT 1 #define FNET_PROTOCOL uint8_t #define FNET_PROTO_TCP 0 +#define FNET_RETURNCODE int +#define FNET_RETURNCODE_OK 0 +#define FNET_RETURNCODE_MISSING_ARGUMENT 1 + #define FNET_STATUS uint8_t #define FNET_STATUS_CONNECTING 1 // Client-only status #define FNET_STATUS_CONNECTED 2 // Client = connected, server = listening @@ -44,13 +46,13 @@ struct fnet_connect_options_t { struct fnet_t * fnet_listen(const char *address, uint16_t port, struct fnet_connect_options_t *options); struct fnet_t * fnet_connect(const char *address, uint16_t port, struct fnet_connect_options_t *options); -void fnet_process(struct fnet_t *connection); -void fnet_write(struct fnet_t *connection, struct buf *buf); -void fnet_close(struct fnet_t *connection); -void fnet_free(struct fnet_t *connection); +FNET_RETURNCODE fnet_process(struct fnet_t *connection); +FNET_RETURNCODE fnet_write(struct fnet_t *connection, struct buf *buf); +FNET_RETURNCODE fnet_close(struct fnet_t *connection); +FNET_RETURNCODE fnet_free(struct fnet_t *connection); -void fnet_step(); -void fnet_main(); +FNET_RETURNCODE fnet_step(); +FNET_RETURNCODE fnet_main(); #ifdef __cplusplus }