mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-04 01:39:01 +08:00
Rename struct mg_connection::label -> data. Make its size configurable
This commit is contained in:
parent
3d75d71767
commit
da5e8e9778
@ -78,7 +78,7 @@ static struct user *getuser(struct mg_http_message *hm) {
|
||||
static void send_notification(struct mg_mgr *mgr, const char *fmt, ...) {
|
||||
struct mg_connection *c;
|
||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->label[0] == 'W') {
|
||||
if (c->data[0] == 'W') {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
mg_ws_vprintf(c, WEBSOCKET_OP_TEXT, fmt, &ap);
|
||||
@ -202,7 +202,7 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
}
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
} else if (mg_http_match_uri(hm, "/api/watch")) {
|
||||
c->label[0] = 'W'; // Mark ourselves as a event listener
|
||||
c->data[0] = 'W'; // Mark ourselves as a event listener
|
||||
mg_ws_upgrade(c, hm, NULL);
|
||||
} else if (mg_http_match_uri(hm, "/api/login")) {
|
||||
mg_http_reply(c, 200, NULL, "{%Q:%Q,%Q:%Q}\n", "user", u->name, "token",
|
||||
|
@ -17,10 +17,10 @@ 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) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// Connection created. Store connect expiration time in c->label
|
||||
*(uint64_t *) c->label = mg_millis() + s_timeout_ms;
|
||||
// Connection created. Store connect expiration time in c->data
|
||||
*(uint64_t *) c->data = mg_millis() + s_timeout_ms;
|
||||
} else if (ev == MG_EV_POLL) {
|
||||
if (mg_millis() > *(uint64_t *) c->label &&
|
||||
if (mg_millis() > *(uint64_t *) c->data &&
|
||||
(c->is_connecting || c->is_resolving)) {
|
||||
mg_error(c, "Connect timeout");
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_WS_OPEN) {
|
||||
c->label[0] = 'W'; // Mark this connection as an established WS client
|
||||
c->data[0] = 'W'; // Mark this connection as an established WS client
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_http_match_uri(hm, "/websocket")) {
|
||||
@ -58,7 +58,7 @@ static void timer_fn(void *arg) {
|
||||
struct mg_mgr *mgr = (struct mg_mgr *) arg;
|
||||
// Broadcast message to all connected websocket clients.
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->label[0] != 'W') continue;
|
||||
if (c->data[0] != 'W') continue;
|
||||
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%Q:%Q,%Q:[%d,%d,%d]}", "method",
|
||||
"notification1", "params", 1, 2, 3);
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
struct mg_http_serve_opts opts = {.root_dir = NULL};
|
||||
mg_http_serve_file(c, hm, "log.txt", &opts);
|
||||
} else if (mg_http_match_uri(hm, "/api/log/live")) {
|
||||
c->label[0] = 'L'; // Mark that connection as live log listener
|
||||
c->data[0] = 'L'; // Mark that connection as live log listener
|
||||
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
||||
@ -34,7 +34,7 @@ static void log_message(const char *filename, const char *message) {
|
||||
static void broadcast_message(struct mg_mgr *mgr, const char *message) {
|
||||
struct mg_connection *c;
|
||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->label[0] == 'L') mg_http_printf_chunk(c, "%s", message);
|
||||
if (c->data[0] == 'L') mg_http_printf_chunk(c, "%s", message);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,13 +55,13 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
MG_INFO(("Connected to %s", s_url));
|
||||
MG_INFO(("Subscribing to %s", s_rx_topic));
|
||||
mg_mqtt_sub(c, topic, s_qos);
|
||||
c->label[0] = 'X'; // Set a label that we're logged in
|
||||
c->data[0] = 'X'; // Set a label that we're logged in
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
// When we receive MQTT message, print it
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
MG_INFO(("Received on %.*s : %.*s", (int) mm->topic.len, mm->topic.ptr,
|
||||
(int) mm->data.len, mm->data.ptr));
|
||||
} else if (ev == MG_EV_POLL && c->label[0] == 'X') {
|
||||
} else if (ev == MG_EV_POLL && c->data[0] == 'X') {
|
||||
static unsigned long prev_second;
|
||||
unsigned long now_second = (*(unsigned long *) ev_data) / 1000;
|
||||
if (now_second != prev_second) {
|
||||
|
@ -15,7 +15,7 @@ static bool s_quit;
|
||||
enum { EHLO, STARTTLS, STARTTLS_WAIT, AUTH, FROM, TO, DATA, BODY, QUIT, END };
|
||||
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
uint8_t *state = (uint8_t *) c->label;
|
||||
uint8_t *state = (uint8_t *) c->data;
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_READ) {
|
||||
|
@ -55,7 +55,7 @@ static void handshake(struct mg_connection *c) {
|
||||
}
|
||||
mg_iobuf_del(r, 0, 2 + r->buf[1]);
|
||||
mg_send(c, reply, sizeof(reply));
|
||||
c->label[0] = STATE_REQUEST;
|
||||
c->data[0] = STATE_REQUEST;
|
||||
}
|
||||
}
|
||||
|
||||
@ -146,15 +146,15 @@ static void request(struct mg_connection *c) {
|
||||
}
|
||||
mg_send(c, r->buf + 3, addr_len + 1 + 2);
|
||||
mg_iobuf_del(r, 0, 6 + addr_len); // Remove request from the input stream
|
||||
c->label[0] = STATE_ESTABLISHED; // Mark ourselves as connected
|
||||
c->data[0] = STATE_ESTABLISHED; // Mark ourselves as connected
|
||||
}
|
||||
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_READ) {
|
||||
// We use the first label byte as a state
|
||||
if (c->label[0] == STATE_HANDSHAKE) handshake(c);
|
||||
if (c->label[0] == STATE_REQUEST) request(c);
|
||||
if (c->label[0] == STATE_ESTABLISHED) exchange(c);
|
||||
if (c->data[0] == STATE_HANDSHAKE) handshake(c);
|
||||
if (c->data[0] == STATE_REQUEST) request(c);
|
||||
if (c->data[0] == STATE_ESTABLISHED) exchange(c);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
disband(c);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ 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;
|
||||
if (mg_http_match_uri(hm, "/websocket")) {
|
||||
mg_ws_upgrade(c, hm, NULL); // Upgrade HTTP to Websocket
|
||||
c->label[0] = 'W'; // Set some unique mark on a connection
|
||||
c->data[0] = 'W'; // Set some unique mark on a connection
|
||||
} else {
|
||||
// Serve static files
|
||||
struct mg_http_serve_opts opts = {.root_dir = s_web_root};
|
||||
@ -37,7 +37,7 @@ static void timer_fn(void *arg) {
|
||||
// Traverse over all connections
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
||||
// Send only to marked connections
|
||||
if (c->label[0] == 'W') mg_ws_send(c, "hi", 2, WEBSOCKET_OP_TEXT);
|
||||
if (c->data[0] == 'W') mg_ws_send(c, "hi", 2, WEBSOCKET_OP_TEXT);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,7 @@ static void ws_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
mg_ws_upgrade(c, evd, NULL);
|
||||
} else if (ev == MG_EV_WS_OPEN) {
|
||||
// c->is_hexdumping = 1;
|
||||
c->label[0] = 'W'; // When WS handhake is done, mark us as WS client
|
||||
c->data[0] = 'W'; // When WS handhake is done, mark us as WS client
|
||||
} else if (ev == MG_EV_WS_MSG) {
|
||||
struct mg_ws_message *wm = (struct mg_ws_message *) evd;
|
||||
uart_write(wm->data.ptr, wm->data.len); // Send to UART
|
||||
@ -77,7 +77,7 @@ static void ws_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
static void tcp_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
if (ev == MG_EV_ACCEPT) {
|
||||
// c->is_hexdumping = 1;
|
||||
c->label[0] = 'T'; // When client is connected, mark us as TCP client
|
||||
c->data[0] = 'T'; // When client is connected, mark us as TCP client
|
||||
} else if (ev == MG_EV_READ) {
|
||||
uart_write(c->recv.buf, c->recv.len); // Send to UART
|
||||
c->recv.len = 0; // Discard received data
|
||||
@ -99,7 +99,7 @@ static void mq_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_MQTT_OPEN) {
|
||||
c->label[0] = 'M';
|
||||
c->data[0] = 'M';
|
||||
mg_mqtt_sub(c, mqtt_topic("rx", "b/rx"), 1); // Subscribe to RX topic
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
struct mg_mqtt_message *mm = evd; // MQTT message
|
||||
@ -131,9 +131,9 @@ static void timer_fn(void *param) {
|
||||
if (len > 0) {
|
||||
// Iterate over all connections. Send data to WS and TCP clients
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->label[0] == 'W') mg_ws_send(c, buf, len, WEBSOCKET_OP_TEXT);
|
||||
if (c->label[0] == 'T') mg_send(c, buf, len);
|
||||
if (c->label[0] == 'M')
|
||||
if (c->data[0] == 'W') mg_ws_send(c, buf, len, WEBSOCKET_OP_TEXT);
|
||||
if (c->data[0] == 'T') mg_send(c, buf, len);
|
||||
if (c->data[0] == 'M')
|
||||
mg_mqtt_pub(c, mqtt_topic("tx", "b/tx"), mg_str_n(buf, len), 1, false);
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_RESOLVE) {
|
||||
// c->rem gets populated with multicast address. Store it in c->label
|
||||
memcpy(c->label, &c->rem, sizeof(c->rem));
|
||||
// c->rem gets populated with multicast address. Store it in c->data
|
||||
memcpy(c->data, &c->rem, sizeof(c->rem));
|
||||
} else if (ev == MG_EV_READ) {
|
||||
MG_INFO(("Got a response"));
|
||||
struct mg_http_message hm;
|
||||
@ -32,7 +32,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
// We can now do mg_printf(c, "haha"); to respond back to the remote side.
|
||||
// But in our case, we should restore the multicast address in order
|
||||
// to have next search to go to the multicast address
|
||||
memcpy(&c->rem, c->label, sizeof(c->rem));
|
||||
memcpy(&c->rem, c->data, sizeof(c->rem));
|
||||
// Discard the content of this response as we expect each SSDP response
|
||||
// to generate at most one MG_EV_READ event.
|
||||
c->recv.len = 0UL;
|
||||
|
@ -10,7 +10,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_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/video1")) {
|
||||
c->label[0] = 'S'; // Mark that connection as live streamer
|
||||
c->data[0] = 'S'; // Mark that connection as live streamer
|
||||
mg_printf(
|
||||
c, "%s",
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
@ -36,7 +36,7 @@ static void broadcast_mjpeg_frame(struct mg_mgr *mgr) {
|
||||
char *data = mg_file_read(&mg_fs_posix, path, &size); // Read next file
|
||||
struct mg_connection *c;
|
||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->label[0] != 'S') continue; // Skip non-stream connections
|
||||
if (c->data[0] != 'S') continue; // Skip non-stream connections
|
||||
if (data == NULL || size == 0) continue; // Skip on file read error
|
||||
mg_printf(c,
|
||||
"--foo\r\nContent-Type: image/jpeg\r\n"
|
||||
|
@ -15,7 +15,7 @@ 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;
|
||||
if (mg_http_match_uri(hm, "/api/watch")) {
|
||||
mg_ws_upgrade(c, hm, NULL); // Upgrade HTTP to Websocket
|
||||
c->label[0] = 'W'; // Set some unique mark on the connection
|
||||
c->data[0] = 'W'; // Set some unique mark on the connection
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {.root_dir = s_web_root};
|
||||
mg_http_serve_dir(c, ev_data, &opts);
|
||||
@ -28,7 +28,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
static void push(struct mg_mgr *mgr, const char *name, const void *data) {
|
||||
struct mg_connection *c;
|
||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->label[0] != 'W') continue;
|
||||
if (c->data[0] != 'W') continue;
|
||||
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%Q:%Q,%Q:%Q}", "name", name, "data",
|
||||
data);
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -78,7 +78,7 @@ static struct user *getuser(struct mg_http_message *hm) {
|
||||
static void send_notification(struct mg_mgr *mgr, const char *fmt, ...) {
|
||||
struct mg_connection *c;
|
||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||
if (c->label[0] == 'W') {
|
||||
if (c->data[0] == 'W') {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
mg_ws_vprintf(c, WEBSOCKET_OP_TEXT, fmt, &ap);
|
||||
@ -201,7 +201,7 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
|
||||
}
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
} else if (mg_http_match_uri(hm, "/api/watch")) {
|
||||
c->label[0] = 'W'; // Mark ourselves as a event listener
|
||||
c->data[0] = 'W'; // Mark ourselves as a event listener
|
||||
mg_ws_upgrade(c, hm, NULL);
|
||||
} else if (mg_http_match_uri(hm, "/api/login")) {
|
||||
mg_http_reply(c, 200, NULL, "{%Q:%Q,%Q:%Q}\n", "user", u->name, "token",
|
||||
|
@ -16,10 +16,10 @@ static bool done = false;
|
||||
// Print HTTP response and signal that we're done
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// Connection created. Store connect expiration time in c->label
|
||||
*(int64_t *) c->label = mg_millis() + s_timeout_ms;
|
||||
// Connection created. Store connect expiration time in c->data
|
||||
*(int64_t *) c->data = mg_millis() + s_timeout_ms;
|
||||
} else if (ev == MG_EV_POLL) {
|
||||
if (mg_millis() > *(int64_t *) c->label &&
|
||||
if (mg_millis() > *(int64_t *) c->data &&
|
||||
(c->is_connecting || c->is_resolving)) {
|
||||
mg_error(c, "Connect timeout");
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -32,13 +32,13 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
MG_INFO(("Connected to %s", s_url));
|
||||
MG_INFO(("Subscribing to %s", s_rx_topic));
|
||||
mg_mqtt_sub(c, topic, s_qos);
|
||||
c->label[0] = 'X'; // Set a label that we're logged in
|
||||
c->data[0] = 'X'; // Set a label that we're logged in
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
// When we receive MQTT message, print it
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
MG_INFO(("Received on %.*s : %.*s", (int) mm->topic.len, mm->topic.ptr,
|
||||
(int) mm->data.len, mm->data.ptr));
|
||||
} else if (ev == MG_EV_POLL && c->label[0] == 'X') {
|
||||
} else if (ev == MG_EV_POLL && c->data[0] == 'X') {
|
||||
static unsigned long prev_second;
|
||||
unsigned long now_second = (*(unsigned long *) ev_data) / 1000;
|
||||
if (now_second != prev_second) {
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
14
mongoose.c
14
mongoose.c
@ -1715,7 +1715,7 @@ static void static_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
struct mg_fd *fd = (struct mg_fd *) fn_data;
|
||||
// Read to send IO buffer directly, avoid extra on-stack buffer
|
||||
size_t n, max = MG_IO_SIZE, space;
|
||||
size_t *cl = (size_t *) &c->label[(sizeof(c->label) - sizeof(size_t)) /
|
||||
size_t *cl = (size_t *) &c->data[(sizeof(c->data) - sizeof(size_t)) /
|
||||
sizeof(size_t) * sizeof(size_t)];
|
||||
if (c->send.size < max) mg_iobuf_resize(&c->send, max);
|
||||
if (c->send.len >= c->send.size) return; // Rate limit
|
||||
@ -1887,8 +1887,8 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
||||
c->is_resp = 0;
|
||||
mg_fs_close(fd);
|
||||
} else {
|
||||
// Track to-be-sent content length at the end of c->label, aligned
|
||||
size_t *clp = (size_t *) &c->label[(sizeof(c->label) - sizeof(size_t)) /
|
||||
// Track to-be-sent content length at the end of c->data, aligned
|
||||
size_t *clp = (size_t *) &c->data[(sizeof(c->data) - sizeof(size_t)) /
|
||||
sizeof(size_t) * sizeof(size_t)];
|
||||
c->pfn = static_cb;
|
||||
c->pfn_data = fd;
|
||||
@ -2342,7 +2342,7 @@ static void mg_hfn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
|
||||
if (mg_http_match_uri(hm, "/quit")) {
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
c->is_draining = 1;
|
||||
c->label[0] = 'X';
|
||||
c->data[0] = 'X';
|
||||
} else if (mg_http_match_uri(hm, "/debug")) {
|
||||
int level = (int) mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG);
|
||||
mg_log_set(level);
|
||||
@ -2351,7 +2351,7 @@ static void mg_hfn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
|
||||
mg_http_reply(c, 200, "", "hi\n");
|
||||
}
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
if (c->label[0] == 'X') *(bool *) fnd = true;
|
||||
if (c->data[0] == 'X') *(bool *) fnd = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4062,8 +4062,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
union usa usa;
|
||||
socklen_t slen = sizeof(usa.sin);
|
||||
if (getsockname(FD(c), &usa.sa, &slen) < 0) (void) 0; // Ignore result
|
||||
MG_INFO(("\n-- %lu %I %s %I %s %ld", c->id, 4, &usa.sin.sin_addr,
|
||||
r ? "<-" : "->", 4, &c->rem.ip, c->label, n));
|
||||
MG_INFO(("\n-- %lu %I %s %I %ld", c->id, 4, &usa.sin.sin_addr,
|
||||
r ? "<-" : "->", 4, &c->rem.ip, n));
|
||||
|
||||
mg_hexdump(buf, (size_t) n);
|
||||
}
|
||||
|
12
mongoose.h
12
mongoose.h
@ -658,14 +658,16 @@ struct timeval {
|
||||
#define MG_ENABLE_PACKED_FS 0
|
||||
#endif
|
||||
|
||||
// Granularity of the send/recv IO buffer growth
|
||||
#ifndef MG_IO_SIZE
|
||||
#define MG_IO_SIZE 2048
|
||||
#define MG_IO_SIZE 2048 // Granularity of the send/recv IO buffer growth
|
||||
#endif
|
||||
|
||||
// Maximum size of the recv IO buffer
|
||||
#ifndef MG_MAX_RECV_SIZE
|
||||
#define MG_MAX_RECV_SIZE (3 * 1024 * 1024)
|
||||
#define MG_MAX_RECV_SIZE (3 * 1024 * 1024) // Maximum recv IO buffer size
|
||||
#endif
|
||||
|
||||
#ifndef MG_DATA_SIZE
|
||||
#define MG_DATA_SIZE 32 // struct mg_connection :: data size
|
||||
#endif
|
||||
|
||||
#ifndef MG_MAX_HTTP_HEADERS
|
||||
@ -1057,7 +1059,7 @@ struct mg_connection {
|
||||
void *fn_data; // User-specified function parameter
|
||||
mg_event_handler_t pfn; // Protocol-specific handler function
|
||||
void *pfn_data; // Protocol-specific function parameter
|
||||
char label[50]; // Arbitrary label
|
||||
char data[MG_DATA_SIZE]; // Arbitrary connection data
|
||||
void *tls; // TLS specific data
|
||||
unsigned is_listening : 1; // Listening connection
|
||||
unsigned is_client : 1; // Outbound (client) connection
|
||||
|
10
src/config.h
10
src/config.h
@ -81,14 +81,16 @@
|
||||
#define MG_ENABLE_PACKED_FS 0
|
||||
#endif
|
||||
|
||||
// Granularity of the send/recv IO buffer growth
|
||||
#ifndef MG_IO_SIZE
|
||||
#define MG_IO_SIZE 2048
|
||||
#define MG_IO_SIZE 2048 // Granularity of the send/recv IO buffer growth
|
||||
#endif
|
||||
|
||||
// Maximum size of the recv IO buffer
|
||||
#ifndef MG_MAX_RECV_SIZE
|
||||
#define MG_MAX_RECV_SIZE (3 * 1024 * 1024)
|
||||
#define MG_MAX_RECV_SIZE (3 * 1024 * 1024) // Maximum recv IO buffer size
|
||||
#endif
|
||||
|
||||
#ifndef MG_DATA_SIZE
|
||||
#define MG_DATA_SIZE 32 // struct mg_connection :: data size
|
||||
#endif
|
||||
|
||||
#ifndef MG_MAX_HTTP_HEADERS
|
||||
|
10
src/http.c
10
src/http.c
@ -355,7 +355,7 @@ static void static_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
struct mg_fd *fd = (struct mg_fd *) fn_data;
|
||||
// Read to send IO buffer directly, avoid extra on-stack buffer
|
||||
size_t n, max = MG_IO_SIZE, space;
|
||||
size_t *cl = (size_t *) &c->label[(sizeof(c->label) - sizeof(size_t)) /
|
||||
size_t *cl = (size_t *) &c->data[(sizeof(c->data) - sizeof(size_t)) /
|
||||
sizeof(size_t) * sizeof(size_t)];
|
||||
if (c->send.size < max) mg_iobuf_resize(&c->send, max);
|
||||
if (c->send.len >= c->send.size) return; // Rate limit
|
||||
@ -527,8 +527,8 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
||||
c->is_resp = 0;
|
||||
mg_fs_close(fd);
|
||||
} else {
|
||||
// Track to-be-sent content length at the end of c->label, aligned
|
||||
size_t *clp = (size_t *) &c->label[(sizeof(c->label) - sizeof(size_t)) /
|
||||
// Track to-be-sent content length at the end of c->data, aligned
|
||||
size_t *clp = (size_t *) &c->data[(sizeof(c->data) - sizeof(size_t)) /
|
||||
sizeof(size_t) * sizeof(size_t)];
|
||||
c->pfn = static_cb;
|
||||
c->pfn_data = fd;
|
||||
@ -982,7 +982,7 @@ static void mg_hfn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
|
||||
if (mg_http_match_uri(hm, "/quit")) {
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
c->is_draining = 1;
|
||||
c->label[0] = 'X';
|
||||
c->data[0] = 'X';
|
||||
} else if (mg_http_match_uri(hm, "/debug")) {
|
||||
int level = (int) mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG);
|
||||
mg_log_set(level);
|
||||
@ -991,7 +991,7 @@ static void mg_hfn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
|
||||
mg_http_reply(c, 200, "", "hi\n");
|
||||
}
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
if (c->label[0] == 'X') *(bool *) fnd = true;
|
||||
if (c->data[0] == 'X') *(bool *) fnd = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ struct mg_connection {
|
||||
void *fn_data; // User-specified function parameter
|
||||
mg_event_handler_t pfn; // Protocol-specific handler function
|
||||
void *pfn_data; // Protocol-specific function parameter
|
||||
char label[50]; // Arbitrary label
|
||||
char data[MG_DATA_SIZE]; // Arbitrary connection data
|
||||
void *tls; // TLS specific data
|
||||
unsigned is_listening : 1; // Listening connection
|
||||
unsigned is_client : 1; // Outbound (client) connection
|
||||
|
@ -101,8 +101,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
||||
union usa usa;
|
||||
socklen_t slen = sizeof(usa.sin);
|
||||
if (getsockname(FD(c), &usa.sa, &slen) < 0) (void) 0; // Ignore result
|
||||
MG_INFO(("\n-- %lu %I %s %I %s %ld", c->id, 4, &usa.sin.sin_addr,
|
||||
r ? "<-" : "->", 4, &c->rem.ip, c->label, n));
|
||||
MG_INFO(("\n-- %lu %I %s %I %ld", c->id, 4, &usa.sin.sin_addr,
|
||||
r ? "<-" : "->", 4, &c->rem.ip, n));
|
||||
|
||||
mg_hexdump(buf, (size_t) n);
|
||||
}
|
||||
|
@ -1914,19 +1914,19 @@ static void test_http_upload(void) {
|
||||
static void eX(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||
c->label[0] = 1;
|
||||
c->data[0] = 1;
|
||||
c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_POLL && c->label[0] != 0) {
|
||||
c->label[0]++;
|
||||
if (c->label[0] == 10) mg_http_printf_chunk(c, "a");
|
||||
if (c->label[0] == 20) {
|
||||
} else if (ev == MG_EV_POLL && c->data[0] != 0) {
|
||||
c->data[0]++;
|
||||
if (c->data[0] == 10) mg_http_printf_chunk(c, "a");
|
||||
if (c->data[0] == 20) {
|
||||
mg_http_printf_chunk(c, "b");
|
||||
mg_http_printf_chunk(c, "c");
|
||||
}
|
||||
if (c->label[0] == 30) {
|
||||
if (c->data[0] == 30) {
|
||||
mg_http_printf_chunk(c, "d");
|
||||
mg_http_printf_chunk(c, "");
|
||||
c->label[0] = 0;
|
||||
c->data[0] = 0;
|
||||
}
|
||||
}
|
||||
(void) ev_data, (void) fn_data;
|
||||
@ -1935,12 +1935,12 @@ static void eX(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
static void eY(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\n");
|
||||
c->label[0] = 1;
|
||||
} else if (ev == MG_EV_POLL && c->label[0] != 0) {
|
||||
c->label[0]++;
|
||||
if (c->label[0] == 10) mg_send(c, "a", 1);
|
||||
if (c->label[0] == 12) mg_send(c, "bc", 2);
|
||||
if (c->label[0] == 30) mg_send(c, "d", 1), c->is_resp = 0, c->label[0] = 0;
|
||||
c->data[0] = 1;
|
||||
} else if (ev == MG_EV_POLL && c->data[0] != 0) {
|
||||
c->data[0]++;
|
||||
if (c->data[0] == 10) mg_send(c, "a", 1);
|
||||
if (c->data[0] == 12) mg_send(c, "bc", 2);
|
||||
if (c->data[0] == 30) mg_send(c, "d", 1), c->is_resp = 0, c->data[0] = 0;
|
||||
}
|
||||
(void) ev_data, (void) fn_data;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user