net/server_epoll.lua
changeset 7640 cccea9136b2d
parent 7633 abe2697b5e92
child 7660 252823632401
--- a/net/server_epoll.lua	Fri Aug 26 16:57:53 2016 +0200
+++ b/net/server_epoll.lua	Sat Aug 27 18:39:23 2016 +0200
@@ -38,6 +38,9 @@
 };
 
 local fds = createtable(10, 0); -- FD -> conn
+
+-- Timer and scheduling --
+
 local timers = {};
 
 local function noop() end
@@ -92,7 +95,7 @@
 		end
 		local new_timeout = f(now);
 		if new_timeout then
-			-- Schedlue for 'delay' from the time actually sheduled,
+			-- Schedule for 'delay' from the time actually scheduled,
 			-- not from now, in order to prevent timer drift.
 			timer[1] = t + new_timeout;
 			resort_timers = true;
@@ -111,6 +114,8 @@
 	return next_delay;
 end
 
+-- Socket handler interface
+
 local interface = {};
 local interface_mt = { __index = interface };
 
@@ -123,12 +128,13 @@
 	return ("%s FD %d"):format(tostring(self.conn), self:getfd());
 end
 
+-- Replace the listener and tell the old one
 function interface:setlistener(listeners)
 	self:on("detach");
 	self.listeners = listeners;
 end
 
--- Call callback
+-- Call a listener callback
 function interface:on(what, ...)
 	local listener = self.listeners["on"..what];
 	if not listener then
@@ -142,6 +148,7 @@
 	return err;
 end
 
+-- Return the file descriptor number
 function interface:getfd()
 	if self.conn then
 		return self.conn:getfd();
@@ -149,18 +156,22 @@
 	return -1;
 end
 
+-- Get IP address
 function interface:ip()
 	return self.peername or self.sockname;
 end
 
+-- Get a port number, doesn't matter which
 function interface:port()
 	return self.sockport or self.peerport;
 end
 
+-- Get local port number
 function interface:clientport()
 	return self.sockport;
 end
 
+-- Get remote port
 function interface:serverport()
 	if self.sockport then
 		return self.sockport;
@@ -169,6 +180,7 @@
 	end
 end
 
+-- Return underlying socket
 function interface:socket()
 	return self.conn;
 end
@@ -180,6 +192,7 @@
 	end
 end
 
+-- Timeout for detecting dead or idle sockets
 function interface:setreadtimeout(t)
 	if t == false then
 		if self._readtimeout then
@@ -204,6 +217,7 @@
 	end
 end
 
+-- Timeout for detecting dead sockets
 function interface:setwritetimeout(t)
 	if t == false then
 		if self._writetimeout then
@@ -224,6 +238,7 @@
 	end
 end
 
+-- lua-epoll flag for currently requested poll state
 function interface:flags()
 	if self._wantread then
 		if self._wantwrite then
@@ -235,6 +250,7 @@
 	end
 end
 
+-- Add or remove sockets or modify epoll flags
 function interface:setflags(r, w)
 	if r ~= nil then self._wantread = r; end
 	if w ~= nil then self._wantwrite = w; end
@@ -320,6 +336,7 @@
 	end
 end
 
+-- The write buffer has been successfully emptied
 function interface:ondrain()
 	if self._toclose then
 		return self:close();
@@ -330,6 +347,7 @@
 	end
 end
 
+-- Add data to write buffer and set flag for wanting to write
 function interface:write(data)
 	local buffer = self.writebuffer;
 	if buffer then
@@ -343,6 +361,7 @@
 end
 interface.send = interface.write;
 
+-- Close, possibly after writing is done
 function interface:close()
 	if self._wantwrite then
 		self:setflags(false, true); -- Flush final buffer contents
@@ -454,6 +473,8 @@
 	return conn;
 end
 
+-- A server interface has new incoming connections waiting
+-- This replaces the onreadable callback
 function interface:onacceptable()
 	local conn, err = self.conn:accept();
 	if not conn then
@@ -466,6 +487,7 @@
 	client:init();
 end
 
+-- Initialization
 function interface:init()
 	if self.tls and not self._tls then
 		self._tls = false; -- This means we should call onconnect when TLS is up
@@ -501,6 +523,7 @@
 	end);
 end
 
+-- Connected!
 function interface:onconnect()
 	self.onwriteable = nil;
 	self:on("connect");
@@ -570,10 +593,12 @@
 
 local quitting = nil;
 
+-- Signal main loop about shutdown via above upvalue
 local function setquitting()
 	quitting = "quitting";
 end
 
+-- Main loop
 local function loop()
 	repeat
 		local t = runtimers(cfg.max_wait);