Software /
code /
prosody
Annotate
net/server_epoll.lua @ 9294:31c02b3109e1
spec/scansion/prosody.cfg.lua: Config file to be used for integration tests
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 12 Sep 2018 13:49:53 +0100 |
parent | 8983:23f66f04e216 |
child | 9300:1df8832bd29e |
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; |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
18 local type = type; |
7797
d0c32d86724f
server_epoll: Flush and close all connections before quitting mainloop so no data is lost in buffers
Kim Alvefur <zash@zash.se>
parents:
7796
diff
changeset
|
19 local next = next; |
7794
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
20 local pairs = pairs; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local log = require "util.logger".init("server_epoll"); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local epoll = require "epoll"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local socket = require "socket"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 local luasec = require "ssl"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 local gettime = require "util.time".now; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local createtable = require "util.table".create; |
7737
f93b3083b46b
net.server_epoll: Use _SOCKETINVALID from LuaSocket
Kim Alvefur <zash@zash.se>
parents:
7707
diff
changeset
|
27 local _SOCKETINVALID = socket._SOCKETINVALID or -1; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
7601
ce20546d7714
net.server_epoll: Make sure a recent LuaSocket is avaliable
Kim Alvefur <zash@zash.se>
parents:
7600
diff
changeset
|
29 assert(socket.tcp6 and socket.tcp4, "Incompatible LuaSocket version"); |
ce20546d7714
net.server_epoll: Make sure a recent LuaSocket is avaliable
Kim Alvefur <zash@zash.se>
parents:
7600
diff
changeset
|
30 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8552
diff
changeset
|
32 -- luacheck: std none |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 |
7657
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
34 local default_config = { __index = { |
8983
23f66f04e216
net.server: Set default read timeout to 14 minutes (fixes #971)
Kim Alvefur <zash@zash.se>
parents:
8781
diff
changeset
|
35 read_timeout = 14 * 60; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 write_timeout = 7; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 tcp_backlog = 128; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 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
|
39 read_retry_delay = 1e-06; |
7599
afbda0bd9cfc
net.server_epoll: Add separate timout for initial connection
Kim Alvefur <zash@zash.se>
parents:
7598
diff
changeset
|
40 connect_timeout = 20; |
7598
335b5ce06062
net.server_epoll: TLS handshake timeout
Kim Alvefur <zash@zash.se>
parents:
7597
diff
changeset
|
41 handshake_timeout = 60; |
7623
a1c040a5754f
net.server_epoll: Make maximum wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7606
diff
changeset
|
42 max_wait = 86400; |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
43 min_wait = 1e-06; |
7657
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
44 }}; |
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
45 local cfg = default_config.__index; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 local fds = createtable(10, 0); -- FD -> conn |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
48 |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
49 -- Timer and scheduling -- |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
50 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 local timers = {}; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 local function noop() end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 local function closetimer(t) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 t[1] = 0; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 t[2] = noop; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
59 -- Set to true when timers have changed |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 local resort_timers = false; |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
61 |
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
62 -- Add absolute timer |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 local function at(time, f) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 local timer = { time, f, close = closetimer }; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 t_insert(timers, timer); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 resort_timers = true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 return timer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 end |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
69 |
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
70 -- Add relative timer |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 local function addtimer(timeout, f) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 return at(gettime() + timeout, f); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
75 -- Run callbacks of expired timers |
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
76 -- Return time until next timeout |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
77 local function runtimers(next_delay, min_wait) |
7629
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
78 -- Any timers at all? |
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
79 if not timers[1] then |
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
80 return next_delay; |
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
81 end |
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
82 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 if resort_timers then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 -- Sort earliest timers to the end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 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
|
86 resort_timers = false; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 -- Iterate from the end and remove completed timers |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 for i = #timers, 1, -1 do |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 local timer = timers[i]; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 local t, f = timer[1], timer[2]; |
7624 | 93 -- Get time for every iteration to increase accuracy |
94 local now = gettime(); | |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 if t > now then |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
96 -- This timer should not fire yet |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 local diff = t - now; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 if diff < next_delay then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 next_delay = diff; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 end |
7558
3a2fe8e10eeb
net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents:
7557
diff
changeset
|
101 break; |
7547
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 local new_timeout = f(now); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 if new_timeout then |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
105 -- Schedule for 'delay' from the time actually scheduled, |
7624 | 106 -- not from now, in order to prevent timer drift. |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 timer[1] = t + new_timeout; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 resort_timers = true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 t_remove(timers, i); |
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 end |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
113 |
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
114 if resort_timers or next_delay < min_wait then |
7558
3a2fe8e10eeb
net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents:
7557
diff
changeset
|
115 -- 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
|
116 -- 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
|
117 -- 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
|
118 -- 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
|
119 -- properly sort all new timers. |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
120 next_delay = min_wait; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 end |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
122 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 return next_delay; |
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 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
126 -- Socket handler interface |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
127 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 local interface = {}; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 local interface_mt = { __index = interface }; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 function interface_mt:__tostring() |
7585
b64218c816de
net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents:
7584
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 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
|
136 end |
7585
b64218c816de
net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents:
7584
diff
changeset
|
137 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
|
138 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
140 -- Replace the listener and tell the old one |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 function interface:setlistener(listeners) |
7604
36408ef6c8c9
net.server_epoll: Call ondetach when listeners are replaced
Kim Alvefur <zash@zash.se>
parents:
7603
diff
changeset
|
142 self:on("detach"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 self.listeners = listeners; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
146 -- Call a listener callback |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
147 function interface:on(what, ...) |
8006
be374a5778f3
net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents:
7910
diff
changeset
|
148 if not self.listeners then |
be374a5778f3
net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents:
7910
diff
changeset
|
149 log("error", "%s has no listeners", self); |
be374a5778f3
net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents:
7910
diff
changeset
|
150 return; |
be374a5778f3
net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents:
7910
diff
changeset
|
151 end |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
152 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
|
153 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
|
154 -- 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
|
155 return; |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
156 end |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
157 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
|
158 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
|
159 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
|
160 end |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
161 return err; |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
162 end |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
163 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
164 -- Return the file descriptor number |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 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
|
166 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
|
167 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
|
168 end |
7737
f93b3083b46b
net.server_epoll: Use _SOCKETINVALID from LuaSocket
Kim Alvefur <zash@zash.se>
parents:
7707
diff
changeset
|
169 return _SOCKETINVALID; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 |
8271
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
172 function interface:server() |
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
173 return self._server or self; |
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
174 end |
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
175 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
176 -- Get IP address |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 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
|
178 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
|
179 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
180 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
181 -- Get a port number, doesn't matter which |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
182 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
|
183 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
|
184 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
185 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
186 -- Get local port number |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
187 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
|
188 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
|
189 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
190 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
191 -- Get remote port |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
192 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
|
193 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
|
194 return self.sockport; |
8271
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
195 elseif self._server then |
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
196 self._server:port(); |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
197 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
200 -- Return underlying socket |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 function interface:socket() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 return self.conn; |
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 |
7910
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
205 function interface:set_mode(new_mode) |
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
206 self._pattern = new_mode; |
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
207 end |
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
208 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 function interface:setoption(k, v) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 -- LuaSec doesn't expose setoption :( |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 if self.conn.setoption then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 self.conn:setoption(k, v); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
216 -- Timeout for detecting dead or idle sockets |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 function interface:setreadtimeout(t) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 if t == false then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 if self._readtimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 self._readtimeout:close(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 self._readtimeout = nil; |
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 return |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
225 t = t or cfg.read_timeout; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 if self._readtimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 self._readtimeout[1] = gettime() + t; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 resort_timers = true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 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
|
231 if self:on("readtimeout") then |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 return cfg.read_timeout; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 else |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
234 self:on("disconnect", "read timeout"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 end); |
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 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
241 -- Timeout for detecting dead sockets |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 function interface:setwritetimeout(t) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 if t == false then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 if self._writetimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 self._writetimeout:close(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 self._writetimeout = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 return |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 t = t or cfg.write_timeout; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 if self._writetimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 self._writetimeout[1] = gettime() + t; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 resort_timers = true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 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
|
256 self:on("disconnect", "write timeout"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 end); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
262 -- lua-epoll flag for currently requested poll state |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 function interface:flags() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 if self._wantread then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 if self._wantwrite then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 return "rw"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 return "r"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
269 elseif self._wantwrite then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 return "w"; |
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 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
274 -- Add or remove sockets or modify epoll flags |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 function interface:setflags(r, w) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 if r ~= nil then self._wantread = r; end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 if w ~= nil then self._wantwrite = w; end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 local flags = self:flags(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 local currentflags = self._flags; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 if flags == currentflags then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 return true; |
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 local fd = self:getfd(); |
7602
a4d5eec36a17
net.server_epoll: Prevent invalid FDs from being added to epoll
Kim Alvefur <zash@zash.se>
parents:
7601
diff
changeset
|
284 if fd < 0 then |
a4d5eec36a17
net.server_epoll: Prevent invalid FDs from being added to epoll
Kim Alvefur <zash@zash.se>
parents:
7601
diff
changeset
|
285 self._wantread, self._wantwrite = nil, nil; |
a4d5eec36a17
net.server_epoll: Prevent invalid FDs from being added to epoll
Kim Alvefur <zash@zash.se>
parents:
7601
diff
changeset
|
286 return nil, "invalid fd"; |
a4d5eec36a17
net.server_epoll: Prevent invalid FDs from being added to epoll
Kim Alvefur <zash@zash.se>
parents:
7601
diff
changeset
|
287 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 local op = "mod"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 if not flags then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 op = "del"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 elseif not currentflags then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
292 op = "add"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 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
|
295 -- 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
|
296 -- op, fd, flags or "", tostring(ok), err); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
297 if not ok then return ok, err end |
7625
6afafd6d4ef0
net.server_epoll: Move management of fd registry to :setflags
Kim Alvefur <zash@zash.se>
parents:
7624
diff
changeset
|
298 if op == "add" then |
6afafd6d4ef0
net.server_epoll: Move management of fd registry to :setflags
Kim Alvefur <zash@zash.se>
parents:
7624
diff
changeset
|
299 fds[fd] = self; |
6afafd6d4ef0
net.server_epoll: Move management of fd registry to :setflags
Kim Alvefur <zash@zash.se>
parents:
7624
diff
changeset
|
300 elseif op == "del" then |
6afafd6d4ef0
net.server_epoll: Move management of fd registry to :setflags
Kim Alvefur <zash@zash.se>
parents:
7624
diff
changeset
|
301 fds[fd] = nil; |
6afafd6d4ef0
net.server_epoll: Move management of fd registry to :setflags
Kim Alvefur <zash@zash.se>
parents:
7624
diff
changeset
|
302 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
303 self._flags = flags; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 return true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
306 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
307 -- Called when socket is readable |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
308 function interface:onreadable() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
309 local data, err, partial = self.conn:receive(self._pattern); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
310 if data then |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
311 self:onconnect(); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
312 self:on("incoming", data); |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
313 else |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
314 if partial and partial ~= "" then |
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
315 self:onconnect(); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
316 self:on("incoming", partial, err); |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
317 end |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
318 if err == "wantread" then |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
319 self:setflags(true, nil); |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
320 elseif err == "wantwrite" then |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
321 self:setflags(nil, true); |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
322 elseif err ~= "timeout" then |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
323 self:on("disconnect", err); |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
324 self:destroy() |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
325 return; |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
326 end |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
327 end |
7691
a5ad858f3a93
net.server_epoll: Check that connection still exists after callback, fixes traceback when closed
Kim Alvefur <zash@zash.se>
parents:
7687
diff
changeset
|
328 if not self.conn then return; end |
7606
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
329 if self.conn:dirty() then |
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
330 self:setreadtimeout(false); |
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
331 self:pausefor(cfg.read_retry_delay); |
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
332 else |
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
333 self:setreadtimeout(); |
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
334 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
335 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
337 -- Called when socket is writable |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
338 function interface:onwritable() |
8552
1fcefc16a55d
server_epoll: Onconnect should happen before first attempt to write to socket
Kim Alvefur <zash@zash.se>
parents:
8542
diff
changeset
|
339 self:onconnect(); |
8776
e7d4344e0fea
net.server_epoll: Handle connection being closed and destroyed in onconnect callback
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
340 if not self.conn then return; end -- could have been closed in onconnect |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 local buffer = self.writebuffer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
342 local data = t_concat(buffer); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 local ok, err, partial = self.conn:send(data); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
344 if ok then |
8541
8186114329d6
net.server_epoll: Remove special casing for having sent an empty write buffer because the wantwrite flag must be unset regardless
Kim Alvefur <zash@zash.se>
parents:
8540
diff
changeset
|
345 self:setflags(nil, false); |
8186114329d6
net.server_epoll: Remove special casing for having sent an empty write buffer because the wantwrite flag must be unset regardless
Kim Alvefur <zash@zash.se>
parents:
8540
diff
changeset
|
346 for i = #buffer, 1, -1 do |
8186114329d6
net.server_epoll: Remove special casing for having sent an empty write buffer because the wantwrite flag must be unset regardless
Kim Alvefur <zash@zash.se>
parents:
8540
diff
changeset
|
347 buffer[i] = nil; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 end |
8541
8186114329d6
net.server_epoll: Remove special casing for having sent an empty write buffer because the wantwrite flag must be unset regardless
Kim Alvefur <zash@zash.se>
parents:
8540
diff
changeset
|
349 self:setwritetimeout(false); |
8186114329d6
net.server_epoll: Remove special casing for having sent an empty write buffer because the wantwrite flag must be unset regardless
Kim Alvefur <zash@zash.se>
parents:
8540
diff
changeset
|
350 self:ondrain(); -- Be aware of writes in ondrain |
7589
125e4f2e0b65
net.server_epoll: Return early on successful write
Kim Alvefur <zash@zash.se>
parents:
7588
diff
changeset
|
351 return; |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
352 elseif partial then |
7591
138f16eb39fb
net.server_epoll: Improve syntax
Kim Alvefur <zash@zash.se>
parents:
7590
diff
changeset
|
353 buffer[1] = data:sub(partial+1); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 for i = #buffer, 2, -1 do |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
355 buffer[i] = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
356 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
357 self:setwritetimeout(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
359 if err == "wantwrite" or err == "timeout" then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
360 self:setflags(nil, true); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
361 elseif err == "wantread" then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
362 self:setflags(true, nil); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
363 elseif 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
|
364 self:on("disconnect", 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 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
369 -- The write buffer has been successfully emptied |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
370 function interface:ondrain() |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
371 return self:on("drain"); |
7547
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 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
374 -- Add data to write buffer and set flag for wanting to write |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
375 function interface:write(data) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
376 local buffer = self.writebuffer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
377 if buffer then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
378 t_insert(buffer, data); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
379 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
380 self.writebuffer = { data }; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
381 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
|
382 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
|
383 self:setflags(nil, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
384 return #data; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
385 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
386 interface.send = interface.write; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
387 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
388 -- Close, possibly after writing is done |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
389 function interface:close() |
8007
67f8954875a4
net.server_epoll: Check write buffer for remaining data to write when closing
Kim Alvefur <zash@zash.se>
parents:
8006
diff
changeset
|
390 if self.writebuffer and self.writebuffer[1] then |
7593
0266434d7df1
net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents:
7592
diff
changeset
|
391 self:setflags(false, true); -- Flush final buffer contents |
0266434d7df1
net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents:
7592
diff
changeset
|
392 self.write, self.send = noop, noop; -- No more writing |
7555
f3abb5f891eb
net.server_epoll: Some logging when closing a connection
Kim Alvefur <zash@zash.se>
parents:
7554
diff
changeset
|
393 log("debug", "Close %s after writing", tostring(self)); |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
394 self.ondrain = interface.close; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
395 else |
7592
10d2f008321d
net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents:
7591
diff
changeset
|
396 log("debug", "Close %s now", tostring(self)); |
7593
0266434d7df1
net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents:
7592
diff
changeset
|
397 self.write, self.send = noop, noop; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
398 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
|
399 self:on("disconnect"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
400 self:destroy(); |
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 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
403 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
404 function interface:destroy() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
405 self:setflags(false, false); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
406 self:setwritetimeout(false); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
407 self:setreadtimeout(false); |
7594
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
408 self.onreadable = noop; |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
409 self.onwritable = noop; |
7594
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
410 self.destroy = noop; |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
411 self.close = noop; |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
412 self.on = noop; |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
413 self.conn:close(); |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
414 self.conn = nil; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
415 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
416 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
417 function interface:ssl() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
418 return self._tls; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
419 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
420 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
421 function interface:starttls(ctx) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
422 if ctx then self.tls = ctx; end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
423 if self.writebuffer and self.writebuffer[1] then |
7592
10d2f008321d
net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents:
7591
diff
changeset
|
424 log("debug", "Start TLS on %s after write", tostring(self)); |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
425 self.ondrain = interface.starttls; |
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
426 self.starttls = false; |
7603
4e2b019ff176
net.server_epoll: Make sure wantwrite is set when starting TLS
Kim Alvefur <zash@zash.se>
parents:
7602
diff
changeset
|
427 self:setflags(nil, true); -- make sure wantwrite is set |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
428 else |
7592
10d2f008321d
net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents:
7591
diff
changeset
|
429 log("debug", "Start TLS on %s now", tostring(self)); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
430 self:setflags(false, false); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
431 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
|
432 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
|
433 self:on("disconnect", err); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
434 self:destroy(); |
7595
166b2e0766a4
net.server_epoll: Return status from starttls method
Kim Alvefur <zash@zash.se>
parents:
7594
diff
changeset
|
435 return conn, err; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
436 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
437 conn:settimeout(0); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
438 self.conn = conn; |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
439 self.ondrain = nil; |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
440 self.onwritable = interface.tlshandskake; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
441 self.onreadable = interface.tlshandskake; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
442 self:setflags(true, true); |
7598
335b5ce06062
net.server_epoll: TLS handshake timeout
Kim Alvefur <zash@zash.se>
parents:
7597
diff
changeset
|
443 self:setwritetimeout(cfg.handshake_timeout); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
444 end |
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:tlshandskake() |
7598
335b5ce06062
net.server_epoll: TLS handshake timeout
Kim Alvefur <zash@zash.se>
parents:
7597
diff
changeset
|
448 self:setwritetimeout(false); |
335b5ce06062
net.server_epoll: TLS handshake timeout
Kim Alvefur <zash@zash.se>
parents:
7597
diff
changeset
|
449 self:setreadtimeout(false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
450 local ok, err = self.conn:dohandshake(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
451 if ok then |
7592
10d2f008321d
net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents:
7591
diff
changeset
|
452 log("debug", "TLS handshake on %s complete", tostring(self)); |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
453 self.onwritable = nil; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
454 self.onreadable = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
455 self._tls = true; |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
456 self:on("status", "ssl-handshake-complete"); |
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
457 self:init(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
458 elseif err == "wantread" then |
7592
10d2f008321d
net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents:
7591
diff
changeset
|
459 log("debug", "TLS handshake on %s to wait until readable", tostring(self)); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
460 self:setflags(true, false); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
461 self:setreadtimeout(cfg.handshake_timeout); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
462 elseif err == "wantwrite" then |
7592
10d2f008321d
net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents:
7591
diff
changeset
|
463 log("debug", "TLS handshake on %s to wait until writable", tostring(self)); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
464 self:setflags(false, true); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
465 self:setwritetimeout(cfg.handshake_timeout); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
466 else |
7592
10d2f008321d
net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents:
7591
diff
changeset
|
467 log("debug", "TLS handshake error on %s: %s", tostring(self), err); |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
468 self:on("disconnect", err); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
469 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
470 end |
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 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
|
474 client:settimeout(0); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
475 local conn = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
476 conn = client; |
8271
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
477 _server = server; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
478 created = gettime(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
479 listeners = listeners; |
7811
24f99d11b792
net.server_epoll: Fix traceback if wrapclient called without a pattern or parent server
Kim Alvefur <zash@zash.se>
parents:
7800
diff
changeset
|
480 _pattern = pattern or (server and server._pattern); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
481 writebuffer = {}; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
482 tls = tls; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
483 }, interface_mt); |
7591
138f16eb39fb
net.server_epoll: Improve syntax
Kim Alvefur <zash@zash.se>
parents:
7590
diff
changeset
|
484 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
485 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
|
486 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
|
487 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
488 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
|
489 conn.sockname, conn.sockport = client:getsockname(); |
7547
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 return conn; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
492 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
493 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
494 -- A server interface has new incoming connections waiting |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
495 -- This replaces the onreadable callback |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
496 function interface:onacceptable() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
497 local conn, err = self.conn:accept(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
498 if not conn then |
7574
97b8506118a9
net.server_epoll: Fix missing quotes in logging call
Kim Alvefur <zash@zash.se>
parents:
7573
diff
changeset
|
499 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
|
500 self:pausefor(cfg.accept_retry_interval); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
501 return; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
502 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
503 local client = wrapsocket(conn, self, nil, self.listeners, self.tls); |
7592
10d2f008321d
net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents:
7591
diff
changeset
|
504 log("debug", "New connection %s", tostring(client)); |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
505 client:init(); |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
506 end |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
507 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
508 -- Initialization |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
509 function interface:init() |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
510 if self.tls and not self._tls then |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
511 return self:starttls(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
512 else |
7626 | 513 self:setwritetimeout(); |
8506
174fd716c9fa
server_epoll: Rework how socket readyness is detected
Kim Alvefur <zash@zash.se>
parents:
8271
diff
changeset
|
514 return self:setflags(true, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
515 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
516 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
517 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
518 function interface:pause() |
7626 | 519 return self:setflags(false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
520 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
521 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
522 function interface:resume() |
7626 | 523 return self:setflags(true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
524 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
525 |
7624 | 526 -- Pause connection for some time |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
527 function interface:pausefor(t) |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
528 if self._pausefor then |
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
529 self._pausefor:close(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
530 end |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
531 if t == false then return; end |
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
532 self:setflags(false); |
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
533 self._pausefor = addtimer(t, function () |
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
534 self._pausefor = nil; |
7707
7bb1cc7278b6
net.server_epoll: Make sure socket exists before checking if there is buffered data in it
Kim Alvefur <zash@zash.se>
parents:
7691
diff
changeset
|
535 if self.conn and self.conn:dirty() then |
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
|
536 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
|
537 end |
7628
7f2159eac697
net.server_epoll: Fix timer returning boolean
Kim Alvefur <zash@zash.se>
parents:
7627
diff
changeset
|
538 self:setflags(true); |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
539 end); |
7547
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 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
542 -- Connected! |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
543 function interface:onconnect() |
8778
0364454bdd77
net.server_epoll: Guard against connection missing the getpeername method (thanks LuaSec)
Kim Alvefur <zash@zash.se>
parents:
8777
diff
changeset
|
544 if self.conn and not self.peername and self.conn.getpeername then |
8777
2bfe963f3041
net.server_epoll: Retry retrieving peername once connected
Kim Alvefur <zash@zash.se>
parents:
8776
diff
changeset
|
545 self.peername, self.peerport = self.conn:getpeername(); |
2bfe963f3041
net.server_epoll: Retry retrieving peername once connected
Kim Alvefur <zash@zash.se>
parents:
8776
diff
changeset
|
546 end |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
547 self.onconnect = noop; |
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
548 self:on("connect"); |
8506
174fd716c9fa
server_epoll: Rework how socket readyness is detected
Kim Alvefur <zash@zash.se>
parents:
8271
diff
changeset
|
549 end |
174fd716c9fa
server_epoll: Rework how socket readyness is detected
Kim Alvefur <zash@zash.se>
parents:
8271
diff
changeset
|
550 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
551 local function addserver(addr, port, listeners, pattern, tls) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
552 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
|
553 if not conn then return conn, err; end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
554 conn:settimeout(0); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
555 local server = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
556 conn = conn; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
557 created = gettime(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
558 listeners = listeners; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
559 _pattern = pattern; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
560 onreadable = interface.onacceptable; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
561 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
|
562 sockname = addr; |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
563 sockport = port; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
564 }, interface_mt); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
565 server:setflags(true, false); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
566 return server; |
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 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
569 -- COMPAT |
7551
838212918f11
net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents:
7550
diff
changeset
|
570 local function wrapclient(conn, addr, port, listeners, pattern, tls) |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
571 local client = wrapsocket(conn, nil, pattern, listeners, tls); |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
572 if not client.peername then |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
573 client.peername, client.peerport = addr, port; |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
574 end |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
575 client:init(); |
7551
838212918f11
net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents:
7550
diff
changeset
|
576 return client; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
577 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
578 |
7800
dadf9ba290c5
server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents:
7799
diff
changeset
|
579 -- New outgoing TCP connection |
7553
4f3e4a092348
net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents:
7552
diff
changeset
|
580 local function addclient(addr, port, listeners, pattern, tls) |
7596
2c4ba55b616c
net.server_epoll: Connect async in addclient
Kim Alvefur <zash@zash.se>
parents:
7595
diff
changeset
|
581 local conn, err = socket.tcp(); |
7553
4f3e4a092348
net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents:
7552
diff
changeset
|
582 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
|
583 conn:settimeout(0); |
7596
2c4ba55b616c
net.server_epoll: Connect async in addclient
Kim Alvefur <zash@zash.se>
parents:
7595
diff
changeset
|
584 conn:connect(addr, port); |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
585 local client = wrapsocket(conn, nil, pattern, listeners, tls) |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
586 client:init(); |
7556
1f777b38b66c
net.server_epoll: Make addclient conform to API (expected by net.http.request)
Kim Alvefur <zash@zash.se>
parents:
7555
diff
changeset
|
587 return client, conn; |
7553
4f3e4a092348
net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents:
7552
diff
changeset
|
588 end |
4f3e4a092348
net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents:
7552
diff
changeset
|
589 |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
590 local function watchfd(fd, onreadable, onwriteable) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
591 local conn = setmetatable({ |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
592 conn = fd; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
593 onreadable = onreadable; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
594 onwriteable = onwriteable; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
595 close = function (self) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
596 self:setflags(false, false); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
597 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
598 }, interface_mt); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
599 if type(fd) == "number" then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
600 conn.getfd = function () |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
601 return fd; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
602 end; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
603 -- Otherwise it'll need to be something LuaSocket-compatible |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
604 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
605 conn:setflags(onreadable, onwriteable); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
606 return conn; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
607 end; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
608 |
7800
dadf9ba290c5
server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents:
7799
diff
changeset
|
609 -- Dump all data from one connection into another |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
610 local function link(from, to) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
611 from.listeners = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
612 onincoming = function (_, data) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
613 from:pause(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
614 to:write(data); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
615 end, |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
616 }, {__index=from.listeners}); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
617 to.listeners = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
618 ondrain = function () |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
619 from:resume(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
620 end, |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
621 }, {__index=to.listeners}); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
622 from:setflags(true, nil); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
623 to:setflags(nil, true); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
624 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
625 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
626 -- XXX What uses this? |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
627 -- net.adns |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
628 function interface:set_send(new_send) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
629 self.send = new_send; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
630 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
631 |
7800
dadf9ba290c5
server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents:
7799
diff
changeset
|
632 -- Close all connections and servers |
7794
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
633 local function closeall() |
7799
93643a61858a
server_epoll: Ignore unused loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7797
diff
changeset
|
634 for fd, conn in pairs(fds) do -- luacheck: ignore 213/fd |
7794
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
635 conn:close(); |
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
636 end |
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
637 end |
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
638 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
639 local quitting = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
640 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
641 -- Signal main loop about shutdown via above upvalue |
7796
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
642 local function setquitting(quit) |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
643 if quit then |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
644 quitting = "quitting"; |
7797
d0c32d86724f
server_epoll: Flush and close all connections before quitting mainloop so no data is lost in buffers
Kim Alvefur <zash@zash.se>
parents:
7796
diff
changeset
|
645 closeall(); |
7796
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
646 else |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
647 quitting = nil; |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
648 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
649 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
650 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
651 -- Main loop |
7795
03081bd5c5bb
net.server_epoll: Add the 'once' argument to loop for parity with server_select
Kim Alvefur <zash@zash.se>
parents:
7794
diff
changeset
|
652 local function loop(once) |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
653 repeat |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
654 local t = runtimers(cfg.max_wait, cfg.min_wait); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
655 local fd, r, w = epoll.wait(t); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
656 if fd then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
657 local conn = fds[fd]; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
658 if conn then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
659 if r then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
660 conn:onreadable(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
661 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
662 if w then |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
663 conn:onwritable(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
664 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
665 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
666 log("debug", "Removing unknown fd %d", fd); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
667 epoll.ctl("del", fd); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
668 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
669 elseif r ~= "timeout" then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
670 log("debug", "epoll_wait error: %s", tostring(r)); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
671 end |
7797
d0c32d86724f
server_epoll: Flush and close all connections before quitting mainloop so no data is lost in buffers
Kim Alvefur <zash@zash.se>
parents:
7796
diff
changeset
|
672 until once or (quitting and next(fds) == nil); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
673 return quitting; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
674 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
675 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
676 return { |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
677 get_backend = function () return "epoll"; end; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
678 addserver = addserver; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
679 addclient = addclient; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
680 add_task = addtimer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
681 at = at; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
682 loop = loop; |
7794
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
683 closeall = closeall; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
684 setquitting = setquitting; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
685 wrapclient = wrapclient; |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
686 watchfd = watchfd; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
687 link = link; |
7657
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
688 set_config = function (newconfig) |
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
689 cfg = setmetatable(newconfig, default_config); |
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
690 end; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
691 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
692 -- libevent emulation |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
693 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
|
694 addevent = function (fd, mode, callback) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
695 local function onevent(self) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
696 local ret = self:callback(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
697 if ret == -1 then |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
698 self:setflags(false, false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
699 elseif ret then |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
700 self:setflags(mode == "r" or mode == "rw", mode == "w" or mode == "rw"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
701 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
702 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
703 |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
704 local conn = setmetatable({ |
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
705 getfd = function () return fd; end; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
706 callback = callback; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
707 onreadable = onevent; |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
708 onwritable = onevent; |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
709 close = function (self) |
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
710 self:setflags(false, false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
711 fds[fd] = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
712 end; |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
713 }, interface_mt); |
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
714 local ok, err = conn:setflags(mode == "r" or mode == "rw", mode == "w" or mode == "rw"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
715 if not ok then return ok, err; end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
716 return conn; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
717 end; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
718 }; |