Comparison

net/server_epoll.lua @ 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
parent 8507:71ef6d509105
child 8541:8186114329d6
comparison
equal deleted inserted replaced
8539:41b5f070d7bb 8540:ba9fd886b34d
304 304
305 -- Called when socket is readable 305 -- Called when socket is readable
306 function interface:onreadable() 306 function interface:onreadable()
307 local data, err, partial = self.conn:receive(self._pattern); 307 local data, err, partial = self.conn:receive(self._pattern);
308 if data then 308 if data then
309 self:onconnect();
309 self:on("incoming", data); 310 self:on("incoming", data);
310 else 311 else
311 if partial then 312 if partial and partial ~= "" then
313 self:onconnect();
312 self:on("incoming", partial, err); 314 self:on("incoming", partial, err);
313 end 315 end
314 if err == "wantread" then 316 if err == "wantread" then
315 self:setflags(true, nil); 317 self:setflags(true, nil);
316 elseif err == "wantwrite" then 318 elseif err == "wantwrite" then
334 function interface:onwriteable() 336 function interface:onwriteable()
335 local buffer = self.writebuffer; 337 local buffer = self.writebuffer;
336 local data = t_concat(buffer); 338 local data = t_concat(buffer);
337 local ok, err, partial = self.conn:send(data); 339 local ok, err, partial = self.conn:send(data);
338 if ok then 340 if ok then
339 for i = #buffer, 1, -1 do 341 if data ~= "" then
340 buffer[i] = nil; 342 for i = #buffer, 1, -1 do
341 end 343 buffer[i] = nil;
342 self:setflags(nil, false); 344 end
343 self:setwritetimeout(false); 345 self:setflags(nil, false);
344 self:ondrain(); -- Be aware of writes in ondrain 346 self:setwritetimeout(false);
347 self:ondrain(); -- Be aware of writes in ondrain
348 end
349 self:onconnect();
345 return; 350 return;
346 end 351 elseif partial then
347 if partial then
348 buffer[1] = data:sub(partial+1); 352 buffer[1] = data:sub(partial+1);
349 for i = #buffer, 2, -1 do 353 for i = #buffer, 2, -1 do
350 buffer[i] = nil; 354 buffer[i] = nil;
351 end 355 end
352 self:setwritetimeout(); 356 self:setwritetimeout();
503 -- Initialization 507 -- Initialization
504 function interface:init() 508 function interface:init()
505 if self.tls and not self._tls then 509 if self.tls and not self._tls then
506 return self:starttls(); 510 return self:starttls();
507 else 511 else
508 self.onwriteable = interface.onfirstwritable;
509 self.onreadable = interface.onfirstreadable;
510 self:setwritetimeout(); 512 self:setwritetimeout();
511 return self:setflags(true, true); 513 return self:setflags(true, true);
512 end 514 end
513 end 515 end
514 516
536 end); 538 end);
537 end 539 end
538 540
539 -- Connected! 541 -- Connected!
540 function interface:onconnect() 542 function interface:onconnect()
541 self:setflags(true, false); 543 self.onconnect = noop;
542 if not self._connected then 544 self:on("connect");
543 self._connected = true;
544 self:on("connect");
545 end
546 end
547
548 function interface:onfirstwritable()
549 self.onreadable = nil;
550 self.onwriteable = nil;
551 self:onconnect();
552 return self:onwriteable();
553 end
554
555 function interface:onfirstreadable()
556 self.onreadable = nil;
557 self.onwriteable = nil;
558 self:onconnect();
559 return self:onreadable();
560 end 545 end
561 546
562 local function addserver(addr, port, listeners, pattern, tls) 547 local function addserver(addr, port, listeners, pattern, tls)
563 local conn, err = socket.bind(addr, port, cfg.tcp_backlog); 548 local conn, err = socket.bind(addr, port, cfg.tcp_backlog);
564 if not conn then return conn, err; end 549 if not conn then return conn, err; end