Move mg_mqtt_next-* to the example, and some nits

This commit is contained in:
cpq committed 2022-08-26 14:20:54 +01:00
1 parent ffa033cca9
commit c7b9ca68c2
10 files changed
+45 -150

No files matched your search

-26
View File
@@ -185,32 +185,6 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
return MQTT_OK;
}
static size_t mg_mqtt_next_topic(struct mg_mqtt_message *msg,
struct mg_str *topic, uint8_t *qos,
size_t pos) {
unsigned char *buf = (unsigned char *) msg->dgram.ptr + pos;
size_t new_pos;
if (pos >= msg->dgram.len) return 0;
topic->len = (size_t) (((unsigned) buf[0]) << 8 | buf[1]);
topic->ptr = (char *) buf + 2;
new_pos = pos + 2 + topic->len + (qos == NULL ? 0 : 1);
if ((size_t) new_pos > msg->dgram.len) return 0;
if (qos != NULL) *qos = buf[2 + topic->len];
return new_pos;
}
size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic,
uint8_t *qos, size_t pos) {
uint8_t tmp;
return mg_mqtt_next_topic(msg, topic, qos == NULL ? &tmp : qos, pos);
}
size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic,
size_t pos) {
return mg_mqtt_next_topic(msg, topic, NULL, pos);
}
static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data,
void *fn_data) {
if (ev == MG_EV_READ) {
-4
View File
@@ -56,10 +56,6 @@ void mg_mqtt_sub(struct mg_connection *, struct mg_str topic, int qos);
int mg_mqtt_parse(const uint8_t *, size_t, uint8_t, struct mg_mqtt_message *);
void mg_mqtt_send_header(struct mg_connection *, uint8_t cmd, uint8_t flags,
uint32_t len);
size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic,
uint8_t *qos, size_t pos);
size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic,
size_t pos);
void mg_mqtt_ping(struct mg_connection *);
void mg_mqtt_pong(struct mg_connection *);
void mg_mqtt_disconnect(struct mg_connection *);
+1 -1
View File
@@ -157,7 +157,7 @@ void mg_close_conn(struct mg_connection *c) {
// Order of operations is important. `MG_EV_CLOSE` event must be fired
// before we deallocate received data, see #1331
mg_call(c, MG_EV_CLOSE, NULL);
MG_DEBUG(("%lu closed", c->id));
MG_DEBUG(("%lu %p closed", c->id, c->fd));
mg_tls_free(c);
mg_iobuf_free(&c->recv);
+5 -4
View File
@@ -322,7 +322,6 @@ static void close_conn(struct mg_connection *c) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP
FreeRTOS_FD_CLR(c->fd, c->mgr->ss, eSELECT_ALL);
#endif
c->fd = NULL;
}
mg_close_conn(c);
}
@@ -422,10 +421,9 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
MG_ERROR(("%lu OOM", lsn->id));
closesocket(fd);
} else {
char buf[40];
// char buf[40];
tomgaddr(&usa, &c->rem, sa_len != sizeof(usa.sin));
mg_straddr(&c->rem, buf, sizeof(buf));
MG_DEBUG(("%lu accepted %s", c->id, buf));
// mg_straddr(&c->rem, buf, sizeof(buf));
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
c->fd = S2PTR(fd);
MG_EPOLL_ADD(c);
@@ -438,6 +436,9 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
c->pfn_data = lsn->pfn_data;
c->fn = lsn->fn;
c->fn_data = lsn->fn_data;
MG_DEBUG(("%lu %p accepted %x.%hu -> %x.%hu", c->id, c->fd,
mg_ntohl(c->rem.ip), mg_ntohs(c->rem.port), mg_ntohl(c->loc.ip),
mg_ntohs(c->loc.port)));
mg_call(c, MG_EV_OPEN, NULL);
mg_call(c, MG_EV_ACCEPT, NULL);
}
+1
View File
@@ -272,6 +272,7 @@ void mg_ws_upgrade(struct mg_connection *c, struct mg_http_message *hm,
ws_handshake(c, wskey, wsproto, fmt, &ap);
va_end(ap);
c->is_websocket = 1;
c->is_resp = 0;
mg_call(c, MG_EV_WS_OPEN, hm);
}
}