模块 luv
require "luv" — libuv 异步事件循环绑定(后端 luv.dll / libluv.so,来自 luvit 的 luv)
local luv = require "luv"
print(luv.version_string()) -- "1.48.0"
事件循环
| API | 说明 | 返回 |
|---|---|---|
luv.run() | 运行默认事件循环直到无活动句柄 | bool |
luv.loop_alive() | 事件循环是否仍有活动引用 | bool |
luv.stop() | 停止事件循环 | - |
luv.now() | 当前时间戳(ms,自 loop 启动起) | num |
luv.walk(cb) | 遍历 loop 中全部句柄,回调 handle | - |
Timer / 句柄
| API | 说明 | 返回 |
|---|---|---|
luv.new_timer() | 新建定时器句柄 | timer |
timer:start(timeout, repeat, cb) | 启动:timeout 后首次触发,repeat 周期循环 | bool |
timer:stop() | 停止定时器 | bool |
timer:again() | 按原参数重启定时器 | bool |
handle:close([cb]) | 关闭句柄(所有 luv 句柄通用) | bool |
handle:is_active() | 句柄是否活动 | bool |
handle:is_closing() | 句柄是否关闭中 | bool |
handle:ref() / unref() | 引用/取消引用(控制 loop 存活) | - |
TCP / UDP / Pipe
| API | 说明 | 返回 |
|---|---|---|
luv.new_tcp() | 新建 TCP 句柄 | tcp |
tcp:bind(ip, port) | 绑定地址 | bool,err |
tcp:listen(backlog, cb) | 开始监听,回调接受新连接 | bool,err |
tcp:accept([client]) | 接受一个连接 | bool,err |
tcp:connect(ip, port, cb) | 连接远端 | bool,err |
stream:read_start(cb) | 开始读流(cb(err, data)) | bool,err |
stream:write(data[, cb]) | 写数据到流 | bool,err |
stream:read_stop() | 停止读流 | bool |
luv.new_udp() / udp:bind / send / recv_start | UDP 数据报收发 | … |
luv.new_pipe(ipc) | 命名管道 / 进程 IPC | pipe |
fs / 进程 / 线程
| API | 说明 | 返回 |
|---|---|---|
luv.fs_open(path, flags, mode) | 打开文件,返回 fd | fd,err |
luv.fs_read(fd, len, pos) | 读文件,返回 data | data,err |
luv.fs_write(fd, data, pos) | 写文件 | n,err |
luv.fs_close(fd) | 关闭文件 | bool,err |
luv.fs_stat / fs_unlink / fs_mkdir / fs_scandir | 文件系统操作 | … |
luv.spawn(options, cb) | spawn 子进程(options.file/args/stdio) | handle |
luv.new_thread(func, ...) | 启动新线程(函数在独立 Lua 状态运行) | thread |
luv.new_work(cb[, after]) | 线程池任务 | work |
luv.new_signal() / sig:start(kind, cb) | 信号处理 | signal |
其它
| API | 说明 | 返回 |
|---|---|---|
luv.version() / version_string() | libuv 版本号 / 版本字符串 | num / str |
luv.hrtime() | 高分辨率时间(ns) | num |
luv.uv_uptime() | 系统开机时长(秒) | num,err |
luv.os_homedir() / os_tmpdir() | 家目录 / 临时目录 | str,err |
luv.cwd() / chdir(path) | 当前工作目录 / 切换目录 | str / bool |
完整 API 与 luvit 官方 luv 一致:https://github.com/luvit/luv