Changeset

8540:ba9fd886b34d

net.server_epoll: Call onconnect listener on first successful read or write
author Kim Alvefur <zash@zash.se>
date Sat, 24 Feb 2018 13:46:16 +0100
parents 8539:41b5f070d7bb
children 8541:8186114329d6
files net/server_epoll.lua
diffstat 1 files changed, 14 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/net/server_epoll.lua	Sat Feb 24 11:51:43 2018 +0100
+++ b/net/server_epoll.lua	Sat Feb 24 13:46:16 2018 +0100
@@ -306,9 +306,11 @@
 function interface:onreadable()
 	local data, err, partial = self.conn:receive(self._pattern);
 	if data then
+		self:onconnect();
 		self:on("incoming", data);
 	else
-		if partial then
+		if partial and partial ~= "" then
+			self:onconnect();
 			self:on("incoming", partial, err);
 		end
 		if err == "wantread" then
@@ -336,15 +338,17 @@
 	local data = t_concat(buffer);
 	local ok, err, partial = self.conn:send(data);
 	if ok then
-		for i = #buffer, 1, -1 do
-			buffer[i] = nil;
+		if data ~= "" then
+			for i = #buffer, 1, -1 do
+				buffer[i] = nil;
+			end
+			self:setflags(nil, false);
+			self:setwritetimeout(false);
+			self:ondrain(); -- Be aware of writes in ondrain
 		end
-		self:setflags(nil, false);
-		self:setwritetimeout(false);
-		self:ondrain(); -- Be aware of writes in ondrain
+		self:onconnect();
 		return;
-	end
-	if partial then
+	elseif partial then
 		buffer[1] = data:sub(partial+1);
 		for i = #buffer, 2, -1 do
 			buffer[i] = nil;
@@ -505,8 +509,6 @@
 	if self.tls and not self._tls then
 		return self:starttls();
 	else
-		self.onwriteable = interface.onfirstwritable;
-		self.onreadable = interface.onfirstreadable;
 		self:setwritetimeout();
 		return self:setflags(true, true);
 	end
@@ -538,25 +540,8 @@
 
 -- Connected!
 function interface:onconnect()
-	self:setflags(true, false);
-	if not self._connected then
-		self._connected = true;
-		self:on("connect");
-	end
-end
-
-function interface:onfirstwritable()
-	self.onreadable = nil;
-	self.onwriteable = nil;
-	self:onconnect();
-	return self:onwriteable();
-end
-
-function interface:onfirstreadable()
-	self.onreadable = nil;
-	self.onwriteable = nil;
-	self:onconnect();
-	return self:onreadable();
+	self.onconnect = noop;
+	self:on("connect");
 end
 
 local function addserver(addr, port, listeners, pattern, tls)