Diff

net/server_epoll.lua @ 8781:53178b6ba589

net.server: Add watchfd, a simple API for watching file descriptors
author Kim Alvefur <zash@zash.se>
date Wed, 09 May 2018 16:15:40 +0200
parent 8778:0364454bdd77
child 8983:23f66f04e216
line wrap: on
line diff
--- a/net/server_epoll.lua	Mon May 07 22:10:29 2018 +0200
+++ b/net/server_epoll.lua	Wed May 09 16:15:40 2018 +0200
@@ -15,6 +15,7 @@
 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 @@
 	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 @@
 	closeall = closeall;
 	setquitting = setquitting;
 	wrapclient = wrapclient;
+	watchfd = watchfd;
 	link = link;
 	set_config = function (newconfig)
 		cfg = setmetatable(newconfig, default_config);