Comparison

net/server_epoll.lua @ 11752:6427e2642976

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Tue, 31 Aug 2021 13:03:44 +0200
parent 11743:856dada099fa
parent 11751:a8c9a78c6a4b
child 11761:dbf378dcf27b
comparison
equal deleted inserted replaced
11750:a8760562a096 11752:6427e2642976
153 153
154 peek = timers:peek(); 154 peek = timers:peek();
155 end 155 end
156 156
157 if readd then 157 if readd then
158 for _, timer in pairs(readd) do 158 for id, timer in pairs(readd) do
159 timers:insert(timer[1], timer[2]); 159 timers:insert(timer[1], timer[2], id);
160 end 160 end
161 peek = timers:peek(); 161 peek = timers:peek();
162 end 162 end
163 163
164 if peek == nil then 164 if peek == nil then
487 local ok, err, partial = self.conn:send(data); 487 local ok, err, partial = self.conn:send(data);
488 self._writable = ok; 488 self._writable = ok;
489 if ok then 489 if ok then
490 self:set(nil, false); 490 self:set(nil, false);
491 if cfg.keep_buffers and type(buffer) == "table" then 491 if cfg.keep_buffers and type(buffer) == "table" then
492 for i = #buffer, 1, -1 do 492 for i = #buffer, 1, -1 do
493 buffer[i] = nil; 493 buffer[i] = nil;
494 end 494 end
495 else 495 else
496 self.writebuffer = nil; 496 self.writebuffer = nil;
497 end 497 end
498 self:setwritetimeout(false); 498 self:setwritetimeout(false);
499 self:ondrain(); -- Be aware of writes in ondrain 499 self:ondrain(); -- Be aware of writes in ondrain
500 return ok; 500 return ok;
501 elseif partial then 501 elseif partial then
502 self:debug("Sent %d out of %d buffered bytes", partial, #data); 502 self:debug("Sent %d out of %d buffered bytes", partial, #data);
503 if cfg.keep_buffers and type(buffer) == "table" then 503 if cfg.keep_buffers and type(buffer) == "table" then
504 buffer[1] = data:sub(partial+1); 504 buffer[1] = data:sub(partial+1);
505 for i = #buffer, 2, -1 do 505 for i = #buffer, 2, -1 do
506 buffer[i] = nil; 506 buffer[i] = nil;
507 end 507 end
508 else 508 else
509 self.writebuffer = data:sub(partial+1); 509 self.writebuffer = data:sub(partial+1);
510 end 510 end
511 self:set(nil, true); 511 self:set(nil, true);
512 self:setwritetimeout(); 512 self:setwritetimeout();
544 if self._writable and cfg.opportunistic_writes and not self._opportunistic_write then 544 if self._writable and cfg.opportunistic_writes and not self._opportunistic_write then
545 self._opportunistic_write = true; 545 self._opportunistic_write = true;
546 local ret, err = self:onwritable(); 546 local ret, err = self:onwritable();
547 self._opportunistic_write = nil; 547 self._opportunistic_write = nil;
548 return ret, err; 548 return ret, err;
549 end 549 end
550 self:setwritetimeout(); 550 self:setwritetimeout();
551 self:set(nil, true); 551 self:set(nil, true);
552 end 552 end
553 return #data; 553 return #data;
554 end 554 end
555 interface.send = interface.write; 555 interface.send = interface.write;
556 556
614 end 614 end
615 615
616 function interface:inittls(tls_ctx, now) 616 function interface:inittls(tls_ctx, now)
617 if self._tls then return end 617 if self._tls then return end
618 if tls_ctx then self.tls_ctx = tls_ctx; end 618 if tls_ctx then self.tls_ctx = tls_ctx; end
619 self._tls = true; 619 self._tls = true;
620 self:debug("Starting TLS now"); 620 self:debug("Starting TLS now");
621 self:updatenames(); -- Can't getpeer/sockname after wrap() 621 self:updatenames(); -- Can't getpeer/sockname after wrap()
622 local ok, conn, err = pcall(luasec.wrap, self.conn, self.tls_ctx); 622 local ok, conn, err = pcall(luasec.wrap, self.conn, self.tls_ctx);
623 if not ok then 623 if not ok then
624 conn, err = ok, conn; 624 conn, err = ok, conn;
625 self:debug("Failed to initialize TLS: %s", err); 625 self:debug("Failed to initialize TLS: %s", err);
626 end 626 end
627 if not conn then 627 if not conn then
628 self:on("disconnect", err); 628 self:on("disconnect", err);
629 self:destroy(); 629 self:destroy();
630 return conn, err; 630 return conn, err;
631 end 631 end
632 conn:settimeout(0); 632 conn:settimeout(0);
633 self.conn = conn; 633 self.conn = conn;
634 if conn.sni then 634 if conn.sni then
635 if self.servername then 635 if self.servername then
636 conn:sni(self.servername); 636 conn:sni(self.servername);
637 elseif self._server and type(self._server.hosts) == "table" and next(self._server.hosts) ~= nil then 637 elseif self._server and type(self._server.hosts) == "table" and next(self._server.hosts) ~= nil then
638 conn:sni(self._server.hosts, true); 638 conn:sni(self._server.hosts, true);
648 for _, tlsa in ipairs(self.extra.tlsa) do 648 for _, tlsa in ipairs(self.extra.tlsa) do
649 self:noise("TLSA: %q", tlsa); 649 self:noise("TLSA: %q", tlsa);
650 conn:settlsa(tlsa.use, tlsa.select, tlsa.match, tlsa.data); 650 conn:settlsa(tlsa.use, tlsa.select, tlsa.match, tlsa.data);
651 end 651 end
652 end 652 end
653 end 653 end
654 self:on("starttls"); 654 self:on("starttls");
655 self.ondrain = nil; 655 self.ondrain = nil;
656 self.onwritable = interface.tlshandshake; 656 self.onwritable = interface.tlshandshake;
657 self.onreadable = interface.tlshandshake; 657 self.onreadable = interface.tlshandshake;
658 if now then 658 if now then
659 return self:tlshandshake() 659 return self:tlshandshake()
660 end 660 end
959 -- Dump all data from one connection into another 959 -- Dump all data from one connection into another
960 local function link(from, to, read_size) 960 local function link(from, to, read_size)
961 from:debug("Linking to %s", to.id); 961 from:debug("Linking to %s", to.id);
962 function from:onincoming(data) 962 function from:onincoming(data)
963 self:pause(); 963 self:pause();
964 to:write(data); 964 to:write(data);
965 end 965 end
966 function to:ondrain() -- luacheck: ignore 212/self 966 function to:ondrain() -- luacheck: ignore 212/self
967 from:resume(); 967 from:resume();
968 end 968 end
969 from:set_mode(read_size); 969 from:set_mode(read_size);
970 from:set(true, nil); 970 from:set(true, nil);
971 to:set(nil, true); 971 to:set(nil, true);
972 end 972 end