lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

It's by design (I swear). Here is the orignal code in the
report:

    require "socket"

    addr = "127.0.0.1"
    port = 10000

    s = assert(socket.connect(addr, port))
    print("connected to peer: ", s:getpeername())

    -- sending "12345\n"
    res = assert(s:receive(2))
    print(">>res:", res) --> 12

    --res = assert(s:receive("*l", res))
    --print(">>res:", res) --> 12345
    res = assert(s:receive(2, res))
    print(">>res:", res) --> 12 <--------------- BUG

The designed use for the optional prefix argument to the
receive() function is so you can pass partial results back
to LuaSocket when you retry a non-blocking I/O operation
that failed to complete due to timeout (and which returned
partial results).

If the original request was for, say, 10 bytes, but the
failed receive() call returned only 3 bytes, you would pass
the 3 bytes as the optional argument and ask for 10 bytes
again. This would try to read the remaining 7 (*not* another
10 bytes) and concatenate with the 3 you already have before
returning. In other words, it would try to complete the
previous request. Doing this in a loop becomes very
convenient.

In the example above, since

    res = assert(s:receive(2, res))
    print(">>res:", res) --> 12 <--------------- not really a BUG

is only asking for 2 bytes, and 'res' contains 2 bytes
already, the call simply returns those two bytes. If you try
the call

    res = assert(s:receive(5, res))
    print(">>res:", res) --> 12345 <--------------- not really a BUG

asking for 5 bytes, you will see that the concatenation is
working.

As for the line pattern, it will only stop reading when it
gets the end-of-line. One could argue that LuaSocket should
look within the optional prefix to see if there is a line
break, but this can't happen if it came from a previous
partial result of the same pattern.

Best regards,
Diego.