commit c9f73341bf3a66d349ca8549cf24f43c2b7334c4
parent d0cb349e6960d1529e92114c2b59c6ef4c1a7d39
Author: Yersa Nordman <yersa@finwo.nl>
Date: Thu, 27 Jul 2023 22:50:40 +0200
Implemented closing connections
Diffstat:
2 files changed, 26 insertions(+), 9 deletions(-)
diff --git a/src/fnet.c b/src/fnet.c
@@ -277,11 +277,8 @@ FNET_RETURNCODE fnet_process(const struct fnet_t *connection) {
}
rbuf->len = n;
if (rbuf->len == 0) {
- // TODO: handle connection close
- printf("zero?\n");
- buf_clear(rbuf);
- free(rbuf);
- return FNET_RETURNCODE_NOT_IMPLEMENTED;
+ fnet_close(conn);
+ break;
}
if (conn->ext.onData) {
conn->ext.onData(&((struct fnet_ev){
@@ -368,6 +365,7 @@ FNET_RETURNCODE fnet_write(const struct fnet_t *connection, struct buf *buf) {
FNET_RETURNCODE fnet_close(const struct fnet_t *connection) {
struct fnet_internal_t *conn = (struct fnet_internal_t *)connection;
+ int i;
// Checking arguments are given
if (!conn) {
@@ -375,9 +373,23 @@ FNET_RETURNCODE fnet_close(const struct fnet_t *connection) {
return FNET_RETURNCODE_MISSING_ARGUMENT;
}
- // TODO: actually close the connection
- // TODO: call onclose
- // TODO: call free
+ if (conn->fds) {
+ for ( i = 0 ; i < conn->nfds ; i++ ) {
+ close(conn->fds[i]);
+ }
+ conn->nfds = 0;
+ free(conn->fds);
+ conn->fds = NULL;
+ }
+
+ if (conn->ext.onClose) {
+ conn->ext.onClose(&((struct fnet_ev){
+ .connection = (struct fnet_t *)conn,
+ .type = FNET_EVENT_CLOSE,
+ .buffer = NULL,
+ .udata = conn->ext.udata,
+ }));
+ }
return FNET_RETURNCODE_OK;
}
diff --git a/test.c b/test.c
@@ -4,13 +4,18 @@
#include "fnet.h"
+void onClose(struct fnet_ev *ev) {
+ printf("Connection closed!\n");
+}
+
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;
+ ev->connection->onData = onData;
+ ev->connection->onClose = onClose;
}
int main() {