From 02f19fc05ae99f076b71007bb1c70f1a91bfb0b9 Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Fri, 17 Jan 2014 11:45:57 +0000
Subject: [PATCH] Using mg_handler_t for iterate_over_connections()

---
 examples/websocket.c |  6 ++++--
 mongoose.c           | 12 ++++++------
 mongoose.h           |  6 ++----
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/examples/websocket.c b/examples/websocket.c
index 67306d9d4..ee29423db 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 d9bdaebf0..245966147 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 720dc3636..02e165dff 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);
-- 
GitLab