[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: flawed LuaSocket select()/tcp.receive() interaction ?
- From: Diego Nehab <diego@...>
- Date: Mon, 20 Nov 2006 12:52:38 -0500 (EST)
First, here is a good start
Assume I call tcp.receive() with lenght '8', and
- Fewer then 8 bytes come in. tcp.receive() does not return, and blocks
until more data arrives. This is not acceptable for me: I should be
able to read all available data when select() indicates something's
there, and continue right away.
This is also the behavior of the C version of recv on
blocking sockets. To get a different behavior, you must put
the socket in non-blocking mode.
- more then 8 bytes of data come in. tcp.receive() does not pass my
length argument to the libc receive() function, but reads a whole
buffer at a time (8192 bytes, in my case). tcp.receive() only returns
the first 8 bytes, as requested. The rest of the data was already
reveied and is stored somewhere by luasocket in a buffer.
Also in C, the fact you pass 8 to recv() doesn't mean the
data isn't waiting for you in an OS buffer. In fact, it
probably is. All LuaSocket does is add an extra layer of
buffering there, so that reading line patters becomes
Here's the flaw: select() will not indicate more data is available, so my
application will not know it has to call tcp.receive() to get the
So, what do I need to do to handle this properly ? Is
there a way to make tcp.receive() have the same behaviour
as my libc's receive(), that is
- don't receive any more then the requested amount of
bytes from the socket, and leave the rest pending so
select() will notice there's still data waiting to be
Yes, it will. There is code there to specifically handle
this case. If you have a sample that doesn't work, this is a
bug and it must be fixed. Are you just assuming that it
- also return when fewer then the requested amount of bytes are
received, returning both the data and it's size
This is not the standard behavior in C either. You have to
put the socket in non-blocking mode. To do that in
LuaSocket, simply set the timeout to 0.