Commit 02f19fc0 authored by Sergey Lyubka's avatar Sergey Lyubka

Using mg_handler_t for iterate_over_connections()

parent 7690f9e3
#include <string.h>
#include "mongoose.h" #include "mongoose.h"
extern const char *find_embedded_file(const char *, size_t *); 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) { if (c->is_websocket) {
char buf[20]; 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); mg_websocket_write(c, 1, buf, len);
} }
return 1;
} }
// This handler is called for each incoming websocket frame, one or more // This handler is called for each incoming websocket frame, one or more
......
...@@ -3651,12 +3651,13 @@ struct mg_connection *mg_connect(struct mg_server *server, const char *host, ...@@ -3651,12 +3651,13 @@ struct mg_connection *mg_connect(struct mg_server *server, const char *host,
static void execute_iteration(struct mg_server *server) { static void execute_iteration(struct mg_server *server) {
struct ll *lp, *tmp; struct ll *lp, *tmp;
struct connection *conn; 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); recv(server->ctl[1], (void *) msg, sizeof(msg), 0);
LINKED_LIST_FOREACH(&server->active_connections, lp, tmp) { LINKED_LIST_FOREACH(&server->active_connections, lp, tmp) {
conn = LINKED_LIST_ENTRY(lp, struct connection, link); 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) { ...@@ -3781,12 +3782,11 @@ void mg_destroy_server(struct mg_server **server) {
} }
// Apply function to all active connections. // Apply function to all active connections.
void mg_iterate_over_connections(struct mg_server *server, void mg_iterate_over_connections(struct mg_server *server, mg_handler_t handler,
void (*func)(struct mg_connection *, void *),
void *param) { void *param) {
// Send closure (function + parameter) to the IO thread to execute // Send closure (function + parameter) to the IO thread to execute
union { void (*f)(struct mg_connection *, void *); void *p; } msg[2]; union { mg_handler_t f; void *p; } msg[2];
msg[0].f = func; msg[0].f = handler;
msg[1].p = param; msg[1].p = param;
send(server->ctl[0], (void *) msg, sizeof(msg), 0); send(server->ctl[0], (void *) msg, sizeof(msg), 0);
} }
......
// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com> // 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 // All rights reserved
// //
// This library is dual-licensed: you can redistribute it and/or modify // This library is dual-licensed: you can redistribute it and/or modify
...@@ -69,9 +69,7 @@ const char **mg_get_valid_option_names(void); ...@@ -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); const char *mg_get_option(const struct mg_server *server, const char *name);
void mg_set_listening_socket(struct mg_server *, int sock); void mg_set_listening_socket(struct mg_server *, int sock);
int mg_get_listening_socket(struct mg_server *); int mg_get_listening_socket(struct mg_server *);
void mg_iterate_over_connections(struct mg_server *, void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *);
void (*func)(struct mg_connection *, void *),
void *param);
// Connection management functions // Connection management functions
void mg_send_status(struct mg_connection *, int status_code); void mg_send_status(struct mg_connection *, int status_code);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment