diff --git a/examples/websocket.c b/examples/websocket.c index 67306d9d443eadefe8b2d150576e4322901ae23d..ee29423db7ae3ca7e264b926c435b69145588563 100644 --- a/examples/websocket.c +++ b/examples/websocket.c @@ -1,13 +1,15 @@ +#include <string.h> #include "mongoose.h" extern const char *find_embedded_file(const char *, size_t *); -static void iterate_callback(struct mg_connection *c, void *param) { +static int iterate_callback(struct mg_connection *c) { if (c->is_websocket) { char buf[20]; - int len = snprintf(buf, sizeof(buf), "%d", * (int *) param); + int len = snprintf(buf, sizeof(buf), "%d", * (int *) c->connection_param); mg_websocket_write(c, 1, buf, len); } + return 1; } // This handler is called for each incoming websocket frame, one or more diff --git a/mongoose.c b/mongoose.c index d9bdaebf092e63d7aacc596f7b36b5cd53fd70f0..2459661475e3db02dca5cf67441a19c54b8543c5 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3651,12 +3651,13 @@ struct mg_connection *mg_connect(struct mg_server *server, const char *host, static void execute_iteration(struct mg_server *server) { struct ll *lp, *tmp; struct connection *conn; - union { void (*f)(struct mg_connection *, void *); void *p; } msg[2]; + union { mg_handler_t f; void *p; } msg[2]; recv(server->ctl[1], (void *) msg, sizeof(msg), 0); LINKED_LIST_FOREACH(&server->active_connections, lp, tmp) { conn = LINKED_LIST_ENTRY(lp, struct connection, link); - msg[0].f(&conn->mg_conn, msg[1].p); + conn->mg_conn.connection_param = msg[1].p; + msg[0].f(&conn->mg_conn); } } @@ -3781,12 +3782,11 @@ void mg_destroy_server(struct mg_server **server) { } // Apply function to all active connections. -void mg_iterate_over_connections(struct mg_server *server, - void (*func)(struct mg_connection *, void *), +void mg_iterate_over_connections(struct mg_server *server, mg_handler_t handler, void *param) { // Send closure (function + parameter) to the IO thread to execute - union { void (*f)(struct mg_connection *, void *); void *p; } msg[2]; - msg[0].f = func; + union { mg_handler_t f; void *p; } msg[2]; + msg[0].f = handler; msg[1].p = param; send(server->ctl[0], (void *) msg, sizeof(msg), 0); } diff --git a/mongoose.h b/mongoose.h index 720dc36367c45ea6a3f5783aeca5c09acf929cbc..02e165dffdaaea87d07dd505d624dd521b55499f 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1,5 +1,5 @@ // Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com> -// Copyright (c) 2013 Cesanta Software Limited +// Copyright (c) 2013-2014 Cesanta Software Limited // All rights reserved // // This library is dual-licensed: you can redistribute it and/or modify @@ -69,9 +69,7 @@ const char **mg_get_valid_option_names(void); const char *mg_get_option(const struct mg_server *server, const char *name); void mg_set_listening_socket(struct mg_server *, int sock); int mg_get_listening_socket(struct mg_server *); -void mg_iterate_over_connections(struct mg_server *, - void (*func)(struct mg_connection *, void *), - void *param); +void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *); // Connection management functions void mg_send_status(struct mg_connection *, int status_code);