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 | 7fa2aa0007e5bdae55ff0ced57ba193154302312 (patch) | |
tree | ea76a040fa49fccc3bc0669effd781d0382c34cd /net/server_event.lua | |
parent | 814f998b90f4b214f7b89f3d8d1d0032f9a2e78a (diff) | |
download | prosody-7fa2aa0007e5bdae55ff0ced57ba193154302312.tar.gz prosody-7fa2aa0007e5bdae55ff0ced57ba193154302312.zip |
net.server: Add watchfd, a simple API for watching file descriptors
Diffstat (limited to 'net/server_event.lua')
-rw-r--r-- | net/server_event.lua | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/net/server_event.lua b/net/server_event.lua index d40b388f..3e949092 100644 --- a/net/server_event.lua +++ b/net/server_event.lua @@ -834,6 +834,34 @@ local function add_task(delay, callback) return event_handle; end +local function watchfd(fd, onreadable, onwriteable) + local handle = {}; + function handle:setflags(r,w) + if r ~= nil then + if r and not self.wantread then + self.wantread = base:addevent(fd, EV_READ, function () + onreadable(self); + end); + elseif not r and self.wantread then + self.wantread:close(); + self.wantread = nil; + end + end + if w ~= nil then + if w and not self.wantwrite then + self.wantwrite = base:addevent(fd, EV_WRITE, function () + onwriteable(self); + end); + elseif not r and self.wantread then + self.wantwrite:close(); + self.wantwrite = nil; + end + end + end + handle:setflags(onreadable, onwriteable); + return handle; +end + return { cfg = cfg, base = base, @@ -850,6 +878,7 @@ return { get_backend = get_backend, hook_signal = hook_signal, add_task = add_task, + watchfd = watchfd, __NAME = SCRIPT_NAME, __DATE = LAST_MODIFIED, |