fnet.c

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

commit eb0cafd4de462ce37f8cc17b0936ecb7b5234393
Author: Yersa Nordman <yersa@finwo.nl>
Date:   Tue, 18 Jul 2023 23:48:25 +0200

Basic project layout

Diffstat:
A.gitignore | 3+++
AMakefile | 27+++++++++++++++++++++++++++
Aconfig.mk | 1+
Apackage.ini | 10++++++++++
Asrc/fnet.c | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/fnet.h | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest.c | 3+++
7 files changed, 147 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,3 @@ +/lib/ +*.o +/fnet_test diff --git a/Makefile b/Makefile @@ -0,0 +1,27 @@ +LIBS:= +SRC:= + +BIN=fnet_test +SRC+=test.c + +SRC+=$(wildcard src/*.c) +SRC+=$(wildcard src/*/*.c) + +override CFLAGS?=-Wall -s -O2 + +INCLUDES:= +INCLUDES+=-I src + +include lib/.dep/config.mk + +OBJ:=$(SRC:.c=.o) +OBJ:=$(OBJ:.cc=.o) + +override CFLAGS+=$(INCLUDES) + +default: $(BIN) + +$(OBJ): $(SRC) + +$(BIN): $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $@ diff --git a/config.mk b/config.mk @@ -0,0 +1 @@ +SRC+=__DIRNAME/src/fnet.c diff --git a/package.ini b/package.ini @@ -0,0 +1,10 @@ +[dependencies] +tidwall/buf=https://raw.githubusercontent.com/finwo/dep-repository/main/tidwall/buf/package.ini + +[export] +config.mk=config.mk +include/finwo/fnet.h=src/fnet.h + +[package] +deps=lib +name=finwo/fnet diff --git a/src/fnet.c b/src/fnet.c @@ -0,0 +1,42 @@ +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdlib.h> + +#include "tidwall/buf.h" + +#include "fnet.h" + +struct fnet_internal_t { + struct fnet_t ext; // KEEP AT TOP, allows casting between fnet_internal_t* and fnet_t* +}; + +struct fnet_t * fnet_listen(const char *address, uint16_t port, struct fnet_connect_options_t *options) { + return NULL; +} + +struct fnet_t * fnet_connect(const char *address, uint16_t port, struct fnet_connect_options_t *options) { + return NULL; +} + +void fnet_process(struct fnet_t *connection) { + struct fnet_internal_t *conn = (struct fnet_internal_t *)connection; +} +void fnet_write(struct fnet_t *connection, struct buf *buf) { + struct fnet_internal_t *conn = (struct fnet_internal_t *)connection; +} + +void fnet_close(struct fnet_t *connection) { + struct fnet_internal_t *conn = (struct fnet_internal_t *)connection; +} + +void fnet_free(struct fnet_t *connection) { + struct fnet_internal_t *conn = (struct fnet_internal_t *)connection; + free(conn); +} + + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/fnet.h b/src/fnet.h @@ -0,0 +1,61 @@ +#ifndef __INCLUDE_FINWO_FNET_H__ +#define __INCLUDE_FINWO_FNET_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdint.h> + +#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_STATUS uint8_t +#define FNET_STATUS_CONNECTING 1 // Client-only status +#define FNET_STATUS_CONNECTED 2 // Client = connected, server = listening +#define FNET_STATUS_ERROR 4 +#define FNET_STATUS_CLOSED 8 + +#define FNET_CALLBACK(NAME) void (*(NAME))(struct fnet_t *connection, void *udata) +#define FNET_CALLBACK_VA(NAME, ...) void (*(NAME))(struct fnet_t *connection, __VA_ARGS__, void *udata) + +struct fnet_t { + FNET_SOCKET sock; + FNET_PROTOCOL proto; + FNET_STATUS status; + FNET_FLAG flags; + FNET_CALLBACK(onConnect); + FNET_CALLBACK_VA(onData, struct buf *data); + FNET_CALLBACK(onClose); + void *udata; +}; + +struct fnet_connect_options_t { + FNET_PROTOCOL proto; + FNET_FLAG flags; + FNET_CALLBACK(onConnect); + FNET_CALLBACK_VA(onData, struct buf *data); + FNET_CALLBACK(onClose); + void *udata; +}; + +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); + +#ifdef __cplusplus +} +#endif + +#endif // __INCLUDE_FINWO_FNET_H__ diff --git a/test.c b/test.c @@ -0,0 +1,3 @@ +int main() { + return 42; +}