lua-users home
lua-l archive

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


On Mon, Nov 10, 2008 at 2:05 AM, Jerome Vuarand
<jerome.vuarand@gmail.com> wrote:
> I think you can simply use the "*a" pattern to do what you asked for.

Great idea, thanks! That fixed size was really annoying me.

> It works just like the code you proposed, except it's not limited to a
> fixed amount of bytes and it properly handles the prefix parameter
> (which you don't). For example:

I wasn't too worried about that. I'm not really sure what the use case
for that parameter is, generally, but I particularly can't see why
you'd want a prefix inserted into essentially random spots in a TCP
stream!

> client:settimeout(0)
>
> function receive(socket, pattern, prefix)
>  if string.match(pattern, "^%*f") then
>    local data, emsg, partial = socket:receive("*a", prefix)
>    if  not data and emsg=='timeout' then
>      return partial

Note the above might not work as you expect. It certainly didn't work
as I expected. Partial is always a string, even when there is no data.

I think thats borderline buggy of luasocket, but that's why my example
is careful to check:

  if not data and emsg=="timeout" and partial~="" then
     return partial
  end



Btw, do these examples seem weird?

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> s=socket.connect("localhost", 80)
> s:settimeout(0)
> = s:receive(0)
nil     timeout
>

 % lua -l socket
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
>  s=socket.connect("localhost", 80)
> = s:receive(0)
.... blocks forever....

What is it waiting for? Either return a zero-length string, or return
an argument error.

Cheers,
Sam