Increase sock limit, fix multiple connections issue

This commit is contained in:
Sergey Lyubka 2022-04-01 15:42:15 +01:00
parent 6990040626
commit 3d9fa10485
4 changed files with 19 additions and 41 deletions

View File

@ -6,14 +6,10 @@ CONFIG_NET_UDP=y
CONFIG_NET_DHCPV4=y
CONFIG_NET_SOCKETS=y
CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=32756
CONFIG_NET_SOCKETS_POLL_MAX=32
CONFIG_NET_LOG=y
CONFIG_LOG=y
#CONFIG_NET_TX_STACK_SIZE=2048
#CONFIG_NET_RX_STACK_SIZE=2048
CONFIG_ISR_STACK_SIZE=2048
CONFIG_MAIN_STACK_SIZE=8192
CONFIG_IDLE_STACK_SIZE=1024
CONFIG_NET_CONFIG_SETTINGS=y
#CONFIG_NET_CONFIG_MY_IPV4_ADDR="169.254.240.10"
#CONFIG_NET_CONFIG_MY_IPV4_NETMASK="255.255.0.0"
#CONFIG_NET_CONFIG_MY_IPV4_GW="169.254.240.5"

View File

@ -52,7 +52,6 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
static void timer_fn(void *arg) {
struct mg_mgr *mgr = (struct mg_mgr *) arg;
return;
if (s_sntp_conn == NULL) s_sntp_conn = mg_sntp_connect(mgr, NULL, sfn, NULL);
if (s_boot_timestamp < 9999) mg_sntp_send(s_sntp_conn, time(NULL));
}

View File

@ -304,7 +304,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
(void) fn_data;
}
static void mg_dns_send(struct mg_connection *c, const struct mg_str *name,
static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
uint16_t txnid, bool ipv6) {
struct {
struct mg_dns_header header;
@ -328,14 +328,7 @@ static void mg_dns_send(struct mg_connection *c, const struct mg_str *name,
if (ipv6) pkt.data[n - 3] = 0x1c; // AAAA query
// memcpy(&pkt.data[n], "\xc0\x0c\x00\x1c\x00\x01", 6); // AAAA query
// n += 6;
mg_send(c, &pkt, sizeof(pkt.header) + n);
#if 0
// Immediately after A query, send AAAA query. Whatever reply comes first,
// we'll use it. Note: we cannot send two queries in a single packet.
// https://stackoverflow.com/questions/4082081/requesting-a-and-aaaa-records-in-single-dns-query
pkt.data[n - 3] = 0x1c; // AAAA query
mg_send(c, &pkt, sizeof(pkt.header) + n);
#endif
return mg_send(c, &pkt, sizeof(pkt.header) + n);
}
static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
@ -364,7 +357,9 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
c->is_resolving = 1;
MG_VERBOSE(("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len,
name->ptr, mg_ntoa(&dnsc->c->rem, buf, sizeof(buf)), d->txnid));
mg_dns_send(dnsc->c, name, d->txnid, ipv6);
if (!mg_dns_send(dnsc->c, name, d->txnid, ipv6)) {
mg_error(dnsc->c, "DNS send");
}
}
}
@ -402,7 +397,7 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) {
va_start(ap, fmt);
mg_vasprintf(&buf, sizeof(mem), fmt, ap);
va_end(ap);
MG_ERROR(("%lu %s", c->id, buf));
MG_ERROR(("%lu %p %s", c->id, c->fd, buf));
c->is_closing = 1; // Set is_closing before sending MG_EV_CALL
mg_call(c, MG_EV_ERROR, buf); // Let user handler to override it
if (buf != mem) free(buf);
@ -2776,8 +2771,9 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
c->is_udp = (strncmp(url, "udp:", 4) == 0);
c->fn = fn;
c->is_client = true;
c->fd = (void *) (size_t) -1; // Set to invalid socket
c->fn_data = fn_data;
MG_DEBUG(("%lu -> %s", c->id, url));
MG_DEBUG(("%lu %p %s", c->id, c->fd, url));
mg_call(c, MG_EV_OPEN, NULL);
mg_resolve(c, url);
}
@ -2799,7 +2795,7 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
c->fn = fn;
c->fn_data = fn_data;
mg_call(c, MG_EV_OPEN, NULL);
MG_DEBUG(("%lu %s %p", c->id, url, c->fd));
MG_DEBUG(("%lu %p %s", c->id, c->fd, url));
}
return c;
}
@ -3222,25 +3218,6 @@ bool mg_sock_would_block(void) {
}
static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
int log_level = n > 0 ? MG_LL_VERBOSE : MG_LL_DEBUG;
char flags[] = {(char) ('0' + c->is_listening),
(char) ('0' + c->is_client),
(char) ('0' + c->is_accepted),
(char) ('0' + c->is_resolving),
(char) ('0' + c->is_connecting),
(char) ('0' + c->is_tls),
(char) ('0' + c->is_tls_hs),
(char) ('0' + c->is_udp),
(char) ('0' + c->is_websocket),
(char) ('0' + c->is_hexdumping),
(char) ('0' + c->is_draining),
(char) ('0' + c->is_closing),
(char) ('0' + c->is_readable),
(char) ('0' + c->is_writable),
'\0'};
MG_LOG(log_level,
("%lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
if (n == 0) {
// Do nothing
} else if (n < 0) {
@ -3289,6 +3266,8 @@ static long mg_sock_send(struct mg_connection *c, const void *buf, size_t len) {
bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
if (c->is_udp) {
long n = mg_sock_send(c, buf, len);
MG_DEBUG(("%lu %p %d:%d %ld err %d (%s)", c->id, c->fd, (int) c->send.len,
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
iolog(c, (char *) buf, n, false);
return n > 0;
} else {
@ -3402,6 +3381,8 @@ static long read_conn(struct mg_connection *c) {
char *buf = (char *) &c->recv.buf[c->recv.len];
size_t len = c->recv.size - c->recv.len;
n = c->is_tls ? mg_tls_recv(c, buf, len) : mg_sock_recv(c, buf, len);
MG_DEBUG(("%lu %p %d:%d %ld err %d (%s)", c->id, c->fd, (int) c->send.len,
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
iolog(c, buf, n, true);
}
return n;
@ -3411,6 +3392,8 @@ static void write_conn(struct mg_connection *c) {
char *buf = (char *) c->send.buf;
size_t len = c->send.len;
long n = c->is_tls ? mg_tls_send(c, buf, len) : mg_sock_send(c, buf, len);
MG_DEBUG(("%lu %p %d:%d %ld err %d (%s)", c->id, c->fd, (int) c->send.len,
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
iolog(c, buf, n, false);
}
@ -3487,6 +3470,7 @@ void mg_connect_resolved(struct mg_connection *c) {
mg_error(c, "connect: %d", MG_SOCK_ERRNO);
}
}
MG_DEBUG(("%lu %p", c->id, c->fd));
}
static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
@ -3636,13 +3620,12 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
}
if ((rc = select((int) maxfd + 1, &rset, &wset, NULL, &tv)) < 0) {
MG_DEBUG(("select: %d %d", rc, MG_SOCK_ERRNO));
MG_ERROR(("select: %d %d", rc, MG_SOCK_ERRNO));
FD_ZERO(&rset);
FD_ZERO(&wset);
}
for (c = mgr->conns; c != NULL; c = c->next) {
// TLS might have stuff buffered, so dig everything
c->is_readable = FD(c) != INVALID_SOCKET && FD_ISSET(FD(c), &rset);
c->is_writable = FD(c) != INVALID_SOCKET && FD_ISSET(FD(c), &wset);
}

View File

@ -17,7 +17,7 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) {
va_start(ap, fmt);
mg_vasprintf(&buf, sizeof(mem), fmt, ap);
va_end(ap);
MG_ERROR(("%lu %s", c->id, buf));
MG_ERROR(("%lu %p %s", c->id, c->fd, buf));
c->is_closing = 1; // Set is_closing before sending MG_EV_CALL
mg_call(c, MG_EV_ERROR, buf); // Let user handler to override it
if (buf != mem) free(buf);