Improve debug and error handling

This commit is contained in:
Quentin Dufour 2019-02-12 17:38:11 +01:00
parent 0d41b47ff9
commit d322a00a5d
2 changed files with 28 additions and 13 deletions

View file

@ -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);

View file

@ -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;
}