mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-09 16:21:45 +00:00
Using mg_handler_t for iterate_over_connections()
This commit is contained in:
1 parent
7690f9e34b
commit
02f19fc05a
3 files changed
+12
-12
No files matched your search
@@ -1,13 +1,15 @@
|
|||||||
|
#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
|
||||||
|
|||||||
+6
-6
@@ -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) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -1,5 +1,5 @@
|
|||||||
// 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);
|
|||||||
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);
|
||||||
|
|||||||
Reference in new issue
Block a user