Diff

net/server_epoll.lua @ 7597:cd34aa6b1632

net.server_epoll: Refactor to use common initialization
author Kim Alvefur <zash@zash.se>
date Thu, 18 Aug 2016 16:59:31 +0200
parent 7596:2c4ba55b616c
child 7598:335b5ce06062
line wrap: on
line diff
--- a/net/server_epoll.lua	Thu Aug 18 17:04:57 2016 +0200
+++ b/net/server_epoll.lua	Thu Aug 18 16:59:31 2016 +0200
@@ -453,13 +453,17 @@
 	end
 	local client = wrapsocket(conn, self, nil, self.listeners, self.tls);
 	log("debug", "New connection %s", tostring(client));
-	if self.tls then
-		client._tls = false;
-		client:starttls();
+	client:init();
+end
+
+function interface:init()
+	if self.tls and not self._tls then
+		self._tls = false; -- This means we should call onconnect when TLS is up
+		return self:starttls();
 	else
-		client:setflags(false, true);
+		self:setflags(false, true);
 	end
-	client:setreadtimeout();
+	self:setreadtimeout();
 end
 
 function interface:pause()
@@ -513,18 +517,11 @@
 
 -- COMPAT
 local function wrapclient(conn, addr, port, listeners, pattern, tls)
-	local client = setmetatable({
-		conn = conn;
-		created = gettime();
-		listeners = listeners;
-		_pattern = pattern;
-		writebuffer = {};
-		tls = tls;
-		onwriteable = interface.onconnect;
-		peer = { addr, port };
-	}, interface_mt);
-	fds[client:getfd()] = client;
-	client:setflags(false, true);
+	local client = wrapsocket(conn, nil, pattern, listeners, tls);
+	if not client.peername then
+		client.peername, client.peerport = addr, port;
+	end
+	client:init();
 	return client;
 end
 
@@ -533,13 +530,8 @@
 	if not conn then return conn, err; end
 	conn:settimeout(0);
 	conn:connect(addr, port);
-	local client = wrapclient(conn, addr, port, listeners, pattern, tls);
-	if tls then
-		client._tls = false;
-		client:starttls();
-	else
-		client:setflags(true, true);
-	end
+	local client = wrapsocket(conn, nil, pattern, listeners, tls)
+	client:init();
 	return client, conn;
 end