diff --git a/lnm/include/lnm/loop.h b/lnm/include/lnm/loop.h index 8493170..6e67f68 100644 --- a/lnm/include/lnm/loop.h +++ b/lnm/include/lnm/loop.h @@ -6,7 +6,7 @@ #include "lnm/common.h" -#define LNM_LOOP_BUF_SIZE 4096 +#define LNM_LOOP_BUF_SIZE 2048 #define LNM_LOOP_INITIAL_CONNS 16 typedef enum { diff --git a/lnm/src/http/lnm_http_loop_process.c b/lnm/src/http/lnm_http_loop_process.c index 7c32868..1fd5530 100644 --- a/lnm/src/http/lnm_http_loop_process.c +++ b/lnm/src/http/lnm_http_loop_process.c @@ -341,11 +341,9 @@ void lnm_http_loop_process(lnm_http_conn *conn) { http_loop_state = ctx->state; process_fns[http_loop_state](conn); - } while ((conn->state == loop_state) && - (conn->state == state_map[http_loop_state]) && + } while ((conn->state == state_map[ctx->state]) && (http_loop_state != ctx->state)); // Check required to prevent overwriting manually set event loop state - conn->state = - conn->state == loop_state ? state_map[http_loop_state] : conn->state; + conn->state = conn->state == loop_state ? state_map[ctx->state] : conn->state; } diff --git a/lnm/src/loop/lnm_loop.c b/lnm/src/loop/lnm_loop.c index 350eb9b..a5839d4 100644 --- a/lnm/src/loop/lnm_loop.c +++ b/lnm/src/loop/lnm_loop.c @@ -76,7 +76,7 @@ lnm_err lnm_loop_accept(lnm_loop *l) { } lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port) { - int listen_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); + int listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd < 0) { return lnm_err_failed_network; @@ -105,6 +105,9 @@ lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port) { return lnm_err_failed_network; } + int flags = fcntl(listen_fd, F_GETFL); + fcntl(listen_fd, F_SETFL, flags | O_NONBLOCK); + l->listen_fd = listen_fd; return lnm_err_ok; @@ -116,7 +119,7 @@ lnm_err lnm_loop_run(lnm_loop *l) { } struct pollfd *poll_args = - malloc((LNM_LOOP_INITIAL_CONNS + 1) * sizeof(struct pollfd)); + calloc(LNM_LOOP_INITIAL_CONNS + 1, sizeof(struct pollfd)); size_t poll_args_cap = LNM_LOOP_INITIAL_CONNS + 1; if (poll_args == NULL) { @@ -128,7 +131,7 @@ lnm_err lnm_loop_run(lnm_loop *l) { poll_args[0].events = POLLIN; while (1) { - size_t poll_args_len = 1; + nfds_t poll_args_len = 1; // Add all open connections to the poll command for (size_t i = 0; i < l->conns.len && poll_args_len < l->conns.open + 1; @@ -176,7 +179,7 @@ lnm_err lnm_loop_run(lnm_loop *l) { } if (poll_args_cap < l->conns.open + 1) { - struct pollfd *buf = malloc((l->conns.open + 1) * sizeof(struct pollfd)); + struct pollfd *buf = calloc(l->conns.open + 1, sizeof(struct pollfd)); if (buf == NULL) { return lnm_err_failed_alloc; diff --git a/lnm/src/loop/lnm_loop_io.c b/lnm/src/loop/lnm_loop_io.c index 5d89191..363d6af 100644 --- a/lnm/src/loop/lnm_loop_io.c +++ b/lnm/src/loop/lnm_loop_io.c @@ -38,6 +38,8 @@ void lnm_loop_conn_io_req(lnm_loop *l, lnm_loop_conn *conn) { void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) { do { + l->data_write(conn); + ssize_t res; do { @@ -49,7 +51,7 @@ void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) { return; } - if (res <= 0) { + if (res < 0) { conn->state = lnm_loop_state_end; return; @@ -59,8 +61,6 @@ void lnm_loop_conn_io_res(lnm_loop *l, lnm_loop_conn *conn) { // writer function more space to work with memmove(conn->w.buf, &conn->w.buf[res], conn->w.size - res); conn->w.size -= res; - - l->data_write(conn); } while (conn->state == lnm_loop_state_res); }