diff options
author | Kim Alvefur <zash@zash.se> | 2018-05-09 16:15:40 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2018-05-09 16:15:40 +0200 |
commit | a247edeac984c22ac4419eb03b5bbbdcd9a2206f (patch) | |
tree | ea76a040fa49fccc3bc0669effd781d0382c34cd /net/server_epoll.lua | |
parent | d6ed959fd38c1f32aa41ba42db0061531b4703fe (diff) | |
download | prosody-a247edeac984c22ac4419eb03b5bbbdcd9a2206f.tar.gz prosody-a247edeac984c22ac4419eb03b5bbbdcd9a2206f.zip |
net.server: Add watchfd, a simple API for watching file descriptors
Diffstat (limited to 'net/server_epoll.lua')
-rw-r--r-- | net/server_epoll.lua | 21 |
1 files changed, 21 insertions, 0 deletions
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); |