How to implement receive(size) luasocket style in luv?

40 views
Skip to first unread message

kohlt...@gmail.com

unread,
Dec 10, 2021, 2:31:20 AM12/10/21
to luvit
I hope this list is still alive since the IRC channel does no longer exist after the demise of freenode.

I am trying to reduce dependencies in some of my embedded projects to free up space and reduce update troubles. Therefore I went on replacing luasocket by luv and made my projects coroutine based.

However, I am unsure how I would "replace" the receive functionality of luasocket which accepts a size argument and returns x amount of bytes. My idea was using a local variable I would append to until the desired length. However since I do not have control over how many bytes every execution receives I would end up with to much at once.

Is this really the best way to do it?

local receiver  = function(socket, size)
    local result = {length = 0} -- appending to a string multiple times does not sound like a very smart idea
    socket:read_start(function(err, data)
        if err then end
        if data then
            result[#result + 1] = data
            result.length = result.length + #data
        end
        if result.length >= then
            -- concat result table, empty result table, snip excess string and put it back into result table
    end)
end

Thank you,
Tristan
Reply all
Reply to author
Forward
0 new messages