Merge pull request #1724 from cesanta/mgread

Pass long *bytes_read to MG_EV_READ
This commit is contained in:
Sergio R. Caprile 2022-09-09 14:09:50 -03:00 committed by GitHub
commit 05019529f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 28 deletions

View File

@ -493,8 +493,7 @@ static void rx_udp(struct mip_if *ifp, struct pkt *pkt) {
} else { } else {
memcpy(&c->recv.buf[c->recv.len], pkt->pay.buf, pkt->pay.len); memcpy(&c->recv.buf[c->recv.len], pkt->pay.buf, pkt->pay.len);
c->recv.len += pkt->pay.len; c->recv.len += pkt->pay.len;
struct mg_str evd = mg_str_n((char *) pkt->pay.buf, pkt->pay.len); mg_call(c, MG_EV_READ, &pkt->pay.len);
mg_call(c, MG_EV_READ, &evd);
} }
} }
} }
@ -630,15 +629,12 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
} else if (n > 0) { } else if (n > 0) {
// Decrypted successfully - trigger MG_EV_READ // Decrypted successfully - trigger MG_EV_READ
io->len += (size_t) n; io->len += (size_t) n;
struct mg_str evd = mg_call(c, MG_EV_READ, &n);
mg_str_n((char *) &io->buf[io->len - (size_t) n], (size_t) n);
mg_call(c, MG_EV_READ, &evd);
} }
} }
} else { } else {
// Plain text connection, data is already in c->recv, trigger MG_EV_READ // Plain text connection, data is already in c->recv, trigger MG_EV_READ
struct mg_str evd = mg_str_n((char *) pkt->pay.buf, pkt->pay.len); mg_call(c, MG_EV_READ, &pkt->pay.len);
mg_call(c, MG_EV_READ, &evd);
} }
} }
} }

View File

@ -4089,9 +4089,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
mg_hexdump(buf, (size_t) n); mg_hexdump(buf, (size_t) n);
} }
if (r) { if (r) {
struct mg_str evd = mg_str_n(buf, (size_t) n);
c->recv.len += (size_t) n; c->recv.len += (size_t) n;
mg_call(c, MG_EV_READ, &evd); mg_call(c, MG_EV_READ, &n);
} else { } else {
mg_iobuf_del(&c->send, 0, (size_t) n); mg_iobuf_del(&c->send, 0, (size_t) n);
// if (c->send.len == 0) mg_iobuf_resize(&c->send, 0); // if (c->send.len == 0) mg_iobuf_resize(&c->send, 0);
@ -4611,8 +4610,8 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
tmp = c->next; tmp = c->next;
mg_call(c, MG_EV_POLL, &now); mg_call(c, MG_EV_POLL, &now);
if (is_resp && !c->is_resp) { if (is_resp && !c->is_resp) {
struct mg_str fake = mg_str_n("", 0); long n = 0;
mg_call(c, MG_EV_READ, &fake); mg_call(c, MG_EV_READ, &n);
} }
MG_VERBOSE(("%lu %c%c %c%c%c%c%c", c->id, c->is_readable ? 'r' : '-', MG_VERBOSE(("%lu %c%c %c%c%c%c%c", c->id, c->is_readable ? 'r' : '-',
c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't', c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't',
@ -6795,8 +6794,7 @@ static void rx_udp(struct mip_if *ifp, struct pkt *pkt) {
} else { } else {
memcpy(&c->recv.buf[c->recv.len], pkt->pay.buf, pkt->pay.len); memcpy(&c->recv.buf[c->recv.len], pkt->pay.buf, pkt->pay.len);
c->recv.len += pkt->pay.len; c->recv.len += pkt->pay.len;
struct mg_str evd = mg_str_n((char *) pkt->pay.buf, pkt->pay.len); mg_call(c, MG_EV_READ, &pkt->pay.len);
mg_call(c, MG_EV_READ, &evd);
} }
} }
} }
@ -6932,15 +6930,12 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) {
} else if (n > 0) { } else if (n > 0) {
// Decrypted successfully - trigger MG_EV_READ // Decrypted successfully - trigger MG_EV_READ
io->len += (size_t) n; io->len += (size_t) n;
struct mg_str evd = mg_call(c, MG_EV_READ, &n);
mg_str_n((char *) &io->buf[io->len - (size_t) n], (size_t) n);
mg_call(c, MG_EV_READ, &evd);
} }
} }
} else { } else {
// Plain text connection, data is already in c->recv, trigger MG_EV_READ // Plain text connection, data is already in c->recv, trigger MG_EV_READ
struct mg_str evd = mg_str_n((char *) pkt->pay.buf, pkt->pay.len); mg_call(c, MG_EV_READ, &pkt->pay.len);
mg_call(c, MG_EV_READ, &evd);
} }
} }
} }

View File

@ -989,11 +989,11 @@ void mg_error(struct mg_connection *c, const char *fmt, ...);
enum { enum {
MG_EV_ERROR, // Error char *error_message MG_EV_ERROR, // Error char *error_message
MG_EV_OPEN, // Connection created NULL MG_EV_OPEN, // Connection created NULL
MG_EV_POLL, // mg_mgr_poll iteration uint64_t *milliseconds MG_EV_POLL, // mg_mgr_poll iteration uint64_t *uptime_millis
MG_EV_RESOLVE, // Host name is resolved NULL MG_EV_RESOLVE, // Host name is resolved NULL
MG_EV_CONNECT, // Connection established NULL MG_EV_CONNECT, // Connection established NULL
MG_EV_ACCEPT, // Connection accepted NULL MG_EV_ACCEPT, // Connection accepted NULL
MG_EV_READ, // Data received from socket struct mg_str * MG_EV_READ, // Data received from socket long *bytes_read
MG_EV_WRITE, // Data written to socket long *bytes_written MG_EV_WRITE, // Data written to socket long *bytes_written
MG_EV_CLOSE, // Connection closed NULL MG_EV_CLOSE, // Connection closed NULL
MG_EV_HTTP_MSG, // HTTP request/response struct mg_http_message * MG_EV_HTTP_MSG, // HTTP request/response struct mg_http_message *
@ -1004,7 +1004,7 @@ enum {
MG_EV_MQTT_CMD, // MQTT low-level command struct mg_mqtt_message * MG_EV_MQTT_CMD, // MQTT low-level command struct mg_mqtt_message *
MG_EV_MQTT_MSG, // MQTT PUBLISH received struct mg_mqtt_message * MG_EV_MQTT_MSG, // MQTT PUBLISH received struct mg_mqtt_message *
MG_EV_MQTT_OPEN, // MQTT CONNACK received int *connack_status_code MG_EV_MQTT_OPEN, // MQTT CONNACK received int *connack_status_code
MG_EV_SNTP_TIME, // SNTP time received uint64_t *milliseconds MG_EV_SNTP_TIME, // SNTP time received uint64_t *epoch_millis
MG_EV_USER, // Starting ID for user events MG_EV_USER, // Starting ID for user events
}; };

View File

@ -9,11 +9,11 @@ void mg_error(struct mg_connection *c, const char *fmt, ...);
enum { enum {
MG_EV_ERROR, // Error char *error_message MG_EV_ERROR, // Error char *error_message
MG_EV_OPEN, // Connection created NULL MG_EV_OPEN, // Connection created NULL
MG_EV_POLL, // mg_mgr_poll iteration uint64_t *milliseconds MG_EV_POLL, // mg_mgr_poll iteration uint64_t *uptime_millis
MG_EV_RESOLVE, // Host name is resolved NULL MG_EV_RESOLVE, // Host name is resolved NULL
MG_EV_CONNECT, // Connection established NULL MG_EV_CONNECT, // Connection established NULL
MG_EV_ACCEPT, // Connection accepted NULL MG_EV_ACCEPT, // Connection accepted NULL
MG_EV_READ, // Data received from socket struct mg_str * MG_EV_READ, // Data received from socket long *bytes_read
MG_EV_WRITE, // Data written to socket long *bytes_written MG_EV_WRITE, // Data written to socket long *bytes_written
MG_EV_CLOSE, // Connection closed NULL MG_EV_CLOSE, // Connection closed NULL
MG_EV_HTTP_MSG, // HTTP request/response struct mg_http_message * MG_EV_HTTP_MSG, // HTTP request/response struct mg_http_message *
@ -24,6 +24,6 @@ enum {
MG_EV_MQTT_CMD, // MQTT low-level command struct mg_mqtt_message * MG_EV_MQTT_CMD, // MQTT low-level command struct mg_mqtt_message *
MG_EV_MQTT_MSG, // MQTT PUBLISH received struct mg_mqtt_message * MG_EV_MQTT_MSG, // MQTT PUBLISH received struct mg_mqtt_message *
MG_EV_MQTT_OPEN, // MQTT CONNACK received int *connack_status_code MG_EV_MQTT_OPEN, // MQTT CONNACK received int *connack_status_code
MG_EV_SNTP_TIME, // SNTP time received uint64_t *milliseconds MG_EV_SNTP_TIME, // SNTP time received uint64_t *epoch_millis
MG_EV_USER, // Starting ID for user events MG_EV_USER, // Starting ID for user events
}; };

View File

@ -135,9 +135,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
mg_hexdump(buf, (size_t) n); mg_hexdump(buf, (size_t) n);
} }
if (r) { if (r) {
struct mg_str evd = mg_str_n(buf, (size_t) n);
c->recv.len += (size_t) n; c->recv.len += (size_t) n;
mg_call(c, MG_EV_READ, &evd); mg_call(c, MG_EV_READ, &n);
} else { } else {
mg_iobuf_del(&c->send, 0, (size_t) n); mg_iobuf_del(&c->send, 0, (size_t) n);
// if (c->send.len == 0) mg_iobuf_resize(&c->send, 0); // if (c->send.len == 0) mg_iobuf_resize(&c->send, 0);
@ -657,8 +656,8 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
tmp = c->next; tmp = c->next;
mg_call(c, MG_EV_POLL, &now); mg_call(c, MG_EV_POLL, &now);
if (is_resp && !c->is_resp) { if (is_resp && !c->is_resp) {
struct mg_str fake = mg_str_n("", 0); long n = 0;
mg_call(c, MG_EV_READ, &fake); mg_call(c, MG_EV_READ, &n);
} }
MG_VERBOSE(("%lu %c%c %c%c%c%c%c", c->id, c->is_readable ? 'r' : '-', MG_VERBOSE(("%lu %c%c %c%c%c%c%c", c->id, c->is_readable ? 'r' : '-',
c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't', c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't',