Software /
code /
prosody
Annotate
net/server_epoll.lua @ 9821:7712488c8e49 0.11
net.server_epoll: Use send_timeout for write timout like other implementations (fixes #1316)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 07 Feb 2019 14:02:15 +0100 |
parent | 9665:4790d1b88919 |
child | 9822:13c2707d2417 |
rev | line source |
---|---|
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- Prosody IM |
9474
619ba78709a5
net.server_epoll: Update copyright year
Kim Alvefur <zash@zash.se>
parents:
9473
diff
changeset
|
2 -- Copyright (C) 2016-2018 Kim Alvefur |
7547
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 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local t_sort = table.sort; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local t_insert = table.insert; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local t_remove = table.remove; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local t_concat = table.concat; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local setmetatable = setmetatable; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 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
|
15 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
|
16 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
|
17 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
|
18 local pairs = pairs; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local log = require "util.logger".init("server_epoll"); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 local socket = require "socket"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local luasec = require "ssl"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local gettime = require "util.time".now; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local createtable = require "util.table".create; |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
24 local inet = require "util.net"; |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
25 local inet_pton = inet.pton; |
7737
f93b3083b46b
net.server_epoll: Use _SOCKETINVALID from LuaSocket
Kim Alvefur <zash@zash.se>
parents:
7707
diff
changeset
|
26 local _SOCKETINVALID = socket._SOCKETINVALID or -1; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 |
9506
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
28 local poller = require "util.poll" |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
29 local EEXIST = poller.EEXIST; |
9507
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9506
diff
changeset
|
30 local ENOENT = poller.ENOENT; |
9506
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
31 |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
32 local poll = assert(poller.new()); |
7601
ce20546d7714
net.server_epoll: Make sure a recent LuaSocket is avaliable
Kim Alvefur <zash@zash.se>
parents:
7600
diff
changeset
|
33 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8552
diff
changeset
|
35 -- luacheck: std none |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 |
7657
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
37 local default_config = { __index = { |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
38 -- If a connection is silent for this long, close it unless onreadtimeout says not to |
8983
23f66f04e216
net.server: Set default read timeout to 14 minutes (fixes #971)
Kim Alvefur <zash@zash.se>
parents:
8781
diff
changeset
|
39 read_timeout = 14 * 60; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
40 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
41 -- How long to wait for a socket to become writable after queuing data to send |
9821
7712488c8e49
net.server_epoll: Use send_timeout for write timout like other implementations (fixes #1316)
Kim Alvefur <zash@zash.se>
parents:
9665
diff
changeset
|
42 send_timeout = 60; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
43 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
44 -- Some number possibly influencing how many pending connections can be accepted |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 tcp_backlog = 128; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
46 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
47 -- If accepting a new incoming connection fails, wait this long before trying again |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 accept_retry_interval = 10; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
49 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
50 -- If there is still more data to read from LuaSocktes buffer, wait this long and read again |
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
|
51 read_retry_delay = 1e-06; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
52 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
53 -- Size of chunks to read from sockets |
9311
9b0604fe01f1
net.server_epoll: Make read_size a server-wide option
Kim Alvefur <zash@zash.se>
parents:
9310
diff
changeset
|
54 read_size = 8192; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
55 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
56 -- Timeout used during between steps in TLS handshakes |
7598
335b5ce06062
net.server_epoll: TLS handshake timeout
Kim Alvefur <zash@zash.se>
parents:
7597
diff
changeset
|
57 handshake_timeout = 60; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
58 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
59 -- Maximum and minimum amount of time to sleep waiting for events (adjusted for pending timers) |
7623
a1c040a5754f
net.server_epoll: Make maximum wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7606
diff
changeset
|
60 max_wait = 86400; |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
61 min_wait = 1e-06; |
7657
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
62 }}; |
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
63 local cfg = default_config.__index; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 local fds = createtable(10, 0); -- FD -> conn |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
66 |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
67 -- Timer and scheduling -- |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
68 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 local timers = {}; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 local function noop() end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 local function closetimer(t) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 t[1] = 0; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 t[2] = noop; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
77 -- Set to true when timers have changed |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 local resort_timers = false; |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
79 |
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
80 -- Add absolute timer |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 local function at(time, f) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 local timer = { time, f, close = closetimer }; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 t_insert(timers, timer); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 resort_timers = true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 return timer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 end |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
87 |
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
88 -- Add relative timer |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 local function addtimer(timeout, f) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 return at(gettime() + timeout, f); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
93 -- Run callbacks of expired timers |
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
94 -- 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
|
95 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
|
96 -- 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
|
97 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
|
98 return next_delay; |
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
99 end |
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
100 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 if resort_timers then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 -- Sort earliest timers to the end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 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
|
104 resort_timers = false; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 -- Iterate from the end and remove completed timers |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 for i = #timers, 1, -1 do |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 local timer = timers[i]; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 local t, f = timer[1], timer[2]; |
7624 | 111 -- Get time for every iteration to increase accuracy |
112 local now = gettime(); | |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 if t > now then |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
114 -- This timer should not fire yet |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 local diff = t - now; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 if diff < next_delay then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 next_delay = diff; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 end |
7558
3a2fe8e10eeb
net.server_epoll: Fix calculation of next timer event
Kim Alvefur <zash@zash.se>
parents:
7557
diff
changeset
|
119 break; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 local new_timeout = f(now); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 if new_timeout then |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
123 -- Schedule for 'delay' from the time actually scheduled, |
7624 | 124 -- 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
|
125 timer[1] = t + new_timeout; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 resort_timers = true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 t_remove(timers, i); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 end |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
131 |
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
132 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
|
133 -- 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
|
134 -- 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
|
135 -- 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
|
136 -- 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
|
137 -- 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
|
138 next_delay = min_wait; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 end |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
140 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 return next_delay; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
144 -- Socket handler interface |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
145 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 local interface = {}; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 local interface_mt = { __index = interface }; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 function interface_mt:__tostring() |
7585
b64218c816de
net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents:
7584
diff
changeset
|
150 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
|
151 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
|
152 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
|
153 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
|
154 end |
9321
146dc1e4c7ff
net.server_epoll: Simplify minimal string representation
Kim Alvefur <zash@zash.se>
parents:
9320
diff
changeset
|
155 return ("FD %d"):format(self:getfd()); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
158 -- Replace the listener and tell the old one |
9387
33e52f727f0f
net.connect: Fix passing request table to new listener
Kim Alvefur <zash@zash.se>
parents:
9385
diff
changeset
|
159 function interface:setlistener(listeners, data) |
7604
36408ef6c8c9
net.server_epoll: Call ondetach when listeners are replaced
Kim Alvefur <zash@zash.se>
parents:
7603
diff
changeset
|
160 self:on("detach"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 self.listeners = listeners; |
9387
33e52f727f0f
net.connect: Fix passing request table to new listener
Kim Alvefur <zash@zash.se>
parents:
9385
diff
changeset
|
162 self:on("attach", data); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
165 -- 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
|
166 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
|
167 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
|
168 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
|
169 return; |
be374a5778f3
net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents:
7910
diff
changeset
|
170 end |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
171 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
|
172 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
|
173 -- 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
|
174 return; |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
175 end |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
176 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
|
177 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
|
178 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
|
179 end |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
180 return err; |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
181 end |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
182 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
183 -- Return the file descriptor number |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 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
|
185 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
|
186 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
|
187 end |
7737
f93b3083b46b
net.server_epoll: Use _SOCKETINVALID from LuaSocket
Kim Alvefur <zash@zash.se>
parents:
7707
diff
changeset
|
188 return _SOCKETINVALID; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 |
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
|
191 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
|
192 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
|
193 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
|
194 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
195 -- Get IP address |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 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
|
197 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
|
198 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
199 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
200 -- 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
|
201 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
|
202 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
|
203 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
204 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
205 -- 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
|
206 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
|
207 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
|
208 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
209 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
210 -- 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
217 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
218 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
219 -- Return underlying socket |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 function interface:socket() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
221 return self.conn; |
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 |
7910
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
224 function interface:set_mode(new_mode) |
9310
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
225 self.read_size = new_mode; |
7910
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
226 end |
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
227 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 function interface:setoption(k, v) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 -- LuaSec doesn't expose setoption :( |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 if self.conn.setoption then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 self.conn:setoption(k, v); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
235 -- Timeout for detecting dead or idle sockets |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 function interface:setreadtimeout(t) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 if t == false then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 if self._readtimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 self._readtimeout:close(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 self._readtimeout = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 return |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 t = t or cfg.read_timeout; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 if self._readtimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 self._readtimeout[1] = gettime() + t; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 resort_timers = true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 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
|
250 if self:on("readtimeout") then |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 return cfg.read_timeout; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 else |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
253 self:on("disconnect", "read timeout"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 end); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
260 -- Timeout for detecting dead sockets |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 function interface:setwritetimeout(t) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 if t == false then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 if self._writetimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 self._writetimeout:close(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 self._writetimeout = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 return |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 end |
9821
7712488c8e49
net.server_epoll: Use send_timeout for write timout like other implementations (fixes #1316)
Kim Alvefur <zash@zash.se>
parents:
9665
diff
changeset
|
269 t = t or cfg.send_timeout; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 if self._writetimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
271 self._writetimeout[1] = gettime() + t; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 resort_timers = true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 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
|
275 self:on("disconnect", "write timeout"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 end); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
281 function interface:add(r, w) |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
282 local fd = self:getfd(); |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
283 if fd < 0 then |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
284 return nil, "invalid fd"; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
285 end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
286 if r == nil then r = self._wantread; end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
287 if w == nil then w = self._wantwrite; end |
9384
5c8006ce8cb8
net.server_epoll: Include errno in log messages
Kim Alvefur <zash@zash.se>
parents:
9383
diff
changeset
|
288 local ok, err, errno = poll:add(fd, r, w); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
289 if not ok then |
9506
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
290 if errno == EEXIST then |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
291 log("debug", "%s already registered!", self); |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
292 return self:set(r, w); -- So try to change its flags |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
293 end |
9441
116a9d0a295c
net.server_epoll: Pass error number to logging
Kim Alvefur <zash@zash.se>
parents:
9439
diff
changeset
|
294 log("error", "Could not register %s: %s(%d)", self, err, errno); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
295 return ok, err; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
296 end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
297 self._wantread, self._wantwrite = r, w; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
298 fds[fd] = self; |
9381
80be543a3785
net.server_epoll: Watching instead of registering, seems more understandable
Kim Alvefur <zash@zash.se>
parents:
9321
diff
changeset
|
299 log("debug", "Watching %s", self); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
300 return true; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
302 |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
303 function interface:set(r, w) |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 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
|
305 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
|
306 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
|
307 end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
308 if r == nil then r = self._wantread; end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
309 if w == nil then w = self._wantwrite; end |
9384
5c8006ce8cb8
net.server_epoll: Include errno in log messages
Kim Alvefur <zash@zash.se>
parents:
9383
diff
changeset
|
310 local ok, err, errno = poll:set(fd, r, w); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
311 if not ok then |
9384
5c8006ce8cb8
net.server_epoll: Include errno in log messages
Kim Alvefur <zash@zash.se>
parents:
9383
diff
changeset
|
312 log("error", "Could not update poller state %s: %s(%d)", self, err, errno); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
313 return ok, err; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
314 end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
315 self._wantread, self._wantwrite = r, w; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
316 return true; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
317 end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
318 |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
319 function interface:del() |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
320 local fd = self:getfd(); |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
321 if fd < 0 then |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
322 return nil, "invalid fd"; |
7625
6afafd6d4ef0
net.server_epoll: Move management of fd registry to :setflags
Kim Alvefur <zash@zash.se>
parents:
7624
diff
changeset
|
323 end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
324 if fds[fd] ~= self then |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
325 return nil, "unregistered fd"; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
326 end |
9384
5c8006ce8cb8
net.server_epoll: Include errno in log messages
Kim Alvefur <zash@zash.se>
parents:
9383
diff
changeset
|
327 local ok, err, errno = poll:del(fd); |
9507
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9506
diff
changeset
|
328 if not ok and errno ~= ENOENT then |
9384
5c8006ce8cb8
net.server_epoll: Include errno in log messages
Kim Alvefur <zash@zash.se>
parents:
9383
diff
changeset
|
329 log("error", "Could not unregister %s: %s(%d)", self, err, errno); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
330 return ok, err; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
331 end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
332 self._wantread, self._wantwrite = nil, nil; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
333 fds[fd] = nil; |
9381
80be543a3785
net.server_epoll: Watching instead of registering, seems more understandable
Kim Alvefur <zash@zash.se>
parents:
9321
diff
changeset
|
334 log("debug", "Unwatched %s", self); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
335 return true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
337 |
9320
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
338 function interface:setflags(r, w) |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
339 if not(self._wantread or self._wantwrite) then |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
340 if not(r or w) then |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
341 return true; -- no change |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
342 end |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
343 return self:add(r, w); |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
344 end |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
345 if not(r or w) then |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
346 return self:del(); |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
347 end |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
348 return self:set(r, w); |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
349 end |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
350 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
351 -- Called when socket is readable |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
352 function interface:onreadable() |
9311
9b0604fe01f1
net.server_epoll: Make read_size a server-wide option
Kim Alvefur <zash@zash.se>
parents:
9310
diff
changeset
|
353 local data, err, partial = self.conn:receive(self.read_size or cfg.read_size); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
354 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
|
355 self:onconnect(); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
356 self:on("incoming", data); |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
357 else |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
358 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
|
359 self:onconnect(); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
360 self:on("incoming", partial, err); |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
361 end |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
362 if err == "wantread" then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
363 self:set(true, nil); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
364 elseif err == "wantwrite" then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
365 self:set(nil, true); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
366 elseif err ~= "timeout" then |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
367 self:on("disconnect", err); |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
368 self:destroy() |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
369 return; |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
370 end |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
371 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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 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
|
377 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
|
378 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
379 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
380 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
381 -- Called when socket is writable |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
382 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
|
383 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
|
384 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
|
385 local buffer = self.writebuffer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
386 local data = t_concat(buffer); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
387 local ok, err, partial = self.conn:send(data); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
388 if ok then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
389 self:set(nil, false); |
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
|
390 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
|
391 buffer[i] = nil; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
392 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
|
393 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
|
394 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
|
395 return; |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
396 elseif partial then |
7591
138f16eb39fb
net.server_epoll: Improve syntax
Kim Alvefur <zash@zash.se>
parents:
7590
diff
changeset
|
397 buffer[1] = data:sub(partial+1); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
398 for i = #buffer, 2, -1 do |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
399 buffer[i] = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
400 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
401 self:setwritetimeout(); |
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 if err == "wantwrite" or err == "timeout" then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
404 self:set(nil, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
405 elseif err == "wantread" then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
406 self:set(true, nil); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
407 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
|
408 self:on("disconnect", err); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
409 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
410 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
411 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
412 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
413 -- The write buffer has been successfully emptied |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
414 function interface:ondrain() |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
415 return self:on("drain"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
416 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
417 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
418 -- 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
|
419 function interface:write(data) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
420 local buffer = self.writebuffer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
421 if buffer then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
422 t_insert(buffer, data); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
423 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
424 self.writebuffer = { data }; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
425 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
|
426 self:setwritetimeout(); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
427 self:set(nil, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
428 return #data; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
429 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
430 interface.send = interface.write; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
431 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
432 -- Close, possibly after writing is done |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
433 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
|
434 if self.writebuffer and self.writebuffer[1] then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
435 self:set(false, true); -- Flush final buffer contents |
7593
0266434d7df1
net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents:
7592
diff
changeset
|
436 self.write, self.send = noop, noop; -- No more writing |
9301
2f9687c09e6e
net.server_epoll: Remove coercion of connections to strings
Kim Alvefur <zash@zash.se>
parents:
9300
diff
changeset
|
437 log("debug", "Close %s after writing", self); |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
438 self.ondrain = interface.close; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
439 else |
9301
2f9687c09e6e
net.server_epoll: Remove coercion of connections to strings
Kim Alvefur <zash@zash.se>
parents:
9300
diff
changeset
|
440 log("debug", "Close %s now", self); |
7593
0266434d7df1
net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents:
7592
diff
changeset
|
441 self.write, self.send = noop, noop; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
442 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
|
443 self:on("disconnect"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
444 self:destroy(); |
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 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
447 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
448 function interface:destroy() |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
449 self:del(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
450 self:setwritetimeout(false); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
451 self:setreadtimeout(false); |
7594
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
452 self.onreadable = noop; |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
453 self.onwritable = noop; |
7594
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
454 self.destroy = noop; |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
455 self.close = noop; |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
456 self.on = noop; |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
457 self.conn:close(); |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
458 self.conn = nil; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
459 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
460 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
461 function interface:ssl() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
462 return self._tls; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
463 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
464 |
9300
1df8832bd29e
net.server_epoll: Rename all variables holding TLS contexts for consistency
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
465 function interface:starttls(tls_ctx) |
1df8832bd29e
net.server_epoll: Rename all variables holding TLS contexts for consistency
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
466 if tls_ctx then self.tls_ctx = tls_ctx; end |
9430
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
467 self.starttls = false; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
468 if self.writebuffer and self.writebuffer[1] then |
9301
2f9687c09e6e
net.server_epoll: Remove coercion of connections to strings
Kim Alvefur <zash@zash.se>
parents:
9300
diff
changeset
|
469 log("debug", "Start TLS on %s after write", self); |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
470 self.ondrain = interface.starttls; |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
471 self:set(nil, true); -- make sure wantwrite is set |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
472 else |
9430
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
473 if self.ondrain == interface.starttls then |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
474 self.ondrain = nil; |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
475 end |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
476 self.onwritable = interface.tlshandskake; |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
477 self.onreadable = interface.tlshandskake; |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
478 self:set(true, true); |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
479 log("debug", "Prepare to start TLS on %s", self); |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
480 end |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
481 end |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
482 |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
483 function interface:tlshandskake() |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
484 self:setwritetimeout(false); |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
485 self:setreadtimeout(false); |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
486 if not self._tls then |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
487 self._tls = true; |
9301
2f9687c09e6e
net.server_epoll: Remove coercion of connections to strings
Kim Alvefur <zash@zash.se>
parents:
9300
diff
changeset
|
488 log("debug", "Start TLS on %s now", self); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
489 self:del(); |
9431
c3c0523a37c6
net.server_epoll: Catch errors from luasec.wrap
Kim Alvefur <zash@zash.se>
parents:
9430
diff
changeset
|
490 local ok, conn, err = pcall(luasec.wrap, self.conn, self.tls_ctx); |
c3c0523a37c6
net.server_epoll: Catch errors from luasec.wrap
Kim Alvefur <zash@zash.se>
parents:
9430
diff
changeset
|
491 if not ok then |
c3c0523a37c6
net.server_epoll: Catch errors from luasec.wrap
Kim Alvefur <zash@zash.se>
parents:
9430
diff
changeset
|
492 conn, err = ok, conn; |
9573
57637247356d
net.server_epoll: Shuffle variables earlier to improve readability
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
493 log("error", "Failed to initialize TLS: %s", err); |
9431
c3c0523a37c6
net.server_epoll: Catch errors from luasec.wrap
Kim Alvefur <zash@zash.se>
parents:
9430
diff
changeset
|
494 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
495 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
|
496 self:on("disconnect", err); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
497 self:destroy(); |
7595
166b2e0766a4
net.server_epoll: Return status from starttls method
Kim Alvefur <zash@zash.se>
parents:
7594
diff
changeset
|
498 return conn, err; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
499 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
500 conn:settimeout(0); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
501 self.conn = conn; |
9430
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
502 self:on("starttls"); |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
503 self.ondrain = nil; |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
504 self.onwritable = interface.tlshandskake; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
505 self.onreadable = interface.tlshandskake; |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
506 return self:init(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
507 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
508 local ok, err = self.conn:dohandshake(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
509 if ok then |
9301
2f9687c09e6e
net.server_epoll: Remove coercion of connections to strings
Kim Alvefur <zash@zash.se>
parents:
9300
diff
changeset
|
510 log("debug", "TLS handshake on %s complete", self); |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
511 self.onwritable = nil; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
512 self.onreadable = nil; |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
513 self:on("status", "ssl-handshake-complete"); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
514 self:setwritetimeout(); |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
515 self:set(true, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
516 elseif err == "wantread" then |
9301
2f9687c09e6e
net.server_epoll: Remove coercion of connections to strings
Kim Alvefur <zash@zash.se>
parents:
9300
diff
changeset
|
517 log("debug", "TLS handshake on %s to wait until readable", self); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
518 self:set(true, false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
519 self:setreadtimeout(cfg.handshake_timeout); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
520 elseif err == "wantwrite" then |
9301
2f9687c09e6e
net.server_epoll: Remove coercion of connections to strings
Kim Alvefur <zash@zash.se>
parents:
9300
diff
changeset
|
521 log("debug", "TLS handshake on %s to wait until writable", self); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
522 self:set(false, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
523 self:setwritetimeout(cfg.handshake_timeout); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
524 else |
9301
2f9687c09e6e
net.server_epoll: Remove coercion of connections to strings
Kim Alvefur <zash@zash.se>
parents:
9300
diff
changeset
|
525 log("debug", "TLS handshake error on %s: %s", self, err); |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
526 self:on("disconnect", err); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
527 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
528 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
529 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
530 |
9310
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
531 local function wrapsocket(client, server, read_size, listeners, tls_ctx) -- luasocket object -> interface object |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
532 client:settimeout(0); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
533 local conn = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
534 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
|
535 _server = server; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
536 created = gettime(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
537 listeners = listeners; |
9310
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
538 read_size = read_size or (server and server.read_size); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
539 writebuffer = {}; |
9432
2e3f66475cc8
net.server_epoll: Inherit Direct TLS flag from servers to their clients
Kim Alvefur <zash@zash.se>
parents:
9431
diff
changeset
|
540 tls_ctx = tls_ctx or (server and server.tls_ctx); |
2e3f66475cc8
net.server_epoll: Inherit Direct TLS flag from servers to their clients
Kim Alvefur <zash@zash.se>
parents:
9431
diff
changeset
|
541 tls_direct = server and server.tls_direct; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
542 }, interface_mt); |
7591
138f16eb39fb
net.server_epoll: Improve syntax
Kim Alvefur <zash@zash.se>
parents:
7590
diff
changeset
|
543 |
9383
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
544 conn:updatenames(); |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
545 return conn; |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
546 end |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
547 |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
548 function interface:updatenames() |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
549 local conn = self.conn; |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
550 local ok, peername, peerport = pcall(conn.getpeername, conn); |
9382
31a2be28729c
net.server_epoll: Protect from errors in get{peer,sock}name
Kim Alvefur <zash@zash.se>
parents:
9381
diff
changeset
|
551 if ok then |
9383
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
552 self.peername, self.peerport = peername, peerport; |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
553 end |
9383
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
554 local ok, sockname, sockport = pcall(conn.getsockname, conn); |
9382
31a2be28729c
net.server_epoll: Protect from errors in get{peer,sock}name
Kim Alvefur <zash@zash.se>
parents:
9381
diff
changeset
|
555 if ok then |
9383
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
556 self.sockname, self.sockport = sockname, sockport; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
557 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
558 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
559 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
560 -- A server interface has new incoming connections waiting |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
561 -- This replaces the onreadable callback |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
562 function interface:onacceptable() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
563 local conn, err = self.conn:accept(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
564 if not conn then |
7574
97b8506118a9
net.server_epoll: Fix missing quotes in logging call
Kim Alvefur <zash@zash.se>
parents:
7573
diff
changeset
|
565 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
|
566 self:pausefor(cfg.accept_retry_interval); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
567 return; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
568 end |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
569 local client = wrapsocket(conn, self, nil, self.listeners); |
7592
10d2f008321d
net.server_epoll: Additional logging of connects and TLS
Kim Alvefur <zash@zash.se>
parents:
7591
diff
changeset
|
570 log("debug", "New connection %s", tostring(client)); |
9430
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
571 client:init(); |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
572 if self.tls_direct then |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
573 client:starttls(self.tls_ctx); |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
574 end |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
575 end |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
576 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
577 -- Initialization |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
578 function interface:init() |
9306
35c128b42509
net.server_epoll: Factor Direct TLS handling out of connection initialization
Kim Alvefur <zash@zash.se>
parents:
9301
diff
changeset
|
579 self:setwritetimeout(); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
580 return self:add(true, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
581 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
582 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
583 function interface:pause() |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
584 return self:set(false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
585 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
586 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
587 function interface:resume() |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
588 return self:set(true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
589 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
590 |
7624 | 591 -- Pause connection for some time |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
592 function interface:pausefor(t) |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
593 if self._pausefor then |
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
594 self._pausefor:close(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
595 end |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
596 if t == false then return; end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
597 self:set(false); |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
598 self._pausefor = addtimer(t, function () |
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
599 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
|
600 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
|
601 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
|
602 end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
603 self:set(true); |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
604 end); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
605 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
606 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
607 -- Connected! |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
608 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
|
609 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
|
610 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
|
611 end |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
612 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
|
613 self:on("connect"); |
8506
174fd716c9fa
server_epoll: Rework how socket readyness is detected
Kim Alvefur <zash@zash.se>
parents:
8271
diff
changeset
|
614 end |
174fd716c9fa
server_epoll: Rework how socket readyness is detected
Kim Alvefur <zash@zash.se>
parents:
8271
diff
changeset
|
615 |
9310
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
616 local function addserver(addr, port, listeners, read_size, tls_ctx) |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
617 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
|
618 if not conn then return conn, err; end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
619 conn:settimeout(0); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
620 local server = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
621 conn = conn; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
622 created = gettime(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
623 listeners = listeners; |
9310
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
624 read_size = read_size; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
625 onreadable = interface.onacceptable; |
9300
1df8832bd29e
net.server_epoll: Rename all variables holding TLS contexts for consistency
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
626 tls_ctx = tls_ctx; |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
627 tls_direct = tls_ctx and true or false; |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
628 sockname = addr; |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
629 sockport = port; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
630 }, interface_mt); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
631 server:add(true, false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
632 return server; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
633 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
634 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
635 -- COMPAT |
9310
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
636 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx) |
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
637 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx); |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
638 if not client.peername then |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
639 client.peername, client.peerport = addr, port; |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
640 end |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
641 local ok, err = client:init(); |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
642 if not ok then return ok, err; end |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
643 if tls_ctx then |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
644 client:starttls(tls_ctx); |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
645 end |
7551
838212918f11
net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents:
7550
diff
changeset
|
646 return client; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
647 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
648 |
7800
dadf9ba290c5
server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents:
7799
diff
changeset
|
649 -- New outgoing TCP connection |
9497
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
650 local function addclient(addr, port, listeners, read_size, tls_ctx, typ) |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
651 local create; |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
652 if not typ then |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
653 local n = inet_pton(addr); |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
654 if not n then return nil, "invalid-ip"; end |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
655 if #n == 16 then |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
656 typ = "tcp6"; |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
657 else |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
658 typ = "tcp4"; |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
659 end |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
660 end |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
661 if typ then |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
662 create = socket[typ]; |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
663 end |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
664 if type(create) ~= "function" then |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
665 return nil, "invalid socket type"; |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
666 end |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
667 local conn, err = create(); |
9498
cc593002f2e2
net.server_epoll: Check return conditions of early socket setup
Kim Alvefur <zash@zash.se>
parents:
9497
diff
changeset
|
668 local ok, err = conn:settimeout(0); |
cc593002f2e2
net.server_epoll: Check return conditions of early socket setup
Kim Alvefur <zash@zash.se>
parents:
9497
diff
changeset
|
669 if not ok then return ok, err; end |
9499
36e379f058c8
net.server: Swich method for connecting sockets with remotes
Kim Alvefur <zash@zash.se>
parents:
9498
diff
changeset
|
670 local ok, err = conn:setpeername(addr, port); |
9498
cc593002f2e2
net.server_epoll: Check return conditions of early socket setup
Kim Alvefur <zash@zash.se>
parents:
9497
diff
changeset
|
671 if not ok and err ~= "timeout" then return ok, err; end |
9310
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
672 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx) |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
673 local ok, err = client:init(); |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
674 if not ok then return ok, err; end |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
675 if tls_ctx then |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
676 client:starttls(tls_ctx); |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
677 end |
7556
1f777b38b66c
net.server_epoll: Make addclient conform to API (expected by net.http.request)
Kim Alvefur <zash@zash.se>
parents:
7555
diff
changeset
|
678 return client, conn; |
7553
4f3e4a092348
net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents:
7552
diff
changeset
|
679 end |
4f3e4a092348
net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents:
7552
diff
changeset
|
680 |
9319
7c954c75b6ac
net.server_epoll: Fix write signal handling in watchfd API
Kim Alvefur <zash@zash.se>
parents:
9314
diff
changeset
|
681 local function watchfd(fd, onreadable, onwritable) |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
682 local conn = setmetatable({ |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
683 conn = fd; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
684 onreadable = onreadable; |
9319
7c954c75b6ac
net.server_epoll: Fix write signal handling in watchfd API
Kim Alvefur <zash@zash.se>
parents:
9314
diff
changeset
|
685 onwritable = onwritable; |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
686 close = function (self) |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
687 self:del(); |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
688 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
689 }, interface_mt); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
690 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
|
691 conn.getfd = function () |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
692 return fd; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
693 end; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
694 -- 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
|
695 end |
9319
7c954c75b6ac
net.server_epoll: Fix write signal handling in watchfd API
Kim Alvefur <zash@zash.se>
parents:
9314
diff
changeset
|
696 conn:add(onreadable, onwritable); |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
697 return conn; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
698 end; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
699 |
7800
dadf9ba290c5
server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents:
7799
diff
changeset
|
700 -- 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
|
701 local function link(from, to) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
702 from.listeners = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
703 onincoming = function (_, data) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
704 from:pause(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
705 to:write(data); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
706 end, |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
707 }, {__index=from.listeners}); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
708 to.listeners = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
709 ondrain = function () |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
710 from:resume(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
711 end, |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
712 }, {__index=to.listeners}); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
713 from:set(true, nil); |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
714 to:set(nil, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
715 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
716 |
9438
916bee81eb7e
net.server_epoll: Make :set_send a noop, should fix net.adns
Kim Alvefur <zash@zash.se>
parents:
9432
diff
changeset
|
717 -- COMPAT |
916bee81eb7e
net.server_epoll: Make :set_send a noop, should fix net.adns
Kim Alvefur <zash@zash.se>
parents:
9432
diff
changeset
|
718 -- net.adns calls this but then replaces :send so this can be a noop |
9439
988392a56495
net.server_epoll: Fix luacheck annotation
Kim Alvefur <zash@zash.se>
parents:
9438
diff
changeset
|
719 function interface:set_send(new_send) -- luacheck: ignore 212 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
720 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
721 |
7800
dadf9ba290c5
server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents:
7799
diff
changeset
|
722 -- 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
|
723 local function closeall() |
7799
93643a61858a
server_epoll: Ignore unused loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7797
diff
changeset
|
724 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
|
725 conn:close(); |
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
726 end |
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
727 end |
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
728 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
729 local quitting = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
730 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
731 -- 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
|
732 local function setquitting(quit) |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
733 if quit then |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
734 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
|
735 closeall(); |
7796
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
736 else |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
737 quitting = nil; |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
738 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
739 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
740 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
741 -- 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
|
742 local function loop(once) |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
743 repeat |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
744 local t = runtimers(cfg.max_wait, cfg.min_wait); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
745 local fd, r, w = poll:wait(t); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
746 if fd then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
747 local conn = fds[fd]; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
748 if conn then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
749 if r then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
750 conn:onreadable(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
751 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
752 if w then |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
753 conn:onwritable(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
754 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
755 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
756 log("debug", "Removing unknown fd %d", fd); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
757 poll:del(fd); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
758 end |
9508
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
759 elseif r ~= "timeout" and r ~= "signal" then |
9445
af7eafe82447
net.server_epoll: Log error number from wait()
Kim Alvefur <zash@zash.se>
parents:
9441
diff
changeset
|
760 log("debug", "epoll_wait error: %s[%d]", r, w); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
761 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
|
762 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
|
763 return quitting; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
764 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
765 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
766 return { |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
767 get_backend = function () return "epoll"; end; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
768 addserver = addserver; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
769 addclient = addclient; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
770 add_task = addtimer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
771 at = at; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
772 loop = loop; |
7794
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
773 closeall = closeall; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
774 setquitting = setquitting; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
775 wrapclient = wrapclient; |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
776 watchfd = watchfd; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
777 link = link; |
7657
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
778 set_config = function (newconfig) |
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
779 cfg = setmetatable(newconfig, default_config); |
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
780 end; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
781 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
782 -- libevent emulation |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
783 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
|
784 addevent = function (fd, mode, callback) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
785 local function onevent(self) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
786 local ret = self:callback(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
787 if ret == -1 then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
788 self:set(false, false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
789 elseif ret then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
790 self:set(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
|
791 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
792 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
793 |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
794 local conn = setmetatable({ |
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
795 getfd = function () return fd; end; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
796 callback = callback; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
797 onreadable = onevent; |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
798 onwritable = onevent; |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
799 close = function (self) |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
800 self:del(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
801 fds[fd] = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
802 end; |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
803 }, interface_mt); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
804 local ok, err = conn:add(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
|
805 if not ok then return ok, err; end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
806 return conn; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
807 end; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
808 }; |