|
From: | David Empson |
Subject: | Re: [lwip-users] function recv |
Date: | Wed, 16 Sep 2009 18:30:46 +1200 |
Whether or not you do a blocking lwip_recv(), the
amount of data returned can be any number of bytes up to the number you
specified.
There is no option to make lwip_recv() block until
a certain minimum number of bytes have been received. You cannot control the pattern of receive data, as
it is affected by many factors beyond your control. You should never
assume that data will be received in the same sized chunks as were
written by the sender.
Your application must be able to deal with
receiving only a few of the bytes you need, storing them in your own
buffer, then calling lwip_recv() again to get the next chunk, and repeating
until you have the number of bytes you want.
For the protocol as you describe it, I suggest
you set up an 8 byte buffer to receive the length and command code, and do
repeated lwip_recv() calls as required to fill that buffer, initially asking for
8 bytes, then if the initial call returns fewer than 8 bytes (and no error),
repeat as required, asking for the remaining bytes and adjusting the memory
pointer to allow for the bytes already received. Once you have the full 8 bytes,
you can interpret the length and command fields, and decide what to do about the
data field which follows.
If you can't cope with 4 gigabytes of data at once,
you will have to do sanity checks on the received length field, and for
acceptable but large lengths you may need to use a relatively small buffer and
process the data in chunks, rather than trying to receive all the data into a
single contiguous buffer. You will also have to make repeated lwip_recv() calls
to receive all of the data (up to the total number of bytes specified in your
protocol's length field).
Error checking will also be required, e.g. to
detect premature closure of the connection.
----- Original Message -----
|
[Prev in Thread] | Current Thread | [Next in Thread] |