Annotate

net/server_epoll.lua @ 7564:f95ffd24e042

net.server_epoll: Clear write flag before calling ondrain, fixes calling of ondisconnect on explicit close
author Kim Alvefur <zash@zash.se>
date Tue, 16 Aug 2016 14:53:29 +0200
parent 7563:71dc0d89a9b8
child 7573:0f0a6febbc5a
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;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local log = require "util.logger".init("server_epoll");
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local epoll = require "epoll";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local socket = require "socket";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local luasec = require "ssl";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local gettime = require "util.time".now;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local createtable = require "util.table".create;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local _ENV = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local cfg = {
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 read_timeout = 900;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 write_timeout = 7;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 tcp_backlog = 128;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 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
31 read_retry_delay = 1e-06;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local fds = createtable(10, 0); -- FD -> conn
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 local timers = {};
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 function noop() end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local function closetimer(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 t[1] = 0;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 t[2] = noop;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 local resort_timers = false;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 local function at(time, f)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local timer = { time, f, close = closetimer };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 t_insert(timers, timer);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 return timer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 local function addtimer(timeout, f)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 return at(gettime() + timeout, f);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 local function runtimers()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 if resort_timers then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 -- Sort earliest timers to the end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 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
58 resort_timers = false;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 --[[ 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
62 for i = #timers, 1, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if timers[i][2] == noop then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 timers[i] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 break;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 --]]
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 local next_delay = 86400;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 -- Iterate from the end and remove completed timers
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 for i = #timers, 1, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 local timer = timers[i];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 local t, f = timer[1], timer[2];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 local now = gettime(); -- inside or before the loop?
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 if t > now then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 local diff = t - now;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 if diff < next_delay then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 next_delay = diff;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
7558
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
83 break;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 local new_timeout = f(now);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 if new_timeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 timer[1] = t + new_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 t_remove(timers, i);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 end
7558
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
93 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
94 -- Timers may be added from within a timer callback.
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
95 -- Those would not be considered for next_dela,
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
96 -- 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
97 -- 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
98 -- properly sort all new timers.
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 next_delay = 1e-6;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 return next_delay;
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 local interface = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 local interface_mt = { __index = interface };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 function interface_mt:__tostring()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 if self.peer then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 if self.conn then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 return ("%d %s [%s]:%d"):format(self:getfd(), tostring(self.conn), self.peer[1], self.peer[2]);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 return ("%d [%s]:%d"):format(self:getfd(), self.peer[1], self.peer[2]);
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 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 return tostring(self:getfd());
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 end
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:setlistener(listeners)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 self.listeners = listeners;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 function interface:getfd()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 return self.conn:getfd();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 function interface:ip()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 return self.peer[1];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 function interface:socket()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 return self.conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 function interface:setoption(k, v)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 -- LuaSec doesn't expose setoption :(
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 if self.conn.setoption then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 self.conn:setoption(k, v);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 function interface:setreadtimeout(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 if t == false then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 if self._readtimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 self._readtimeout:close();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 self._readtimeout = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 return
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 t = t or cfg.read_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 if self._readtimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 self._readtimeout[1] = gettime() + t;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 self._readtimeout = addtimer(t, function ()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 if self:onreadtimeout() then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 return cfg.read_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 else
7563
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
158 self:ondisconnect("read timeout");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 end);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 function interface:onreadtimeout()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 if self.listeners.onreadtimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 return self.listeners.onreadtimeout(self);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 function interface:setwritetimeout(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 if t == false then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 if self._writetimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 self._writetimeout:close();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 self._writetimeout = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 return
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 t = t or cfg.write_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 if self._writetimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 self._writetimeout[1] = gettime() + t;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 self._writetimeout = addtimer(t, function ()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 self.listeners.ondisconnect(self, "write timeout");
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 end);
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 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 function interface:flags()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 if self._wantread then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 if self._wantwrite then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 return "rw";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 return "r";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 elseif self._wantwrite then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 return "w";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 function interface:setflags(r, w)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 if r ~= nil then self._wantread = r; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 if w ~= nil then self._wantwrite = w; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 local flags = self:flags();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 local currentflags = self._flags;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 if flags == currentflags then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 return true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 local fd = self:getfd();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 local op = "mod";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 if not flags then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 op = "del";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 elseif not currentflags then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 op = "add";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 local ok, err = epoll.ctl(op, fd, flags);
7552
f1ae74ebeb81 net.server_epoll: Log epoll_ctl() calls and return values
Kim Alvefur <zash@zash.se>
parents: 7551
diff changeset
218 log("debug", "epoll_ctl(%q, %d, %q) -> %s" .. (err and ", %q" or ""),
f1ae74ebeb81 net.server_epoll: Log epoll_ctl() calls and return values
Kim Alvefur <zash@zash.se>
parents: 7551
diff changeset
219 op, fd, flags or "", tostring(ok), err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 if not ok then return ok, err end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 self._flags = flags;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 return true;
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 function interface:onreadable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 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
227 if data or partial then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 self.listeners.onincoming(self, data or partial, err);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 if err == "wantread" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 self:setflags(true, nil);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 elseif err == "wantwrite" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 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
234 elseif not data and err ~= "timeout" then
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 self.listeners.ondisconnect(self, err);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 self:destroy()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 return;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 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
240 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
241 self:pausefor(cfg.read_retry_delay);
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
242 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 function interface:onwriteable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 local buffer = self.writebuffer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 local data = t_concat(buffer);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 local ok, err, partial = self.conn:send(data);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 if ok then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 for i = #buffer, 1, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 buffer[i] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 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
253 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
254 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
255 self:ondrain(); -- Be aware of writes in ondrain
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 elseif partial then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 buffer[1] = data:sub(partial+1)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 for i = #buffer, 2, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 buffer[i] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 self:setwritetimeout();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 if err == "wantwrite" or err == "timeout" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 self:setflags(nil, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 elseif err == "wantread" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 self:setflags(true, nil);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 elseif err and err ~= "timeout" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 self.listeners.ondisconnect(self, err);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 function interface:ondrain()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 if self.listeners.ondrain then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 self.listeners.ondrain(self);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 if self._starttls then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 self:starttls();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 elseif self._toclose then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 self:close();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 end
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 function interface:write(data)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 local buffer = self.writebuffer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 if buffer then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 t_insert(buffer, data);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 self.writebuffer = { data };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 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
291 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
292 self:setflags(nil, true);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 return #data;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 interface.send = interface.write;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 function interface:close()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 if self._wantwrite then
7555
f3abb5f891eb net.server_epoll: Some logging when closing a connection
Kim Alvefur <zash@zash.se>
parents: 7554
diff changeset
299 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
300 self._toclose = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 else
7555
f3abb5f891eb net.server_epoll: Some logging when closing a connection
Kim Alvefur <zash@zash.se>
parents: 7554
diff changeset
302 log("debug", "Close %s", tostring(self));
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 self.close = noop;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 self.listeners.ondisconnect(self);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309 function interface:destroy()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 self:setflags(false, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 self:setwritetimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 self:setreadtimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 fds[self:getfd()] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 return self.conn:close();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
315 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
316
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 function interface:ssl()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 return self._tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 function interface:starttls(ctx)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 if ctx then self.tls = ctx; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 if self.writebuffer and self.writebuffer[1] then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 self._starttls = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 self:setflags(false, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 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
328 if not conn then
7563
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
329 self:ondisconnect(err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 conn:settimeout(0);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 self.conn = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 self._starttls = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 self.onwriteable = interface.tlshandskake;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336 self.onreadable = interface.tlshandskake;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 self:setflags(true, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 function interface:tlshandskake()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 local ok, err = self.conn:dohandshake();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 if ok then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 self.onwriteable = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 self.onreadable = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346 self:setflags(true, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 local old = self._tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 self._tls = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 self.starttls = false;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 if old == false then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 self:onconnect();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 elseif self.listeners.onstatus then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 self.listeners.onstatus(self, "ssl-handshake-complete");
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 elseif err == "wantread" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 self:setflags(true, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 self:setwritetimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 self:setreadtimeout(cfg.handshake_timeout);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 elseif err == "wantwrite" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 self:setflags(false, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 self:setreadtimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
362 self:setwritetimeout(cfg.handshake_timeout);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 else
7563
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
364 self:ondisconnect(err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369 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
370 client:settimeout(0);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371 local conn = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 conn = client;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373 server = server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374 created = gettime();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 listeners = listeners;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
376 _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
377 onwriteable = interface.onconnect;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378 writebuffer = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
379 tls = tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380 }, interface_mt);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
381 if client.getpeername then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
382 conn.peer = {client:getpeername()}
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 fds[conn:getfd()] = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 return conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 function interface:onacceptable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 local conn, err = self.conn:accept();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 if not conn then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392 log(debug, "Error accepting new client: %s, server will be paused for %ds", err, cfg.accept_retry_interval);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 self:pausefor(cfg.accept_retry_interval);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 return;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 local client = wrapsocket(conn, self, nil, self.listeners, self.tls);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 if self.tls then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 client._tls = false;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 client:starttls();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 else
7554
c2decf88e249 net.server_epoll: Use first writable event to signal onconnect
Kim Alvefur <zash@zash.se>
parents: 7553
diff changeset
401 client:setflags(false, true);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 client:setreadtimeout();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
404 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406 function interface:pause()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 self:setflags(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
409
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
410 function interface:resume()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
411 self:setflags(true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
412 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
413
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414 function interface:pausefor(t)
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
415 if self._pausefor then
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
416 self._pausefor:close();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
417 end
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
418 if t == false then return; end
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
419 self:setflags(false);
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
420 self._pausefor = addtimer(t, function ()
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
421 self._pausefor = nil;
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
422 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
423 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
424 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
425 end
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
426 end);
7547
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
7563
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
429 function interface:ondisconnect(err)
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
430 if self.listeners.ondisconnect then
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
431 self.listeners.ondisconnect(self, err);
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
432 end
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
433 end
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
434
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 function interface:onconnect()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 self.onwriteable = nil;
7563
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
437 if self.listeners.onconnect then
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
438 self.listeners.onconnect(self);
71dc0d89a9b8 net.server_epoll: Wrap calling of ondisconnect listener in additional indirection
Kim Alvefur <zash@zash.se>
parents: 7560
diff changeset
439 end
7554
c2decf88e249 net.server_epoll: Use first writable event to signal onconnect
Kim Alvefur <zash@zash.se>
parents: 7553
diff changeset
440 self:setflags(true);
c2decf88e249 net.server_epoll: Use first writable event to signal onconnect
Kim Alvefur <zash@zash.se>
parents: 7553
diff changeset
441 return self:onwriteable();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
442 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
443
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
444 local function addserver(addr, port, listeners, pattern, tls)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
445 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
446 if not conn then return conn, err; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
447 conn:settimeout(0);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
448 local server = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
449 conn = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
450 created = gettime();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
451 listeners = listeners;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
452 _pattern = pattern;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
453 onreadable = interface.onacceptable;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
454 tls = tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
455 peer = { addr, port };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
456 }, interface_mt);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
457 server:setflags(true, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
458 fds[server:getfd()] = server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
459 return server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
460 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
461
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
462 -- COMPAT
7551
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
463 local function wrapclient(conn, addr, port, listeners, pattern, tls)
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
464 local client = setmetatable({
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
465 conn = conn;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
466 created = gettime();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
467 listeners = listeners;
7551
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
468 _pattern = pattern;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
469 writebuffer = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
470 tls = tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
471 onwriteable = interface.onconnect;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
472 peer = { addr, port };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
473 }, interface_mt);
7551
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
474 fds[client:getfd()] = client;
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
475 client:setflags(false, true);
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
476 return client;
7547
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
7553
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
479 local function addclient(addr, port, listeners, pattern, tls)
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
480 local conn, err = socket.connect(addr, port);
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
481 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
482 conn:settimeout(0);
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
483 local client = wrapclient(conn, addr, port, listeners, pattern, tls);
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
484 if tls then
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
485 client._tls = false;
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
486 client:starttls();
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
487 else
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
488 client:setflags(true, true);
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
489 end
7556
1f777b38b66c net.server_epoll: Make addclient conform to API (expected by net.http.request)
Kim Alvefur <zash@zash.se>
parents: 7555
diff changeset
490 return client, conn;
7553
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
491 end
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
492
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
493 local function link(from, to)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
494 from.listeners = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
495 onincoming = function (_, data)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
496 from:pause();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
497 to:write(data);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
498 end,
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
499 }, {__index=from.listeners});
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
500 to.listeners = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
501 ondrain = function ()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
502 from:resume();
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 }, {__index=to.listeners});
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
505 from:setflags(true, nil);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
506 to:setflags(nil, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
507 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
508
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
509 -- XXX What uses this?
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
510 -- net.adns
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
511 function interface:set_send(new_send)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
512 self.send = new_send;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
513 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
514
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
515 local quitting = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
516
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
517 local function setquitting()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
518 quitting = "quitting";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
519 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
520
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
521 local function loop()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
522 repeat
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
523 local t = runtimers();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
524 local fd, r, w = epoll.wait(t);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
525 if fd then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
526 local conn = fds[fd];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
527 if conn then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
528 if r then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
529 conn:onreadable();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
530 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
531 if w then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
532 conn:onwriteable();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
533 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
534 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
535 log("debug", "Removing unknown fd %d", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
536 epoll.ctl("del", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
537 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
538 elseif r ~= "timeout" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
539 log("debug", "epoll_wait error: %s", tostring(r));
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
540 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
541 until quitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
542 return quitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
543 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
544
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
545 return {
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
546 get_backend = function () return "epoll"; end;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
547 addserver = addserver;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
548 addclient = addclient;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
549 add_task = addtimer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
550 at = at;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
551 loop = loop;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
552 setquitting = setquitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
553 wrapclient = wrapclient;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
554 link = link;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
555
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
556 -- libevent emulation
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
557 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
558 addevent = function (fd, mode, callback)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
559 local function onevent(self)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
560 local ret = self:callback();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
561 if ret == -1 then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
562 epoll.ctl("del", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
563 elseif ret then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
564 epoll.ctl("mod", fd, mode);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
565 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
566 end
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 conn = {
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
569 callback = callback;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
570 onreadable = onevent;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
571 onwriteable = onevent;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
572 close = function ()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
573 fds[fd] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
574 return epoll.ctl("del", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
575 end;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
576 };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
577 fds[fd] = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
578 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
579 if not ok then return ok, err; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
580 return conn;
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 };