commit d0cb349e6960d1529e92114c2b59c6ef4c1a7d39
parent f1bf2c971335f8b4c1784b9228719ff0ad527827
Author: Yersa Nordman <yersa@finwo.nl>
Date: Thu, 27 Jul 2023 22:37:18 +0200
Show in terminal that we're receiving data
Diffstat:
| M | src/fnet.c | | | 68 | ++++++++++++++++++++++++++++++++++++++++++++++++-------------------- |
| M | src/fnet.h | | | 15 | +++++++++------ |
| M | test.c | | | 7 | ++++++- |
3 files changed, 63 insertions(+), 27 deletions(-)
diff --git a/src/fnet.c b/src/fnet.c
@@ -28,9 +28,6 @@ struct fnet_internal_t {
FNET_SOCKET *fds;
int nfds;
FNET_FLAG flags;
- FNET_CALLBACK(onConnect);
- FNET_CALLBACK(onData);
- FNET_CALLBACK(onClose);
};
struct fnet_internal_t *connections = NULL;
@@ -76,15 +73,15 @@ int64_t _fnet_now() {
struct fnet_internal_t * _fnet_init(const struct fnet_options_t *options) {
// 1-to-1 copy, don't touch the options
struct fnet_internal_t *conn = malloc(sizeof(struct fnet_internal_t));
- conn->ext.proto = options->proto;
- conn->ext.status = FNET_STATUS_INITIALIZING;
- conn->ext.udata = options->udata;
- conn->flags = options->flags;
- conn->onConnect = options->onConnect;
- conn->onData = options->onData;
- conn->onClose = options->onClose;
- conn->nfds = 0;
- conn->fds = NULL;
+ conn->ext.proto = options->proto;
+ conn->ext.status = FNET_STATUS_INITIALIZING;
+ conn->ext.udata = options->udata;
+ conn->flags = options->flags;
+ conn->ext.onConnect = options->onConnect;
+ conn->ext.onData = options->onData;
+ conn->ext.onClose = options->onClose;
+ conn->nfds = 0;
+ conn->fds = NULL;
// Aanndd add to the connection tracking list
conn->next = connections;
@@ -153,7 +150,7 @@ struct fnet_t * fnet_listen(const char *address, uint16_t port, const struct fne
addrinfo = addrinfo->ai_next;
}
- conn->fds = calloc(naddrs, sizeof(FNET_SOCKET));
+ conn->fds = malloc(naddrs * sizeof(FNET_SOCKET));
if (!conn->fds) {
fprintf(stderr, "%s\n", strerror(ENOMEM));
fnet_free((struct fnet_t *)conn);
@@ -242,10 +239,12 @@ struct fnet_t * fnet_connect(const char *address, uint16_t port, const struct fn
FNET_RETURNCODE fnet_process(const struct fnet_t *connection) {
struct fnet_internal_t *conn = (struct fnet_internal_t *)connection;
struct fnet_internal_t *nconn = NULL;
- int i;
+ int i, n;
FNET_SOCKET nfd;
struct sockaddr_storage addr;
socklen_t addrlen = sizeof(addr);
+ struct buf *rbuf = NULL;
+
// Checking arguments are given
if (!conn) {
@@ -264,10 +263,39 @@ FNET_RETURNCODE fnet_process(const struct fnet_t *connection) {
/* return FNET_RETURNCODE_NOT_IMPLEMENTED; */
/* } */
- /* if (conn->ext.status & FNET_STATUS_CONNECTED) { */
- /* // TODO: handle client connection */
- /* return FNET_RETURNCODE_NOT_IMPLEMENTED; */
- /* } */
+ if (conn->ext.status & FNET_STATUS_CONNECTED) {
+ rbuf = malloc(sizeof(struct buf));
+ rbuf->data = malloc(BUFSIZ);
+ rbuf->cap = BUFSIZ;
+ for ( i = 0 ; i < conn->nfds ; i++ ) {
+ n = read(conn->fds[i], rbuf->data, rbuf->cap);
+ if (n < 0) {
+ if (errno == EAGAIN) continue;
+ buf_clear(rbuf);
+ free(rbuf);
+ return FNET_RETURNCODE_ERRNO;
+ }
+ rbuf->len = n;
+ if (rbuf->len == 0) {
+ // TODO: handle connection close
+ printf("zero?\n");
+ buf_clear(rbuf);
+ free(rbuf);
+ return FNET_RETURNCODE_NOT_IMPLEMENTED;
+ }
+ if (conn->ext.onData) {
+ conn->ext.onData(&((struct fnet_ev){
+ .connection = (struct fnet_t *)conn,
+ .type = FNET_EVENT_DATA,
+ .buffer = rbuf,
+ .udata = conn->ext.udata,
+ }));
+ }
+ }
+ buf_clear(rbuf);
+ free(rbuf);
+ return FNET_RETURNCODE_OK;
+ }
if (conn->ext.status & FNET_STATUS_LISTENING) {
for ( i = 0 ; i < conn->nfds ; i++ ) {
@@ -305,8 +333,8 @@ FNET_RETURNCODE fnet_process(const struct fnet_t *connection) {
nconn->nfds = 1;
nconn->ext.status = FNET_STATUS_CONNECTED;
- if (conn->onConnect) {
- conn->onConnect(&((struct fnet_ev){
+ if (conn->ext.onConnect) {
+ conn->ext.onConnect(&((struct fnet_ev){
.connection = (struct fnet_t *)nconn,
.type = FNET_EVENT_CONNECT,
.buffer = NULL,
diff --git a/src/fnet.h b/src/fnet.h
@@ -38,12 +38,6 @@ extern "C" {
#define FNET_CALLBACK(NAME) void (*(NAME))(struct fnet_ev *event)
-struct fnet_t {
- FNET_PROTOCOL proto;
- FNET_STATUS status;
- void *udata;
-};
-
struct fnet_ev {
struct fnet_t *connection;
FNET_EVENT type;
@@ -51,6 +45,15 @@ struct fnet_ev {
void *udata;
};
+struct fnet_t {
+ FNET_PROTOCOL proto;
+ FNET_STATUS status;
+ FNET_CALLBACK(onConnect);
+ FNET_CALLBACK(onData);
+ FNET_CALLBACK(onClose);
+ void *udata;
+};
+
struct fnet_options_t {
FNET_PROTOCOL proto;
FNET_FLAG flags;
diff --git a/test.c b/test.c
@@ -4,8 +4,13 @@
#include "fnet.h"
+void onData(struct fnet_ev *ev) {
+ printf("Data(%d): %.*s\n", ev->buffer->len, (int)(ev->buffer->len), ev->buffer->data);
+}
+
void onConnect(struct fnet_ev *ev) {
printf("Connection!!\n");
+ ev->connection->onData = onData;
}
int main() {
@@ -18,7 +23,7 @@ int main() {
.udata = NULL,
};
- struct fnet_t *conn = fnet_listen("0.0.0.0", 1337, &opts);
+ fnet_listen("0.0.0.0", 1337, &opts);
fnet_main();
return 42;