fnet.c

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

commit d658340373b67bd962b751b7d04bfd7bcb52aba0
parent ba5158ea1e4ecafbbc4a7560828b9d5132362407
Author: Yersa Nordman <yersa@finwo.nl>
Date:   Thu,  3 Aug 2023 01:55:27 +0200

Added minimal readme

Diffstat:
AREADME.md | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 93 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,93 @@ +fnet +==== + +## Installation + +The easiest way to add fnet to your project is to use [dep][dep] to install it +as a dependency, ensure you include lib/.dep/config.mk in your makefile, and add +`#include "finwo/fnet.h"` in the files where you're using it. + +```sh +dep add finwo/fnet +``` + +## Usage + +### Connect to remote + +```c +#include <string.h> +#include "finwo/fnet.h" +#include "tidwall/buf.h" + +void onData(struct fnet_ev *ev) { + printf("Data received: %s\n", ev->buffer->data); +} + +void onTick(struct fnet_ev *ev) { + const char *data = "Hello world!"; + int cnt = *((int*)ev->udata); + + fnet_write(ev->connection, &((struct buf){ + .len = strlen(data), + .data = data, + })); + + cnt++; + *((int*)ev->udata) = cnt; + if (cnt > 10) { + fnet_close(ev->connection); + exit(0); + } +} + +int main() { + int cnt = 0; + + fnet_connect(addr, port, &((struct fnet_options_t){ + .proto = FNET_PROTO_TCP, + .flags = 0, + .onConnect = NULL, + .onData = onData, + .onTick = onTick, + .onClose = NULL, + .udata = &cnt, + })); + + fnet_main(); + return 0; +} +``` + +### Listen for connections + +```c +#include "finwo/fnet.h" + +// Just an echo +void onData(struct fnet_ev *ev) { + fnet_write(ev->connection, ev->buffer); +} + +// Attach our onData to the connection +void onConnect(struct fnet_ev *ev) { + ev->connection->onData = onData; +} + +int main() { + fnet_listen("0.0.0.0", 80, &((struct fnet_options_t){ + .proto = FNET_PROTO_TCP, + .flags = 0, + .onConnect = onConnect, + .onData = NULL, + .onTick = NULL, + .onClose = NULL, + .udata = NULL, + })); + + fnet_main(); + return 0; +} +``` + +[dep]: https://github.com/finwo/dep