feat(lnm): implement event loop state switching

lnm
Jef Roosens 2023-12-02 16:45:37 +01:00
parent 13ccfef94d
commit 799821d9fc
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 29 additions and 3 deletions

View File

@ -264,6 +264,25 @@ void (*process_fns[])(lnm_http_conn *conn) = {
lnm_http_loop_process_finish, 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) { void lnm_http_loop_process(lnm_http_conn *conn) {
const lnm_http_loop_ctx *ctx = conn->ctx; 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; lnm_loop_state loop_state = conn->state;
// We stop processing if: // We stop processing if:
// - the event loop state has changed, as we need to switch to the other I/O // - the event loop state has been explicitely changed inside the executed
// loop // 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 // - the process fn returned without changing the HTTP loop state, indicating
// it's waiting for I/O // it's waiting for I/O
do { do {
http_loop_state = ctx->state; http_loop_state = ctx->state;
process_fns[http_loop_state](conn); 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;
} }