Comparison

net/server_epoll.lua @ 7817:4e3654031693

net.server_epoll: Reuse interface metatable for event compat layer
author Kim Alvefur <zash@zash.se>
date Fri, 06 Jan 2017 17:01:48 +0100
parent 7811:24f99d11b792
child 7910:91db637be237
comparison
equal deleted inserted replaced
7811:24f99d11b792 7817:4e3654031693
669 event = { EV_READ = "r", EV_WRITE = "w", EV_READWRITE = "rw", EV_LEAVE = -1 }; 669 event = { EV_READ = "r", EV_WRITE = "w", EV_READWRITE = "rw", EV_LEAVE = -1 };
670 addevent = function (fd, mode, callback) 670 addevent = function (fd, mode, callback)
671 local function onevent(self) 671 local function onevent(self)
672 local ret = self:callback(); 672 local ret = self:callback();
673 if ret == -1 then 673 if ret == -1 then
674 epoll.ctl("del", fd); 674 self:setflags(false, false);
675 elseif ret then 675 elseif ret then
676 epoll.ctl("mod", fd, mode); 676 self:setflags(mode == "r" or mode == "rw", mode == "w" or mode == "rw");
677 end 677 end
678 end 678 end
679 679
680 local conn = { 680 local conn = setmetatable({
681 getfd = function () return fd; end;
681 callback = callback; 682 callback = callback;
682 onreadable = onevent; 683 onreadable = onevent;
683 onwriteable = onevent; 684 onwriteable = onevent;
684 close = function () 685 close = function (self)
686 self:setflags(false, false);
685 fds[fd] = nil; 687 fds[fd] = nil;
686 return epoll.ctl("del", fd);
687 end; 688 end;
688 }; 689 }, interface_mt);
689 fds[fd] = conn; 690 local ok, err = conn:setflags(mode == "r" or mode == "rw", mode == "w" or mode == "rw");
690 local ok, err = epoll.ctl("add", fd, mode or "r");
691 if not ok then return ok, err; end 691 if not ok then return ok, err; end
692 return conn; 692 return conn;
693 end; 693 end;
694 }; 694 };