From 799821d9fc0d8e6d101a11054f4f0760b1e77f18 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 2 Dec 2023 16:45:37 +0100 Subject: [PATCH] feat(lnm): implement event loop state switching --- lnm/src/http/lnm_http_loop_process.c | 32 +++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lnm/src/http/lnm_http_loop_process.c b/lnm/src/http/lnm_http_loop_process.c index 9090df2..7fb2cf9 100644 --- a/lnm/src/http/lnm_http_loop_process.c +++ b/lnm/src/http/lnm_http_loop_process.c @@ -264,6 +264,25 @@ void (*process_fns[])(lnm_http_conn *conn) = { lnm_http_loop_process_finish, }; +lnm_loop_state state_map[] = { + // parse_req + lnm_loop_state_req, + // route + lnm_loop_state_req, + // parse_headers + lnm_loop_state_req, + // steps + lnm_loop_state_req, + // write_status_line + lnm_loop_state_res, + // write_headers + lnm_loop_state_res, + // write_body + lnm_loop_state_res, + // finish + lnm_loop_state_res, +}; + void lnm_http_loop_process(lnm_http_conn *conn) { const lnm_http_loop_ctx *ctx = conn->ctx; @@ -271,13 +290,20 @@ void lnm_http_loop_process(lnm_http_conn *conn) { lnm_loop_state loop_state = conn->state; // We stop processing if: - // - the event loop state has changed, as we need to switch to the other I/O - // loop + // - the event loop state has been explicitely changed inside the executed + // step, as we need to switch to the other I/O loop + // - the event loop state needs to be changed because the next step should be + // run in another event loop state // - the process fn returned without changing the HTTP loop state, indicating // it's waiting for I/O do { http_loop_state = ctx->state; process_fns[http_loop_state](conn); - } while ((conn->state == loop_state) && (http_loop_state != ctx->state)); + } while ((conn->state == loop_state) && + (conn->state == state_map[loop_state]) && + (http_loop_state != ctx->state)); + + // Check required to prevent overwriting manually set event loop state + conn->state = conn->state == loop_state ? state_map[loop_state] : conn->state; }