Quantcast

luasocket, howto read/write Non-blocking TPC socket

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

luasocket, howto read/write Non-blocking TPC socket

Axel Kittenberger
Hello, I didn't stumble on a good example code, so I'm checking that
way, as far I understood the luasocket documentation.
This code wants to receive whole line and send arbitrary data in
nonblocking mode. I just want to ask, since in normal operation
this things are harder to test, as the buffers are usually
written/read in one sweep.

(Suppose socket is opened and set to nonblock via socket:settimeout(0))

-- This function is called by select when ready to read.
local rBuf = ""
local function canRead(socket)
    local l, err, rBuf = socket:receive("*l", rBuf)
    if not l then
        if err ~= "timeout" then
            print("connection failed: ", err)
            os.exit(-1)
        end
    else
        rBuf = ""
    end
    print("Received Line:" l)
end

(Is that a correct way of doing it?)
And for writing I came up with this:
Function gets also called with no arguments if the buffer is non empty
and the socket
selected for being writeable.

local wBuf = ""
function write(...)
    wBuf = wBuf..table.concat({...})
    local s, err = socket:send(wBuf)
    if not s and err~="timeout" then
        print("connection failed: ", err)
        os.exit(-1)
    end
    wBuf = wBuf:sub(s + 1, -1)
end

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: luasocket, howto read/write Non-blocking TPC socket

Jon Smirl
On Wed, Dec 1, 2010 at 9:23 AM, Axel Kittenberger <[hidden email]> wrote:
> Hello, I didn't stumble on a good example code, so I'm checking that

Prosody is an example of how to do this...
http://prosody.im/

> way, as far I understood the luasocket documentation.
> This code wants to receive whole line and send arbitrary data in
> nonblocking mode. I just want to ask, since in normal operation
> this things are harder to test, as the buffers are usually
> written/read in one sweep.
>
> (Suppose socket is opened and set to nonblock via socket:settimeout(0))
>
> -- This function is called by select when ready to read.
> local rBuf = ""
> local function canRead(socket)
>    local l, err, rBuf = socket:receive("*l", rBuf)
>    if not l then
>        if err ~= "timeout" then
>            print("connection failed: ", err)
>            os.exit(-1)
>        end
>    else
>        rBuf = ""
>    end
>    print("Received Line:" l)
> end
>
> (Is that a correct way of doing it?)
> And for writing I came up with this:
> Function gets also called with no arguments if the buffer is non empty
> and the socket
> selected for being writeable.
>
> local wBuf = ""
> function write(...)
>    wBuf = wBuf..table.concat({...})
>    local s, err = socket:send(wBuf)
>    if not s and err~="timeout" then
>        print("connection failed: ", err)
>        os.exit(-1)
>    end
>    wBuf = wBuf:sub(s + 1, -1)
> end
>
>



--
Jon Smirl
[hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: luasocket, howto read/write Non-blocking TPC socket

Valerio
In reply to this post by Axel Kittenberger
Hello Axel,

have you seen this example:

http://www.lua.org/pil/9.4.html ?

On Wed, Dec 1, 2010 at 3:23 PM, Axel Kittenberger <[hidden email]> wrote:
Hello, I didn't stumble on a good example code, so I'm checking that
way, as far I understood the luasocket documentation.
This code wants to receive whole line and send arbitrary data in
nonblocking mode. I just want to ask, since in normal operation
this things are harder to test, as the buffers are usually
written/read in one sweep.

(Suppose socket is opened and set to nonblock via socket:settimeout(0))

-- This function is called by select when ready to read.
local rBuf = ""
local function canRead(socket)
   local l, err, rBuf = socket:receive("*l", rBuf)
   if not l then
       if err ~= "timeout" then
           print("connection failed: ", err)
           os.exit(-1)
       end
   else
       rBuf = ""
   end
   print("Received Line:" l)
end

(Is that a correct way of doing it?)
And for writing I came up with this:
Function gets also called with no arguments if the buffer is non empty
and the socket
selected for being writeable.

local wBuf = ""
function write(...)
   wBuf = wBuf..table.concat({...})
   local s, err = socket:send(wBuf)
   if not s and err~="timeout" then
       print("connection failed: ", err)
       os.exit(-1)
   end
   wBuf = wBuf:sub(s + 1, -1)
end


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: luasocket, howto read/write Non-blocking TPC socket

Axel Kittenberger
In my humble opinion this example is no longer correct with current luasocket.
In case of timeout it now returns 3 values:
nil, "timeout", partialread

- Axel

On Thu, Dec 2, 2010 at 7:37 PM, Valerio Schiavoni
<[hidden email]> wrote:

> Hello Axel,
> have you seen this example:
> http://www.lua.org/pil/9.4.html ?
>
> On Wed, Dec 1, 2010 at 3:23 PM, Axel Kittenberger <[hidden email]> wrote:
>>
>> Hello, I didn't stumble on a good example code, so I'm checking that
>> way, as far I understood the luasocket documentation.
>> This code wants to receive whole line and send arbitrary data in
>> nonblocking mode. I just want to ask, since in normal operation
>> this things are harder to test, as the buffers are usually
>> written/read in one sweep.
>>
>> (Suppose socket is opened and set to nonblock via socket:settimeout(0))
>>
>> -- This function is called by select when ready to read.
>> local rBuf = ""
>> local function canRead(socket)
>>    local l, err, rBuf = socket:receive("*l", rBuf)
>>    if not l then
>>        if err ~= "timeout" then
>>            print("connection failed: ", err)
>>            os.exit(-1)
>>        end
>>    else
>>        rBuf = ""
>>    end
>>    print("Received Line:" l)
>> end
>>
>> (Is that a correct way of doing it?)
>> And for writing I came up with this:
>> Function gets also called with no arguments if the buffer is non empty
>> and the socket
>> selected for being writeable.
>>
>> local wBuf = ""
>> function write(...)
>>    wBuf = wBuf..table.concat({...})
>>    local s, err = socket:send(wBuf)
>>    if not s and err~="timeout" then
>>        print("connection failed: ", err)
>>        os.exit(-1)
>>    end
>>    wBuf = wBuf:sub(s + 1, -1)
>> end
>>
>
>

Loading...