Software /
code /
prosody
Comparison
net/server_epoll.lua @ 8008:62ed3795be13
net.server_epoll: Attempt to simplify state control
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 27 Mar 2017 04:30:48 +0200 |
parent | 8007:67f8954875a4 |
child | 8091:acecf1413233 |
comparison
equal
deleted
inserted
replaced
8007:67f8954875a4 | 8008:62ed3795be13 |
---|---|
357 end | 357 end |
358 end | 358 end |
359 | 359 |
360 -- The write buffer has been successfully emptied | 360 -- The write buffer has been successfully emptied |
361 function interface:ondrain() | 361 function interface:ondrain() |
362 if self._toclose then | 362 return self:on("drain"); |
363 return self:close(); | |
364 elseif self._starttls then | |
365 return self:starttls(); | |
366 else | |
367 return self:on("drain"); | |
368 end | |
369 end | 363 end |
370 | 364 |
371 -- Add data to write buffer and set flag for wanting to write | 365 -- Add data to write buffer and set flag for wanting to write |
372 function interface:write(data) | 366 function interface:write(data) |
373 local buffer = self.writebuffer; | 367 local buffer = self.writebuffer; |
386 function interface:close() | 380 function interface:close() |
387 if self.writebuffer and self.writebuffer[1] then | 381 if self.writebuffer and self.writebuffer[1] then |
388 self:setflags(false, true); -- Flush final buffer contents | 382 self:setflags(false, true); -- Flush final buffer contents |
389 self.write, self.send = noop, noop; -- No more writing | 383 self.write, self.send = noop, noop; -- No more writing |
390 log("debug", "Close %s after writing", tostring(self)); | 384 log("debug", "Close %s after writing", tostring(self)); |
391 self._toclose = true; | 385 self.ondrain = interface.close; |
392 else | 386 else |
393 log("debug", "Close %s now", tostring(self)); | 387 log("debug", "Close %s now", tostring(self)); |
394 self.write, self.send = noop, noop; | 388 self.write, self.send = noop, noop; |
395 self.close = noop; | 389 self.close = noop; |
396 self:on("disconnect"); | 390 self:on("disconnect"); |
417 | 411 |
418 function interface:starttls(ctx) | 412 function interface:starttls(ctx) |
419 if ctx then self.tls = ctx; end | 413 if ctx then self.tls = ctx; end |
420 if self.writebuffer and self.writebuffer[1] then | 414 if self.writebuffer and self.writebuffer[1] then |
421 log("debug", "Start TLS on %s after write", tostring(self)); | 415 log("debug", "Start TLS on %s after write", tostring(self)); |
422 self._starttls = true; | 416 self.ondrain = interface.starttls; |
417 self.starttls = false; | |
423 self:setflags(nil, true); -- make sure wantwrite is set | 418 self:setflags(nil, true); -- make sure wantwrite is set |
424 else | 419 else |
425 log("debug", "Start TLS on %s now", tostring(self)); | 420 log("debug", "Start TLS on %s now", tostring(self)); |
426 self:setflags(false, false); | 421 self:setflags(false, false); |
427 local conn, err = luasec.wrap(self.conn, ctx or self.tls); | 422 local conn, err = luasec.wrap(self.conn, ctx or self.tls); |
430 self:destroy(); | 425 self:destroy(); |
431 return conn, err; | 426 return conn, err; |
432 end | 427 end |
433 conn:settimeout(0); | 428 conn:settimeout(0); |
434 self.conn = conn; | 429 self.conn = conn; |
435 self._starttls = nil; | 430 self.ondrain = nil; |
436 self.onwriteable = interface.tlshandskake; | 431 self.onwriteable = interface.tlshandskake; |
437 self.onreadable = interface.tlshandskake; | 432 self.onreadable = interface.tlshandskake; |
438 self:setflags(true, true); | 433 self:setflags(true, true); |
439 self:setwritetimeout(cfg.handshake_timeout); | 434 self:setwritetimeout(cfg.handshake_timeout); |
440 end | 435 end |
446 local ok, err = self.conn:dohandshake(); | 441 local ok, err = self.conn:dohandshake(); |
447 if ok then | 442 if ok then |
448 log("debug", "TLS handshake on %s complete", tostring(self)); | 443 log("debug", "TLS handshake on %s complete", tostring(self)); |
449 self.onwriteable = nil; | 444 self.onwriteable = nil; |
450 self.onreadable = nil; | 445 self.onreadable = nil; |
451 self:setflags(true, true); | |
452 local old = self._tls; | |
453 self._tls = true; | 446 self._tls = true; |
454 self.starttls = false; | 447 self:on("status", "ssl-handshake-complete"); |
455 if old == false then | 448 self:init(); |
456 self:init(); | |
457 else | |
458 self:setflags(true, true); | |
459 self:on("status", "ssl-handshake-complete"); | |
460 end | |
461 elseif err == "wantread" then | 449 elseif err == "wantread" then |
462 log("debug", "TLS handshake on %s to wait until readable", tostring(self)); | 450 log("debug", "TLS handshake on %s to wait until readable", tostring(self)); |
463 self:setflags(true, false); | 451 self:setflags(true, false); |
464 self:setreadtimeout(cfg.handshake_timeout); | 452 self:setreadtimeout(cfg.handshake_timeout); |
465 elseif err == "wantwrite" then | 453 elseif err == "wantwrite" then |
509 end | 497 end |
510 | 498 |
511 -- Initialization | 499 -- Initialization |
512 function interface:init() | 500 function interface:init() |
513 if self.tls and not self._tls then | 501 if self.tls and not self._tls then |
514 self._tls = false; -- This means we should call onconnect when TLS is up | |
515 return self:starttls(); | 502 return self:starttls(); |
516 else | 503 else |
517 self.onwriteable = interface.onconnect; | 504 self.onwriteable = interface.onconnect; |
518 self:setwritetimeout(); | 505 self:setwritetimeout(); |
519 return self:setflags(false, true); | 506 return self:setflags(false, true); |
544 end); | 531 end); |
545 end | 532 end |
546 | 533 |
547 -- Connected! | 534 -- Connected! |
548 function interface:onconnect() | 535 function interface:onconnect() |
536 if not self._connected then | |
537 self._connected = true; | |
538 self:on("connect"); | |
539 end | |
549 self.onwriteable = nil; | 540 self.onwriteable = nil; |
550 self:on("connect"); | 541 self:setflags(true, false); |
551 self:setflags(true); | |
552 return self:onwriteable(); | 542 return self:onwriteable(); |
553 end | 543 end |
554 | 544 |
555 local function addserver(addr, port, listeners, pattern, tls) | 545 local function addserver(addr, port, listeners, pattern, tls) |
556 local conn, err = socket.bind(addr, port, cfg.tcp_backlog); | 546 local conn, err = socket.bind(addr, port, cfg.tcp_backlog); |