split evt loop

This commit is contained in:
Quentin 2020-02-05 23:21:27 +01:00
parent c5d48ecd37
commit 077d357f56

View file

@ -272,51 +272,53 @@ void evt_core_loop(struct evt_core_ctx* ctx) {
continue; continue;
} }
// 1. Handle errors
for (n = 0 ; n < num_fd; n++) { for (n = 0 ; n < num_fd; n++) {
// 1. Handle errors if (!(events[n].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP))) continue;
if (events[n].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) {
int err_fd = events[n].data.fd;
int evt = events[n].events;
if (evt & EPOLLRDHUP) fprintf(stderr, "Epoll Read Hup Event fd=%d.\n", err_fd);
if (evt & EPOLLHUP) fprintf(stderr, "Epoll Hup Event fd=%d.\n", err_fd);
if (evt & EPOLLERR) {
int error = 0;
socklen_t errlen = sizeof(error);
if (getsockopt(err_fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == 0) {
fprintf(stderr, "Socket %d error = %s\n", err_fd, strerror(error));
} else if (errno == EBADF) {
fprintf(stderr, "fd=%d is invalid. Probably closed\n", err_fd);
continue;
} else {
fprintf(stderr, "Using fd=%d as socket produced error: ", err_fd);
perror("getsockopt");
}
fprintf(stderr, "Epoll Err Event. ");
}
fdinfo = evt_core_get_from_fd(ctx, err_fd); int err_fd = events[n].data.fd;
if (fdinfo != NULL) { int evt = events[n].events;
if (fdinfo->cat->err_cb != NULL) { if (evt & EPOLLRDHUP) fprintf(stderr, "Epoll Read Hup Event fd=%d.\n", err_fd);
if (evt & EPOLLHUP) fprintf(stderr, "Epoll Hup Event fd=%d.\n", err_fd);
timing_fx_start (&tfx_err, fdinfo->cat->name, fdinfo->url, fdinfo->fd); if (evt & EPOLLERR) {
return_code = fdinfo->cat->err_cb(ctx, fdinfo); int error = 0;
timing_fx_stop(&tfx_err); socklen_t errlen = sizeof(error);
if (getsockopt(err_fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == 0) {
if (return_code == 1) { fprintf(stderr, "Socket %d error = %s\n", err_fd, strerror(error));
fprintf(stderr, "Error on fd=%d on cat=%s is handled by app, not clearing it\n", err_fd, fdinfo->cat->name); } else if (errno == EBADF) {
continue; fprintf(stderr, "fd=%d is invalid. Probably closed\n", err_fd);
} continue;
}
fprintf(stderr, "Clearing fd=%d on cat=%s\n", err_fd, fdinfo->cat->name);
evt_core_rm_fd (ctx, err_fd);
} else { } else {
fprintf(stderr, "The file descriptor %d is not registered in a category, this is probably a logic error\n", err_fd); fprintf(stderr, "Using fd=%d as socket produced error: ", err_fd);
close (err_fd); perror("getsockopt");
} }
continue; fprintf(stderr, "Epoll Err Event. ");
} }
// 2. Fetch info and call appropriate function fdinfo = evt_core_get_from_fd(ctx, err_fd);
if (fdinfo != NULL) {
if (fdinfo->cat->err_cb != NULL) {
timing_fx_start (&tfx_err, fdinfo->cat->name, fdinfo->url, fdinfo->fd);
return_code = fdinfo->cat->err_cb(ctx, fdinfo);
timing_fx_stop(&tfx_err);
if (return_code == 1) {
fprintf(stderr, "Error on fd=%d on cat=%s is handled by app, not clearing it\n", err_fd, fdinfo->cat->name);
continue;
}
}
fprintf(stderr, "Clearing fd=%d on cat=%s\n", err_fd, fdinfo->cat->name);
evt_core_rm_fd (ctx, err_fd);
} else {
fprintf(stderr, "The file descriptor %d is not registered in a category, this is probably a logic error\n", err_fd);
close (err_fd);
}
}
// 2. Fetch info and call appropriate function
for (n = 0 ; n < num_fd; n++) {
if (events[n].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) continue;
fdinfo = g_hash_table_lookup(ctx->socklist, &(events[n].data.fd)); fdinfo = g_hash_table_lookup(ctx->socklist, &(events[n].data.fd));
if (fdinfo == NULL) { if (fdinfo == NULL) {
fprintf(stderr, "Ignoring file descriptor %d as it is not registered. This is a bug.\n", events[n].data.fd); fprintf(stderr, "Ignoring file descriptor %d as it is not registered. This is a bug.\n", events[n].data.fd);