Switch to epoll for checking connection completion.
This commit is contained in:
@@ -21,6 +21,8 @@
|
|||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "rand.h"
|
#include "rand.h"
|
||||||
|
|
||||||
|
#pragma GCC diagnostic ignored "-Wvla"
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
char *blob_dir;
|
char *blob_dir;
|
||||||
char *node;
|
char *node;
|
||||||
@@ -277,17 +279,24 @@ static void conn_check(struct conn *conn) {
|
|||||||
int error;
|
int error;
|
||||||
socklen_t len = sizeof(error);
|
socklen_t len = sizeof(error);
|
||||||
assert(getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) == 0);
|
assert(getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) == 0);
|
||||||
if (!error) {
|
if (error) {
|
||||||
list_del(&conn->conn_list);
|
fprintf(stderr, "Connection failed: %s\n", strerror(error));
|
||||||
list_add(&conn->conn_list, &conn_open_head);
|
shutdown_flag = true;
|
||||||
assert(!epoll_ctl(epoll_fd, EPOLL_CTL_DEL, conn->fd, NULL));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (error == EINPROGRESS) {
|
list_del(&conn->conn_list);
|
||||||
return;
|
list_add(&conn->conn_list, &conn_open_head);
|
||||||
|
assert(!epoll_ctl(epoll_fd, EPOLL_CTL_DEL, conn->fd, NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void conn_check_all() {
|
||||||
|
struct epoll_event evs[config.num_conns];
|
||||||
|
int nfds = epoll_wait(epoll_fd, evs, (int) config.num_conns, 0);
|
||||||
|
assert(nfds >= 0);
|
||||||
|
for (int i = 0; i < nfds; i++) {
|
||||||
|
struct conn *conn = evs[i].data.ptr;
|
||||||
|
conn_check(conn);
|
||||||
}
|
}
|
||||||
fprintf(stderr, "Connection failed: %s\n", strerror(error));
|
|
||||||
shutdown_flag = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void conn_cycle() {
|
static void conn_cycle() {
|
||||||
@@ -295,9 +304,7 @@ static void conn_cycle() {
|
|||||||
list_for_each_entry_safe(iter, next, &conn_open_head, conn_list) {
|
list_for_each_entry_safe(iter, next, &conn_open_head, conn_list) {
|
||||||
conn_send_message(iter);
|
conn_send_message(iter);
|
||||||
}
|
}
|
||||||
list_for_each_entry_safe(iter, next, &conn_pending_head, conn_list) {
|
conn_check_all();
|
||||||
conn_check(iter);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user