Diff

net/server_event.lua @ 2434:4403d4473783

net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
author Matthew Wild <mwild1@gmail.com>
date Sun, 10 Jan 2010 15:25:19 +0000
parent 2375:31297a5641e3
child 2453:44d4c69155e0
line wrap: on
line diff
--- a/net/server_event.lua	Sun Jan 10 14:09:17 2010 +0000
+++ b/net/server_event.lua	Sun Jan 10 15:25:19 2010 +0000
@@ -13,7 +13,6 @@
 
 --]]
 
-
 local SCRIPT_NAME           = "server_event.lua"
 local SCRIPT_VERSION        = "0.05"
 local SCRIPT_AUTHOR         = "blastbeat"
@@ -77,6 +76,7 @@
 local EV_READ = event.EV_READ
 local EV_WRITE = event.EV_WRITE
 local EV_TIMEOUT = event.EV_TIMEOUT
+local EV_SIGNAL = event.EV_SIGNAL
 
 local EV_READWRITE = bitor( EV_READ, EV_WRITE )
 
@@ -786,6 +786,21 @@
 	return base:method();
 end
 
+-- We need to hold onto the events to stop them
+-- being garbage-collected
+local signal_events = {}; -- [signal_num] -> event object
+function hook_signal(signal_num, handler)
+	local function _handler(event)
+		local ret = handler();
+		if ret ~= false then -- Continue handling this signal?
+			return EV_SIGNAL; -- Yes
+		end
+		return -1; -- Close this event
+	end
+	signal_events[signal_num] = base:addevent(signal_num, EV_SIGNAL, _handler);
+	return signal_events[signal_num];
+end
+
 return {
 
 	cfg = cfg,
@@ -800,6 +815,7 @@
 	setquitting = setquitting,
 	closeall = closeallservers,
 	get_backend = get_backend,
+	hook_signal = hook_signal,
 
 	__NAME = SCRIPT_NAME,
 	__DATE = LAST_MODIFIED,