[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: flawed LuaSocket select()/tcp.receive() interaction ?
- From: lua@...
- Date: Mon, 20 Nov 2006 11:08:43 +0100
I run select() on a connected tcp socket. Variable-length packets are received
on this socket. When data comes in, I want to read all data that came in over
the socket - no more, no less - but I can't figure out how to do this using
I can call receive with a size as pattern. This will cause receive to read
exactly that amount of bytes and return. It will not return before exactly this
amount of bytes is received, though. But something is flawed here:
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.
- 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. 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
- 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 read.
- also return when fewer then the requested amount of bytes are
received, returning both the data and it's size