lua-buffet

用于 Lua 的套接字式缓冲区对象

$ opm get un-def/lua-buffet

lua-buffet

[!Luarocks](https://luarocks.org/modules/undef/lua-buffet) [!OPM](https://opm.openresty.org.cn/package/un-def/lua-buffet/) [!构建状态](https://travis-ci.org/un-def/lua-buffet) [!许可证][license]

用于 Lua 的套接字式缓冲区对象

名称

“buffet” 这个词是 “buffer” 和 “socket” 的混合词。

描述

一个 buffet 是一个对象,它与流行的 Lua 库 LuaSocketLua Nginx 模块 中的套接字对象具有相同的接口,但它不进行任何实际的网络通信。相反,buffet 从任意来源接收数据的网络栈。数据源可以是字符串、字符串表或生成字符串的迭代器函数。

buffet 以流式方式工作。也就是说,buffet 不会尝试一次性读取和存储整个源数据,而是在必要时尽可能少地读取。这意味着 buffet 可以有效地用作无限量数据源(如 真实 套接字或文件 I/O 读取器)的代理。

另一个可能的用途是单元测试,其中 buffet 可以用作模拟对象,而不是真正的套接字对象。

基本用法

    local buffet = require('buffet')
    local buffet_resty = require('buffet.resty')
    
    -- Input data is a string.
    -- Read data in chunks of 3 bytes.
    do
        local bf = buffet_resty.new('data string')
        print(buffet.is_closed(bf))   -- false
        repeat
            local data, err, partial = bf:receive(3)
            print(data, err, partial)
        until err
        print(buffet.is_closed(bf))   -- true
    end
    
    -- Input data is a table containing data chunks.
    -- Read data line by line.
    do
        local bf = buffet_resty.new({'line 1\nline', ' 2\nli', 'ne 3\n'})
        repeat
            local data, err, partial = bf:receive('*l')
            print(data, err, partial)
        until err
    end
    
    -- Input data is a function producing data chunks.
    -- Read data splitted by the specified pattern, up to 4 bytes at once.
    do
        local iterator = coroutine.wrap(function()
            coroutine.yield('first-==-se')
            coroutine.yield('cond-==')
            coroutine.yield('-thi')
            coroutine.yield('rd')
            coroutine.yield(nil, 'some error')
            coroutine.yield('unreachable')
        end)
        local bf = buffet_resty.new(iterator)
        local reader = bf:receiveuntil('-==-')
        print(buffet.get_iterator_error(bf))   -- nil
        repeat
            local data, err, partial = reader(4)
            print(data, err, partial)
        until err
        print(buffet.get_iterator_error(bf))   -- some error
    end
    
    -- Send data.
    do
        local bf = buffet_resty.new()
        for i = 1, 5 do
            local char = string.char(0x40 + i)
            bf:send(string.rep(char, i))
        end
        local send_buffer = buffet.get_send_buffer(bf)
        print(#send_buffer)   -- 5
        for _, chunk in ipairs(send_buffer) do
            print(chunk)
        end
        print(buffet.get_sent_data(bf))   -- ABBCCCDDDDEEEEE
    end

有关更高级的用法,请参阅 示例 目录。

文档

文档可在 https://undef.im/lua-buffet/ 获取。

变更日志

有关详细的变更日志,请参阅 CHANGELOG.md

待办事项

OpenResty

ngx.socket.tcp

  • [x] 构造函数 (ngx.socket.tcp ~ buffet.resty.new)

  • [x] :connect (无操作)

  • [x] :sslhandshake (无操作)

  • [x] :send

  • [x] :receive

    • [x] :receive()

    • [x] :receive('*l')

    • [x] :receive('*a')

    • [x] :receive(size)

  • [x] :receiveany

  • [x] :receiveuntil

    • [x] iterator()

    • [x] iterator(size)

    • [x] inclusive 选项

  • [x] :close

  • [x] :settimeout (无操作)

  • [x] :settimeouts (无操作)

  • [x] :setoption (无操作)

  • [x] :setkeepalive (等同于 :close)

  • [x] :getreusedtimes (无操作)

ngx.socket.udp

...

LuaSocket

...

许可证

[MIT 许可证][license]。

[license]: https://github.com/un-def/lua-buffet/blob/master/LICENSE

作者

un.def

许可证

mit

版本