http://lwip.100.n7.nabble.com/TCP-Raw-API-questions-about-efficiency-and-threads-td26473.html
Hi,
LwIP RAW API is simple once you understand it.
You do not create separate threads, all the connections are handled in one thread !
The way to have multiple connections is to add a user ARG value. Every connection has
its own PCB. Meaning for every connection the TCP stack will allocate a new PCB and when
it calls your accept cal-back you use this PCB to differentiate from other connections.
Also inside your accept call-back you can allocate your own structure and assign it to the
new connection. You assigns it to ARG !
Inside recv or any other call-back you simply read the ARG parameter and use it to differentiate
the connections.
You can either allocate your structure from the TCP memory or reallocate it statically and define
a maximum connection that you will be handling.
In order to use RAW API efficiently you can use the poll call back to send more data. What do I
mean by that ?
Lets assume you got some request for data and you are handling it inside the recv function.
If you send back all the data you are busy inside that recv instant and blocking other parallel
connections. One way to avoid this is to start sending (a portion) and get out of the recv. The rest of the send will be handled inside the poll function. the poll function is called periodically as long as the
current connection is a live... house keeping.
Once you no longer need that connection can gracefully close it from inside the poll function.
Another way to use the poll call back is to put some data in a queue (from main thread) while from
inside the poll call back (TCP own thread) read that data and send it to the open connection.
Regarding the ping... 200-300 ms is very slow even on the WAN. You are doing something wrong.
Check for the examples that are around and get some ideas how implement your code.
Hope that above helped.
BR,
Noam.
Dear,
I am sorry if I was not clear but you should not do what you did. No need to copy the data and process it
In the poll function.
I meant that if you get a request (like in HTTP) and you need to send lots of data you start the send process it in the
recv and continue in the poll call-back, if needed.
You should cycle all the data in the chain in one go (inside recv). Once you handled all the data you inform
the TCP stack that you have finished processing and exit.
Something like this (TCP):
// take the first pBuf address and process the chain
pBufChain = pBuf;
do
{
// process data…..
} while((pBufChain = pBufChain->next) != NULL);
If data can be spread over several buffers you may need copy and concatenate data inside the loop.
Do remember that you have pBufChain->len and pBuf->tot_len… the first is the chunk length and the
second is the total length of all the chained buffers, if any.
Once you are done processing the data you inform the TCP stack you are done:
// we need to free the pBuf
if(pBuf != NULL)
{
// Inform TCP that we have taken the data.
tcp_recved(pcb, pBuf->tot_len);
// free the buffer !
pbuf_free(pBuf);
pBuf = NULL;
}
If it is UDP you are not calling tcp_recved but data handling and freeing is similar.
Here are a few link that can give you more information and examples:
https://github.com/pabigot/lwip-contrib/tree/master/apps
Also take your time and read the rawapi.txt under the LwIP doc directory
Hope the above helped.
BR,
Noam.
From: lwip-users [mailto:lwip-users-bounces+noam=[hidden email]] On Behalf Of P?rvu Mihai
Sent: Tuesday, June 07, 2016 3:44 PM
To: Mailing list for lwIP users
Subject: Re: [lwip-users] TCP Raw API questions about efficiency and threads