Annotate

net/server_epoll.lua @ 12189:82c8e855c850

mod_storage_xep0227: treat roster metadata pseudo-entry correctly The roster version is stored in a pseudo-item which has the key `false`. The if condition in the touched code attempts to guard against this, but it does not take into account that the jid prepping returns nil instead of false. By moving the jid prepping into the if, we can check for the metadata entry safely.
author Jonas Schäfer <jonas@wielicki.name>
date Sat, 15 Jan 2022 15:40:29 +0100
parent 12110:b5b799a2a10c
child 12342:cba0b93320b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Prosody IM
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_insert = table.insert;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local t_concat = table.concat;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local setmetatable = setmetatable;
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
12 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
13 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
14 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
15 local pairs = pairs;
11413
9fb23779c863 net.server_epoll: Support for passing DANE TLSA data to LuaSec (0.8 needed)
Kim Alvefur <zash@zash.se>
parents: 11267
diff changeset
16 local ipairs = ipairs;
10586
f86ed818a7f8 net.server_epoll: Collect full traceback from errors in listeners
Kim Alvefur <zash@zash.se>
parents: 10571
diff changeset
17 local traceback = debug.traceback;
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
18 local logger = require "util.logger";
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
19 local log = logger.init("server_epoll");
7547
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";
10492
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
22 local realtime = require "util.time".now;
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
23 local monotonic = require "util.time".monotonic;
10059
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
24 local indexedbheap = require "util.indexedbheap";
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 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
26 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
27 local inet_pton = inet.pton;
7737
f93b3083b46b net.server_epoll: Use _SOCKETINVALID from LuaSocket
Kim Alvefur <zash@zash.se>
parents: 7707
diff changeset
28 local _SOCKETINVALID = socket._SOCKETINVALID or -1;
12110
b5b799a2a10c util.id: Adjust entropy levels, with rationales
Kim Alvefur <zash@zash.se>
parents: 12091
diff changeset
29 local new_id = require "util.id".short;
10586
f86ed818a7f8 net.server_epoll: Collect full traceback from errors in listeners
Kim Alvefur <zash@zash.se>
parents: 10571
diff changeset
30 local xpcall = require "util.xpcall".xpcall;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
9506
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9499
diff changeset
32 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
33 local EEXIST = poller.EEXIST;
9507
33d21f020b66 net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents: 9506
diff changeset
34 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
35
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9499
diff changeset
36 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
37
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local _ENV = nil;
8555
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8552
diff changeset
39 -- luacheck: std none
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
7657
252823632401 net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents: 7637
diff changeset
41 local default_config = { __index = {
9665
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
42 -- 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
43 read_timeout = 14 * 60;
9665
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
44
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
45 -- How long to wait for a socket to become writable after queuing data to send
9825
5ccce16c6449 net.server_epoll: Increase send_timeout to 3 minutes (to match server_event)
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
46 send_timeout = 180;
9665
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
47
9824
729de68f0068 net.server_epoll: Separate timeout for initial connection attempts
Kim Alvefur <zash@zash.se>
parents: 9823
diff changeset
48 -- How long to wait for a socket to become writable after creation
729de68f0068 net.server_epoll: Separate timeout for initial connection attempts
Kim Alvefur <zash@zash.se>
parents: 9823
diff changeset
49 connect_timeout = 20;
9665
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
50
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
51 -- 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
52 tcp_backlog = 128;
9665
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
53
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
54 -- 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
55 accept_retry_interval = 10;
9665
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
56
11671
4e4e26e3df8d net.server_epoll: Fix typo
Kim Alvefur <zash@zash.se>
parents: 11665
diff changeset
57 -- If there is still more data to read from LuaSockets 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
58 read_retry_delay = 1e-06;
9665
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
59
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
60 -- 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
61 read_size = 8192;
9665
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
62
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
63 -- Timeout used during between steps in TLS handshakes
9822
13c2707d2417 net.server_epoll: Rename handshake_timeout to ssl_handshake_timeout (fixes #1319)
Kim Alvefur <zash@zash.se>
parents: 9821
diff changeset
64 ssl_handshake_timeout = 60;
9665
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
65
4790d1b88919 server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents: 9664
diff changeset
66 -- 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
67 max_wait = 86400;
11694
d6be4dda1f60 net.server_epoll: Set minimum wait time to 1ms, matching epoll
Kim Alvefur <zash@zash.se>
parents: 11689
diff changeset
68 min_wait = 0.001;
10081
72adf1f39404 net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents: 10080
diff changeset
69
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
70 -- Enable extra noisy debug logging
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
71 -- TODO disable once considered stable
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
72 verbose = true;
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
73
10081
72adf1f39404 net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents: 10080
diff changeset
74 -- EXPERIMENTAL
72adf1f39404 net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents: 10080
diff changeset
75 -- Whether to kill connections in case of callback errors.
72adf1f39404 net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents: 10080
diff changeset
76 fatal_errors = false;
10228
e77bf4222fae net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 10098
diff changeset
77
10992
df3ee12acd8c net.server_epoll: Add setting for turning off callback protections
Kim Alvefur <zash@zash.se>
parents: 10991
diff changeset
78 -- Or disable protection (like server_select) for potential performance gains
df3ee12acd8c net.server_epoll: Add setting for turning off callback protections
Kim Alvefur <zash@zash.se>
parents: 10991
diff changeset
79 protect_listeners = true;
df3ee12acd8c net.server_epoll: Add setting for turning off callback protections
Kim Alvefur <zash@zash.se>
parents: 10991
diff changeset
80
10228
e77bf4222fae net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 10098
diff changeset
81 -- Attempt writes instantly
e77bf4222fae net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 10098
diff changeset
82 opportunistic_writes = false;
11687
153d2fdd97d3 net.server_epoll: Add way to enable TCP keeplives on all connections
Kim Alvefur <zash@zash.se>
parents: 11686
diff changeset
83
153d2fdd97d3 net.server_epoll: Add way to enable TCP keeplives on all connections
Kim Alvefur <zash@zash.se>
parents: 11686
diff changeset
84 -- TCP Keepalives
153d2fdd97d3 net.server_epoll: Add way to enable TCP keeplives on all connections
Kim Alvefur <zash@zash.se>
parents: 11686
diff changeset
85 tcp_keepalive = false; -- boolean | number
11689
f9f6e82fb572 net.server_epoll: Add setting for disabling the Nagle algorithm
Kim Alvefur <zash@zash.se>
parents: 11688
diff changeset
86
f9f6e82fb572 net.server_epoll: Add setting for disabling the Nagle algorithm
Kim Alvefur <zash@zash.se>
parents: 11688
diff changeset
87 -- Whether to let the Nagle algorithm stay enabled
f9f6e82fb572 net.server_epoll: Add setting for disabling the Nagle algorithm
Kim Alvefur <zash@zash.se>
parents: 11688
diff changeset
88 nagle = true;
11696
917eca7be82b net.server_epoll: Optionally let go of buffers
Kim Alvefur <zash@zash.se>
parents: 11695
diff changeset
89
917eca7be82b net.server_epoll: Optionally let go of buffers
Kim Alvefur <zash@zash.se>
parents: 11695
diff changeset
90 -- Reuse write buffer tables
917eca7be82b net.server_epoll: Optionally let go of buffers
Kim Alvefur <zash@zash.se>
parents: 11695
diff changeset
91 keep_buffers = true;
11811
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
92
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
93 --- How long to wait after getting the shutdown signal before forcefully tearing down every socket
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
94 shutdown_deadline = 5;
7657
252823632401 net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents: 7637
diff changeset
95 }};
252823632401 net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents: 7637
diff changeset
96 local cfg = default_config.__index;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 local fds = createtable(10, 0); -- FD -> conn
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
99
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
100 -- Timer and scheduling --
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
101
10059
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
102 local timers = indexedbheap.create();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 local function noop() end
11761
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
105
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
106 -- Keep track of recently closed timers to avoid re-adding them
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
107 local closedtimers = {};
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
108
10985
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
109 local function closetimer(id)
11761
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
110 if timers:remove(id) then
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
111 closedtimers[id] = true;
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
112 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114
10985
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
115 local function reschedule(id, time)
10492
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
116 time = monotonic() + time;
10985
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
117 timers:reprioritize(id, time);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 end
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
119
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
120 -- Add relative timer
10982
276f16101d59 net.server_epoll: Make API-compatible with util.timer
Kim Alvefur <zash@zash.se>
parents: 10861
diff changeset
121 local function addtimer(timeout, f, param)
10492
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
122 local time = monotonic() + timeout;
10985
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
123 if param ~= nil then
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
124 local timer_callback = f
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
125 function f(current_time, timer_id)
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
126 local t = timer_callback(current_time, timer_id, param)
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
127 return t;
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
128 end
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
129 end
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
130 local id = timers:insert(f, time);
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
131 return id;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
134 -- Run callbacks of expired timers
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
135 -- 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
136 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
137 -- Any timers at all?
10492
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
138 local elapsed = monotonic();
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
139 local now = realtime();
10059
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
140 local peek = timers:peek();
11262
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
141 local readd;
10059
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
142 while peek do
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
143
10492
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
144 if peek > elapsed then
10059
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
145 break;
10681
e531273341d3 net.server_epoll: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 10059
diff changeset
146 end
10059
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
147
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
148 local _, timer, id = timers:pop();
10990
03e76ad725b2 net.server_epoll: ... and include a traceback
Kim Alvefur <zash@zash.se>
parents: 10989
diff changeset
149 local ok, ret = xpcall(timer, traceback, now, id);
11761
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
150 if ok and type(ret) == "number" and not closedtimers[id] then
10492
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
151 local next_time = elapsed+ret;
11262
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
152 -- Delay insertion of timers to be re-added
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
153 -- so they don't get called again this tick
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
154 if readd then
11264
2cdcf55c6dd5 Merge 0.11->trunk
Kim Alvefur <zash@zash.se>
parents: 11239 11262
diff changeset
155 readd[id] = { timer, next_time };
11262
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
156 else
11264
2cdcf55c6dd5 Merge 0.11->trunk
Kim Alvefur <zash@zash.se>
parents: 11239 11262
diff changeset
157 readd = { [id] = { timer, next_time } };
11262
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
158 end
10989
b824fe9d0c8b net.server_epoll: Report errors in timers
Kim Alvefur <zash@zash.se>
parents: 10987
diff changeset
159 elseif not ok then
b824fe9d0c8b net.server_epoll: Report errors in timers
Kim Alvefur <zash@zash.se>
parents: 10987
diff changeset
160 log("error", "Error in timer: %s", ret);
10681
e531273341d3 net.server_epoll: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 10059
diff changeset
161 end
10059
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
162
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
163 peek = timers:peek();
10681
e531273341d3 net.server_epoll: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 10059
diff changeset
164 end
11262
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
165
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
166 if readd then
11751
a8c9a78c6a4b net.server_epoll: Fix to preserve ids of readded timers
Kim Alvefur <zash@zash.se>
parents: 11265
diff changeset
167 for id, timer in pairs(readd) do
11752
6427e2642976 Merge 0.11->trunk
Kim Alvefur <zash@zash.se>
parents: 11743 11751
diff changeset
168 timers:insert(timer[1], timer[2], id);
11262
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
169 end
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
170 peek = timers:peek();
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
171 end
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
172
11761
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
173 if next(closedtimers) ~= nil then
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
174 closedtimers = {};
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
175 end
dbf378dcf27b net.server_epoll: Prevent removed timers from being readded
Kim Alvefur <zash@zash.se>
parents: 11752
diff changeset
176
10059
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
177 if peek == nil then
7629
0fcde9869cc1 net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents: 7628
diff changeset
178 return next_delay;
11262
2c559953ad41 net.server_epoll: Ensure timers can't run more than once per tick
Kim Alvefur <zash@zash.se>
parents: 11063
diff changeset
179 else
11264
2cdcf55c6dd5 Merge 0.11->trunk
Kim Alvefur <zash@zash.se>
parents: 11239 11262
diff changeset
180 next_delay = peek - elapsed;
7629
0fcde9869cc1 net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents: 7628
diff changeset
181 end
0fcde9869cc1 net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents: 7628
diff changeset
182
10059
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
183 if next_delay < min_wait then
c8c3f2eba898 net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents: 9994
diff changeset
184 return min_wait;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 return next_delay;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
189 -- Socket handler interface
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
190
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 local interface = {};
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 local interface_mt = { __index = interface };
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 function interface_mt:__tostring()
7585
b64218c816de net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents: 7584
diff changeset
195 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
196 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
197 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
198 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
199 end
9321
146dc1e4c7ff net.server_epoll: Simplify minimal string representation
Kim Alvefur <zash@zash.se>
parents: 9320
diff changeset
200 return ("FD %d"):format(self:getfd());
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
203 interface.log = log;
11497
960674938665 net.server_epoll: Remove unnecessary luacheck annotations
Kim Alvefur <zash@zash.se>
parents: 11413
diff changeset
204 function interface:debug(msg, ...)
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
205 self.log("debug", msg, ...);
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
206 end
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
207
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
208 interface.noise = interface.debug;
11497
960674938665 net.server_epoll: Remove unnecessary luacheck annotations
Kim Alvefur <zash@zash.se>
parents: 11413
diff changeset
209 function interface:noise(msg, ...)
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
210 if cfg.verbose then
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
211 return self:debug(msg, ...);
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
212 end
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
213 end
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
214
11497
960674938665 net.server_epoll: Remove unnecessary luacheck annotations
Kim Alvefur <zash@zash.se>
parents: 11413
diff changeset
215 function interface:error(msg, ...)
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
216 self.log("error", msg, ...);
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
217 end
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
218
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
219 -- 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
220 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
221 self:on("detach");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 self.listeners = listeners;
9387
33e52f727f0f net.connect: Fix passing request table to new listener
Kim Alvefur <zash@zash.se>
parents: 9385
diff changeset
223 self:on("attach", data);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
226 -- 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
227 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
228 if not self.listeners then
10589
3540118699cf net.server_epoll: Log error about missing *all* callbacks at 'error' level
Kim Alvefur <zash@zash.se>
parents: 10588
diff changeset
229 self:error("Interface is missing listener callbacks");
8006
be374a5778f3 net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents: 7910
diff changeset
230 return;
be374a5778f3 net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents: 7910
diff changeset
231 end
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
232 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
233 if not listener then
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
234 self:noise("Missing listener 'on%s'", what); -- uncomment for development and debugging
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
235 return;
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
236 end
10992
df3ee12acd8c net.server_epoll: Add setting for turning off callback protections
Kim Alvefur <zash@zash.se>
parents: 10991
diff changeset
237 if not cfg.protect_listeners then
df3ee12acd8c net.server_epoll: Add setting for turning off callback protections
Kim Alvefur <zash@zash.se>
parents: 10991
diff changeset
238 return listener(self, ...);
df3ee12acd8c net.server_epoll: Add setting for turning off callback protections
Kim Alvefur <zash@zash.se>
parents: 10991
diff changeset
239 end
10991
aa85cb5b859f net.server_epoll: Allow setting a custom error handler for listener
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
240 local onerror = self.listeners.onerror or traceback;
aa85cb5b859f net.server_epoll: Allow setting a custom error handler for listener
Kim Alvefur <zash@zash.se>
parents: 10990
diff changeset
241 local ok, err = xpcall(listener, onerror, self, ...);
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
242 if not ok then
10081
72adf1f39404 net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents: 10080
diff changeset
243 if cfg.fatal_errors then
10588
ceadf3d3fecf net.server_epoll: Log errors caught in listeners on 'error' level
Kim Alvefur <zash@zash.se>
parents: 10587
diff changeset
244 self:error("Closing due to error calling on%s: %s", what, err);
10081
72adf1f39404 net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents: 10080
diff changeset
245 self:destroy();
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
246 else
11239
06a5919e2496 net.server_epoll: Increase log level for error in callback
Kim Alvefur <zash@zash.se>
parents: 11068
diff changeset
247 self:error("Error calling on%s: %s", what, err);
10081
72adf1f39404 net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents: 10080
diff changeset
248 end
10080
39ca4202ae1e net.server_epoll: Return listener error message
Kim Alvefur <zash@zash.se>
parents: 10061
diff changeset
249 return nil, err;
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
250 end
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
251 return err;
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
252 end
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
253
10278
94c584d67533 net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents: 10234
diff changeset
254 -- Allow this one to be overridden
94c584d67533 net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents: 10234
diff changeset
255 function interface:onincoming(...)
94c584d67533 net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents: 10234
diff changeset
256 return self:on("incoming", ...);
94c584d67533 net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents: 10234
diff changeset
257 end
94c584d67533 net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents: 10234
diff changeset
258
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
259 -- Return the file descriptor number
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 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
261 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
262 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
263 end
7737
f93b3083b46b net.server_epoll: Use _SOCKETINVALID from LuaSocket
Kim Alvefur <zash@zash.se>
parents: 7707
diff changeset
264 return _SOCKETINVALID;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266
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
267 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
268 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
269 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
270
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
271 -- Get IP address
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 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
273 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
274 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
275
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
276 -- 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
277 function interface:port()
11793
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
278 return self.peerport or self.sockport;
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
279 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
280
11793
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
281 -- Client-side port (usually a random high port)
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
282 function interface:clientport()
11793
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
283 if self._server then
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
284 return self.peerport;
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
285 else
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
286 return self.sockport;
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
287 end
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
288 end
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
289
11793
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
290 -- Get port on the server
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
291 function interface:serverport()
11793
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
292 if self._server then
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
293 return self.sockport;
11793
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
294 else
9006ff4838ff net.server_epoll: Try to make port number related methods sane
Kim Alvefur <zash@zash.se>
parents: 11761
diff changeset
295 return self.peerport;
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
296 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
299 -- Return underlying socket
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 function interface:socket()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 return self.conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303
7910
91db637be237 net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents: 7817
diff changeset
304 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
305 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
306 end
91db637be237 net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents: 7817
diff changeset
307
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308 function interface:setoption(k, v)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309 -- LuaSec doesn't expose setoption :(
11685
247561bf6fca net.server_epoll: Log failures to set socket options
Kim Alvefur <zash@zash.se>
parents: 11677
diff changeset
310 local ok, ret, err = pcall(self.conn.setoption, self.conn, k, v);
247561bf6fca net.server_epoll: Log failures to set socket options
Kim Alvefur <zash@zash.se>
parents: 11677
diff changeset
311 if not ok then
247561bf6fca net.server_epoll: Log failures to set socket options
Kim Alvefur <zash@zash.se>
parents: 11677
diff changeset
312 self:noise("Setting option %q = %q failed: %s", k, v, ret);
247561bf6fca net.server_epoll: Log failures to set socket options
Kim Alvefur <zash@zash.se>
parents: 11677
diff changeset
313 return ok, ret;
247561bf6fca net.server_epoll: Log failures to set socket options
Kim Alvefur <zash@zash.se>
parents: 11677
diff changeset
314 elseif not ret then
247561bf6fca net.server_epoll: Log failures to set socket options
Kim Alvefur <zash@zash.se>
parents: 11677
diff changeset
315 self:noise("Setting option %q = %q failed: %s", k, v, err);
247561bf6fca net.server_epoll: Log failures to set socket options
Kim Alvefur <zash@zash.se>
parents: 11677
diff changeset
316 return ret, err;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 end
11685
247561bf6fca net.server_epoll: Log failures to set socket options
Kim Alvefur <zash@zash.se>
parents: 11677
diff changeset
318 return ret;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
321 -- Timeout for detecting dead or idle sockets
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 function interface:setreadtimeout(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 if t == false then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 if self._readtimeout then
10985
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
325 closetimer(self._readtimeout);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 self._readtimeout = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 return
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 t = t or cfg.read_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 if self._readtimeout then
10985
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
332 reschedule(self._readtimeout, t);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 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
335 if self:on("readtimeout") then
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
336 self:noise("Read timeout handled");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 return cfg.read_timeout;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 else
10417
d8a0b0d21c81 net.server_epoll: Improve read timeout debug messages
Kim Alvefur <zash@zash.se>
parents: 10416
diff changeset
339 self:debug("Read timeout not handled, disconnecting");
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
340 self:on("disconnect", "read timeout");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 end);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
347 -- Timeout for detecting dead sockets
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 function interface:setwritetimeout(t)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 if t == false then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 if self._writetimeout then
10985
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
351 closetimer(self._writetimeout);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 self._writetimeout = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 return
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 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
356 t = t or cfg.send_timeout;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 if self._writetimeout then
10985
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
358 reschedule(self._writetimeout, t);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 self._writetimeout = addtimer(t, function ()
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
361 self:noise("Write timeout");
10638
92918212ee83 net.server_epoll: Different error to distinguish connection timeout
Kim Alvefur <zash@zash.se>
parents: 10589
diff changeset
362 self:on("disconnect", self._connected and "write timeout" or "connection timeout");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 end);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
368 function interface:add(r, w)
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
369 local fd = self:getfd();
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
370 if fd < 0 then
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
371 return nil, "invalid fd";
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 end
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
373 if r == nil then r = self._wantread; end
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
374 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
375 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
376 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
377 if errno == EEXIST then
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
378 self:debug("FD already registered in poller! (EEXIST)");
9506
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9499
diff changeset
379 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
380 end
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
381 self:debug("Could not register in poller: %s(%d)", err, errno);
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
382 return ok, err;
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
383 end
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
384 self._wantread, self._wantwrite = r, w;
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
385 fds[fd] = self;
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
386 self:noise("Registered in poller");
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
387 return true;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
390 function interface:set(r, w)
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 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
392 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
393 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
394 end
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
395 if r == nil then r = self._wantread; end
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
396 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
397 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
398 if not ok then
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
399 self:debug("Could not update poller state: %s(%d)", err, errno);
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
400 return ok, err;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 end
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
402 self._wantread, self._wantwrite = r, w;
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
403 return true;
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
404 end
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
405
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
406 function interface:del()
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
407 local fd = self:getfd();
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
408 if fd < 0 then
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
409 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
410 end
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
411 if fds[fd] ~= self then
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
412 return nil, "unregistered fd";
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
413 end
9384
5c8006ce8cb8 net.server_epoll: Include errno in log messages
Kim Alvefur <zash@zash.se>
parents: 9383
diff changeset
414 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
415 if not ok and errno ~= ENOENT then
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
416 self:debug("Could not unregister: %s(%d)", err, errno);
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
417 return ok, err;
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
418 end
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
419 self._wantread, self._wantwrite = nil, nil;
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
420 fds[fd] = nil;
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
421 self:noise("Unregistered from poller");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422 return true;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
423 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
424
9320
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
425 function interface:setflags(r, w)
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
426 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
427 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
428 return true; -- no change
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
429 end
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
430 return self:add(r, w);
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
431 end
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
432 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
433 return self:del();
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
434 end
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
435 return self:set(r, w);
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
436 end
8cc498cb0df7 net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents: 9319
diff changeset
437
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
438 -- Called when socket is readable
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
439 function interface:onreadable()
9311
9b0604fe01f1 net.server_epoll: Make read_size a server-wide option
Kim Alvefur <zash@zash.se>
parents: 9310
diff changeset
440 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
441 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
442 self:onconnect();
10278
94c584d67533 net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents: 10234
diff changeset
443 self:onincoming(data);
7687
ae24deae6488 net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents: 7658
diff changeset
444 else
9931
485231f8b75d net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents: 9822
diff changeset
445 if err == "wantread" then
485231f8b75d net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents: 9822
diff changeset
446 self:set(true, nil);
485231f8b75d net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents: 9822
diff changeset
447 err = "timeout";
485231f8b75d net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents: 9822
diff changeset
448 elseif err == "wantwrite" then
485231f8b75d net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents: 9822
diff changeset
449 self:set(nil, true);
11702
c206430e2643 net.server_epoll: Ensure timeouts match epoll flags
Kim Alvefur <zash@zash.se>
parents: 11701
diff changeset
450 self:setwritetimeout();
9931
485231f8b75d net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents: 9822
diff changeset
451 err = "timeout";
11592
64cfa396bb84 net.server_epoll: Fix reporting of socket connect timeout
Kim Alvefur <zash@zash.se>
parents: 11497
diff changeset
452 elseif err == "timeout" and not self._connected then
64cfa396bb84 net.server_epoll: Fix reporting of socket connect timeout
Kim Alvefur <zash@zash.se>
parents: 11497
diff changeset
453 err = "connection timeout";
9931
485231f8b75d net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents: 9822
diff changeset
454 end
8540
ba9fd886b34d net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents: 8507
diff changeset
455 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
456 self:onconnect();
10278
94c584d67533 net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents: 10234
diff changeset
457 self:onincoming(partial, err);
7687
ae24deae6488 net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents: 7658
diff changeset
458 end
11814
f27b9319e0da net.server_epoll: Separate handling of "closed" from other errors
Kim Alvefur <zash@zash.se>
parents: 11813
diff changeset
459 if err == "closed" and self._connected then
f27b9319e0da net.server_epoll: Separate handling of "closed" from other errors
Kim Alvefur <zash@zash.se>
parents: 11813
diff changeset
460 self:debug("Connection closed by remote");
f27b9319e0da net.server_epoll: Separate handling of "closed" from other errors
Kim Alvefur <zash@zash.se>
parents: 11813
diff changeset
461 self:close(err);
f27b9319e0da net.server_epoll: Separate handling of "closed" from other errors
Kim Alvefur <zash@zash.se>
parents: 11813
diff changeset
462 return;
f27b9319e0da net.server_epoll: Separate handling of "closed" from other errors
Kim Alvefur <zash@zash.se>
parents: 11813
diff changeset
463 elseif err ~= "timeout" then
f27b9319e0da net.server_epoll: Separate handling of "closed" from other errors
Kim Alvefur <zash@zash.se>
parents: 11813
diff changeset
464 self:debug("Read error, closing (%s)", err);
7687
ae24deae6488 net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents: 7658
diff changeset
465 self:on("disconnect", err);
11814
f27b9319e0da net.server_epoll: Separate handling of "closed" from other errors
Kim Alvefur <zash@zash.se>
parents: 11813
diff changeset
466 self:destroy();
7687
ae24deae6488 net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents: 7658
diff changeset
467 return;
ae24deae6488 net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents: 7658
diff changeset
468 end
ae24deae6488 net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents: 7658
diff changeset
469 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
470 if not self.conn then return; end
10098
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
471 if self._limit and (data or partial) then
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
472 local cost = self._limit * #(data or partial);
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
473 if cost > cfg.min_wait then
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
474 self:setreadtimeout(false);
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
475 self:pausefor(cost);
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
476 return;
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
477 end
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
478 end
11701
a3572205c5bf net.server_epoll: Skip reset of read timeout when not reading
Kim Alvefur <zash@zash.se>
parents: 11700
diff changeset
479 if not self._wantread then return end
a3572205c5bf net.server_epoll: Skip reset of read timeout when not reading
Kim Alvefur <zash@zash.se>
parents: 11700
diff changeset
480 if self.conn:dirty() then
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
481 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
482 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
483 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
484 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
485 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
486 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
487
7573
0f0a6febbc5a net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents: 7564
diff changeset
488 -- Called when socket is writable
8542
8c39ec8e6018 net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents: 8541
diff changeset
489 function interface:onwritable()
11920
ff4e34c448a4 net.server_epoll: Try harder to avoid reentrant opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11858
diff changeset
490 self._writing = true; -- prevent reentrant writes etc
8552
1fcefc16a55d server_epoll: Onconnect should happen before first attempt to write to socket
Kim Alvefur <zash@zash.se>
parents: 8542
diff changeset
491 self:onconnect();
12011
9dc36fdbdba1 net.server_epoll: Ensure calls to :write() return something
Kim Alvefur <zash@zash.se>
parents: 11972
diff changeset
492 if not self.conn then return nil, "no-conn"; end -- could have been closed in onconnect
11741
dcf38ac6a38c net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents: 11738
diff changeset
493 self:on("predrain");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
494 local buffer = self.writebuffer;
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
495 local data = buffer or "";
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
496 if type(buffer) == "table" then
11698
9a2a98621c73 net.server_epoll: Optimize concatenation of exactly 2 buffer chunks
Kim Alvefur <zash@zash.se>
parents: 11697
diff changeset
497 if buffer[3] then
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
498 data = t_concat(data);
11698
9a2a98621c73 net.server_epoll: Optimize concatenation of exactly 2 buffer chunks
Kim Alvefur <zash@zash.se>
parents: 11697
diff changeset
499 elseif buffer[2] then
9a2a98621c73 net.server_epoll: Optimize concatenation of exactly 2 buffer chunks
Kim Alvefur <zash@zash.se>
parents: 11697
diff changeset
500 data = buffer[1] .. buffer[2];
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
501 else
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
502 data = buffer[1] or "";
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
503 end
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
504 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
505 local ok, err, partial = self.conn:send(data);
11738
3f1450ec37f4 net.server_epoll: Improve efficiency of opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11703
diff changeset
506 self._writable = ok;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
507 if ok then
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
508 self:set(nil, false);
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
509 if cfg.keep_buffers and type(buffer) == "table" then
11805
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
510 for i = #buffer, 1, -1 do
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
511 buffer[i] = nil;
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
512 end
11696
917eca7be82b net.server_epoll: Optionally let go of buffers
Kim Alvefur <zash@zash.se>
parents: 11695
diff changeset
513 else
917eca7be82b net.server_epoll: Optionally let go of buffers
Kim Alvefur <zash@zash.se>
parents: 11695
diff changeset
514 self.writebuffer = nil;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
515 end
11922
28f5c8061dad net.server_epoll: Fix streaming downloads (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 11920
diff changeset
516 self._writing = nil;
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
517 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
518 self:ondrain(); -- Be aware of writes in ondrain
11695
1573099157c9 net.server_epoll: Propagate returns from opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11694
diff changeset
519 return ok;
8540
ba9fd886b34d net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents: 8507
diff changeset
520 elseif partial then
10305
2fadbf209678 net.server_epoll: Log size of partial writes (debug)
Kim Alvefur <zash@zash.se>
parents: 10300
diff changeset
521 self:debug("Sent %d out of %d buffered bytes", partial, #data);
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
522 if cfg.keep_buffers and type(buffer) == "table" then
11805
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
523 buffer[1] = data:sub(partial+1);
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
524 for i = #buffer, 2, -1 do
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
525 buffer[i] = nil;
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
526 end
11696
917eca7be82b net.server_epoll: Optionally let go of buffers
Kim Alvefur <zash@zash.se>
parents: 11695
diff changeset
527 else
11703
e57c5d9c722c net.server_epoll: Fix traceback-causing typo
Kim Alvefur <zash@zash.se>
parents: 11702
diff changeset
528 self.writebuffer = data:sub(partial+1);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
529 end
10228
e77bf4222fae net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 10098
diff changeset
530 self:set(nil, true);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
531 self:setwritetimeout();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
532 end
11920
ff4e34c448a4 net.server_epoll: Try harder to avoid reentrant opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11858
diff changeset
533 self._writing = nil;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
534 if err == "wantwrite" or err == "timeout" then
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
535 self:set(nil, true);
11702
c206430e2643 net.server_epoll: Ensure timeouts match epoll flags
Kim Alvefur <zash@zash.se>
parents: 11701
diff changeset
536 self:setwritetimeout();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
537 elseif err == "wantread" then
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
538 self:set(true, nil);
11702
c206430e2643 net.server_epoll: Ensure timeouts match epoll flags
Kim Alvefur <zash@zash.se>
parents: 11701
diff changeset
539 self:setreadtimeout();
7687
ae24deae6488 net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents: 7658
diff changeset
540 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
541 self:on("disconnect", err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
542 self:destroy();
11695
1573099157c9 net.server_epoll: Propagate returns from opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11694
diff changeset
543 return ok, err;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
544 end
12011
9dc36fdbdba1 net.server_epoll: Ensure calls to :write() return something
Kim Alvefur <zash@zash.se>
parents: 11972
diff changeset
545 return true, err;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
546 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
547
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
548 -- The write buffer has been successfully emptied
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
549 function interface:ondrain()
8008
62ed3795be13 net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents: 8007
diff changeset
550 return self:on("drain");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
551 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
552
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
553 -- 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
554 function interface:write(data)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
555 local buffer = self.writebuffer;
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
556 if type(buffer) == "table" then
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
557 t_insert(buffer, data);
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
558 elseif type(buffer) == "string" then
11696
917eca7be82b net.server_epoll: Optionally let go of buffers
Kim Alvefur <zash@zash.se>
parents: 11695
diff changeset
559 self:noise("Allocating buffer!")
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
560 self.writebuffer = { buffer, data };
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
561 elseif buffer == nil then
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
562 self.writebuffer = data;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
563 end
11920
ff4e34c448a4 net.server_epoll: Try harder to avoid reentrant opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11858
diff changeset
564 if not self._write_lock and not self._writing then
11738
3f1450ec37f4 net.server_epoll: Improve efficiency of opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11703
diff changeset
565 if self._writable and cfg.opportunistic_writes and not self._opportunistic_write then
11665
148075532021 net.server_epoll: Prevent stack overflow of opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11660
diff changeset
566 self._opportunistic_write = true;
11695
1573099157c9 net.server_epoll: Propagate returns from opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11694
diff changeset
567 local ret, err = self:onwritable();
11665
148075532021 net.server_epoll: Prevent stack overflow of opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11660
diff changeset
568 self._opportunistic_write = nil;
11695
1573099157c9 net.server_epoll: Propagate returns from opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11694
diff changeset
569 return ret, err;
11805
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
570 end
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
571 self:setwritetimeout();
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
572 self:set(nil, true);
9581
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
573 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
574 return #data;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
575 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
576 interface.send = interface.write;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
577
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
578 -- Close, possibly after writing is done
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
579 function interface:close()
11813
790bffbb9047 net.server_epoll: Skip attempt to flush write buffer when not connected
Kim Alvefur <zash@zash.se>
parents: 11812
diff changeset
580 if self._connected and self.writebuffer and (self.writebuffer[1] or type(self.writebuffer) == "string") then
11972
520ce76440ad net.server_epoll: Prevent loop attempting to send last data after close
Kim Alvefur <zash@zash.se>
parents: 11922
diff changeset
581 self._connected = false;
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
582 self:set(false, true); -- Flush final buffer contents
11702
c206430e2643 net.server_epoll: Ensure timeouts match epoll flags
Kim Alvefur <zash@zash.se>
parents: 11701
diff changeset
583 self:setreadtimeout(false);
11658
36942fa001b4 net.server_epoll: Ensure timeout after closing
Kim Alvefur <zash@zash.se>
parents: 11595
diff changeset
584 self:setwritetimeout();
7593
0266434d7df1 net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents: 7592
diff changeset
585 self.write, self.send = noop, noop; -- No more writing
10416
6af8c812a885 net.server_epoll: Clarify a debug message
Kim Alvefur <zash@zash.se>
parents: 10332
diff changeset
586 self:debug("Close after writing remaining buffered data");
8008
62ed3795be13 net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents: 8007
diff changeset
587 self.ondrain = interface.close;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
588 else
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
589 self:debug("Closing now");
7593
0266434d7df1 net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents: 7592
diff changeset
590 self.write, self.send = noop, noop;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
591 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
592 self:on("disconnect");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
593 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
594 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
595 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
596
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
597 function interface:destroy()
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
598 self:del();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
599 self:setwritetimeout(false);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
600 self:setreadtimeout(false);
7594
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
601 self.onreadable = noop;
8542
8c39ec8e6018 net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents: 8541
diff changeset
602 self.onwritable = noop;
7594
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
603 self.destroy = noop;
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
604 self.close = noop;
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
605 self.on = noop;
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
606 self.conn:close();
5e884501a9ac net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents: 7593
diff changeset
607 self.conn = nil;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
608 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
609
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
610 function interface:ssl()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
611 return self._tls;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
612 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
613
11595
8985efc6792d net.server_epoll: Add missing method for changing TLS context
Kim Alvefur <zash@zash.se>
parents: 11592
diff changeset
614 function interface:set_sslctx(sslctx)
8985efc6792d net.server_epoll: Add missing method for changing TLS context
Kim Alvefur <zash@zash.se>
parents: 11592
diff changeset
615 self._sslctx = sslctx;
8985efc6792d net.server_epoll: Add missing method for changing TLS context
Kim Alvefur <zash@zash.se>
parents: 11592
diff changeset
616 end
8985efc6792d net.server_epoll: Add missing method for changing TLS context
Kim Alvefur <zash@zash.se>
parents: 11592
diff changeset
617
9300
1df8832bd29e net.server_epoll: Rename all variables holding TLS contexts for consistency
Kim Alvefur <zash@zash.se>
parents: 8983
diff changeset
618 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
619 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
620 self.starttls = false;
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
621 if self.writebuffer and (self.writebuffer[1] or type(self.writebuffer) == "string") then
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
622 self:debug("Start TLS after write");
8008
62ed3795be13 net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents: 8007
diff changeset
623 self.ondrain = interface.starttls;
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
624 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
625 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
626 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
627 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
628 end
11672
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
629 self.onwritable = interface.inittls;
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
630 self.onreadable = interface.inittls;
9430
412ff404bf58 net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents: 9387
diff changeset
631 self:set(true, true);
11699
1275dad71afd net.server_epoll: Use only fatal "write" timeout during TLS negotiation
Kim Alvefur <zash@zash.se>
parents: 11698
diff changeset
632 self:setreadtimeout(false);
11659
00295a8e5bcf net.server_epoll: Separate handling of new incoming and outgoing connections
Kim Alvefur <zash@zash.se>
parents: 11658
diff changeset
633 self:setwritetimeout(cfg.ssl_handshake_timeout);
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
634 self:debug("Prepared to start TLS");
9430
412ff404bf58 net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents: 9387
diff changeset
635 end
412ff404bf58 net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents: 9387
diff changeset
636 end
412ff404bf58 net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents: 9387
diff changeset
637
11676
993462e5e4d4 net.server_epoll: Refactor immediate TLS handshake start
Kim Alvefur <zash@zash.se>
parents: 11675
diff changeset
638 function interface:inittls(tls_ctx, now)
11672
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
639 if self._tls then return end
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
640 if tls_ctx then self.tls_ctx = tls_ctx; end
11805
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
641 self._tls = true;
11839
19b50ce9ef5a net.server_epoll: Prevent starttls on direct TLS connections
Kim Alvefur <zash@zash.se>
parents: 11814
diff changeset
642 self.starttls = false;
11672
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
643 self:debug("Starting TLS now");
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
644 self:updatenames(); -- Can't getpeer/sockname after wrap()
11805
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
645 local ok, conn, err = pcall(luasec.wrap, self.conn, self.tls_ctx);
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
646 if not ok then
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
647 conn, err = ok, conn;
11672
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
648 self:debug("Failed to initialize TLS: %s", err);
11805
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
649 end
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
650 if not conn then
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
651 self:on("disconnect", err);
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
652 self:destroy();
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
653 return conn, err;
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
654 end
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
655 conn:settimeout(0);
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
656 self.conn = conn;
11672
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
657 if conn.sni then
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
658 if self.servername then
11063
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
659 conn:sni(self.servername);
11672
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
660 elseif self._server and type(self._server.hosts) == "table" and next(self._server.hosts) ~= nil then
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
661 conn:sni(self._server.hosts, true);
9431
c3c0523a37c6 net.server_epoll: Catch errors from luasec.wrap
Kim Alvefur <zash@zash.se>
parents: 9430
diff changeset
662 end
11672
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
663 end
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
664 if self.extra and self.extra.tlsa and conn.settlsa then
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
665 -- TODO Error handling
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
666 if not conn:setdane(self.servername or self.extra.dane_hostname) then
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
667 self:debug("Could not enable DANE on connection");
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
668 else
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
669 self:debug("Enabling DANE with %d TLSA records", #self.extra.tlsa);
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
670 self:noise("DANE hostname is %q", self.servername or self.extra.dane_hostname);
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
671 for _, tlsa in ipairs(self.extra.tlsa) do
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
672 self:noise("TLSA: %q", tlsa);
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
673 conn:settlsa(tlsa.use, tlsa.select, tlsa.match, tlsa.data);
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
674 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
675 end
11805
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
676 end
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
677 self:on("starttls");
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
678 self.ondrain = nil;
11672
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
679 self.onwritable = interface.tlshandshake;
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
680 self.onreadable = interface.tlshandshake;
11676
993462e5e4d4 net.server_epoll: Refactor immediate TLS handshake start
Kim Alvefur <zash@zash.se>
parents: 11675
diff changeset
681 if now then
993462e5e4d4 net.server_epoll: Refactor immediate TLS handshake start
Kim Alvefur <zash@zash.se>
parents: 11675
diff changeset
682 return self:tlshandshake()
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
683 end
11699
1275dad71afd net.server_epoll: Use only fatal "write" timeout during TLS negotiation
Kim Alvefur <zash@zash.se>
parents: 11698
diff changeset
684 self:setreadtimeout(false);
11674
8253f6a30afc net.server_epoll: Use TLS handshake timeout after initiating handshake
Kim Alvefur <zash@zash.se>
parents: 11673
diff changeset
685 self:setwritetimeout(cfg.ssl_handshake_timeout);
11675
139d80774f11 net.server_epoll: Keep socket registered in epoll trough TLS wrapping
Kim Alvefur <zash@zash.se>
parents: 11674
diff changeset
686 self:set(true, true);
11672
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
687 end
79f8e29e88a0 net.server_epoll: Factor out TLS initialization into a method
Kim Alvefur <zash@zash.se>
parents: 11671
diff changeset
688
10847
42c8dce6e765 net.server_epoll: Fix typo in internal method name
Kim Alvefur <zash@zash.se>
parents: 10846
diff changeset
689 function interface:tlshandshake()
9430
412ff404bf58 net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents: 9387
diff changeset
690 self:setreadtimeout(false);
10846
d0b098d0f6c3 net.server_epoll: Log some noise before TLS handshake step
Kim Alvefur <zash@zash.se>
parents: 10683
diff changeset
691 self:noise("Continuing TLS handshake");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
692 local ok, err = self.conn:dohandshake();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
693 if ok then
10309
2a31571efce0 net.server_epoll: Guard against nil return from TLS info method
Kim Alvefur <zash@zash.se>
parents: 10307
diff changeset
694 local info = self.conn.info and self.conn:info();
2a31571efce0 net.server_epoll: Guard against nil return from TLS info method
Kim Alvefur <zash@zash.se>
parents: 10307
diff changeset
695 if type(info) == "table" then
10307
6a997f0ceba8 net.server_epoll: Log TLS version and cipher for all completed handshakes
Kim Alvefur <zash@zash.se>
parents: 10305
diff changeset
696 self:debug("TLS handshake complete (%s with %s)", info.protocol, info.cipher);
6a997f0ceba8 net.server_epoll: Log TLS version and cipher for all completed handshakes
Kim Alvefur <zash@zash.se>
parents: 10305
diff changeset
697 else
6a997f0ceba8 net.server_epoll: Log TLS version and cipher for all completed handshakes
Kim Alvefur <zash@zash.se>
parents: 10305
diff changeset
698 self:debug("TLS handshake complete");
6a997f0ceba8 net.server_epoll: Log TLS version and cipher for all completed handshakes
Kim Alvefur <zash@zash.se>
parents: 10305
diff changeset
699 end
11700
79d30de13fdb net.server_epoll: Reduce timer churn during TLS handshake
Kim Alvefur <zash@zash.se>
parents: 11699
diff changeset
700 self:setwritetimeout(false);
8542
8c39ec8e6018 net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents: 8541
diff changeset
701 self.onwritable = nil;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
702 self.onreadable = nil;
8008
62ed3795be13 net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents: 8007
diff changeset
703 self:on("status", "ssl-handshake-complete");
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
704 self:set(true, true);
11677
98bcc19361db net.server_epoll: Call onconnect immediately after TLS handshake completion
Kim Alvefur <zash@zash.se>
parents: 11676
diff changeset
705 self:onconnect();
98bcc19361db net.server_epoll: Call onconnect immediately after TLS handshake completion
Kim Alvefur <zash@zash.se>
parents: 11676
diff changeset
706 self:onreadable();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
707 elseif err == "wantread" then
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
708 self:noise("TLS handshake to wait until readable");
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
709 self:set(true, false);
11699
1275dad71afd net.server_epoll: Use only fatal "write" timeout during TLS negotiation
Kim Alvefur <zash@zash.se>
parents: 11698
diff changeset
710 self:setwritetimeout(cfg.ssl_handshake_timeout);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
711 elseif err == "wantwrite" then
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
712 self:noise("TLS handshake to wait until writable");
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
713 self:set(false, true);
9822
13c2707d2417 net.server_epoll: Rename handshake_timeout to ssl_handshake_timeout (fixes #1319)
Kim Alvefur <zash@zash.se>
parents: 9821
diff changeset
714 self:setwritetimeout(cfg.ssl_handshake_timeout);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
715 else
10649
228277be4a28 net.server_epoll: Reduce log level of TLS handshake errors to debug
Kim Alvefur <zash@zash.se>
parents: 10638
diff changeset
716 self:debug("TLS handshake error: %s", err);
7583
0498daffa6f0 net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents: 7574
diff changeset
717 self:on("disconnect", err);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
718 self:destroy();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
719 end
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
11063
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
722 local function wrapsocket(client, server, read_size, listeners, tls_ctx, extra) -- luasocket object -> interface object
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
723 client:settimeout(0);
10279
0e1701197722 net.server_epoll: Make log tag accessible as a field
Kim Alvefur <zash@zash.se>
parents: 10278
diff changeset
724 local conn_id = ("conn%s"):format(new_id());
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
725 local conn = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
726 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
727 _server = server;
10492
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
728 created = realtime();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
729 listeners = listeners;
9310
2d2d4c293efa net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents: 9309
diff changeset
730 read_size = read_size or (server and server.read_size);
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
731 writebuffer = nil;
9432
2e3f66475cc8 net.server_epoll: Inherit Direct TLS flag from servers to their clients
Kim Alvefur <zash@zash.se>
parents: 9431
diff changeset
732 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
733 tls_direct = server and server.tls_direct;
10279
0e1701197722 net.server_epoll: Make log tag accessible as a field
Kim Alvefur <zash@zash.se>
parents: 10278
diff changeset
734 id = conn_id;
0e1701197722 net.server_epoll: Make log tag accessible as a field
Kim Alvefur <zash@zash.se>
parents: 10278
diff changeset
735 log = logger.init(conn_id);
11063
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
736 extra = extra;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
737 }, interface_mt);
7591
138f16eb39fb net.server_epoll: Improve syntax
Kim Alvefur <zash@zash.se>
parents: 7590
diff changeset
738
11063
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
739 if extra then
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
740 if extra.servername then
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
741 conn.servername = extra.servername;
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
742 end
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
743 end
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
744
9383
3260902a5b22 net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents: 9382
diff changeset
745 conn:updatenames();
3260902a5b22 net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents: 9382
diff changeset
746 return conn;
3260902a5b22 net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents: 9382
diff changeset
747 end
3260902a5b22 net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents: 9382
diff changeset
748
3260902a5b22 net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents: 9382
diff changeset
749 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
750 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
751 local ok, peername, peerport = pcall(conn.getpeername, conn);
10330
d86b1304451a net.server_epoll: Handle getpeer/sockname returning a normal error
Kim Alvefur <zash@zash.se>
parents: 10329
diff changeset
752 if ok and peername then
10854
472fe13a05f9 net.server_epoll: Handle missing ports from getsock/peername (as in the case of unix sockets)
Matthew Wild <mwild1@gmail.com>
parents: 10847
diff changeset
753 self.peername, self.peerport = peername, peerport or 0;
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
754 end
9383
3260902a5b22 net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents: 9382
diff changeset
755 local ok, sockname, sockport = pcall(conn.getsockname, conn);
10330
d86b1304451a net.server_epoll: Handle getpeer/sockname returning a normal error
Kim Alvefur <zash@zash.se>
parents: 10329
diff changeset
756 if ok and sockname then
10854
472fe13a05f9 net.server_epoll: Handle missing ports from getsock/peername (as in the case of unix sockets)
Matthew Wild <mwild1@gmail.com>
parents: 10847
diff changeset
757 self.sockname, self.sockport = sockname, sockport or 0;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
758 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
759 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
760
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
761 -- A server interface has new incoming connections waiting
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
762 -- This replaces the onreadable callback
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
763 function interface:onacceptable()
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
764 local conn, err = self.conn:accept();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
765 if not conn then
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
766 self: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
767 self:pausefor(cfg.accept_retry_interval);
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
768 return;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
769 end
9309
33d500c25d76 net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents: 9306
diff changeset
770 local client = wrapsocket(conn, self, nil, self.listeners);
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
771 client:debug("New connection %s on server %s", client, self);
11686
f4c3dcc907d8 net.server_epoll: Add an (empty) method for setting socket options
Kim Alvefur <zash@zash.se>
parents: 11685
diff changeset
772 client:defaultoptions();
11738
3f1450ec37f4 net.server_epoll: Improve efficiency of opportunistic writes
Kim Alvefur <zash@zash.se>
parents: 11703
diff changeset
773 client._writable = cfg.opportunistic_writes;
9309
33d500c25d76 net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents: 9306
diff changeset
774 if self.tls_direct then
11659
00295a8e5bcf net.server_epoll: Separate handling of new incoming and outgoing connections
Kim Alvefur <zash@zash.se>
parents: 11658
diff changeset
775 client:add(true, true);
11676
993462e5e4d4 net.server_epoll: Refactor immediate TLS handshake start
Kim Alvefur <zash@zash.se>
parents: 11675
diff changeset
776 client:inittls(self.tls_ctx, true);
9695
5ec4eaf76de8 net.server_epoll: Call onconnect right after accept()ing a new client
Kim Alvefur <zash@zash.se>
parents: 9666
diff changeset
777 else
11659
00295a8e5bcf net.server_epoll: Separate handling of new incoming and outgoing connections
Kim Alvefur <zash@zash.se>
parents: 11658
diff changeset
778 client:add(true, false);
9695
5ec4eaf76de8 net.server_epoll: Call onconnect right after accept()ing a new client
Kim Alvefur <zash@zash.se>
parents: 9666
diff changeset
779 client:onconnect();
11660
68f0196ece2a net.server_epoll: Immediately attempt to read from newly accepted connections
Kim Alvefur <zash@zash.se>
parents: 11659
diff changeset
780 client:onreadable();
9309
33d500c25d76 net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents: 9306
diff changeset
781 end
7597
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
782 end
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
783
11659
00295a8e5bcf net.server_epoll: Separate handling of new incoming and outgoing connections
Kim Alvefur <zash@zash.se>
parents: 11658
diff changeset
784 -- Initialization for outgoing connections
7597
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
785 function interface:init()
9824
729de68f0068 net.server_epoll: Separate timeout for initial connection attempts
Kim Alvefur <zash@zash.se>
parents: 9823
diff changeset
786 self:setwritetimeout(cfg.connect_timeout);
11686
f4c3dcc907d8 net.server_epoll: Add an (empty) method for setting socket options
Kim Alvefur <zash@zash.se>
parents: 11685
diff changeset
787 self:defaultoptions();
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
788 return self:add(true, true);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
789 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
790
11686
f4c3dcc907d8 net.server_epoll: Add an (empty) method for setting socket options
Kim Alvefur <zash@zash.se>
parents: 11685
diff changeset
791 function interface:defaultoptions()
11689
f9f6e82fb572 net.server_epoll: Add setting for disabling the Nagle algorithm
Kim Alvefur <zash@zash.se>
parents: 11688
diff changeset
792 if cfg.nagle == false then
f9f6e82fb572 net.server_epoll: Add setting for disabling the Nagle algorithm
Kim Alvefur <zash@zash.se>
parents: 11688
diff changeset
793 self:setoption("tcp-nodelay", true);
f9f6e82fb572 net.server_epoll: Add setting for disabling the Nagle algorithm
Kim Alvefur <zash@zash.se>
parents: 11688
diff changeset
794 end
11687
153d2fdd97d3 net.server_epoll: Add way to enable TCP keeplives on all connections
Kim Alvefur <zash@zash.se>
parents: 11686
diff changeset
795 if cfg.tcp_keepalive then
153d2fdd97d3 net.server_epoll: Add way to enable TCP keeplives on all connections
Kim Alvefur <zash@zash.se>
parents: 11686
diff changeset
796 self:setoption("keepalive", true);
11688
3fc564f7441b net.server_epoll: Support setting keepalive idle time
Kim Alvefur <zash@zash.se>
parents: 11687
diff changeset
797 if type(cfg.tcp_keepalive) == "number" then
3fc564f7441b net.server_epoll: Support setting keepalive idle time
Kim Alvefur <zash@zash.se>
parents: 11687
diff changeset
798 self:setoption("tcp-keepidle", cfg.tcp_keepalive);
3fc564f7441b net.server_epoll: Support setting keepalive idle time
Kim Alvefur <zash@zash.se>
parents: 11687
diff changeset
799 end
11687
153d2fdd97d3 net.server_epoll: Add way to enable TCP keeplives on all connections
Kim Alvefur <zash@zash.se>
parents: 11686
diff changeset
800 end
11686
f4c3dcc907d8 net.server_epoll: Add an (empty) method for setting socket options
Kim Alvefur <zash@zash.se>
parents: 11685
diff changeset
801 end
f4c3dcc907d8 net.server_epoll: Add an (empty) method for setting socket options
Kim Alvefur <zash@zash.se>
parents: 11685
diff changeset
802
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
803 function interface:pause()
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
804 self:noise("Pause reading");
11702
c206430e2643 net.server_epoll: Ensure timeouts match epoll flags
Kim Alvefur <zash@zash.se>
parents: 11701
diff changeset
805 self:setreadtimeout(false);
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
806 return self:set(false);
7547
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
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
809 function interface:resume()
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
810 self:noise("Resume reading");
11702
c206430e2643 net.server_epoll: Ensure timeouts match epoll flags
Kim Alvefur <zash@zash.se>
parents: 11701
diff changeset
811 self:setreadtimeout();
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
812 return self:set(true);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
813 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
814
7624
23fb3a084956 net.server_epoll: Comments
Kim Alvefur <zash@zash.se>
parents: 7623
diff changeset
815 -- Pause connection for some time
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
816 function interface:pausefor(t)
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
817 self:noise("Pause for %fs", t);
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
818 if self._pausefor then
10985
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
819 closetimer(self._pausefor);
758ce12586de net.server_epoll: Optimize away table allocation for timer objects
Kim Alvefur <zash@zash.se>
parents: 10984
diff changeset
820 self._pausefor = nil;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
821 end
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
822 if t == false then return; end
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
823 self:set(false);
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
824 self._pausefor = addtimer(t, function ()
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
825 self._pausefor = nil;
9994
524b8cd76780 net.server_epoll: Restore wantread flag after pause (fixes #1354)
Kim Alvefur <zash@zash.se>
parents: 9932
diff changeset
826 self:set(true);
11743
856dada099fa net.server_epoll: Split, attempt to clarify dirty noise message
Kim Alvefur <zash@zash.se>
parents: 11741
diff changeset
827 self:noise("Resuming after pause");
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
828 if self.conn and self.conn:dirty() then
11743
856dada099fa net.server_epoll: Split, attempt to clarify dirty noise message
Kim Alvefur <zash@zash.se>
parents: 11741
diff changeset
829 self:noise("Have buffered incoming data to process");
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
830 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
831 end
7559
b1b27dedf4cf net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents: 7558
diff changeset
832 end);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
833 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
834
10098
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
835 function interface:setlimit(Bps)
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
836 if Bps > 0 then
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
837 self._limit = 1/Bps;
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
838 else
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
839 self._limit = nil;
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
840 end
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
841 end
dda1c7ccb209 server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents: 10091
diff changeset
842
9581
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
843 function interface:pause_writes()
10300
78f2a3a9c537 net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents: 10289
diff changeset
844 if self._write_lock then
78f2a3a9c537 net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents: 10289
diff changeset
845 return
78f2a3a9c537 net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents: 10289
diff changeset
846 end
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
847 self:noise("Pause writes");
9581
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
848 self._write_lock = true;
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
849 self:setwritetimeout(false);
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
850 self:set(nil, false);
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
851 end
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
852
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
853 function interface:resume_writes()
10300
78f2a3a9c537 net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents: 10289
diff changeset
854 if not self._write_lock then
78f2a3a9c537 net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents: 10289
diff changeset
855 return
78f2a3a9c537 net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents: 10289
diff changeset
856 end
10587
863aa36a9dd8 net.server_epoll: Add option for reducing debug logging
Kim Alvefur <zash@zash.se>
parents: 10586
diff changeset
857 self:noise("Resume writes");
9581
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
858 self._write_lock = nil;
11697
6c3d8aa3bbd9 net.server_epoll: Avoid allocating a buffer table for single writes
Kim Alvefur <zash@zash.se>
parents: 11696
diff changeset
859 if self.writebuffer and (self.writebuffer[1] or type(self.writebuffer) == "string") then
9581
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
860 self:setwritetimeout();
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
861 self:set(nil, true);
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
862 end
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
863 end
1be99aedb0b3 net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents: 9573
diff changeset
864
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
865 -- Connected!
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
866 function interface:onconnect()
10638
92918212ee83 net.server_epoll: Different error to distinguish connection timeout
Kim Alvefur <zash@zash.se>
parents: 10589
diff changeset
867 self._connected = true;
9594
dcf466e04f81 net.server_epoll: Use method to update peername on connect
Kim Alvefur <zash@zash.se>
parents: 9587
diff changeset
868 self:updatenames();
10331
76bd77ad6a2a server_epoll: Log full string represestation when connected
Kim Alvefur <zash@zash.se>
parents: 10330
diff changeset
869 self:debug("Connected (%s)", self);
8540
ba9fd886b34d net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents: 8507
diff changeset
870 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
871 self:on("connect");
8506
174fd716c9fa server_epoll: Rework how socket readyness is detected
Kim Alvefur <zash@zash.se>
parents: 8271
diff changeset
872 end
174fd716c9fa server_epoll: Rework how socket readyness is detected
Kim Alvefur <zash@zash.se>
parents: 8271
diff changeset
873
10861
e12690a37795 net.server_epoll: Add way to start accepting clients on an arbitrary server socket
Kim Alvefur <zash@zash.se>
parents: 10854
diff changeset
874 local function wrapserver(conn, addr, port, listeners, config)
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
875 local server = setmetatable({
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
876 conn = conn;
10492
8e1d9bba9244 net.server_epoll: Use monotonic time for scheduling
Kim Alvefur <zash@zash.se>
parents: 10491
diff changeset
877 created = realtime();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
878 listeners = listeners;
9835
20bf5b47c1fb net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents: 9825
diff changeset
879 read_size = config and config.read_size;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
880 onreadable = interface.onacceptable;
9835
20bf5b47c1fb net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents: 9825
diff changeset
881 tls_ctx = config and config.tls_ctx;
20bf5b47c1fb net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents: 9825
diff changeset
882 tls_direct = config and config.tls_direct;
9847
adc0672b700e net.server_epoll: Add support for SNI (#409)
Kim Alvefur <zash@zash.se>
parents: 9835
diff changeset
883 hosts = config and config.sni_hosts;
7584
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
884 sockname = addr;
98ee3ed105cf net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents: 7583
diff changeset
885 sockport = port;
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
886 log = logger.init(("serv%s"):format(new_id()));
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
887 }, interface_mt);
10089
90e459f48cbd net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents: 10088
diff changeset
888 server:debug("Server %s created", server);
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
889 server:add(true, false);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
890 return server;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
891 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
892
10861
e12690a37795 net.server_epoll: Add way to start accepting clients on an arbitrary server socket
Kim Alvefur <zash@zash.se>
parents: 10854
diff changeset
893 local function listen(addr, port, listeners, config)
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
894 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
895 if not conn then return conn, err; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
896 conn:settimeout(0);
10861
e12690a37795 net.server_epoll: Add way to start accepting clients on an arbitrary server socket
Kim Alvefur <zash@zash.se>
parents: 10854
diff changeset
897 return wrapserver(conn, addr, port, listeners, config);
e12690a37795 net.server_epoll: Add way to start accepting clients on an arbitrary server socket
Kim Alvefur <zash@zash.se>
parents: 10854
diff changeset
898 end
e12690a37795 net.server_epoll: Add way to start accepting clients on an arbitrary server socket
Kim Alvefur <zash@zash.se>
parents: 10854
diff changeset
899
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
900 -- COMPAT
9835
20bf5b47c1fb net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents: 9825
diff changeset
901 local function addserver(addr, port, listeners, read_size, tls_ctx)
20bf5b47c1fb net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents: 9825
diff changeset
902 return listen(addr, port, listeners, {
9310
2d2d4c293efa net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents: 9309
diff changeset
903 read_size = read_size;
9300
1df8832bd29e net.server_epoll: Rename all variables holding TLS contexts for consistency
Kim Alvefur <zash@zash.se>
parents: 8983
diff changeset
904 tls_ctx = tls_ctx;
9309
33d500c25d76 net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents: 9306
diff changeset
905 tls_direct = tls_ctx and true or false;
9835
20bf5b47c1fb net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents: 9825
diff changeset
906 });
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
907 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
908
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
909 -- COMPAT
11063
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
910 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx, extra)
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
911 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx, extra);
7597
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
912 if not client.peername then
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
913 client.peername, client.peerport = addr, port;
cd34aa6b1632 net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents: 7596
diff changeset
914 end
9473
5fdda751333a net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents: 9445
diff changeset
915 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
916 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
917 if tls_ctx then
33d500c25d76 net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents: 9306
diff changeset
918 client:starttls(tls_ctx);
33d500c25d76 net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents: 9306
diff changeset
919 end
7551
838212918f11 net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents: 7550
diff changeset
920 return client;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
921 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
922
7800
dadf9ba290c5 server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents: 7799
diff changeset
923 -- New outgoing TCP connection
11063
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
924 local function addclient(addr, port, listeners, read_size, tls_ctx, typ, extra)
9497
183ff7a8051b net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents: 9477
diff changeset
925 local create;
183ff7a8051b net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents: 9477
diff changeset
926 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
927 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
928 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
929 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
930 typ = "tcp6";
183ff7a8051b net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents: 9477
diff changeset
931 else
183ff7a8051b net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents: 9477
diff changeset
932 typ = "tcp4";
183ff7a8051b net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents: 9477
diff changeset
933 end
183ff7a8051b net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents: 9477
diff changeset
934 end
183ff7a8051b net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents: 9477
diff changeset
935 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
936 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
937 end
183ff7a8051b net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents: 9477
diff changeset
938 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
939 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
940 end
5fdda751333a net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents: 9445
diff changeset
941 local conn, err = create();
10082
1df53ef1f60d net.server_epoll: Return errors from creating sockets
Kim Alvefur <zash@zash.se>
parents: 10081
diff changeset
942 if not conn then return conn, err; end
9498
cc593002f2e2 net.server_epoll: Check return conditions of early socket setup
Kim Alvefur <zash@zash.se>
parents: 9497
diff changeset
943 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
944 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
945 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
946 if not ok and err ~= "timeout" then return ok, err; end
11063
30d3f6f85eb8 net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents: 10681
diff changeset
947 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx, extra)
9473
5fdda751333a net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents: 9445
diff changeset
948 local ok, err = client:init();
10332
e05ea8223b32 net.server_epoll: Save IP and port from connection creation call
Kim Alvefur <zash@zash.se>
parents: 10331
diff changeset
949 if not client.peername then
e05ea8223b32 net.server_epoll: Save IP and port from connection creation call
Kim Alvefur <zash@zash.se>
parents: 10331
diff changeset
950 -- otherwise not set until connected
e05ea8223b32 net.server_epoll: Save IP and port from connection creation call
Kim Alvefur <zash@zash.se>
parents: 10331
diff changeset
951 client.peername, client.peerport = addr, port;
e05ea8223b32 net.server_epoll: Save IP and port from connection creation call
Kim Alvefur <zash@zash.se>
parents: 10331
diff changeset
952 end
9473
5fdda751333a net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents: 9445
diff changeset
953 if not ok then return ok, err; end
10328
5c2b31c823b9 net.server_epoll: Move a log message to improve ordering
Kim Alvefur <zash@zash.se>
parents: 10309
diff changeset
954 client:debug("Client %s created", client);
9309
33d500c25d76 net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents: 9306
diff changeset
955 if tls_ctx then
33d500c25d76 net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents: 9306
diff changeset
956 client:starttls(tls_ctx);
33d500c25d76 net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents: 9306
diff changeset
957 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
958 return client, conn;
7553
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
959 end
4f3e4a092348 net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents: 7552
diff changeset
960
9319
7c954c75b6ac net.server_epoll: Fix write signal handling in watchfd API
Kim Alvefur <zash@zash.se>
parents: 9314
diff changeset
961 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
962 local conn = setmetatable({
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
963 conn = fd;
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
964 onreadable = onreadable;
9319
7c954c75b6ac net.server_epoll: Fix write signal handling in watchfd API
Kim Alvefur <zash@zash.se>
parents: 9314
diff changeset
965 onwritable = onwritable;
8781
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
966 close = function (self)
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
967 self:del();
8781
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
968 end
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
969 }, interface_mt);
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
970 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
971 conn.getfd = function ()
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
972 return fd;
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
973 end;
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
974 -- 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
975 end
10420
49a828819696 net.server_epoll: Save log tag in a field on FD watchers too
Kim Alvefur <zash@zash.se>
parents: 10419
diff changeset
976 conn.id = new_id();
49a828819696 net.server_epoll: Save log tag in a field on FD watchers too
Kim Alvefur <zash@zash.se>
parents: 10419
diff changeset
977 conn.log = logger.init(("fdwatch%s"):format(conn.id));
9319
7c954c75b6ac net.server_epoll: Fix write signal handling in watchfd API
Kim Alvefur <zash@zash.se>
parents: 9314
diff changeset
978 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
979 return conn;
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
980 end;
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
981
7800
dadf9ba290c5 server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents: 7799
diff changeset
982 -- Dump all data from one connection into another
10283
dff360d00dfa net.server_epoll: Handle read size argument to link
Kim Alvefur <zash@zash.se>
parents: 10282
diff changeset
983 local function link(from, to, read_size)
10280
6b8b255f1d10 net.server_epoll: Add debug logging for various connection events
Kim Alvefur <zash@zash.se>
parents: 10279
diff changeset
984 from:debug("Linking to %s", to.id);
10284
cd669975031b net.server_epoll: Fix link function to not replace listeners
Kim Alvefur <zash@zash.se>
parents: 10283
diff changeset
985 function from:onincoming(data)
cd669975031b net.server_epoll: Fix link function to not replace listeners
Kim Alvefur <zash@zash.se>
parents: 10283
diff changeset
986 self:pause();
11805
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
987 to:write(data);
10284
cd669975031b net.server_epoll: Fix link function to not replace listeners
Kim Alvefur <zash@zash.se>
parents: 10283
diff changeset
988 end
10285
bcf61e22f841 net.server_epoll: Ignore unused self argument [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10284
diff changeset
989 function to:ondrain() -- luacheck: ignore 212/self
11805
2327e03d1f8d net.server_epoll: Fix indentation messed up in last merge
Kim Alvefur <zash@zash.se>
parents: 11804
diff changeset
990 from:resume();
10284
cd669975031b net.server_epoll: Fix link function to not replace listeners
Kim Alvefur <zash@zash.se>
parents: 10283
diff changeset
991 end
10283
dff360d00dfa net.server_epoll: Handle read size argument to link
Kim Alvefur <zash@zash.se>
parents: 10282
diff changeset
992 from:set_mode(read_size);
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
993 from:set(true, nil);
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
994 to:set(nil, true);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
995 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
996
9438
916bee81eb7e net.server_epoll: Make :set_send a noop, should fix net.adns
Kim Alvefur <zash@zash.se>
parents: 9432
diff changeset
997 -- COMPAT
916bee81eb7e net.server_epoll: Make :set_send a noop, should fix net.adns
Kim Alvefur <zash@zash.se>
parents: 9432
diff changeset
998 -- 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
999 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
1000 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1001
7800
dadf9ba290c5 server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents: 7799
diff changeset
1002 -- 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
1003 local function closeall()
7799
93643a61858a server_epoll: Ignore unused loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7797
diff changeset
1004 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
1005 conn:close();
424bbbeb9257 server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents: 7737
diff changeset
1006 end
424bbbeb9257 server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents: 7737
diff changeset
1007 end
424bbbeb9257 server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents: 7737
diff changeset
1008
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1009 local quitting = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1010
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
1011 -- 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
1012 local function setquitting(quit)
12609aee3b3b server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents: 7795
diff changeset
1013 if quit then
12609aee3b3b server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents: 7795
diff changeset
1014 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
1015 closeall();
11810
fe0cdbad19c3 net.server_epoll: Close sockets added after shutdown signal (fixes #1670)
Kim Alvefur <zash@zash.se>
parents: 11751
diff changeset
1016 addtimer(1, function ()
fe0cdbad19c3 net.server_epoll: Close sockets added after shutdown signal (fixes #1670)
Kim Alvefur <zash@zash.se>
parents: 11751
diff changeset
1017 if quitting then
fe0cdbad19c3 net.server_epoll: Close sockets added after shutdown signal (fixes #1670)
Kim Alvefur <zash@zash.se>
parents: 11751
diff changeset
1018 closeall();
fe0cdbad19c3 net.server_epoll: Close sockets added after shutdown signal (fixes #1670)
Kim Alvefur <zash@zash.se>
parents: 11751
diff changeset
1019 return 1;
fe0cdbad19c3 net.server_epoll: Close sockets added after shutdown signal (fixes #1670)
Kim Alvefur <zash@zash.se>
parents: 11751
diff changeset
1020 end
fe0cdbad19c3 net.server_epoll: Close sockets added after shutdown signal (fixes #1670)
Kim Alvefur <zash@zash.se>
parents: 11751
diff changeset
1021 end);
11811
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
1022 if cfg.shutdown_deadline then
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
1023 addtimer(cfg.shutdown_deadline, function ()
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
1024 if quitting then
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
1025 for fd, conn in pairs(fds) do -- luacheck: ignore 213/fd
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
1026 conn:destroy();
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
1027 end
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
1028 end
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
1029 end);
ae43166fe931 net.server_epoll: Add a hard deadline on shutdown to extra-fix #1670
Kim Alvefur <zash@zash.se>
parents: 11810
diff changeset
1030 end
7796
12609aee3b3b server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents: 7795
diff changeset
1031 else
12609aee3b3b server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents: 7795
diff changeset
1032 quitting = nil;
12609aee3b3b server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents: 7795
diff changeset
1033 end
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1034 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1035
7637
cccea9136b2d net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents: 7630
diff changeset
1036 -- 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
1037 local function loop(once)
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1038 repeat
7658
e14a124c4d73 net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents: 7657
diff changeset
1039 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
1040 local fd, r, w = poll:wait(t);
12084
59557bc3c4b8 net.server_epoll: Process all queued events from epoll before timers
Kim Alvefur <zash@zash.se>
parents: 11811
diff changeset
1041 while fd do
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1042 local conn = fds[fd];
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1043 if conn then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1044 if r then
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1045 conn:onreadable();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1046 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1047 if w then
8542
8c39ec8e6018 net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents: 8541
diff changeset
1048 conn:onwritable();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1049 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1050 else
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1051 log("debug", "Removing unknown fd %d", fd);
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
1052 poll:del(fd);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1053 end
12084
59557bc3c4b8 net.server_epoll: Process all queued events from epoll before timers
Kim Alvefur <zash@zash.se>
parents: 11811
diff changeset
1054 fd, r, w = poll:wait(0);
59557bc3c4b8 net.server_epoll: Process all queued events from epoll before timers
Kim Alvefur <zash@zash.se>
parents: 11811
diff changeset
1055 end
59557bc3c4b8 net.server_epoll: Process all queued events from epoll before timers
Kim Alvefur <zash@zash.se>
parents: 11811
diff changeset
1056 if 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
1057 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
1058 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
1059 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
1060 return quitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1061 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1062
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1063 return {
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1064 get_backend = function () return "epoll"; end;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1065 addserver = addserver;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1066 addclient = addclient;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1067 add_task = addtimer;
10983
0862325842ec net.server_epoll: Signal API-compatibilty with util.timer
Kim Alvefur <zash@zash.se>
parents: 10982
diff changeset
1068 timer = {
0862325842ec net.server_epoll: Signal API-compatibilty with util.timer
Kim Alvefur <zash@zash.se>
parents: 10982
diff changeset
1069 -- API-compatible with util.timer
0862325842ec net.server_epoll: Signal API-compatibilty with util.timer
Kim Alvefur <zash@zash.se>
parents: 10982
diff changeset
1070 add_task = addtimer;
0862325842ec net.server_epoll: Signal API-compatibilty with util.timer
Kim Alvefur <zash@zash.se>
parents: 10982
diff changeset
1071 stop = closetimer;
0862325842ec net.server_epoll: Signal API-compatibilty with util.timer
Kim Alvefur <zash@zash.se>
parents: 10982
diff changeset
1072 reschedule = reschedule;
10987
62355b801772 net.server_epoll: Expose way to turn monotonic time into wall clock time
Kim Alvefur <zash@zash.se>
parents: 10985
diff changeset
1073 to_absolute_time = function (t)
62355b801772 net.server_epoll: Expose way to turn monotonic time into wall clock time
Kim Alvefur <zash@zash.se>
parents: 10985
diff changeset
1074 return t-monotonic()+realtime();
62355b801772 net.server_epoll: Expose way to turn monotonic time into wall clock time
Kim Alvefur <zash@zash.se>
parents: 10985
diff changeset
1075 end;
10983
0862325842ec net.server_epoll: Signal API-compatibilty with util.timer
Kim Alvefur <zash@zash.se>
parents: 10982
diff changeset
1076 };
9835
20bf5b47c1fb net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents: 9825
diff changeset
1077 listen = listen;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1078 loop = loop;
7794
424bbbeb9257 server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents: 7737
diff changeset
1079 closeall = closeall;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1080 setquitting = setquitting;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1081 wrapclient = wrapclient;
10861
e12690a37795 net.server_epoll: Add way to start accepting clients on an arbitrary server socket
Kim Alvefur <zash@zash.se>
parents: 10854
diff changeset
1082 wrapserver = wrapserver;
8781
53178b6ba589 net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents: 8778
diff changeset
1083 watchfd = watchfd;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1084 link = link;
7657
252823632401 net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents: 7637
diff changeset
1085 set_config = function (newconfig)
252823632401 net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents: 7637
diff changeset
1086 cfg = setmetatable(newconfig, default_config);
252823632401 net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents: 7637
diff changeset
1087 end;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1088
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1089 -- libevent emulation
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1090 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
1091 addevent = function (fd, mode, callback)
10088
97da8452c6a6 net.server_epoll: Deprecate libevent emulation layer
Kim Alvefur <zash@zash.se>
parents: 10082
diff changeset
1092 log("warn", "Using deprecated libevent emulation, please update code to use watchfd API instead");
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1093 local function onevent(self)
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1094 local ret = self:callback();
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1095 if ret == -1 then
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
1096 self:set(false, false);
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1097 elseif ret then
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
1098 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
1099 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1100 end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1101
7817
4e3654031693 net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents: 7811
diff changeset
1102 local conn = setmetatable({
4e3654031693 net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents: 7811
diff changeset
1103 getfd = function () return fd; end;
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1104 callback = callback;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1105 onreadable = onevent;
8542
8c39ec8e6018 net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents: 8541
diff changeset
1106 onwritable = onevent;
7817
4e3654031693 net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents: 7811
diff changeset
1107 close = function (self)
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
1108 self:del();
7547
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1109 fds[fd] = nil;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1110 end;
7817
4e3654031693 net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents: 7811
diff changeset
1111 }, interface_mt);
10420
49a828819696 net.server_epoll: Save log tag in a field on FD watchers too
Kim Alvefur <zash@zash.se>
parents: 10419
diff changeset
1112 conn.id = conn:getfd();
49a828819696 net.server_epoll: Save log tag in a field on FD watchers too
Kim Alvefur <zash@zash.se>
parents: 10419
diff changeset
1113 conn.log = logger.init(("fdwatch%d"):format(conn.id));
9314
c9eef7e8ee65 net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents: 9311
diff changeset
1114 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
1115 if not ok then return ok, err; end
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1116 return conn;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1117 end;
b327322ce2dd net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1118 };