From d322a00a5d7c39693e13c8846d76abc0e2548508 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 12 Feb 2019 17:38:11 +0100 Subject: [PATCH] Improve debug and error handling --- src/algo_naive.c | 21 ++++++++++++--------- src/evt_core.c | 20 ++++++++++++++++---- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/algo_naive.c b/src/algo_naive.c index a32137f..c27b110 100644 --- a/src/algo_naive.c +++ b/src/algo_naive.c @@ -12,17 +12,20 @@ void free_naive(void* app_ctx) { } void on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) { - int conn_sock, err; - struct sockaddr in_addr; + int conn_sock1, conn_sock2; + struct sockaddr addr; socklen_t in_len; struct epoll_event current_event; - in_len = sizeof(in_addr); + in_len = sizeof(addr); - conn_sock = accept(fd, &in_addr, &in_len); - if (conn_sock == -1) goto co_error; - evt_core_add_fd (ctx, "tcp-read", fd); - evt_core_add_fd (ctx, "tcp-write", dup(fd)); + conn_sock1 = accept(fd, &addr, &in_len); + if (conn_sock1 == -1) goto co_error; + conn_sock2 = dup(conn_sock1); + if (conn_sock2 == -1) goto co_error; + printf("fd=%d accepts, creating fds=%d,%d\n", fd, conn_sock1, conn_sock2); + evt_core_add_fd (ctx, "tcp-read", conn_sock1); + evt_core_add_fd (ctx, "tcp-write", conn_sock2); return; @@ -33,7 +36,7 @@ co_error: void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) { // Get target file descriptor - struct evt_core_cat* udp = g_hash_table_lookup (ctx->catlist, "udp-data"); + struct evt_core_cat* udp = g_hash_table_lookup (ctx->catlist, "udp-write"); if (udp == NULL || udp->socklist->len < 1) goto co_error; int udp_fd = g_array_index(udp->socklist, int, 0); @@ -66,7 +69,7 @@ co_error: void udp_to_tcp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) { // Get target file descriptor - struct evt_core_cat* tcp = g_hash_table_lookup (ctx->catlist, "tcp-data"); + struct evt_core_cat* tcp = g_hash_table_lookup (ctx->catlist, "tcp-write"); if (tcp == NULL || tcp->socklist->len < 1) goto co_error; int tcp_fd = g_array_index(tcp->socklist, int, 0); diff --git a/src/evt_core.c b/src/evt_core.c index 1e27d8b..4b85a0e 100644 --- a/src/evt_core.c +++ b/src/evt_core.c @@ -103,12 +103,24 @@ void evt_core_loop(struct evt_core_ctx* ctx) { } for (n = 0 ; n < num_fd; n++) { - if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (!(events[n].events & EPOLLIN))) { + if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP)) { /* An error has occured on this fd, or the socket is not ready for reading (why were we notified then?) */ - - fprintf (stderr, "An epoll error occured with fd=%d\n", events[n].data.fd); - close (events[n].data.fd); + int err_fd = events[n].data.fd; + fprintf (stderr, "An epoll error occured with fd=%d\n", err_fd); + struct evt_core_cat* cat = g_hash_table_lookup (ctx->socklist, &err_fd); + if (cat != NULL) { + fprintf(stderr, "The fd was belonging to category %s\n", cat->name); + g_hash_table_remove(ctx->socklist, &err_fd); + for (int i = 0; i < cat->socklist->len; i++) { + if (g_array_index(cat->socklist, int, i) == err_fd) { + g_array_remove_index(cat->socklist, i); + } + } + } else { + fprintf(stderr, "The file descriptor is not registered in a category, this is probably a logic error\n"); + close (events[n].data.fd); + } continue; }