Comparison

net/server_epoll.lua @ 8781:53178b6ba589

net.server: Add watchfd, a simple API for watching file descriptors
author Kim Alvefur <zash@zash.se>
date Wed, 09 May 2018 16:15:40 +0200
parent 8778:0364454bdd77
child 8983:23f66f04e216
comparison
equal deleted inserted replaced
8780:4cab4ee5dfcc 8781:53178b6ba589
13 local t_remove = table.remove; 13 local t_remove = table.remove;
14 local t_concat = table.concat; 14 local t_concat = table.concat;
15 local setmetatable = setmetatable; 15 local setmetatable = setmetatable;
16 local tostring = tostring; 16 local tostring = tostring;
17 local pcall = pcall; 17 local pcall = pcall;
18 local type = type;
18 local next = next; 19 local next = next;
19 local pairs = pairs; 20 local pairs = pairs;
20 local log = require "util.logger".init("server_epoll"); 21 local log = require "util.logger".init("server_epoll");
21 local epoll = require "epoll"; 22 local epoll = require "epoll";
22 local socket = require "socket"; 23 local socket = require "socket";
583 conn:connect(addr, port); 584 conn:connect(addr, port);
584 local client = wrapsocket(conn, nil, pattern, listeners, tls) 585 local client = wrapsocket(conn, nil, pattern, listeners, tls)
585 client:init(); 586 client:init();
586 return client, conn; 587 return client, conn;
587 end 588 end
589
590 local function watchfd(fd, onreadable, onwriteable)
591 local conn = setmetatable({
592 conn = fd;
593 onreadable = onreadable;
594 onwriteable = onwriteable;
595 close = function (self)
596 self:setflags(false, false);
597 end
598 }, interface_mt);
599 if type(fd) == "number" then
600 conn.getfd = function ()
601 return fd;
602 end;
603 -- Otherwise it'll need to be something LuaSocket-compatible
604 end
605 conn:setflags(onreadable, onwriteable);
606 return conn;
607 end;
588 608
589 -- Dump all data from one connection into another 609 -- Dump all data from one connection into another
590 local function link(from, to) 610 local function link(from, to)
591 from.listeners = setmetatable({ 611 from.listeners = setmetatable({
592 onincoming = function (_, data) 612 onincoming = function (_, data)
661 at = at; 681 at = at;
662 loop = loop; 682 loop = loop;
663 closeall = closeall; 683 closeall = closeall;
664 setquitting = setquitting; 684 setquitting = setquitting;
665 wrapclient = wrapclient; 685 wrapclient = wrapclient;
686 watchfd = watchfd;
666 link = link; 687 link = link;
667 set_config = function (newconfig) 688 set_config = function (newconfig)
668 cfg = setmetatable(newconfig, default_config); 689 cfg = setmetatable(newconfig, default_config);
669 end; 690 end;
670 691