Improve debug and error handling
This commit is contained in:
parent
0d41b47ff9
commit
d322a00a5d
2 changed files with 28 additions and 13 deletions
|
@ -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) {
|
void on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
|
||||||
int conn_sock, err;
|
int conn_sock1, conn_sock2;
|
||||||
struct sockaddr in_addr;
|
struct sockaddr addr;
|
||||||
socklen_t in_len;
|
socklen_t in_len;
|
||||||
struct epoll_event current_event;
|
struct epoll_event current_event;
|
||||||
|
|
||||||
in_len = sizeof(in_addr);
|
in_len = sizeof(addr);
|
||||||
|
|
||||||
conn_sock = accept(fd, &in_addr, &in_len);
|
conn_sock1 = accept(fd, &addr, &in_len);
|
||||||
if (conn_sock == -1) goto co_error;
|
if (conn_sock1 == -1) goto co_error;
|
||||||
evt_core_add_fd (ctx, "tcp-read", fd);
|
conn_sock2 = dup(conn_sock1);
|
||||||
evt_core_add_fd (ctx, "tcp-write", dup(fd));
|
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;
|
return;
|
||||||
|
|
||||||
|
@ -33,7 +36,7 @@ co_error:
|
||||||
|
|
||||||
void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
|
void tcp_to_udp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
|
||||||
// Get target file descriptor
|
// 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;
|
if (udp == NULL || udp->socklist->len < 1) goto co_error;
|
||||||
int udp_fd = g_array_index(udp->socklist, int, 0);
|
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) {
|
void udp_to_tcp(struct evt_core_ctx* ctx, struct evt_core_cat* cat, int fd) {
|
||||||
// Get target file descriptor
|
// 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;
|
if (tcp == NULL || tcp->socklist->len < 1) goto co_error;
|
||||||
int tcp_fd = g_array_index(tcp->socklist, int, 0);
|
int tcp_fd = g_array_index(tcp->socklist, int, 0);
|
||||||
|
|
||||||
|
|
|
@ -103,12 +103,24 @@ void evt_core_loop(struct evt_core_ctx* ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (n = 0 ; n < num_fd; n++) {
|
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
|
/* An error has occured on this fd, or the socket is not
|
||||||
ready for reading (why were we notified then?) */
|
ready for reading (why were we notified then?) */
|
||||||
|
int err_fd = events[n].data.fd;
|
||||||
fprintf (stderr, "An epoll error occured with fd=%d\n", 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);
|
close (events[n].data.fd);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue