aboutsummaryrefslogtreecommitdiffstats
path: root/net/server_select.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2018-05-09 16:15:40 +0200
committerKim Alvefur <zash@zash.se>2018-05-09 16:15:40 +0200
commita247edeac984c22ac4419eb03b5bbbdcd9a2206f (patch)
treeea76a040fa49fccc3bc0669effd781d0382c34cd /net/server_select.lua
parentd6ed959fd38c1f32aa41ba42db0061531b4703fe (diff)
downloadprosody-a247edeac984c22ac4419eb03b5bbbdcd9a2206f.tar.gz
prosody-a247edeac984c22ac4419eb03b5bbbdcd9a2206f.zip
net.server: Add watchfd, a simple API for watching file descriptors
Diffstat (limited to 'net/server_select.lua')
-rw-r--r--net/server_select.lua43
1 files changed, 43 insertions, 0 deletions
diff --git a/net/server_select.lua b/net/server_select.lua
index cfd08f37..3b83bb6d 100644
--- a/net/server_select.lua
+++ b/net/server_select.lua
@@ -1034,6 +1034,48 @@ local addclient = function( address, port, listeners, pattern, sslctx, typ )
end
end
+local closewatcher = function (handler)
+ local socket = handler.conn;
+ _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
+ _readlistlen = removesocket( _readlist, socket, _readlistlen )
+ _socketlist[ socket ] = nil
+end;
+
+local addremove = function (handler, read, send)
+ local socket = handler.conn
+ _socketlist[ socket ] = handler
+ if read ~= nil then
+ if read then
+ _readlistlen = addsocket( _readlist, socket, _readlistlen )
+ else
+ _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
+ end
+ end
+ if send ~= nil then
+ if send then
+ _sendlistlen = addsocket( _sendlist, socket, _sendlistlen )
+ else
+ _readlistlen = removesocket( _readlist, socket, _readlistlen )
+ end
+ end
+end
+
+local watchfd = function ( fd, onreadable, onwriteable )
+ local socket = fd
+ if type(fd) == "number" then
+ socket = { getfd = function () return fd; end }
+ end
+ local handler = {
+ conn = socket;
+ readbuffer = onreadable or id;
+ sendbuffer = onwriteable or id;
+ close = closewatcher;
+ setflags = addremove;
+ };
+ addremove( handler, onreadable, onwriteable )
+ return handler
+end
+
----------------------------------// BEGIN //--
use "setmetatable" ( _socketlist, { __mode = "k" } )
@@ -1058,6 +1100,7 @@ return {
addclient = addclient,
wrapclient = wrapclient,
+ watchfd = watchfd,
loop = loop,
link = link,