socket api

ikerbelloso
Posts: 20
Joined: Wed Jul 27, 2016 7:34 am

socket api

Postby ikerbelloso » Tue Apr 25, 2017 1:03 pm

Hi there,
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.
Am I missing something? is there any other appropriate mode to do that?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: socket api

Postby WiFive » Tue Apr 25, 2017 2:38 pm

It shouldn't have to be LWIP_SOCK_OPEN status to be allocated, just have no netconn.

Who is online

Users browsing this forum: Bing [Bot] and 115 guests