Annotate

net/server_epoll.lua @ 7587:ff81a34bffb0

net.server_epoll: Comment some overly verbose logging
author Kim Alvefur <zash@zash.se>
date Thu, 18 Aug 2016 16:02:20 +0200
parent 7586:846fdbbc62ba
child 7588:fd6ca110edce
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;
7547
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 local fds = createtable(10, 0); -- FD -> conn
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local timers = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local function noop() end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 local function closetimer(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 t[1] = 0;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 t[2] = noop;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
44 -- Set to true when timers have changed
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local resort_timers = false;
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
46
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
47 -- Add absolute timer
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 local function at(time, f)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local timer = { time, f, close = closetimer };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 t_insert(timers, timer);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 return timer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
54
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
55 -- Add relative timer
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 local function addtimer(timeout, f)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 return at(gettime() + timeout, f);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
60 -- Run callbacks of expired timers
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
61 -- Return time until next timeout
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local function runtimers()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if resort_timers then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 -- Sort earliest timers to the end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 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
66 resort_timers = false;
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 --[[ 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
70 for i = #timers, 1, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 if timers[i][2] == noop then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 timers[i] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 break;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 --]]
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 local next_delay = 86400;
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 -- Iterate from the end and remove completed timers
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 for i = #timers, 1, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 local timer = timers[i];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 local t, f = timer[1], timer[2];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 local now = gettime(); -- inside or before the loop?
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 if t > now then
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
87 -- This timer should not fire yet
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 local diff = t - now;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 if diff < next_delay then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 next_delay = diff;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end
7558
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
92 break;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 local new_timeout = f(now);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 if new_timeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 timer[1] = t + new_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 t_remove(timers, i);
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 end
7558
3a2fe8e10eeb net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents: 7557
diff changeset
102 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
103 -- 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
104 -- 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
105 -- 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
106 -- 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
107 -- properly sort all new timers.
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 next_delay = 1e-6;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 return next_delay;
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 local interface = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 local interface_mt = { __index = interface };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 function interface_mt:__tostring()
7585
b64218c816de net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents: 7584
diff changeset
117 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
118 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
119 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
120 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
121 end
7585
b64218c816de net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents: 7584
diff changeset
122 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
123 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 function interface:setlistener(listeners)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 self.listeners = listeners;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
129 -- Call callback
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
130 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
131 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
132 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
133 -- 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
134 return;
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
135 end
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
136 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
137 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
138 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
139 end
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
140 return 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
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 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
144 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
145 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
146 end
846fdbbc62ba net.server_epoll: Return something as FD when no connection exists
Kim Alvefur <zash@zash.se>
parents: 7585
diff changeset
147 return -1;
7547
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 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
151 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
152 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
153
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
154 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
155 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
156 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
157
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
158 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
159 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
160 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
161
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
162 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
163 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
164 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
165 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
166 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
167 end
7547
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 function interface:socket()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 return self.conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 function interface:setoption(k, v)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 -- LuaSec doesn't expose setoption :(
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 if self.conn.setoption then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 self.conn:setoption(k, v);
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 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 function interface:setreadtimeout(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 if t == false then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 if self._readtimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 self._readtimeout:close();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 self._readtimeout = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 return
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 t = t or cfg.read_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 if self._readtimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 self._readtimeout[1] = gettime() + t;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 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
195 if self:on("readtimeout") then
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 return cfg.read_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 else
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
198 self:on("disconnect", "read timeout");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 self:destroy();
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 end);
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 function interface:setwritetimeout(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 if t == false then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 if self._writetimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 self._writetimeout:close();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 self._writetimeout = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 return
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 t = t or cfg.write_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 if self._writetimeout then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 self._writetimeout[1] = gettime() + t;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 resort_timers = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 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
219 self:on("disconnect", "write timeout");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 end);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 end
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:flags()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 if self._wantread then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 if self._wantwrite then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 return "rw";
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 return "r";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 elseif self._wantwrite then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 return "w";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 function interface:setflags(r, w)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 if r ~= nil then self._wantread = r; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 if w ~= nil then self._wantwrite = w; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 local flags = self:flags();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 local currentflags = self._flags;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 if flags == currentflags then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 return true;
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 local fd = self:getfd();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 local op = "mod";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 if not flags then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 op = "del";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 elseif not currentflags then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 op = "add";
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 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
252 -- 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
253 -- op, fd, flags or "", tostring(ok), err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 if not ok then return ok, err end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 self._flags = flags;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 return true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
259 -- Called when socket is readable
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 function interface:onreadable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 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
262 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
263 self:on("incoming", data or partial, err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 if 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 == "wantwrite" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 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
269 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
270 self:on("disconnect", err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 self:destroy()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 return;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 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
275 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
276 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
277 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
280 -- Called when socket is writable
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 function interface:onwriteable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 local buffer = self.writebuffer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 local data = t_concat(buffer);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 local ok, err, partial = self.conn:send(data);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 if ok then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 for i = #buffer, 1, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 buffer[i] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 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
289 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
290 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
291 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
292 elseif partial then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 buffer[1] = data:sub(partial+1)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 for i = #buffer, 2, -1 do
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 buffer[i] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 self:setwritetimeout();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 if err == "wantwrite" or err == "timeout" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 self:setflags(nil, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 elseif err == "wantread" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 self:setflags(true, nil);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 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
304 self:on("disconnect", err);
7547
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:ondrain()
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
310 self:on("drain");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 if self._starttls then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 self:starttls();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 elseif self._toclose then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 self: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 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 function interface:write(data)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 local buffer = self.writebuffer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 if buffer then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 t_insert(buffer, data);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 self.writebuffer = { data };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 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
325 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
326 self:setflags(nil, true);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 return #data;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 interface.send = interface.write;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 function interface:close()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 if self._wantwrite then
7555
f3abb5f891eb net.server_epoll: Some logging when closing a connection
Kim Alvefur <zash@zash.se>
parents: 7554
diff changeset
333 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
334 self._toclose = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 else
7555
f3abb5f891eb net.server_epoll: Some logging when closing a connection
Kim Alvefur <zash@zash.se>
parents: 7554
diff changeset
336 log("debug", "Close %s", tostring(self));
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 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
338 self:on("disconnect");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 function interface:destroy()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 self:setflags(false, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 self:setwritetimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346 self:setreadtimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 fds[self:getfd()] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 return self.conn:close();
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 function interface:ssl()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 return self._tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 function interface:starttls(ctx)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 if ctx then self.tls = ctx; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 if self.writebuffer and self.writebuffer[1] then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 self._starttls = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 self:setflags(false, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 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
362 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
363 self:on("disconnect", err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 conn:settimeout(0);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367 self.conn = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368 self._starttls = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369 self.onwriteable = interface.tlshandskake;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 self.onreadable = interface.tlshandskake;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371 self:setflags(true, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 function interface:tlshandskake()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
376 local ok, err = self.conn:dohandshake();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
377 if ok then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378 self.onwriteable = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
379 self.onreadable = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380 self:setflags(true, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
381 local old = self._tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
382 self._tls = true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 self.starttls = false;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 if old == false then
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
385 self:on("connect");
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
386 else
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
387 self:on("status", "ssl-handshake-complete");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 elseif err == "wantread" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 self:setflags(true, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 self:setwritetimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392 self:setreadtimeout(cfg.handshake_timeout);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 elseif err == "wantwrite" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 self:setflags(false, true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 self:setreadtimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 self:setwritetimeout(cfg.handshake_timeout);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 else
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
398 self:on("disconnect", err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 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
404 client:settimeout(0);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405 local conn = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406 conn = client;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 server = server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408 created = gettime();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
409 listeners = listeners;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
410 _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
411 onwriteable = interface.onconnect;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
412 writebuffer = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
413 tls = tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414 }, interface_mt);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
415 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
416 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
417 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
418 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
419 conn.sockname, conn.sockport = client:getsockname();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
420 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
421
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422 fds[conn:getfd()] = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
423 return conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
424 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
425
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
426 function interface:onacceptable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 local conn, err = self.conn:accept();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428 if not conn then
7574
97b8506118a9 net.server_epoll: Fix missing quotes in logging call
Kim Alvefur <zash@zash.se>
parents: 7573
diff changeset
429 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
430 self:pausefor(cfg.accept_retry_interval);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 return;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 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
434 if self.tls then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 client._tls = false;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 client:starttls();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
437 else
7554
c2decf88e249 net.server_epoll: Use first writable event to signal onconnect
Kim Alvefur <zash@zash.se>
parents: 7553
diff changeset
438 client:setflags(false, true);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
439 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
440 client:setreadtimeout();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
441 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
442
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
443 function interface:pause()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
444 self:setflags(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
445 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
446
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
447 function interface:resume()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
448 self:setflags(true);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
449 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
450
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
451 function interface:pausefor(t)
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
452 if self._pausefor then
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
453 self._pausefor:close();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
454 end
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
455 if t == false then return; end
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
456 self:setflags(false);
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
457 self._pausefor = addtimer(t, function ()
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
458 self._pausefor = nil;
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
459 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
460 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
461 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
462 end
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
463 end);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
464 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
465
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
466 function interface:onconnect()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
467 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
468 self:on("connect");
7554
c2decf88e249 net.server_epoll: Use first writable event to signal onconnect
Kim Alvefur <zash@zash.se>
parents: 7553
diff changeset
469 self:setflags(true);
c2decf88e249 net.server_epoll: Use first writable event to signal onconnect
Kim Alvefur <zash@zash.se>
parents: 7553
diff changeset
470 return self:onwriteable();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
471 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
472
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
473 local function addserver(addr, port, listeners, pattern, tls)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
474 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
475 if not conn then return conn, err; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
476 conn:settimeout(0);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
477 local server = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
478 conn = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
479 created = gettime();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
480 listeners = listeners;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
481 _pattern = pattern;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
482 onreadable = interface.onacceptable;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
483 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
484 sockname = addr;
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
485 sockport = port;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
486 }, interface_mt);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
487 server:setflags(true, false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
488 fds[server:getfd()] = server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
489 return server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
490 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
491
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
492 -- COMPAT
7551
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
493 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
494 local client = setmetatable({
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
495 conn = conn;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
496 created = gettime();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
497 listeners = listeners;
7551
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
498 _pattern = pattern;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
499 writebuffer = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
500 tls = tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
501 onwriteable = interface.onconnect;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
502 peer = { addr, port };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
503 }, interface_mt);
7551
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
504 fds[client:getfd()] = client;
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
505 client:setflags(false, true);
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
506 return client;
7547
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
7553
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
509 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
510 local conn, err = socket.connect(addr, port);
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
511 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
512 conn:settimeout(0);
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
513 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
514 if tls then
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
515 client._tls = false;
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
516 client:starttls();
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
517 else
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
518 client:setflags(true, true);
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
519 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
520 return client, conn;
7553
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
521 end
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
522
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
523 local function link(from, to)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
524 from.listeners = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
525 onincoming = function (_, data)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
526 from:pause();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
527 to:write(data);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
528 end,
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
529 }, {__index=from.listeners});
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
530 to.listeners = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
531 ondrain = function ()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
532 from:resume();
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 }, {__index=to.listeners});
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
535 from:setflags(true, nil);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
536 to:setflags(nil, true);
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
539 -- XXX What uses this?
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
540 -- net.adns
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
541 function interface:set_send(new_send)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
542 self.send = new_send;
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 local quitting = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
546
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
547 local function setquitting()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
548 quitting = "quitting";
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
551 local function loop()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
552 repeat
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
553 local t = runtimers();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
554 local fd, r, w = epoll.wait(t);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
555 if fd then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
556 local conn = fds[fd];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
557 if conn then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
558 if r then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
559 conn:onreadable();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
560 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
561 if w then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
562 conn:onwriteable();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
563 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
564 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
565 log("debug", "Removing unknown fd %d", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
566 epoll.ctl("del", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
567 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
568 elseif r ~= "timeout" then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
569 log("debug", "epoll_wait error: %s", tostring(r));
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 until quitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
572 return quitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
573 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
574
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
575 return {
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
576 get_backend = function () return "epoll"; end;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
577 addserver = addserver;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
578 addclient = addclient;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
579 add_task = addtimer;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
580 at = at;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
581 loop = loop;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
582 setquitting = setquitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
583 wrapclient = wrapclient;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
584 link = link;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
585
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
586 -- libevent emulation
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
587 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
588 addevent = function (fd, mode, callback)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
589 local function onevent(self)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
590 local ret = self:callback();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
591 if ret == -1 then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
592 epoll.ctl("del", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
593 elseif ret then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
594 epoll.ctl("mod", fd, mode);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
595 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
596 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
597
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
598 local conn = {
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
599 callback = callback;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
600 onreadable = onevent;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
601 onwriteable = onevent;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
602 close = function ()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
603 fds[fd] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
604 return epoll.ctl("del", fd);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
605 end;
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 fds[fd] = conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
608 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
609 if not ok then return ok, err; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
610 return conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
611 end;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
612 };