LWIP + RTOS - how to avoid netconn to block the flow forever?

When the LwIP function netconn_accept netconn_accept() or netconn_recv() , if we use RTOS, it blocks the stream and waits for the connection to time out or forever, depending on the setting of LWIP_SO_RCVTIME0 . The timeout duration is SYS_ARCH_TIMEOUT .

SYS_ARCH_TIMEOUT defined as 0xffffffff in the kernel to include part of the LwIP stack, so I think it cannot be expected.

In fact, I want him to check if the connection is created, if not, then it will continue the flow. However, if I call netconn_accept() , it will simply block the thread and wait forever (or for a very long time) ... I do not want to change the value of define SYS_ARCH_TIMEOUT , because I need a different timeout in a different situation ...

What is a good way to do this? Thanks.

+4
source share
4 answers

Polling for a TCP connection (or acceptance) is usually bad practice. Unzip the new thread dedicated exclusively to the blocking call to netconn_accept ().

I understand the limitations of using RTOS, but spawning only one auxiliary stream with minimal stack space should not be a serious problem.

I believe that it is not so difficult to implement a solution to the classical problems of the manufacturer and the consumer .

If you are talking about FreeRTOS, it has all the necessary tools - semaphores and threads.

+5
source

Do not use the lock API at all. The lwIP stack provides a proprietary, non-blocking, event-driven API that is more efficient than blocking and does not require RTOS blocking. A YouTube video (at http://youtu.be/MBk5wJ_8jEc ) shows how this API was used in a real-time system based on the state of the QP state machine.

+5
source

Create a new thread trying to make this connection. As it is not connected, put the thread to sleep for a while so that RTOS can make a context switch! (transition to another task)

+3
source

You can use the netconn_set_recvtimeout function to set the timeout in the listening socket to something small, such as 1 ms.

Eg. (Error handling terminated, binding, ease of listening)

 struct netconn *conn = netconn_new(NETCONN_TCP); if (conn) { if (netconn_bind(conn, IP_ADDR_ANY, 1025/*PORT_NUMBER*/) != ERR_OK) { return; } if (netconn_listen(conn) != ERR_OK) { return; } netconn_set_recvtimeout(conn, 1); } 

Then, calls for acceptance delay a maximum of 1 ms:

 struct netconn *newConn; err_t result = netconn_accept(conn, &newConn); if (result == ERR_OK) { // Handle the connected netconn here } else if (result == ERR_TIMEOUT) { // No pending connections } else { // A problem with the listen socket accepting the connection } 
+1
source

Source: https://habr.com/ru/post/1415552/


All Articles