I'm using the socket api to be use to reuse some code and I'm finding that the sockets are single use.
As far as I've seen until now (some more investigation needed) the sockets have three possible states
https://github.com/espressif/esp-idf/bl ... ets.c#L223
Following the code I've found that when a I close a socket after a listen, that socket ends in LWIP_SOCK_CLOSE status and never again goes back to LWIP_SOCK_OPEN status so it can be allocated again.
I've done a little test adding
an LWIP_SOCK_IDLE status and use it as a free socket indication so i check that state in the allocator and switch to that state in the free function and I've got it work.
Code: Select all
@@ -236,9 +236,10 @@ struct lwip_sock {
#if ESP_THREAD_SAFE
-#define LWIP_SOCK_OPEN 0
-#define LWIP_SOCK_CLOSING 1
-#define LWIP_SOCK_CLOSED 2
+#define LWIP_SOCK_IDLE 0
+#define LWIP_SOCK_OPEN 1
+#define LWIP_SOCK_CLOSING 2
+#define LWIP_SOCK_CLOSED 3
#define LWIP_SOCK_LOCK(sock) \
do{\
@@ -565,7 +566,7 @@ alloc_socket(struct netconn *newconn, int accepted)
continue;
}
- if (!sockets[i].conn && (sockets[i].state == LWIP_SOCK_OPEN)) {
+ if (!sockets[i].conn && (sockets[i].state == LWIP_SOCK_IDLE)) {
found = true;
oldest = i;
continue;
@@ -687,6 +688,9 @@ free_socket(struct lwip_sock *sock, int is_tcp)
netbuf_delete((struct netbuf *)lastdata);
}
}
+ SYS_ARCH_PROTECT(lev);
+ sock->state = LWIP_SOCK_IDLE;
+ SYS_ARCH_UNPROTECT(lev);
}
/* Below this, the well-known socket functions are implemented.