#pragma comment(lib, "Ws2_32.lib")
#include <iostream>
#include "mongoose.h"
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_HTTP_MSG) {
std::cout << "Received HTTP request\n";
// mg_http_reply(c, 200, "Content-Type: text/plain\r\n", "Hello, %s\n", "world");
}
}
int main() {
struct mg_mgr mgr;
mg_mgr_init(&mgr);
mg_log_set("3");
mg_http_listen(&mgr, "0.0.0.0:29906", fn, NULL); // Create listening connection
for (;;) mg_mgr_poll(&mgr, 1000); // Block forever
}
This is the minimal "Dynamic RESTful server" from the Mongoose web server tutorial section, however, I have commented out the section of code which causes it to reply to requests, and replaced it with a console message "Received HTTP request".
On its Github page, Mongoose claims that it is event-driven and non-blocking. However, when I run this code and send the server three concurrent HTTP requests, I receive the following output:
2022-02-14 05:44:35 3 mongoose.c:3569:mg_listen 1 accepting on 0.0.0.0:29906 (port 29906)
2022-02-14 05:44:37 3 mongoose.c:3467:accept_con 2 accepted 127.0.0.1:52957
Received HTTP request
2022-02-14 05:44:38 3 mongoose.c:3467:accept_con 3 accepted 127.0.0.1:52958
2022-02-14 05:44:44 3 mongoose.c:3170:iolog 2 00100000000010 0:0 -1 err 0
2022-02-14 05:44:44 3 mongoose.c:3353:close_conn 2 closed
Received HTTP request
2022-02-14 05:44:45 3 mongoose.c:3170:iolog 3 00100000000010 0:0 -1 err 0
2022-02-14 05:44:45 3 mongoose.c:3353:close_conn 3 closed
2022-02-14 05:44:45 3 mongoose.c:3467:accept_con 4 accepted 127.0.0.1:52961
Received HTTP request
2022-02-14 05:44:45 3 mongoose.c:3467:accept_con 5 accepted 127.0.0.1:52962
2022-02-14 05:44:47 3 mongoose.c:3170:iolog 4 00100000000010 0:0 -1 err 0
2022-02-14 05:44:47 3 mongoose.c:3353:close_conn 4 closed
The server tries to process the first request, waits for it to timeout, then tries to process the second request, waits for it to timeout...
My question is, how can I prevent this from occurring, and allow the processing of HTTP requests in a non-blocking manner (i.e. they would all show "Received HTTP request" at the same time)?