Skip to content

Commit

Permalink
remove fn_data from event handler signature
Browse files Browse the repository at this point in the history
  • Loading branch information
scaprile committed Jan 9, 2024
1 parent b8bfa55 commit 6f4a783
Show file tree
Hide file tree
Showing 72 changed files with 750 additions and 814 deletions.
2 changes: 1 addition & 1 deletion examples/arduino/teensy41-http/teensy41-http.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function
// Simple HTTP server that runs on port 8000
// Mongoose event handler function, gets called by the mg_mgr_poll()
// See https://mongoose.ws/documentation/#2-minute-integration-guide
static void simple_http_listener(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void simple_http_listener(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
// The MG_EV_HTTP_MSG event means HTTP request. `hm` holds parsed request,
// see https://mongoose.ws/documentation/#struct-mg_http_message
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/w5500-http/w5500-http.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void setup() {
// Setup HTTP listener. Respond "ok" on any HTTP request
mg_http_listen(
&mgr, "http://0.0.0.0",
[](struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
[](struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "ok\n");
},
&mgr);
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/w5500-mqtt/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void mqtt_publish(const char *message) {
if (mqtt_connection) mg_mqtt_pub(mqtt_connection, &opts);
}

static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_MQTT_OPEN) {
MG_INFO(("%lu CONNECTED to %s", c->id, MQTT_SERVER));
struct mg_mqtt_opts opts = {};
Expand Down
3 changes: 1 addition & 2 deletions examples/captive-dns-server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ uint8_t answer[] = {
1, 2, 3, 4 // 4 bytes - IP address
};

static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) {
c->is_hexdumping = 1;
} else if (ev == MG_EV_READ) {
Expand All @@ -41,7 +41,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
}
mg_iobuf_del(&c->recv, 0, c->recv.len);
}
(void) fn_data;
(void) ev_data;
}

Expand Down
7 changes: 3 additions & 4 deletions examples/device-dashboard/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int ui_event_next(int no, struct ui_event *e) {

// SNTP connection event handler. When we get a response from an SNTP server,
// adjust s_boot_timestamp. We'll get a valid time from that point on
static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void sfn(struct mg_connection *c, int ev, void *ev_data) {
uint64_t *expiration_time = (uint64_t *) c->data;
if (ev == MG_EV_OPEN) {
*expiration_time = mg_millis() + 3000; // Store expiration time in 3s
Expand All @@ -62,7 +62,6 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
} else if (ev == MG_EV_POLL) {
if (mg_millis() > *expiration_time) c->is_closing = 1;
}
(void) fn_data;
}

static void timer_sntp_fn(void *param) { // SNTP timer function. Sync up time
Expand Down Expand Up @@ -262,9 +261,9 @@ static void handle_device_eraselast(struct mg_connection *c) {
}

// HTTP request handler function
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_ACCEPT) {
if (fn_data != NULL) { // TLS listener!
if (c->fn_data != NULL) { // TLS listener!
struct mg_tls_opts opts = {0};
opts.cert = mg_unpacked("/certs/server_cert.pem");
opts.key = mg_unpacked("/certs/server_key.pem");
Expand Down
3 changes: 1 addition & 2 deletions examples/embedded-filesystem/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
const char *s_listening_url = "http://0.0.0.0:8000";

// HTTP request handler function
void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_serve_opts opts = {
.root_dir = "/web_root",
.fs = &mg_fs_packed
};
mg_http_serve_dir(c, ev_data, &opts);
}
(void) fn_data;
}

int main(void) {
Expand Down
3 changes: 1 addition & 2 deletions examples/esp32/micropython/mongoose/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ static void t_fn(void *arg) { // Tick periodically
(void) arg;
}

static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "hi\n");
(void) fn_data;
(void) ev_data;
}

Expand Down
6 changes: 3 additions & 3 deletions examples/esp8266/http-client-server/src/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#define SERVER_URL "http://0.0.0.0:80"
#define CLIENT_URL "http://info.cern.ch"

// Event handler for an server (accepted) connection
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
// Event handler for a server (accepted) connection
static void cb(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
mg_http_reply(c, 200, "", "Hello from ESP!\n");
}
Expand All @@ -20,7 +20,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
// To enable TLS for HTTP,
// 1. Copy "ca.pem" file to the ESP8266 flash FS
// 2. Add TLS init snippet for the connection, see examples/http-client
static void cb2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void cb2(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_CONNECT) {
struct mg_str s = mg_url_host(CLIENT_URL);
mg_printf(c, "GET / HTTP/1.0\r\nHost: %.*s\r\n\r\n", (int) s.len, s.ptr);
Expand Down
3 changes: 1 addition & 2 deletions examples/file-upload-html-form/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
// Also, consider changing -DMG_IO_SIZE=SOME_BIG_VALUE to increase IO buffer
// increment when reading data.
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void cb(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
MG_INFO(("New request to: [%.*s], body size: %lu", (int) hm->uri.len,
Expand All @@ -35,7 +35,6 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
mg_http_serve_dir(c, ev_data, &opts);
}
}
(void) fn_data;
}

int main(void) {
Expand Down
3 changes: 1 addition & 2 deletions examples/file-upload-multiple-posts/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// HTTP request handler function. It implements the following endpoints:
// /upload - Saves the next file chunk
// all other URI - serves web_root/ directory
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void cb(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_http_match_uri(hm, "/upload")) {
Expand All @@ -25,7 +25,6 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
mg_http_serve_dir(c, ev_data, &opts);
}
}
(void) fn_data;
}

int main(void) {
Expand Down
4 changes: 2 additions & 2 deletions examples/file-upload-single-post/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// HTTP request handler function. It implements the following endpoints:
// /upload - Saves the next file chunk
// all other URI - serves web_root/ directory
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void cb(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_READ) {
// Parse the incoming data ourselves. If we can parse the request,
// store two size_t variables in the c->data: expected len and recv len.
Expand All @@ -38,7 +38,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
}
}
}
(void) fn_data, (void) ev_data;
(void) ev_data;
}

int main(void) {
Expand Down
6 changes: 3 additions & 3 deletions examples/http-client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static const char *s_post_data = NULL; // POST data
static const uint64_t s_timeout_ms = 1500; // Connect timeout in milliseconds

// Print HTTP response and signal that we're done
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) {
// Connection created. Store connect expiration time in c->data
*(uint64_t *) c->data = mg_millis() + s_timeout_ms;
Expand Down Expand Up @@ -50,9 +50,9 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
printf("%.*s", (int) hm->message.len, hm->message.ptr);
c->is_draining = 1; // Tell mongoose to close this connection
*(bool *) fn_data = true; // Tell event loop to stop
*(bool *) c->fn_data = true; // Tell event loop to stop
} else if (ev == MG_EV_ERROR) {
*(bool *) fn_data = true; // Error, tell event loop to stop
*(bool *) c->fn_data = true; // Error, tell event loop to stop
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/http-proxy-client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "mongoose.h"

// Print HTTP response and signal that we're done
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
const char *url = fn_data;
static void fn(struct mg_connection *c, int ev, void *ev_data) {
const char *url = c->fn_data;
static bool connected;
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
Expand Down
5 changes: 2 additions & 3 deletions examples/http-restful-server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ static const char *s_tls_key =

// We use the same event handler function for HTTP and HTTPS connections
// fn_data is NULL for plain HTTP, and non-NULL for HTTPS
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_ACCEPT && fn_data != NULL) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_ACCEPT && c->fn_data != NULL) {
struct mg_tls_opts opts = {
#ifdef TLS_TWOWAY
.ca = mg_str(s_tls_ca),
Expand Down Expand Up @@ -89,7 +89,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
mg_http_serve_dir(c, ev_data, &opts);
}
}
(void) fn_data;
}

int main(void) {
Expand Down
8 changes: 4 additions & 4 deletions examples/http-reverse-proxy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ static void forward_request(struct mg_http_message *hm,
(int) hm->uri.len, hm->uri.ptr));
}

static void fn2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
struct mg_connection *c2 = (struct mg_connection *) fn_data;
static void fn2(struct mg_connection *c, int ev, void *ev_data) {
struct mg_connection *c2 = (struct mg_connection *) c->fn_data;
if (ev == MG_EV_READ) {
// All incoming data from the backend, forward to the client
if (c2 != NULL) mg_send(c2, c->recv.buf, c->recv.len);
Expand All @@ -48,8 +48,8 @@ static void fn2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
(void) ev_data;
}

static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
struct mg_connection *c2 = fn_data;
static void fn(struct mg_connection *c, int ev, void *ev_data) {
struct mg_connection *c2 = c->fn_data;
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
// Client request, create backend connection Note that we're passing
Expand Down
3 changes: 1 addition & 2 deletions examples/http-server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void signal_handler(int signo) {

// Event handler for the listening connection.
// Simply serve static files from `s_root_dir`
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void cb(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = ev_data, tmp = {0};
struct mg_str unknown = mg_str_n("?", 1), *cl;
Expand All @@ -33,7 +33,6 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
(int) hm->uri.len, hm->uri.ptr, (int) tmp.uri.len, tmp.uri.ptr,
(int) cl->len, cl->ptr));
}
(void) fn_data;
}

static void usage(const char *prog) {
Expand Down
6 changes: 3 additions & 3 deletions examples/http-streaming-client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
static const char *s_url = "http://info.cern.ch";

// Print HTTP response and signal that we're done
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_CONNECT) {
// Connected to server. Extract host name from URL
struct mg_str host = mg_url_host(s_url);
Expand Down Expand Up @@ -54,9 +54,9 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
}
}
} else if (ev == MG_EV_CLOSE) {
*(bool *) fn_data = true; // tell event loop to stop
*(bool *) c->fn_data = true; // tell event loop to stop
} else if (ev == MG_EV_ERROR) {
*(bool *) fn_data = true; // Error, tell event loop to stop
*(bool *) c->fn_data = true; // Error, tell event loop to stop
}
(void) ev_data;
}
Expand Down
3 changes: 1 addition & 2 deletions examples/huge-response/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static size_t printdata(mg_pfn_t out, void *ptr, va_list *ap) {
return n;
}

static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = ev_data;
if (mg_http_match_uri(hm, "/api/data")) {
Expand All @@ -66,7 +66,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
mg_http_serve_dir(c, hm, &opts);
}
}
(void) fn_data;
}

static void timer_fn(void *arg) {
Expand Down
3 changes: 1 addition & 2 deletions examples/json-rpc-over-websocket/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void rpc_mul(struct mg_rpc_req *r) {
// This RESTful server implements the following endpoints:
// /websocket - upgrade to Websocket, and implement websocket echo server
// any other URI serves static files from s_web_root
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) {
// c->is_hexdumping = 1;
} else if (ev == MG_EV_WS_OPEN) {
Expand All @@ -51,7 +51,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (io.buf) mg_ws_send(c, (char *) io.buf, io.len, WEBSOCKET_OP_TEXT);
mg_iobuf_free(&io);
}
(void) fn_data;
}

static void timer_fn(void *arg) {
Expand Down
3 changes: 1 addition & 2 deletions examples/live-log/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// /api/log/static - returns contents of log.txt file
// /api/log/live - hangs forever, and returns live log messages
// all other URI - serves web_root/ directory
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void cb(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_http_match_uri(hm, "/api/log/static")) {
Expand All @@ -21,7 +21,6 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
mg_http_serve_dir(c, ev_data, &opts);
}
}
(void) fn_data;
}

static void log_message(const char *filename, const char *message) {
Expand Down
3 changes: 1 addition & 2 deletions examples/microchip/same54-xpro/mqtt-client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG
}
}

static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) {
MG_INFO(("%lu CREATED", c->id));
// c->is_hexdumping = 1;
Expand Down Expand Up @@ -66,7 +66,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
MG_INFO(("%lu CLOSED", c->id));
s_conn = NULL; // Mark that we're closed
}
(void) fn_data;
}

// Timer function - recreate client connection if it is closed
Expand Down
3 changes: 1 addition & 2 deletions examples/micropython/mongoose/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ static void t_fn(void *arg) { // Tick periodically
(void) arg;
}

static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "hi\n");
(void) fn_data;
(void) ev_data;
}

Expand Down
10 changes: 5 additions & 5 deletions examples/mip-pcap/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ static size_t pcap_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
return received;
}

static void fn2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn2(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
MG_DEBUG(("Got response (%d) %.*s...", (int) hm->message.len, 12,
hm->message.ptr));
c->is_draining = 1;
} else if (ev == MG_EV_CONNECT) {
mg_printf(c, "GET %s HTTP/1.1\r\n\r\n", mg_url_uri((char *) fn_data));
mg_printf(c, "GET %s HTTP/1.1\r\n\r\n", mg_url_uri((char *) c->fn_data));
} else if (ev == MG_EV_CLOSE) {
free(fn_data);
free(c->fn_data);
}
}

static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_http_match_uri(hm, "/api/debug")) {
Expand All @@ -75,7 +75,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
hm->message.ptr);
}
}
(void) ev_data, (void) fn_data;
(void) ev_data;
}

int main(int argc, char *argv[]) {
Expand Down
Loading

0 comments on commit 6f4a783

Please sign in to comment.