Annotate

net/server_epoll.lua @ 7600:a18532076efe

net.server_epoll: Trigger onconnect for TLS connections when writable after handshake
author Kim Alvefur <zash@zash.se>
date Thu, 18 Aug 2016 17:15:32 +0200
parent 7599:afbda0bd9cfc
child 7601:ce20546d7714
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Prosody IM
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2016 Kim Alvefur
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 --
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 -- server_epoll
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 -- Server backend based on https://luarocks.org/modules/zash/lua-epoll
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local t_sort = table.sort;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local t_insert = table.insert;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local t_remove = table.remove;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local t_concat = table.concat;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local setmetatable = setmetatable;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local tostring = tostring;
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
17 local pcall = pcall;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local log = require "util.logger".init("server_epoll");
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local epoll = require "epoll";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local socket = require "socket";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local luasec = require "ssl";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local gettime = require "util.time".now;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local createtable = require "util.table".create;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local _ENV = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local cfg = {
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 read_timeout = 900;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 write_timeout = 7;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 tcp_backlog = 128;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 accept_retry_interval = 10;
7560
785fa1138293 net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents: 7559
diff changeset
32 read_retry_delay = 1e-06;
7599
afbda0bd9cfc net.server_epoll: Add separate timout for initial connection
Kim Alvefur <zash@zash.se>
parents: 7598
diff changeset
33 connect_timeout = 20;
7598
335b5ce06062 net.server_epoll: TLS handshake timeout
Kim Alvefur <zash@zash.se>
parents: 7597
diff changeset
34 handshake_timeout = 60;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 local fds = createtable(10, 0); -- FD -> conn
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local timers = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local function noop() end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local function closetimer(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 t[1] = 0;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 t[2] = noop;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
46 -- Set to true when timers have changed
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 local resort_timers = false;
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
48
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
49 -- Add absolute timer
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 local function at(time, f)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 local timer = { time, f, close = closetimer };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 t_insert(timers, timer);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 return timer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
56
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
57 -- Add relative timer
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 local function addtimer(timeout, f)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 return at(gettime() + timeout, f);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
62 -- Run callbacks of expired timers
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
63 -- Return time until next timeout
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 local function runtimers()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 if resort_timers then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 -- Sort earliest timers to the end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 t_sort(timers, function (a, b) return a[1] > b[1]; end);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 resort_timers = false;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 --[[ Is it worth it to skip the noop calls?
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 for i = #timers, 1, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if timers[i][2] == noop then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 timers[i] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 break;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 --]]
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 local next_delay = 86400;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 -- Iterate from the end and remove completed timers
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 for i = #timers, 1, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 local timer = timers[i];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 local t, f = timer[1], timer[2];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 local now = gettime(); -- inside or before the loop?
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 if t > now then
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
89 -- This timer should not fire yet
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 local diff = t - now;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if diff < next_delay then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 next_delay = diff;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
7558
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
94 break;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 local new_timeout = f(now);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 if new_timeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 timer[1] = t + new_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 t_remove(timers, i);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 end
7558
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
104 if resort_timers or next_delay < 1e-6 then
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
105 -- Timers may be added from within a timer callback.
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
106 -- Those would not be considered for next_delay,
7558
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
107 -- and we might sleep for too long, so instead
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
108 -- we return a shorter timeout so we can
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
109 -- properly sort all new timers.
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 next_delay = 1e-6;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 return next_delay;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 local interface = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 local interface_mt = { __index = interface };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 function interface_mt:__tostring()
7585
b64218c816de net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents: 7584
diff changeset
119 if self.sockname and self.peername then
b64218c816de net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents: 7584
diff changeset
120 return ("FD %d (%s, %d, %s, %d)"):format(self:getfd(), self.peername, self.peerport, self.sockname, self.sockport);
b64218c816de net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents: 7584
diff changeset
121 elseif self.sockname or self.peername then
b64218c816de net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents: 7584
diff changeset
122 return ("FD %d (%s, %d)"):format(self:getfd(), self.sockname or self.peername, self.sockport or self.peerport);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 end
7585
b64218c816de net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents: 7584
diff changeset
124 return ("%s FD %d"):format(tostring(self.conn), self:getfd());
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 function interface:setlistener(listeners)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 self.listeners = listeners;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
131 -- Call callback
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
132 function interface:on(what, ...)
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
133 local listener = self.listeners["on"..what];
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
134 if not listener then
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
135 -- log("debug", "Missing listener 'on%s'", what); -- uncomment for development and debugging
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
136 return;
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
137 end
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
138 local ok, err = pcall(listener, self, ...);
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
139 if not ok then
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
140 log("error", "Error calling on%s: %s", what, err);
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
141 end
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
142 return err;
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
143 end
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
144
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 function interface:getfd()
7586
846fdbbc62ba net.server_epoll: Return something as FD when no connection exists
Kim Alvefur <zash@zash.se>
parents: 7585
diff changeset
146 if self.conn then
846fdbbc62ba net.server_epoll: Return something as FD when no connection exists
Kim Alvefur <zash@zash.se>
parents: 7585
diff changeset
147 return self.conn:getfd();
846fdbbc62ba net.server_epoll: Return something as FD when no connection exists
Kim Alvefur <zash@zash.se>
parents: 7585
diff changeset
148 end
846fdbbc62ba net.server_epoll: Return something as FD when no connection exists
Kim Alvefur <zash@zash.se>
parents: 7585
diff changeset
149 return -1;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 function interface:ip()
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
153 return self.peername or self.sockname;
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
154 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
155
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
156 function interface:port()
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
157 return self.sockport or self.peerport;
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
158 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
159
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
160 function interface:clientport()
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
161 return self.sockport;
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
162 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
163
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
164 function interface:serverport()
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
165 if self.sockport then
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
166 return self.sockport;
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
167 elseif self.server then
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
168 self.server:port();
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
169 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 function interface:socket()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 return self.conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 function interface:setoption(k, v)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 -- LuaSec doesn't expose setoption :(
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 if self.conn.setoption then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 self.conn:setoption(k, v);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 function interface:setreadtimeout(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 if t == false then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 if self._readtimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 self._readtimeout:close();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 self._readtimeout = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 return
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 t = t or cfg.read_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 if self._readtimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 self._readtimeout[1] = gettime() + t;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 self._readtimeout = addtimer(t, function ()
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
197 if self:on("readtimeout") then
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 return cfg.read_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 else
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
200 self:on("disconnect", "read timeout");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 end);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 function interface:setwritetimeout(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 if t == false then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 if self._writetimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 self._writetimeout:close();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 self._writetimeout = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 return
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 t = t or cfg.write_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 if self._writetimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 self._writetimeout[1] = gettime() + t;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 self._writetimeout = addtimer(t, function ()
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
221 self:on("disconnect", "write timeout");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 end);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 function interface:flags()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 if self._wantread then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 if self._wantwrite then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 return "rw";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 return "r";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 elseif self._wantwrite then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 return "w";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 function interface:setflags(r, w)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 if r ~= nil then self._wantread = r; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 if w ~= nil then self._wantwrite = w; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 local flags = self:flags();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 local currentflags = self._flags;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 if flags == currentflags then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 return true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 local fd = self:getfd();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 local op = "mod";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 if not flags then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 op = "del";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 elseif not currentflags then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 op = "add";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 local ok, err = epoll.ctl(op, fd, flags);
7587
ff81a34bffb0 net.server_epoll: Comment some overly verbose logging
Kim Alvefur <zash@zash.se>
parents: 7586
diff changeset
254 -- log("debug", "epoll_ctl(%q, %d, %q) -> %s" .. (err and ", %q" or ""),
ff81a34bffb0 net.server_epoll: Comment some overly verbose logging
Kim Alvefur <zash@zash.se>
parents: 7586
diff changeset
255 -- op, fd, flags or "", tostring(ok), err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 if not ok then return ok, err end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 self._flags = flags;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 return true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
261 -- Called when socket is readable
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 function interface:onreadable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 local data, err, partial = self.conn:receive(self._pattern);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 if data or partial then
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
265 self:on("incoming", data or partial, err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 if err == "wantread" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 self:setflags(true, nil);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 elseif err == "wantwrite" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 self:setflags(nil, true);
7557
f0dd902534b0 net.server_epoll: Don't mistake success for an error
Kim Alvefur <zash@zash.se>
parents: 7556
diff changeset
271 elseif not data and err ~= "timeout" then
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
272 self:on("disconnect", err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 self:destroy()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 return;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 end
7560
785fa1138293 net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents: 7559
diff changeset
276 if self.conn:dirty() then
7590
fd07ab702f83 net.server_epoll: Remove read timeout when waiting to read more from buffer
Kim Alvefur <zash@zash.se>
parents: 7589
diff changeset
277 self:setreadtimeout(false);
7560
785fa1138293 net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents: 7559
diff changeset
278 self:pausefor(cfg.read_retry_delay);
7590
fd07ab702f83 net.server_epoll: Remove read timeout when waiting to read more from buffer
Kim Alvefur <zash@zash.se>
parents: 7589
diff changeset
279 else
fd07ab702f83 net.server_epoll: Remove read timeout when waiting to read more from buffer
Kim Alvefur <zash@zash.se>
parents: 7589
diff changeset
280 self:setreadtimeout();
7560
785fa1138293 net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents: 7559
diff changeset
281 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
284 -- Called when socket is writable
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 function interface:onwriteable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 local buffer = self.writebuffer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 local data = t_concat(buffer);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 local ok, err, partial = self.conn:send(data);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 if ok then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 for i = #buffer, 1, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 buffer[i] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 end
7564
f95ffd24e042 net.server_epoll: Clear write flag before calling ondrain, fixes calling of ondisconnect on explicit close
Kim Alvefur <zash@zash.se>
parents: 7563
diff changeset
293 self:setflags(nil, false);
f95ffd24e042 net.server_epoll: Clear write flag before calling ondrain, fixes calling of ondisconnect on explicit close
Kim Alvefur <zash@zash.se>
parents: 7563
diff changeset
294 self:setwritetimeout(false);
f95ffd24e042 net.server_epoll: Clear write flag before calling ondrain, fixes calling of ondisconnect on explicit close
Kim Alvefur <zash@zash.se>
parents: 7563
diff changeset
295 self:ondrain(); -- Be aware of writes in ondrain
7589
125e4f2e0b65 net.server_epoll: Return early on successful write
Kim Alvefur <zash@zash.se>
parents: 7588
diff changeset
296 return;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 elseif partial then
7591
138f16eb39fb net.server_epoll: Improve syntax
Kim Alvefur <zash@zash.se>
parents: 7590
diff changeset
298 buffer[1] = data:sub(partial+1);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 for i = #buffer, 2, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 buffer[i] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 self:setwritetimeout();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 if err == "wantwrite" or err == "timeout" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 self:setflags(nil, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 elseif err == "wantread" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 self:setflags(true, nil);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308 elseif err and err ~= "timeout" then
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
309 self:on("disconnect", err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 function interface:ondrain()
7588
fd6ca110edce net.server_epoll: Don't call ondrain when connection is to be closed
Kim Alvefur <zash@zash.se>
parents: 7587
diff changeset
315 if self._toclose then
fd6ca110edce net.server_epoll: Don't call ondrain when connection is to be closed
Kim Alvefur <zash@zash.se>
parents: 7587
diff changeset
316 return self:close();
fd6ca110edce net.server_epoll: Don't call ondrain when connection is to be closed
Kim Alvefur <zash@zash.se>
parents: 7587
diff changeset
317 elseif self._starttls then
fd6ca110edce net.server_epoll: Don't call ondrain when connection is to be closed
Kim Alvefur <zash@zash.se>
parents: 7587
diff changeset
318 return self:starttls();
fd6ca110edce net.server_epoll: Don't call ondrain when connection is to be closed
Kim Alvefur <zash@zash.se>
parents: 7587
diff changeset
319 else
fd6ca110edce net.server_epoll: Don't call ondrain when connection is to be closed
Kim Alvefur <zash@zash.se>
parents: 7587
diff changeset
320 return self:on("drain");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 function interface:write(data)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 local buffer = self.writebuffer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 if buffer then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 t_insert(buffer, data);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 self.writebuffer = { data };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 end
7550
8c2bc1b6d84a net.server_epoll: Remove last traces of code for bypassing buffering on writable sockets
Kim Alvefur <zash@zash.se>
parents: 7547
diff changeset
331 self:setwritetimeout();
8c2bc1b6d84a net.server_epoll: Remove last traces of code for bypassing buffering on writable sockets
Kim Alvefur <zash@zash.se>
parents: 7547
diff changeset
332 self:setflags(nil, true);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 return #data;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 interface.send = interface.write;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 function interface:close()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 if self._wantwrite then
7593
0266434d7df1 net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents: 7592
diff changeset
339 self:setflags(false, true); -- Flush final buffer contents
0266434d7df1 net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents: 7592
diff changeset
340 self.write, self.send = noop, noop; -- No more writing
7555
f3abb5f891eb net.server_epoll: Some logging when closing a connection
Kim Alvefur <zash@zash.se>
parents: 7554
diff changeset
341 log("debug", "Close %s after writing", tostring(self));
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 self._toclose = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 else
7592
10d2f008321d net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents: 7591
diff changeset
344 log("debug", "Close %s now", tostring(self));
7593
0266434d7df1 net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents: 7592
diff changeset
345 self.write, self.send = noop, noop;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346 self.close = noop;
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
347 self:on("disconnect");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 function interface:destroy()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 self:setflags(false, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 self:setwritetimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 self:setreadtimeout(false);
7594
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
356 self.onreadable = noop;
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
357 self.onwriteable = noop;
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
358 self.destroy = noop;
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
359 self.close = noop;
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
360 self.on = noop;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 fds[self:getfd()] = nil;
7594
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
362 self.conn:close();
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
363 self.conn = nil;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 function interface:ssl()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367 return self._tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 function interface:starttls(ctx)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371 if ctx then self.tls = ctx; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 if self.writebuffer and self.writebuffer[1] then
7592
10d2f008321d net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents: 7591
diff changeset
373 log("debug", "Start TLS on %s after write", tostring(self));
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374 self._starttls = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 else
7592
10d2f008321d net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents: 7591
diff changeset
376 log("debug", "Start TLS on %s now", tostring(self));
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
377 self:setflags(false, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378 local conn, err = luasec.wrap(self.conn, ctx or self.tls);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
379 if not conn then
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
380 self:on("disconnect", err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
381 self:destroy();
7595
166b2e0766a4 net.server_epoll: Return status from starttls method
Kim Alvefur <zash@zash.se>
parents: 7594
diff changeset
382 return conn, err;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 conn:settimeout(0);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 self.conn = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 self._starttls = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 self.onwriteable = interface.tlshandskake;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 self.onreadable = interface.tlshandskake;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 self:setflags(true, true);
7598
335b5ce06062 net.server_epoll: TLS handshake timeout
Kim Alvefur <zash@zash.se>
parents: 7597
diff changeset
390 self:setwritetimeout(cfg.handshake_timeout);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 function interface:tlshandskake()
7598
335b5ce06062 net.server_epoll: TLS handshake timeout
Kim Alvefur <zash@zash.se>
parents: 7597
diff changeset
395 self:setwritetimeout(false);
335b5ce06062 net.server_epoll: TLS handshake timeout
Kim Alvefur <zash@zash.se>
parents: 7597
diff changeset
396 self:setreadtimeout(false);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 local ok, err = self.conn:dohandshake();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 if ok then
7592
10d2f008321d net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents: 7591
diff changeset
399 log("debug", "TLS handshake on %s complete", tostring(self));
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 self.onwriteable = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 self.onreadable = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402 self:setflags(true, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 local old = self._tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
404 self._tls = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405 self.starttls = false;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406 if old == false then
7600
a18532076efe net.server_epoll: Trigger onconnect for TLS connections when writable after handshake
Kim Alvefur <zash@zash.se>
parents: 7599
diff changeset
407 self.onwriteable = interface.onconnect;
a18532076efe net.server_epoll: Trigger onconnect for TLS connections when writable after handshake
Kim Alvefur <zash@zash.se>
parents: 7599
diff changeset
408 self:setflags(false, true);
a18532076efe net.server_epoll: Trigger onconnect for TLS connections when writable after handshake
Kim Alvefur <zash@zash.se>
parents: 7599
diff changeset
409 self:setwritetimeout();
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
410 else
7600
a18532076efe net.server_epoll: Trigger onconnect for TLS connections when writable after handshake
Kim Alvefur <zash@zash.se>
parents: 7599
diff changeset
411 self:setflags(true, true);
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
412 self:on("status", "ssl-handshake-complete");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
413 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414 elseif err == "wantread" then
7592
10d2f008321d net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents: 7591
diff changeset
415 log("debug", "TLS handshake on %s to wait until readable", tostring(self));
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
416 self:setflags(true, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
417 self:setreadtimeout(cfg.handshake_timeout);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
418 elseif err == "wantwrite" then
7592
10d2f008321d net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents: 7591
diff changeset
419 log("debug", "TLS handshake on %s to wait until writable", tostring(self));
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
420 self:setflags(false, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
421 self:setwritetimeout(cfg.handshake_timeout);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422 else
7592
10d2f008321d net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents: 7591
diff changeset
423 log("debug", "TLS handshake error on %s: %s", tostring(self), err);
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
424 self:on("disconnect", err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
425 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
426 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
429 local function wrapsocket(client, server, pattern, listeners, tls) -- luasocket object -> interface object
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
430 client:settimeout(0);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 local conn = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 conn = client;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 server = server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
434 created = gettime();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 listeners = listeners;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 _pattern = pattern or server._pattern;
7554
c2decf88e249 net.server_epoll: Use first writable event to signal onconnect
Kim Alvefur <zash@zash.se>
parents: 7553
diff changeset
437 onwriteable = interface.onconnect;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
438 writebuffer = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
439 tls = tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
440 }, interface_mt);
7591
138f16eb39fb net.server_epoll: Improve syntax
Kim Alvefur <zash@zash.se>
parents: 7590
diff changeset
441
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
442 if client.getpeername then
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
443 conn.peername, conn.peerport = client:getpeername();
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
444 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
445 if client.getsockname then
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
446 conn.sockname, conn.sockport = client:getsockname();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
447 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
448
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
449 fds[conn:getfd()] = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
450 return conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
451 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
452
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
453 function interface:onacceptable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
454 local conn, err = self.conn:accept();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
455 if not conn then
7574
97b8506118a9 net.server_epoll: Fix missing quotes in logging call
Kim Alvefur <zash@zash.se>
parents: 7573
diff changeset
456 log("debug", "Error accepting new client: %s, server will be paused for %ds", err, cfg.accept_retry_interval);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
457 self:pausefor(cfg.accept_retry_interval);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
458 return;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
459 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
460 local client = wrapsocket(conn, self, nil, self.listeners, self.tls);
7592
10d2f008321d net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents: 7591
diff changeset
461 log("debug", "New connection %s", tostring(client));
7597
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
462 client:init();
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
463 end
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
464
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
465 function interface:init()
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
466 if self.tls and not self._tls then
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
467 self._tls = false; -- This means we should call onconnect when TLS is up
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
468 return self:starttls();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
469 else
7597
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
470 self:setflags(false, true);
7599
afbda0bd9cfc net.server_epoll: Add separate timout for initial connection
Kim Alvefur <zash@zash.se>
parents: 7598
diff changeset
471 self:setwritetimeout(cfg.connect_timeout);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
472 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
473 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
474
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
475 function interface:pause()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
476 self:setflags(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
477 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
478
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
479 function interface:resume()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
480 self:setflags(true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
481 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
482
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
483 function interface:pausefor(t)
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
484 if self._pausefor then
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
485 self._pausefor:close();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
486 end
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
487 if t == false then return; end
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
488 self:setflags(false);
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
489 self._pausefor = addtimer(t, function ()
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
490 self._pausefor = nil;
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
491 self:setflags(true);
7560
785fa1138293 net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents: 7559
diff changeset
492 if self.conn:dirty() then
785fa1138293 net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents: 7559
diff changeset
493 self:onreadable();
785fa1138293 net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents: 7559
diff changeset
494 end
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
495 end);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
496 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
497
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
498 function interface:onconnect()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
499 self.onwriteable = nil;
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
500 self:on("connect");
7554
c2decf88e249 net.server_epoll: Use first writable event to signal onconnect
Kim Alvefur <zash@zash.se>
parents: 7553
diff changeset
501 self:setflags(true);
c2decf88e249 net.server_epoll: Use first writable event to signal onconnect
Kim Alvefur <zash@zash.se>
parents: 7553
diff changeset
502 return self:onwriteable();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
503 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
504
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
505 local function addserver(addr, port, listeners, pattern, tls)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
506 local conn, err = socket.bind(addr, port, cfg.tcp_backlog);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
507 if not conn then return conn, err; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
508 conn:settimeout(0);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
509 local server = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
510 conn = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
511 created = gettime();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
512 listeners = listeners;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
513 _pattern = pattern;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
514 onreadable = interface.onacceptable;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
515 tls = tls;
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
516 sockname = addr;
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
517 sockport = port;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
518 }, interface_mt);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
519 server:setflags(true, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
520 fds[server:getfd()] = server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
521 return server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
522 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
523
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
524 -- COMPAT
7551
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
525 local function wrapclient(conn, addr, port, listeners, pattern, tls)
7597
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
526 local client = wrapsocket(conn, nil, pattern, listeners, tls);
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
527 if not client.peername then
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
528 client.peername, client.peerport = addr, port;
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
529 end
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
530 client:init();
7551
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
531 return client;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
532 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
533
7553
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
534 local function addclient(addr, port, listeners, pattern, tls)
7596
2c4ba55b616c net.server_epoll: Connect async in addclient
Kim Alvefur <zash@zash.se>
parents: 7595
diff changeset
535 local conn, err = socket.tcp();
7553
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
536 if not conn then return conn, err; end
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
537 conn:settimeout(0);
7596
2c4ba55b616c net.server_epoll: Connect async in addclient
Kim Alvefur <zash@zash.se>
parents: 7595
diff changeset
538 conn:connect(addr, port);
7597
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
539 local client = wrapsocket(conn, nil, pattern, listeners, tls)
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
540 client:init();
7556
1f777b38b66c net.server_epoll: Make addclient conform to API (expected by net.http.request)
Kim Alvefur <zash@zash.se>
parents: 7555
diff changeset
541 return client, conn;
7553
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
542 end
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
543
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
544 local function link(from, to)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
545 from.listeners = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
546 onincoming = function (_, data)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
547 from:pause();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
548 to:write(data);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
549 end,
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
550 }, {__index=from.listeners});
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
551 to.listeners = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
552 ondrain = function ()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
553 from:resume();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
554 end,
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
555 }, {__index=to.listeners});
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
556 from:setflags(true, nil);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
557 to:setflags(nil, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
558 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
559
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
560 -- XXX What uses this?
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
561 -- net.adns
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
562 function interface:set_send(new_send)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
563 self.send = new_send;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
564 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
565
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
566 local quitting = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
567
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
568 local function setquitting()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
569 quitting = "quitting";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
570 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
571
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
572 local function loop()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
573 repeat
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
574 local t = runtimers();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
575 local fd, r, w = epoll.wait(t);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
576 if fd then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
577 local conn = fds[fd];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
578 if conn then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
579 if r then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
580 conn:onreadable();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
581 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
582 if w then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
583 conn:onwriteable();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
584 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
585 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
586 log("debug", "Removing unknown fd %d", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
587 epoll.ctl("del", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
588 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
589 elseif r ~= "timeout" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
590 log("debug", "epoll_wait error: %s", tostring(r));
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
591 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
592 until quitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
593 return quitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
594 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
595
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
596 return {
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
597 get_backend = function () return "epoll"; end;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
598 addserver = addserver;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
599 addclient = addclient;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
600 add_task = addtimer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
601 at = at;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
602 loop = loop;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
603 setquitting = setquitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
604 wrapclient = wrapclient;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
605 link = link;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
606
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
607 -- libevent emulation
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
608 event = { EV_READ = "r", EV_WRITE = "w", EV_READWRITE = "rw", EV_LEAVE = -1 };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
609 addevent = function (fd, mode, callback)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
610 local function onevent(self)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
611 local ret = self:callback();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
612 if ret == -1 then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
613 epoll.ctl("del", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
614 elseif ret then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
615 epoll.ctl("mod", fd, mode);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
616 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
617 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
618
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
619 local conn = {
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
620 callback = callback;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
621 onreadable = onevent;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
622 onwriteable = onevent;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
623 close = function ()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
624 fds[fd] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
625 return epoll.ctl("del", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
626 end;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
627 };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
628 fds[fd] = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
629 local ok, err = epoll.ctl("add", fd, mode or "r");
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
630 if not ok then return ok, err; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
631 return conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
632 end;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
633 };