netconn_close whilst blocked in netconn_recv

jcsbanks
Posts: 149
Joined: Tue Mar 28, 2017 8:03 pm

netconn_close whilst blocked in netconn_recv

Postby jcsbanks » Sat Jan 12, 2019 11:37 pm

I have WiFi in APSTA mode and the webserver will serve http on port 80 on both networks. I have a websocket server using netconn api (for efficiency compared to sockets) on another port.

Once the websocket it opened, it blocks on netconn_recv waiting for data.

If I want to change between AP and STA to serve the webpage, all is fine, but unless the websocket is closed in one network (by closing the browser tab which sends the proper request to close the websocket), it won't open on the other. I'd like the network most recently served a webpage to own the websocket, so I want to be able to shut it down from the server side.

I can detect the event I want to trigger the netconn_close with a structure pointer to the connection, but it doesn't seem to have the same effect as closing the web browser tab that has opened the websocket.

So is there a "nice" way to unblock a netconn_recv by closing or deleting its connection from the server side when the client hasn't been closed?

It might be a good opportunity to redo the webserver and websocket code so they are both on port 80 and use non blocking sockets, but I wondered if there was a simple tweak to do it with netconn.

jcsbanks
Posts: 149
Joined: Tue Mar 28, 2017 8:03 pm

Re: netconn_close whilst blocked in netconn_recv

Postby jcsbanks » Sun Jan 13, 2019 1:33 pm

Best solution so far is to use netconn_set_recvtimeout(newconn, timeout) to give netconn_recv a timeout. Existing code tests that netconn_recv returns ESP_OK, in this case it will return a timeout and effectively close the websocket allowing it be opened elsewhere.

ESP_Angus
Posts: 1349
Joined: Sun May 08, 2016 4:11 am

Re: netconn_close whilst blocked in netconn_recv

Postby ESP_Angus » Tue Jan 15, 2019 12:41 am

LWIP doesn't support closing a netconn from one task while another task is blocked on it.

(Upstream LWIP also doesn't support doing this for the BSD sockets API, but we've added support for this to the BSD Sockets API in ESP-IDF's LWIP fork.)

What you're doing with a timeout is a good solution.

Return to “ESP32 IDF”

Who is online

Users browsing this forum: jcsbanks and 23 guests