mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-19 11:09:42 +00:00
Add struct mg_queue and API, change examples/multi-threaded, add tests
This commit is contained in:
1 parent
c0f4bde5fc
commit
bf9d69ab56
20 files changed
+427
-94
No files matched your search
@@ -1,17 +1,26 @@
|
||||
PROG ?= example
|
||||
ROOT ?= $(realpath $(CURDIR)/../..)
|
||||
CWD ?= $(realpath $(CURDIR))
|
||||
DOCKER ?= docker run --rm -it -e Tmp=. -e WINEDEBUG=-all -v $(ROOT):$(ROOT) -w $(CWD)
|
||||
SOURCES = main.c mongoose.c # Source code files
|
||||
CFLAGS = -W -Wall -Wextra -g -I. # Build options
|
||||
|
||||
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
|
||||
CFLAGS_MONGOOSE += -DMG_ENABLE_ATOMIC=1
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
# Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD
|
||||
PROG ?= example.exe # Use .exe suffix for the binary
|
||||
CC = gcc # Use MinGW gcc compiler
|
||||
CFLAGS += -lws2_32 # Link against Winsock library
|
||||
DELETE = cmd /C del /f /q /s # Command prompt command to delete files
|
||||
else
|
||||
# Mac, Linux
|
||||
PROG ?= example
|
||||
DELETE = rm -rf
|
||||
endif
|
||||
|
||||
all: $(PROG)
|
||||
$(RUN) ./$(PROG)
|
||||
$(RUN) ./$(PROG) $(ARGS)
|
||||
|
||||
$(PROG):
|
||||
$(CC) ../../mongoose.c -I../.. -pthread $(CFLAGS) -o $(PROG) main.c
|
||||
|
||||
$(PROG).exe:
|
||||
$(DOCKER) mdashnet/vc98 wine cl ../../mongoose.c main.c -I../.. /MD ws2_32.lib /Fe$@
|
||||
$(DOCKER) mdashnet/vc98 wine $@
|
||||
$(PROG): $(SOURCES)
|
||||
$(CC) $(SOURCES) $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) -o $@
|
||||
|
||||
clean:
|
||||
rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb log.txt
|
||||
$(DELETE) $(PROG) *.o *.obj *.exe *.dSYM
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2020 Cesanta Software Limited
|
||||
// Copyright (c) 2020-2023 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Multithreading example.
|
||||
@@ -6,14 +6,20 @@
|
||||
// some time to simulate long processing time, produces an output and
|
||||
// hands over that output to the request handler function.
|
||||
//
|
||||
// We pass POST body to the worker thread, and respond with a calculated CRC
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
struct thread_data {
|
||||
struct mg_queue queue; // Worker -> Connection queue
|
||||
struct mg_str body; // Copy of message body
|
||||
};
|
||||
|
||||
static void start_thread(void (*f)(void *), void *p) {
|
||||
#ifdef _WIN32
|
||||
#define usleep(x) Sleep((x) / 1000)
|
||||
_beginthread((void(__cdecl *)(void *)) f, 0, p);
|
||||
#else
|
||||
#define closesocket(x) close(x)
|
||||
#include <pthread.h>
|
||||
pthread_t thread_id = (pthread_t) 0;
|
||||
pthread_attr_t attr;
|
||||
@@ -24,71 +30,51 @@ static void start_thread(void (*f)(void *), void *p) {
|
||||
#endif
|
||||
}
|
||||
|
||||
struct userdata {
|
||||
int sock; // Paired socket, worker thread owns it
|
||||
struct mg_str
|
||||
body; // data to be processed
|
||||
};
|
||||
static void worker_thread(void *param) {
|
||||
struct thread_data *d = (struct thread_data *) param;
|
||||
char buf[100]; // On-stack buffer for the message queue
|
||||
|
||||
static void thread_function(void *param) {
|
||||
struct userdata *p = (struct userdata *) param;
|
||||
sleep(2); // Simulate long execution
|
||||
// Respond, wakeup event manager
|
||||
if (p->body.len)
|
||||
send(p->sock, p->body.ptr, p->body.len, 0); // if there is a body, echo it
|
||||
else
|
||||
send(p->sock, "hi", 2, 0); // otherwise just respond "hi"
|
||||
close(p->sock); // Close the connection
|
||||
free((void *) p->body.ptr); // free body
|
||||
free(p); // free userdata
|
||||
}
|
||||
d->queue.buf = buf; // Caller passed us an empty queue with NULL
|
||||
d->queue.len = sizeof(buf); // buffer. Initialise it now
|
||||
usleep(1 * 1000 * 1000); // Simulate long execution time
|
||||
|
||||
static void link_conns(struct mg_connection *c1, struct mg_connection *c2) {
|
||||
c1->fn_data = c2;
|
||||
c2->fn_data = c1;
|
||||
}
|
||||
|
||||
static void unlink_conns(struct mg_connection *c1, struct mg_connection *c2) {
|
||||
c1->fn_data = c2->fn_data = NULL;
|
||||
}
|
||||
|
||||
// Pipe event handler
|
||||
static void pcb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
struct mg_connection *parent = (struct mg_connection *) fn_data;
|
||||
MG_INFO(("%lu %p %d %p", c->id, c->fd, ev, parent));
|
||||
if (parent == NULL) { // If parent connection closed, close too
|
||||
c->is_closing = 1;
|
||||
} else if (ev == MG_EV_READ) { // Got data from the worker thread
|
||||
mg_http_reply(parent, 200, "Host: foo.com\r\n", "%.*s\n", c->recv.len,
|
||||
c->recv.buf); // Respond!
|
||||
c->recv.len = 0; // Tell Mongoose we've consumed data
|
||||
} else if (ev == MG_EV_OPEN) {
|
||||
link_conns(c, parent);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
unlink_conns(c, parent);
|
||||
// Send a response to the connection
|
||||
if (d->body.len == 0) {
|
||||
mg_queue_printf(&d->queue, "Send me POST data");
|
||||
} else {
|
||||
uint32_t crc = mg_crc32(0, d->body.ptr, d->body.len);
|
||||
mg_queue_printf(&d->queue, "crc32: %#x", crc);
|
||||
free((char *) d->body.ptr);
|
||||
}
|
||||
|
||||
// Wait until connection reads our message, then it is safe to quit
|
||||
while (mg_queue_next(&d->queue, NULL) != MG_QUEUE_EMPTY) usleep(1000);
|
||||
MG_INFO(("done, cleaning up..."));
|
||||
free(d);
|
||||
}
|
||||
|
||||
// HTTP request callback
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
// Received HTTP request. Allocate thread data and spawn a worker thread
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_http_match_uri(hm, "/fast")) {
|
||||
// Single-threaded code path, for performance comparison
|
||||
// The /fast URI responds immediately
|
||||
mg_http_reply(c, 200, "Host: foo.com\r\n", "hi\n");
|
||||
} else {
|
||||
// Multithreading code path
|
||||
struct userdata *data = calloc(1, sizeof(*data)); // worker will free it
|
||||
// parse headers or use request body, duplicate data and pass it to worker
|
||||
// thread
|
||||
data->body = mg_strdup(hm->body); // worker will free it
|
||||
data->sock = mg_mkpipe(c->mgr, pcb, c, true); // Create pipe
|
||||
start_thread(thread_function, data); // Start thread and pass data
|
||||
struct thread_data *d = (struct thread_data *) calloc(1, sizeof(*d));
|
||||
d->body = mg_strdup(hm->body); // Pass received body to the worker
|
||||
start_thread(worker_thread, d); // Start a thread
|
||||
*(void **) c->data = d; // Memorise data pointer in c->data
|
||||
} else if (ev == MG_EV_POLL) {
|
||||
// Poll event. Delivered to us every mg_mgr_poll interval or faster
|
||||
struct thread_data *d = *(struct thread_data **) c->data;
|
||||
size_t len;
|
||||
char *buf;
|
||||
// Check if we have a message from the worker
|
||||
if (d != NULL && (len = mg_queue_next(&d->queue, &buf)) != MG_QUEUE_EMPTY) {
|
||||
// Got message from worker. Send a response and cleanup
|
||||
mg_http_reply(c, 200, "", "%.*s\n", (int) len, buf);
|
||||
mg_queue_del(&d->queue); // Delete message: signal worker that we're done
|
||||
}
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
if (c->fn_data != NULL) unlink_conns(c, c->fn_data);
|
||||
}
|
||||
(void) fn_data;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
@@ -96,7 +82,7 @@ int main(void) {
|
||||
mg_mgr_init(&mgr);
|
||||
mg_log_set(MG_LL_DEBUG); // Set debug log level
|
||||
mg_http_listen(&mgr, "http://localhost:8000", fn, NULL); // Create listener
|
||||
for (;;) mg_mgr_poll(&mgr, 1000); // Event loop
|
||||
mg_mgr_free(&mgr); // Cleanup
|
||||
for (;;) mg_mgr_poll(&mgr, 10); // Event loop. Use 10ms poll interval
|
||||
mg_mgr_free(&mgr); // Cleanup
|
||||
return 0;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../mongoose.c
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../mongoose.h
|
||||
Reference in new issue
Block a user