From a247edeac984c22ac4419eb03b5bbbdcd9a2206f Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 9 May 2018 16:15:40 +0200 Subject: net.server: Add watchfd, a simple API for watching file descriptors --- net/server_epoll.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'net/server_epoll.lua') diff --git a/net/server_epoll.lua b/net/server_epoll.lua index 564444f8..0881f797 100644 --- a/net/server_epoll.lua +++ b/net/server_epoll.lua @@ -15,6 +15,7 @@ local t_concat = table.concat; local setmetatable = setmetatable; local tostring = tostring; local pcall = pcall; +local type = type; local next = next; local pairs = pairs; local log = require "util.logger".init("server_epoll"); @@ -586,6 +587,25 @@ local function addclient(addr, port, listeners, pattern, tls) return client, conn; end +local function watchfd(fd, onreadable, onwriteable) + local conn = setmetatable({ + conn = fd; + onreadable = onreadable; + onwriteable = onwriteable; + close = function (self) + self:setflags(false, false); + end + }, interface_mt); + if type(fd) == "number" then + conn.getfd = function () + return fd; + end; + -- Otherwise it'll need to be something LuaSocket-compatible + end + conn:setflags(onreadable, onwriteable); + return conn; +end; + -- Dump all data from one connection into another local function link(from, to) from.listeners = setmetatable({ @@ -663,6 +683,7 @@ return { closeall = closeall; setquitting = setquitting; wrapclient = wrapclient; + watchfd = watchfd; link = link; set_config = function (newconfig) cfg = setmetatable(newconfig, default_config); -- cgit v1.2.3