Software /
code /
prosody
Annotate
net/server_select.lua @ 12481:2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
This now requires that the network backend exposes a tls_builder
function, which essentially wraps the former util.sslconfig.new()
function, passing a factory to create the eventual SSL context.
That allows a net.server backend to pick whatever it likes as SSL
context factory, as long as it understands the config table passed by
the SSL config builder. Heck, a backend could even mock and replace the
entire SSL config builder API.
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Sat, 02 Apr 2022 11:15:33 +0200 |
parent | 12480:7e9ebdc75ce4 |
child | 12487:db634db8e069 |
rev | line source |
---|---|
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5664
diff
changeset
|
1 -- |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
2 -- server.lua by blastbeat of the luadch project |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
3 -- Re-used here under the MIT/X Consortium License |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5664
diff
changeset
|
4 -- |
2925 | 5 -- Modifications (C) 2008-2010 Matthew Wild, Waqas Hussain |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
6 -- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
7 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
8 -- // wrapping luadch stuff // -- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
9 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
10 local use = function( what ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
11 return _G[ what ] |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
12 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
13 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
14 local log, table_concat = require ("util.logger").init("socket"), table.concat; |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
15 local out_put = function (...) return log("debug", table_concat{...}); end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
16 local out_error = function (...) return log("warn", table_concat{...}); end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
17 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
18 ----------------------------------// DECLARATION //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
19 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
20 --// constants //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
21 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
22 local STAT_UNIT = 1 -- byte |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
23 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
24 --// lua functions //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
25 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
26 local type = use "type" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
27 local pairs = use "pairs" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
28 local ipairs = use "ipairs" |
3685
63efe4eefa07
net.server_select: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents:
3682
diff
changeset
|
29 local tonumber = use "tonumber" |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
30 local tostring = use "tostring" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
31 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
32 --// lua libs //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
33 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
34 local table = use "table" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
35 local string = use "string" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
36 local coroutine = use "coroutine" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
37 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
38 --// lua lib methods //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
39 |
3682
d12141cbbaa0
net.server_select: Make changes required for sub-second timer precision.
Waqas Hussain <waqas20@gmail.com>
parents:
3543
diff
changeset
|
40 local math_min = math.min |
d12141cbbaa0
net.server_select: Make changes required for sub-second timer precision.
Waqas Hussain <waqas20@gmail.com>
parents:
3543
diff
changeset
|
41 local math_huge = math.huge |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
42 local table_concat = table.concat |
6481
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
43 local table_insert = table.insert |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
44 local string_sub = string.sub |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
45 local coroutine_wrap = coroutine.wrap |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
46 local coroutine_yield = coroutine.yield |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
47 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
48 --// extern libs //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
49 |
6782
ec172dbe9d14
net.server_{select,event}: Don't rely on LuaSocket and LuaSec being present in the globals table
Kim Alvefur <zash@zash.se>
parents:
6459
diff
changeset
|
50 local has_luasec, luasec = pcall ( require , "ssl" ) |
2597
40a174e8cdf6
net.server_select: Fix traceback with no LuaSec available (thanks Nolan)
Matthew Wild <mwild1@gmail.com>
parents:
2582
diff
changeset
|
51 local luasocket = use "socket" or require "socket" |
3682
d12141cbbaa0
net.server_select: Make changes required for sub-second timer precision.
Waqas Hussain <waqas20@gmail.com>
parents:
3543
diff
changeset
|
52 local luasocket_gettime = luasocket.gettime |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
53 local inet = require "util.net"; |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
54 local inet_pton = inet.pton; |
12481
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
12480
diff
changeset
|
55 local sslconfig = require "util.sslconfig"; |
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
12480
diff
changeset
|
56 local tls_impl = require "net.tls_luasec"; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
57 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
58 --// extern lib methods //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
59 |
6782
ec172dbe9d14
net.server_{select,event}: Don't rely on LuaSocket and LuaSec being present in the globals table
Kim Alvefur <zash@zash.se>
parents:
6459
diff
changeset
|
60 local ssl_wrap = ( has_luasec and luasec.wrap ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
61 local socket_bind = luasocket.bind |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
62 local socket_select = luasocket.select |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
63 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
64 --// functions //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
65 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
66 local id |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
67 local loop |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
68 local stats |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
69 local idfalse |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
70 local closeall |
3686
27d3e2a2a248
net.server_select: Made another global local.
Waqas Hussain <waqas20@gmail.com>
parents:
3685
diff
changeset
|
71 local addsocket |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
72 local addserver |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
73 local listen |
5288
2777f34adbd3
net.server_select: Fix global access
Kim Alvefur <zash@zash.se>
parents:
5280
diff
changeset
|
74 local addtimer |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
75 local getserver |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
76 local wrapserver |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
77 local getsettings |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
78 local closesocket |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
79 local removesocket |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
80 local removeserver |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
81 local wrapconnection |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
82 local changesettings |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
83 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
84 --// tables //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
85 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
86 local _server |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
87 local _readlist |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
88 local _timerlist |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
89 local _sendlist |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
90 local _socketlist |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
91 local _closelist |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
92 local _readtimes |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
93 local _writetimes |
7094
e8f202856fc0
server_select: Keep track of which server listeners are full
Kim Alvefur <zash@zash.se>
parents:
6381
diff
changeset
|
94 local _fullservers |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
95 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
96 --// simple data types //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
97 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
98 local _ |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
99 local _readlistlen |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
100 local _sendlistlen |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
101 local _timerlistlen |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
102 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
103 local _sendtraffic |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
104 local _readtraffic |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
105 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
106 local _selecttimeout |
5531
483f795f6f99
net.server*: Allow the TCP backlog parameter to be set in the config
Kim Alvefur <zash@zash.se>
parents:
5492
diff
changeset
|
107 local _tcpbacklog |
7095
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
108 local _accepretry |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
109 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
110 local _starttime |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
111 local _currenttime |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
112 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
113 local _maxsendlen |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
114 local _maxreadlen |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
115 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
116 local _checkinterval |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
117 local _sendtimeout |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
118 local _readtimeout |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
119 |
5331
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
120 local _maxselectlen |
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
121 local _maxfd |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
122 |
3685
63efe4eefa07
net.server_select: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents:
3682
diff
changeset
|
123 local _maxsslhandshake |
63efe4eefa07
net.server_select: Made some globals local.
Waqas Hussain <waqas20@gmail.com>
parents:
3682
diff
changeset
|
124 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
125 ----------------------------------// DEFINITION //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
126 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
127 _server = { } -- key = port, value = table; list of listening servers |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
128 _readlist = { } -- array with sockets to read from |
9993
02a41315d275
Fix various spelling mistakes [codespell]
Kim Alvefur <zash@zash.se>
parents:
9916
diff
changeset
|
129 _sendlist = { } -- array with sockets to write to |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
130 _timerlist = { } -- array of timer functions |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
131 _socketlist = { } -- key = socket, value = wrapped socket (handlers) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
132 _readtimes = { } -- key = handler, value = timestamp of last data reading |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
133 _writetimes = { } -- key = handler, value = timestamp of last data writing/sending |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
134 _closelist = { } -- handlers to close |
7094
e8f202856fc0
server_select: Keep track of which server listeners are full
Kim Alvefur <zash@zash.se>
parents:
6381
diff
changeset
|
135 _fullservers = { } -- servers in a paused state while there are too many clients |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
136 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
137 _readlistlen = 0 -- length of readlist |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
138 _sendlistlen = 0 -- length of sendlist |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8528
diff
changeset
|
139 _timerlistlen = 0 -- length of timerlist |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
140 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
141 _sendtraffic = 0 -- some stats |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
142 _readtraffic = 0 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
143 |
3775
f3f2a7810108
net/server_select.lua: Reduce select() timeout back to 1s
Matthew Wild <mwild1@gmail.com>
parents:
3687
diff
changeset
|
144 _selecttimeout = 1 -- timeout of socket.select |
5531
483f795f6f99
net.server*: Allow the TCP backlog parameter to be set in the config
Kim Alvefur <zash@zash.se>
parents:
5492
diff
changeset
|
145 _tcpbacklog = 128 -- some kind of hint to the OS |
7095
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
146 _accepretry = 10 -- seconds to wait until the next attempt of a full server to accept |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
147 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
148 _maxsendlen = 51000 * 1024 -- max len of send buffer |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
149 _maxreadlen = 25000 * 1024 -- max len of read buffer |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
150 |
5633
d999027577ed
net.server_select: Default checkinterval to 30s, so that read timeouts are actually detected
Matthew Wild <mwild1@gmail.com>
parents:
5632
diff
changeset
|
151 _checkinterval = 30 -- interval in secs to check idle clients |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
152 _sendtimeout = 60000 -- allowed send idle time in secs |
8983
23f66f04e216
net.server: Set default read timeout to 14 minutes (fixes #971)
Kim Alvefur <zash@zash.se>
parents:
8781
diff
changeset
|
153 _readtimeout = 14 * 60 -- allowed read idle time in secs |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
154 |
9993
02a41315d275
Fix various spelling mistakes [codespell]
Kim Alvefur <zash@zash.se>
parents:
9916
diff
changeset
|
155 local is_windows = package.config:sub(1,1) == "\\" -- check the directory separator, to determine whether this is Windows |
5642
5862bb57a3f1
net.server_select: Ensure _maxfd = math.huge on Windows, always.
Waqas Hussain <waqas20@gmail.com>
parents:
5548
diff
changeset
|
156 _maxfd = (is_windows and math.huge) or luasocket._SETSIZE or 1024 -- max fd number, limit to 1024 by default to prevent glibc buffer overflow, but not on Windows |
5331
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
157 _maxselectlen = luasocket._SETSIZE or 1024 -- But this still applies on Windows |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
158 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
159 _maxsslhandshake = 30 -- max handshake round-trips |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
160 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
161 ----------------------------------// PRIVATE //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
162 |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
163 wrapserver = function( listeners, socket, ip, serverport, pattern, sslctx, ssldirect ) -- this function wraps a server -- FIXME Make sure FD < _maxfd |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
164 |
5331
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
165 if socket:getfd() >= _maxfd then |
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
166 out_error("server.lua: Disallowed FD number: "..socket:getfd()) |
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
167 socket:close() |
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
168 return nil, "fd-too-large" |
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
169 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
170 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
171 local connections = 0 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
172 |
4388
eded80beab58
net.server_select: Never call onincoming with nil data when a listener doesn't have an onconnect handler
Matthew Wild <mwild1@gmail.com>
parents:
4353
diff
changeset
|
173 local dispatch, disconnect = listeners.onconnect, listeners.ondisconnect |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
174 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
175 local accept = socket.accept |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
176 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
177 --// public methods of the object //-- |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
178 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
179 local handler = { } |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
180 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
181 handler.shutdown = function( ) end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
182 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
183 handler.ssl = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
184 return sslctx ~= nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
185 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
186 handler.sslctx = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
187 return sslctx |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
188 end |
9851
75d2874502c3
net.server_select: SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
189 handler.hosts = {} -- sni |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
190 handler.remove = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
191 connections = connections - 1 |
5280
1514a6147e85
net.server_select: Pause servers while they are full
Kim Alvefur <zash@zash.se>
parents:
5279
diff
changeset
|
192 if handler then |
1514a6147e85
net.server_select: Pause servers while they are full
Kim Alvefur <zash@zash.se>
parents:
5279
diff
changeset
|
193 handler.resume( ) |
1514a6147e85
net.server_select: Pause servers while they are full
Kim Alvefur <zash@zash.se>
parents:
5279
diff
changeset
|
194 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
195 end |
4904
5d2ff4627b72
net.server_select: Don't close client connections when closing a server (it wasn't working anyway)
Matthew Wild <mwild1@gmail.com>
parents:
4903
diff
changeset
|
196 handler.close = function() |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
197 socket:close( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
198 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
199 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
4675
80f2d952d4c5
net.server_select: Remove server from _server table when closing
Matthew Wild <mwild1@gmail.com>
parents:
4429
diff
changeset
|
200 _server[ip..":"..serverport] = nil; |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
201 _socketlist[ socket ] = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
202 handler = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
203 socket = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
204 --mem_free( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
205 out_put "server.lua: closed server handler and removed sockets from list" |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
206 end |
5330
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
207 handler.pause = function( hard ) |
5279
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
208 if not handler.paused then |
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
209 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
5330
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
210 if hard then |
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
211 _socketlist[ socket ] = nil |
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
212 socket:close( ) |
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
213 socket = nil; |
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
214 end |
5279
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
215 handler.paused = true; |
7097
5a71aaa2c542
server_select: Add logging of when servers are paused and resumed
Kim Alvefur <zash@zash.se>
parents:
7096
diff
changeset
|
216 out_put("server.lua: server [", ip, "]:", serverport, " paused") |
5279
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
217 end |
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
218 end |
5330
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
219 handler.resume = function( ) |
5279
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
220 if handler.paused then |
5330
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
221 if not socket then |
5531
483f795f6f99
net.server*: Allow the TCP backlog parameter to be set in the config
Kim Alvefur <zash@zash.se>
parents:
5492
diff
changeset
|
222 socket = socket_bind( ip, serverport, _tcpbacklog ); |
5330
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
223 socket:settimeout( 0 ) |
0a0ca2eb991b
net.server_select: Be less aggressive about server pause
Kim Alvefur <zash@zash.se>
parents:
5289
diff
changeset
|
224 end |
5279
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
225 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
226 _socketlist[ socket ] = handler |
7094
e8f202856fc0
server_select: Keep track of which server listeners are full
Kim Alvefur <zash@zash.se>
parents:
6381
diff
changeset
|
227 _fullservers[ handler ] = nil |
5279
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
228 handler.paused = false; |
7097
5a71aaa2c542
server_select: Add logging of when servers are paused and resumed
Kim Alvefur <zash@zash.se>
parents:
7096
diff
changeset
|
229 out_put("server.lua: server [", ip, "]:", serverport, " resumed") |
5279
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
230 end |
e8f7962056f5
net.server_select: Add pause and resume methods
Kim Alvefur <zash@zash.se>
parents:
5197
diff
changeset
|
231 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
232 handler.ip = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
233 return ip |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
234 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
235 handler.serverport = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
236 return serverport |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
237 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
238 handler.socket = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
239 return socket |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
240 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
241 handler.readbuffer = function( ) |
5331
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
242 if _readlistlen >= _maxselectlen or _sendlistlen >= _maxselectlen then |
5280
1514a6147e85
net.server_select: Pause servers while they are full
Kim Alvefur <zash@zash.se>
parents:
5279
diff
changeset
|
243 handler.pause( ) |
7094
e8f202856fc0
server_select: Keep track of which server listeners are full
Kim Alvefur <zash@zash.se>
parents:
6381
diff
changeset
|
244 _fullservers[ handler ] = _currenttime |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
245 out_put( "server.lua: refused new client connection: server full" ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
246 return false |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
247 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
248 local client, err = accept( socket ) -- try to accept |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
249 if client then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
250 local ip, clientport = client:getpeername( ) |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
251 local handler, client, err = wrapconnection( handler, listeners, client, ip, serverport, clientport, pattern, sslctx, ssldirect ) -- wrap new client socket |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
252 if err then -- error while wrapping ssl socket |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
253 return false |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
254 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
255 connections = connections + 1 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
256 out_put( "server.lua: accepted new client connection from ", tostring(ip), ":", tostring(clientport), " to ", tostring(serverport)) |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
257 if dispatch and not ssldirect then -- SSL connections will notify onconnect when handshake completes |
4388
eded80beab58
net.server_select: Never call onincoming with nil data when a listener doesn't have an onconnect handler
Matthew Wild <mwild1@gmail.com>
parents:
4353
diff
changeset
|
258 return dispatch( handler ); |
eded80beab58
net.server_select: Never call onincoming with nil data when a listener doesn't have an onconnect handler
Matthew Wild <mwild1@gmail.com>
parents:
4353
diff
changeset
|
259 end |
eded80beab58
net.server_select: Never call onincoming with nil data when a listener doesn't have an onconnect handler
Matthew Wild <mwild1@gmail.com>
parents:
4353
diff
changeset
|
260 return; |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
261 elseif err then -- maybe timeout or something else |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
262 out_put( "server.lua: error with new client connection: ", tostring(err) ) |
7096
95e4c81291cc
server_select: Pause and mark server as full if accepting a client fails (fixes #597)
Kim Alvefur <zash@zash.se>
parents:
7095
diff
changeset
|
263 handler.pause( ) |
95e4c81291cc
server_select: Pause and mark server as full if accepting a client fails (fixes #597)
Kim Alvefur <zash@zash.se>
parents:
7095
diff
changeset
|
264 _fullservers[ handler ] = _currenttime |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
265 return false |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
266 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
267 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
268 return handler |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
269 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
270 |
10233
600eee3c4752
net.server: Accept and save an 'extra' field for client connections
Kim Alvefur <zash@zash.se>
parents:
9993
diff
changeset
|
271 wrapconnection = function( server, listeners, socket, ip, serverport, clientport, pattern, sslctx, ssldirect, extra ) -- this function wraps a client to a handler object |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
272 |
5331
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
273 if socket:getfd() >= _maxfd then |
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
274 out_error("server.lua: Disallowed FD number: "..socket:getfd()) -- PROTIP: Switch to libevent |
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
275 socket:close( ) -- Should we send some kind of error here? |
5663
4e51ceee469c
net.server_select: Make sure there is a server to pause when hitting maxfd (Thanks elghinn)
Kim Alvefur <zash@zash.se>
parents:
5642
diff
changeset
|
276 if server then |
7094
e8f202856fc0
server_select: Keep track of which server listeners are full
Kim Alvefur <zash@zash.se>
parents:
6381
diff
changeset
|
277 _fullservers[ server ] = _currenttime |
5663
4e51ceee469c
net.server_select: Make sure there is a server to pause when hitting maxfd (Thanks elghinn)
Kim Alvefur <zash@zash.se>
parents:
5642
diff
changeset
|
278 server.pause( ) |
4e51ceee469c
net.server_select: Make sure there is a server to pause when hitting maxfd (Thanks elghinn)
Kim Alvefur <zash@zash.se>
parents:
5642
diff
changeset
|
279 end |
5331
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
280 return nil, nil, "fd-too-large" |
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
281 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
282 socket:settimeout( 0 ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
283 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
284 --// local import of socket methods //-- |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
285 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
286 local send |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
287 local receive |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
288 local shutdown |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
289 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
290 --// private closures of the object //-- |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
291 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
292 local ssl |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
293 |
10851
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
294 local pending |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
295 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
296 local dispatch = listeners.onincoming |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
297 local status = listeners.onstatus |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
298 local disconnect = listeners.ondisconnect |
11741
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
299 local predrain = listeners.onpredrain |
3002
d1f864ed44c0
net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
Matthew Wild <mwild1@gmail.com>
parents:
3001
diff
changeset
|
300 local drain = listeners.ondrain |
5846
b8c7656481ac
server_select: fix onreadtimeout support so that listeners can override the default (disconnect) behaviour
Matthew Wild <mwild1@gmail.com>
parents:
5824
diff
changeset
|
301 local onreadtimeout = listeners.onreadtimeout; |
6381
9ffd582c65d8
net.server_select: 'listener'->'listeners' (fixes undefined global access)
Matthew Wild <mwild1@gmail.com>
parents:
6379
diff
changeset
|
302 local detach = listeners.ondetach |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
303 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
304 local bufferqueue = { } -- buffer array |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
305 local bufferqueuelen = 0 -- end of buffer array |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
306 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
307 local toclose |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
308 local needtls |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
309 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
310 local bufferlen = 0 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
311 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
312 local noread = false |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
313 local nosend = false |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
314 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
315 local sendtraffic, readtraffic = 0, 0 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
316 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
317 local maxsendlen = _maxsendlen |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
318 local maxreadlen = _maxreadlen |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
319 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
320 --// public methods of the object //-- |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
321 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
322 local handler = bufferqueue -- saves a table ^_^ |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
323 |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
324 handler.extra = extra |
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
325 if extra then |
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
326 handler.servername = extra.servername |
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
327 end |
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
328 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
329 handler.dispatch = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
330 return dispatch |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
331 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
332 handler.disconnect = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
333 return disconnect |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
334 end |
5846
b8c7656481ac
server_select: fix onreadtimeout support so that listeners can override the default (disconnect) behaviour
Matthew Wild <mwild1@gmail.com>
parents:
5824
diff
changeset
|
335 handler.onreadtimeout = onreadtimeout; |
b8c7656481ac
server_select: fix onreadtimeout support so that listeners can override the default (disconnect) behaviour
Matthew Wild <mwild1@gmail.com>
parents:
5824
diff
changeset
|
336 |
9387
33e52f727f0f
net.connect: Fix passing request table to new listener
Kim Alvefur <zash@zash.se>
parents:
9385
diff
changeset
|
337 handler.setlistener = function( self, listeners, data ) |
6379
a280bd6ccce2
net.server_{select,event}: Add 'ondetach' callback for listener objects, to notify them when another listener is being assigned to a connection
Matthew Wild <mwild1@gmail.com>
parents:
6314
diff
changeset
|
338 if detach then |
a280bd6ccce2
net.server_{select,event}: Add 'ondetach' callback for listener objects, to notify them when another listener is being assigned to a connection
Matthew Wild <mwild1@gmail.com>
parents:
6314
diff
changeset
|
339 detach(self) -- Notify listener that it is no longer responsible for this connection |
a280bd6ccce2
net.server_{select,event}: Add 'ondetach' callback for listener objects, to notify them when another listener is being assigned to a connection
Matthew Wild <mwild1@gmail.com>
parents:
6314
diff
changeset
|
340 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
341 dispatch = listeners.onincoming |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
342 disconnect = listeners.ondisconnect |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
343 status = listeners.onstatus |
11741
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
344 predrain = listeners.onpredrain |
3002
d1f864ed44c0
net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
Matthew Wild <mwild1@gmail.com>
parents:
3001
diff
changeset
|
345 drain = listeners.ondrain |
5846
b8c7656481ac
server_select: fix onreadtimeout support so that listeners can override the default (disconnect) behaviour
Matthew Wild <mwild1@gmail.com>
parents:
5824
diff
changeset
|
346 handler.onreadtimeout = listeners.onreadtimeout |
6379
a280bd6ccce2
net.server_{select,event}: Add 'ondetach' callback for listener objects, to notify them when another listener is being assigned to a connection
Matthew Wild <mwild1@gmail.com>
parents:
6314
diff
changeset
|
347 detach = listeners.ondetach |
9385
58fe099043ed
net.server: Call a 'onattach' callback when replacing listeners
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
348 if listeners.onattach then |
9387
33e52f727f0f
net.connect: Fix passing request table to new listener
Kim Alvefur <zash@zash.se>
parents:
9385
diff
changeset
|
349 listeners.onattach(self, data) |
9385
58fe099043ed
net.server: Call a 'onattach' callback when replacing listeners
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
350 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
351 end |
10851
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
352 handler._setpending = function( ) |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
353 pending = true |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
354 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
355 handler.getstats = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
356 return readtraffic, sendtraffic |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
357 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
358 handler.ssl = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
359 return ssl |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
360 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
361 handler.sslctx = function ( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
362 return sslctx |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
363 end |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
364 handler.ssl_info = function( ) |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
365 return socket.info and socket:info() |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
366 end |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
367 handler.ssl_peercertificate = function( ) |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
368 return socket.getpeercertificate and socket:getpeercertificate() |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
369 end |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
370 handler.ssl_peerverification = function( ) |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
371 return socket.getpeerverification and socket:getpeerverification() |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
372 end |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
373 handler.ssl_peerfinished = function( ) |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
374 return socket.getpeerfinished and socket:getpeerfinished() |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
375 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
376 handler.send = function( _, data, i, j ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
377 return send( socket, data, i, j ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
378 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
379 handler.receive = function( pattern, prefix ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
380 return receive( socket, pattern, prefix ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
381 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
382 handler.shutdown = function( pattern ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
383 return shutdown( socket, pattern ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
384 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
385 handler.setoption = function (self, option, value) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
386 if socket.setoption then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
387 return socket:setoption(option, value); |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
388 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
389 return false, "setoption not implemented"; |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
390 end |
4980
2e35cfe11835
net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic.
Matthew Wild <mwild1@gmail.com>
parents:
4962
diff
changeset
|
391 handler.force_close = function ( self, err ) |
4962
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
392 if bufferqueuelen ~= 0 then |
4980
2e35cfe11835
net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic.
Matthew Wild <mwild1@gmail.com>
parents:
4962
diff
changeset
|
393 out_put("server.lua: discarding unwritten data for ", tostring(ip), ":", tostring(clientport)) |
4962
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
394 bufferqueuelen = 0; |
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
395 end |
4980
2e35cfe11835
net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic.
Matthew Wild <mwild1@gmail.com>
parents:
4962
diff
changeset
|
396 return self:close(err); |
4962
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
397 end |
4980
2e35cfe11835
net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic.
Matthew Wild <mwild1@gmail.com>
parents:
4962
diff
changeset
|
398 handler.close = function( self, err ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
399 if not handler then return true; end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
400 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
401 _readtimes[ handler ] = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
402 if bufferqueuelen ~= 0 then |
10852
2e48aebdb915
net.server_select: Pass conn/handler to readbuffer/sendbuffer
Matthew Wild <mwild1@gmail.com>
parents:
10851
diff
changeset
|
403 handler:sendbuffer() -- Try now to send any outstanding data |
4962
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
404 if bufferqueuelen ~= 0 then -- Still not empty, so we'll try again later |
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
405 if handler then |
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
406 handler.write = nil -- ... but no further writing allowed |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
407 end |
4962
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
408 toclose = true |
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
409 return false |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
410 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
411 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
412 if socket then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
413 _ = shutdown and shutdown( socket ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
414 socket:close( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
415 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
416 _socketlist[ socket ] = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
417 socket = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
418 else |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
419 out_put "server.lua: socket already closed" |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
420 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
421 if handler then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
422 _writetimes[ handler ] = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
423 _closelist[ handler ] = nil |
4903
c576d2175fa4
net.server_select: Call ondisconnect handler, even if we closed the socket ourselves (fixes HTTP session hoarding, thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4888
diff
changeset
|
424 local _handler = handler; |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
425 handler = nil |
4903
c576d2175fa4
net.server_select: Call ondisconnect handler, even if we closed the socket ourselves (fixes HTTP session hoarding, thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4888
diff
changeset
|
426 if disconnect then |
4980
2e35cfe11835
net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic.
Matthew Wild <mwild1@gmail.com>
parents:
4962
diff
changeset
|
427 disconnect(_handler, err or false); |
2e35cfe11835
net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic.
Matthew Wild <mwild1@gmail.com>
parents:
4962
diff
changeset
|
428 disconnect = nil |
4903
c576d2175fa4
net.server_select: Call ondisconnect handler, even if we closed the socket ourselves (fixes HTTP session hoarding, thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4888
diff
changeset
|
429 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
430 end |
3310
ddcab5be2a5e
net.server_select: Fix a snippet of code that was living at the wrong indentation level
Matthew Wild <mwild1@gmail.com>
parents:
3008
diff
changeset
|
431 if server then |
ddcab5be2a5e
net.server_select: Fix a snippet of code that was living at the wrong indentation level
Matthew Wild <mwild1@gmail.com>
parents:
3008
diff
changeset
|
432 server.remove( ) |
ddcab5be2a5e
net.server_select: Fix a snippet of code that was living at the wrong indentation level
Matthew Wild <mwild1@gmail.com>
parents:
3008
diff
changeset
|
433 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
434 out_put "server.lua: closed client handler and removed socket from list" |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
435 return true |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
436 end |
7066
7d5a1fb9bf9e
net.server_select: Add 'server' method to client connections (present in server_event)
Kim Alvefur <zash@zash.se>
parents:
7043
diff
changeset
|
437 handler.server = function ( ) |
7d5a1fb9bf9e
net.server_select: Add 'server' method to client connections (present in server_event)
Kim Alvefur <zash@zash.se>
parents:
7043
diff
changeset
|
438 return server |
7d5a1fb9bf9e
net.server_select: Add 'server' method to client connections (present in server_event)
Kim Alvefur <zash@zash.se>
parents:
7043
diff
changeset
|
439 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
440 handler.ip = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
441 return ip |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
442 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
443 handler.serverport = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
444 return serverport |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
445 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
446 handler.clientport = function( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
447 return clientport |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
448 end |
6047
4db0403bfc0d
net.server_{select,event}: Add compat code for supporting the same client port API on connections
Kim Alvefur <zash@zash.se>
parents:
5949
diff
changeset
|
449 handler.port = handler.clientport -- COMPAT server_event |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
450 local write = function( self, data ) |
7736
5a07fe977002
net.server_select: Prevent writes after a handler is closed (fixes #783 I hope)
Kim Alvefur <zash@zash.se>
parents:
7097
diff
changeset
|
451 if not handler then return false end |
5337
c22dd451487f
net.server_select: Use # operator instead of string.len
Kim Alvefur <zash@zash.se>
parents:
5331
diff
changeset
|
452 bufferlen = bufferlen + #data |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
453 if bufferlen > maxsendlen then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
454 _closelist[ handler ] = "send buffer exceeded" -- cannot close the client at the moment, have to wait to the end of the cycle |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
455 return false |
9580
5fe91bfb734c
net.server_select: Still allow buffering outgoing data on write-locked connections
Kim Alvefur <zash@zash.se>
parents:
9579
diff
changeset
|
456 elseif not nosend and socket and not _sendlist[ socket ] then |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
457 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
458 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
459 bufferqueuelen = bufferqueuelen + 1 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
460 bufferqueue[ bufferqueuelen ] = data |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
461 if handler then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
462 _writetimes[ handler ] = _writetimes[ handler ] or _currenttime |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
463 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
464 return true |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
465 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
466 handler.write = write |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
467 handler.bufferqueue = function( self ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
468 return bufferqueue |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
469 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
470 handler.socket = function( self ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
471 return socket |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
472 end |
3001
fa00c6f563b4
net.server_select, net.server_event: Rename :pattern() method to :set_mode() for select backend, and add :set_mode() to event backend
Matthew Wild <mwild1@gmail.com>
parents:
3000
diff
changeset
|
473 handler.set_mode = function( self, new ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
474 pattern = new or pattern |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
475 return pattern |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
476 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
477 handler.set_send = function ( self, newsend ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
478 send = newsend or send |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
479 return send |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
480 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
481 handler.bufferlen = function( self, readlen, sendlen ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
482 maxsendlen = sendlen or maxsendlen |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
483 maxreadlen = readlen or maxreadlen |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
484 return bufferlen, maxreadlen, maxsendlen |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
485 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
486 handler.lock_read = function (self, switch) |
9574
03214e6e3272
net.server_select: Deprecate connection:lock_read() method
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
487 out_error( "server.lua, lock_read() is deprecated, use pause() and resume()" ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
488 if switch == true then |
9576
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
489 return self:pause() |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
490 elseif switch == false then |
9576
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
491 return self:resume() |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
492 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
493 return noread |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
494 end |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2970
diff
changeset
|
495 handler.pause = function (self) |
9576
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
496 local tmp = _readlistlen |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
497 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
498 _readtimes[ handler ] = nil |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
499 if _readlistlen ~= tmp then |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
500 noread = true |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
501 end |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
502 return noread; |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2970
diff
changeset
|
503 end |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2970
diff
changeset
|
504 handler.resume = function (self) |
9576
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
505 if noread then |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
506 noread = false |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
507 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
508 _readtimes[ handler ] = _currenttime |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
509 end |
913a17d6c449
net.server_select: Move code from :lock_read into :pause and :resume
Kim Alvefur <zash@zash.se>
parents:
9574
diff
changeset
|
510 return noread; |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2970
diff
changeset
|
511 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
512 handler.lock = function( self, switch ) |
9578
1ef33635f3f0
net.server_select: Deprecate :lock method
Kim Alvefur <zash@zash.se>
parents:
9577
diff
changeset
|
513 out_error( "server.lua, lock() is deprecated" ) |
9577
388b93b3eecf
server_select: Fix :lock method
Kim Alvefur <zash@zash.se>
parents:
9576
diff
changeset
|
514 handler.lock_read (self, switch) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
515 if switch == true then |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
516 handler.pause_writes (self) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
517 elseif switch == false then |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
518 handler.resume_writes (self) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
519 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
520 return noread, nosend |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
521 end |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
522 handler.pause_writes = function (self) |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
523 local tmp = _sendlistlen |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
524 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
525 _writetimes[ handler ] = nil |
9916
72501e3c1427
net.server_select: Fix write pause/resume functions
Kim Alvefur <zash@zash.se>
parents:
9853
diff
changeset
|
526 nosend = true |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
527 end |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
528 handler.resume_writes = function (self) |
9916
72501e3c1427
net.server_select: Fix write pause/resume functions
Kim Alvefur <zash@zash.se>
parents:
9853
diff
changeset
|
529 nosend = false |
11043
d105e787f5ed
net.server_select: Fix traceback (thanks eta)
Kim Alvefur <zash@zash.se>
parents:
10852
diff
changeset
|
530 if bufferlen > 0 and socket then |
9916
72501e3c1427
net.server_select: Fix write pause/resume functions
Kim Alvefur <zash@zash.se>
parents:
9853
diff
changeset
|
531 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
532 end |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
533 end |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9580
diff
changeset
|
534 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
535 local _readbuffer = function( ) -- this function reads data |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
536 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern" |
3002
d1f864ed44c0
net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
Matthew Wild <mwild1@gmail.com>
parents:
3001
diff
changeset
|
537 if not err or (err == "wantread" or err == "timeout") then -- received something |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
538 local buffer = buffer or part or "" |
5337
c22dd451487f
net.server_select: Use # operator instead of string.len
Kim Alvefur <zash@zash.se>
parents:
5331
diff
changeset
|
539 local len = #buffer |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
540 if len > maxreadlen then |
4980
2e35cfe11835
net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic.
Matthew Wild <mwild1@gmail.com>
parents:
4962
diff
changeset
|
541 handler:close( "receive buffer exceeded" ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
542 return false |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
543 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
544 local count = len * STAT_UNIT |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
545 readtraffic = readtraffic + count |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
546 _readtraffic = _readtraffic + count |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
547 _readtimes[ handler ] = _currenttime |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
548 --out_put( "server.lua: read data '", buffer:gsub("[^%w%p ]", "."), "', error: ", err ) |
10851
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
549 if pending then -- connection established |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
550 pending = nil |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
551 if listeners.onconnect then |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
552 listeners.onconnect(handler) |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
553 end |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
554 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
555 return dispatch( handler, buffer, err ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
556 else -- connections was closed or fatal error |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
557 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " read error: ", tostring(err) ) |
4980
2e35cfe11835
net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic.
Matthew Wild <mwild1@gmail.com>
parents:
4962
diff
changeset
|
558 _ = handler and handler:force_close( err ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
559 return false |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
560 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
561 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
562 local _sendbuffer = function( ) -- this function sends data |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
563 local succ, err, byte, buffer, count; |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
564 if socket then |
10851
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
565 if pending then |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
566 pending = nil |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
567 if listeners.onconnect then |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
568 listeners.onconnect(handler); |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
569 end |
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
570 end |
11741
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
571 if predrain then |
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
572 predrain(handler); |
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
573 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
574 buffer = table_concat( bufferqueue, "", 1, bufferqueuelen ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
575 succ, err, byte = send( socket, buffer, 1, bufferlen ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
576 count = ( succ or byte or 0 ) * STAT_UNIT |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
577 sendtraffic = sendtraffic + count |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
578 _sendtraffic = _sendtraffic + count |
5338
3df649ec43ea
net.server_select: Optimization, clean bufferqueue when it makes sense.
Kim Alvefur <zash@zash.se>
parents:
5337
diff
changeset
|
579 for i = bufferqueuelen,1,-1 do |
3df649ec43ea
net.server_select: Optimization, clean bufferqueue when it makes sense.
Kim Alvefur <zash@zash.se>
parents:
5337
diff
changeset
|
580 bufferqueue[ i ] = nil |
3df649ec43ea
net.server_select: Optimization, clean bufferqueue when it makes sense.
Kim Alvefur <zash@zash.se>
parents:
5337
diff
changeset
|
581 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
582 --out_put( "server.lua: sended '", buffer, "', bytes: ", tostring(succ), ", error: ", tostring(err), ", part: ", tostring(byte), ", to: ", tostring(ip), ":", tostring(clientport) ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
583 else |
4962
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
584 succ, err, count = false, "unexpected close", 0; |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
585 end |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8528
diff
changeset
|
586 if succ then -- sending successful |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
587 bufferqueuelen = 0 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
588 bufferlen = 0 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
589 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
590 _writetimes[ handler ] = nil |
3002
d1f864ed44c0
net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
Matthew Wild <mwild1@gmail.com>
parents:
3001
diff
changeset
|
591 if drain then |
d1f864ed44c0
net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
Matthew Wild <mwild1@gmail.com>
parents:
3001
diff
changeset
|
592 drain(handler) |
d1f864ed44c0
net.server_select, net.server_event: Support for ondrain listener callback for when send buffer is empty
Matthew Wild <mwild1@gmail.com>
parents:
3001
diff
changeset
|
593 end |
3398
abc4a52aef02
net.server_select: Remove 'now' parameter from starttls(), and base it on whether the sendbuffer is empty instead
Matthew Wild <mwild1@gmail.com>
parents:
3382
diff
changeset
|
594 _ = needtls and handler:starttls(nil) |
4962
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
595 _ = toclose and handler:force_close( ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
596 return true |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
597 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
598 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
599 bufferqueue[ 1 ] = buffer -- insert new buffer in queue |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
600 bufferqueuelen = 1 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
601 bufferlen = bufferlen - byte |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
602 _writetimes[ handler ] = _currenttime |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
603 return true |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
604 else -- connection was closed during sending or fatal error |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
605 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) ) |
4980
2e35cfe11835
net.server_select: Never call ondisconnect() directly, go via handler:close() or handler:force_close() - fixes cases where ondisconnect() could be called multiple times for the same connection, leading to issues with s2sout retry logic.
Matthew Wild <mwild1@gmail.com>
parents:
4962
diff
changeset
|
606 _ = handler and handler:force_close( err ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
607 return false |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
608 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
609 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
610 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
611 -- Set the sslctx |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
612 local handshake; |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
613 function handler.set_sslctx(self, new_sslctx) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
614 sslctx = new_sslctx; |
4347
4b5290dd4e58
net.server_select: Small fix to SSL handshake logic, read/wrote were being checked despite it not being possible for them to be other than nil
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
615 local read, wrote |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
616 handshake = coroutine_wrap( function( client ) -- create handshake coroutine |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
617 local err |
7501
8c3d93b67560
net.server_select: remove unused one-letter loop variable [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7468
diff
changeset
|
618 for _ = 1, _maxsslhandshake do |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
619 _sendlistlen = ( wrote and removesocket( _sendlist, client, _sendlistlen ) ) or _sendlistlen |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
620 _readlistlen = ( read and removesocket( _readlist, client, _readlistlen ) ) or _readlistlen |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
621 read, wrote = nil, nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
622 _, err = client:dohandshake( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
623 if not err then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
624 out_put( "server.lua: ssl handshake done" ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
625 handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
626 handler.sendbuffer = _sendbuffer |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
627 _ = status and status( handler, "ssl-handshake-complete" ) |
5490
ab821566ede8
Backed out changeset f2631a14b953
Matthew Wild <mwild1@gmail.com>
parents:
5482
diff
changeset
|
628 if self.autostart_ssl and listeners.onconnect then |
ab821566ede8
Backed out changeset f2631a14b953
Matthew Wild <mwild1@gmail.com>
parents:
5482
diff
changeset
|
629 listeners.onconnect(self); |
6433
b93402cec1c4
net.server_select: When an SSL handshake is connected, if there is pending data to be written to the socket, mark the socket as waiting to send (thanks daurnimator)
Matthew Wild <mwild1@gmail.com>
parents:
6382
diff
changeset
|
630 if bufferqueuelen ~= 0 then |
b93402cec1c4
net.server_select: When an SSL handshake is connected, if there is pending data to be written to the socket, mark the socket as waiting to send (thanks daurnimator)
Matthew Wild <mwild1@gmail.com>
parents:
6382
diff
changeset
|
631 _sendlistlen = addsocket(_sendlist, client, _sendlistlen) |
b93402cec1c4
net.server_select: When an SSL handshake is connected, if there is pending data to be written to the socket, mark the socket as waiting to send (thanks daurnimator)
Matthew Wild <mwild1@gmail.com>
parents:
6382
diff
changeset
|
632 end |
5490
ab821566ede8
Backed out changeset f2631a14b953
Matthew Wild <mwild1@gmail.com>
parents:
5482
diff
changeset
|
633 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
634 _readlistlen = addsocket(_readlist, client, _readlistlen) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
635 return true |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
636 else |
4347
4b5290dd4e58
net.server_select: Small fix to SSL handshake logic, read/wrote were being checked despite it not being possible for them to be other than nil
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
637 if err == "wantwrite" then |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
638 _sendlistlen = addsocket(_sendlist, client, _sendlistlen) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
639 wrote = true |
4347
4b5290dd4e58
net.server_select: Small fix to SSL handshake logic, read/wrote were being checked despite it not being possible for them to be other than nil
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
640 elseif err == "wantread" then |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
641 _readlistlen = addsocket(_readlist, client, _readlistlen) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
642 read = true |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
643 else |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
644 break; |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
645 end |
4348
5b240c6b5334
net.server_select: Update error logging logic during SSL handshake (now shows correctly when a handshake exceeded max. allowed iterations)
Matthew Wild <mwild1@gmail.com>
parents:
4347
diff
changeset
|
646 err = nil; |
5b240c6b5334
net.server_select: Update error logging logic during SSL handshake (now shows correctly when a handshake exceeded max. allowed iterations)
Matthew Wild <mwild1@gmail.com>
parents:
4347
diff
changeset
|
647 coroutine_yield( ) -- handshake not finished |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
648 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
649 end |
10474
175b72700d79
net.server_select: Remove prefix added to TLS handshaker errors
Kim Alvefur <zash@zash.se>
parents:
10234
diff
changeset
|
650 err = ( err or "handshake too long" ); |
7043
14bc5593b97e
server_select: Forward the actual TLS handshake error message
Kim Alvefur <zash@zash.se>
parents:
7041
diff
changeset
|
651 out_put( "server.lua: ", err ); |
14bc5593b97e
server_select: Forward the actual TLS handshake error message
Kim Alvefur <zash@zash.se>
parents:
7041
diff
changeset
|
652 _ = handler and handler:force_close(err) |
5484
17ce73907c48
net.server_select: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
5482
diff
changeset
|
653 return false, err -- handshake failed |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
654 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
655 ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
656 end |
6782
ec172dbe9d14
net.server_{select,event}: Don't rely on LuaSocket and LuaSec being present in the globals table
Kim Alvefur <zash@zash.se>
parents:
6459
diff
changeset
|
657 if has_luasec then |
4349
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
658 handler.starttls = function( self, _sslctx) |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
659 if _sslctx then |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
660 handler:set_sslctx(_sslctx); |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
661 end |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
662 if bufferqueuelen > 0 then |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
663 out_put "server.lua: we need to do tls, but delaying until send buffer empty" |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
664 needtls = true |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
665 return |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
666 end |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
667 out_put( "server.lua: attempting to start tls on " .. tostring( socket ) ) |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
668 local oldsocket, err = socket |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
669 socket, err = sslctx:wrap(socket) -- wrap socket |
9851
75d2874502c3
net.server_select: SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
670 |
4349
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
671 if not socket then |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
672 out_put( "server.lua: error while starting tls on client: ", tostring(err or "unknown error") ) |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
673 return nil, err -- fatal error |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
674 end |
4349
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
675 |
9851
75d2874502c3
net.server_select: SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
676 if socket.sni then |
75d2874502c3
net.server_select: SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
677 if self.servername then |
75d2874502c3
net.server_select: SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
678 socket:sni(self.servername); |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
679 elseif next(sslctx._sni_contexts) ~= nil then |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
11741
diff
changeset
|
680 socket:sni(sslctx._sni_contexts, true); |
9851
75d2874502c3
net.server_select: SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
681 end |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
682 end |
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
683 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
684 socket:settimeout( 0 ) |
4349
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
685 |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
686 -- add the new socket to our system |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
687 send = socket.send |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
688 receive = socket.receive |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
689 shutdown = id |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
690 _socketlist[ socket ] = handler |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
691 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5664
diff
changeset
|
692 |
4349
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
693 -- remove traces of the old socket |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
694 _readlistlen = removesocket( _readlist, oldsocket, _readlistlen ) |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
695 _sendlistlen = removesocket( _sendlist, oldsocket, _sendlistlen ) |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
696 _socketlist[ oldsocket ] = nil |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
697 |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
698 handler.starttls = nil |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
699 needtls = nil |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
700 |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
701 -- Secure now (if handshake fails connection will close) |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
702 ssl = true |
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
703 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
704 handler.readbuffer = handshake |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
705 handler.sendbuffer = handshake |
5484
17ce73907c48
net.server_select: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
5482
diff
changeset
|
706 return handshake( socket ) -- do handshake |
4349
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
707 end |
4429
8e02ba75ef06
net.server_select: Autostart SSL negotiation as the last part of socket setup to avoid a traceback (#262) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4399
diff
changeset
|
708 end |
2582
d6afb6d919df
net.server_select: Only allow starttls if luasec is available (thanks Nolan)
Matthew Wild <mwild1@gmail.com>
parents:
2581
diff
changeset
|
709 |
4429
8e02ba75ef06
net.server_select: Autostart SSL negotiation as the last part of socket setup to avoid a traceback (#262) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4399
diff
changeset
|
710 handler.readbuffer = _readbuffer |
8e02ba75ef06
net.server_select: Autostart SSL negotiation as the last part of socket setup to avoid a traceback (#262) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4399
diff
changeset
|
711 handler.sendbuffer = _sendbuffer |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
712 send = socket.send |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
713 receive = socket.receive |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
714 shutdown = ( ssl and id ) or socket.shutdown |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
715 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
716 _socketlist[ socket ] = handler |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
717 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
4429
8e02ba75ef06
net.server_select: Autostart SSL negotiation as the last part of socket setup to avoid a traceback (#262) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4399
diff
changeset
|
718 |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
719 if sslctx and ssldirect and has_luasec then |
4429
8e02ba75ef06
net.server_select: Autostart SSL negotiation as the last part of socket setup to avoid a traceback (#262) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4399
diff
changeset
|
720 out_put "server.lua: auto-starting ssl negotiation..." |
8e02ba75ef06
net.server_select: Autostart SSL negotiation as the last part of socket setup to avoid a traceback (#262) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4399
diff
changeset
|
721 handler.autostart_ssl = true; |
5484
17ce73907c48
net.server_select: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
5482
diff
changeset
|
722 local ok, err = handler:starttls(sslctx); |
17ce73907c48
net.server_select: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
5482
diff
changeset
|
723 if ok == false then |
17ce73907c48
net.server_select: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
5482
diff
changeset
|
724 return nil, nil, err |
17ce73907c48
net.server_select: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
5482
diff
changeset
|
725 end |
4429
8e02ba75ef06
net.server_select: Autostart SSL negotiation as the last part of socket setup to avoid a traceback (#262) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4399
diff
changeset
|
726 end |
8e02ba75ef06
net.server_select: Autostart SSL negotiation as the last part of socket setup to avoid a traceback (#262) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
4399
diff
changeset
|
727 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
728 return handler, socket |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
729 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
730 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
731 id = function( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
732 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
733 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
734 idfalse = function( ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
735 return false |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
736 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
737 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
738 addsocket = function( list, socket, len ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
739 if not list[ socket ] then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
740 len = len + 1 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
741 list[ len ] = socket |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
742 list[ socket ] = len |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
743 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
744 return len; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
745 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
746 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
747 removesocket = function( list, socket, len ) -- this function removes sockets from a list ( copied from copas ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
748 local pos = list[ socket ] |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
749 if pos then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
750 list[ socket ] = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
751 local last = list[ len ] |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
752 list[ len ] = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
753 if last ~= socket then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
754 list[ last ] = pos |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
755 list[ pos ] = last |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
756 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
757 return len - 1 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
758 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
759 return len |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
760 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
761 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
762 closesocket = function( socket ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
763 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
764 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
765 _socketlist[ socket ] = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
766 socket:close( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
767 --mem_free( ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
768 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
769 |
3003
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
770 local function link(sender, receiver, buffersize) |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
771 local sender_locked; |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
772 local _sendbuffer = receiver.sendbuffer; |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
773 function receiver.sendbuffer() |
10852
2e48aebdb915
net.server_select: Pass conn/handler to readbuffer/sendbuffer
Matthew Wild <mwild1@gmail.com>
parents:
10851
diff
changeset
|
774 _sendbuffer(receiver); |
3003
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
775 if sender_locked and receiver.bufferlen() < buffersize then |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
776 sender:lock_read(false); -- Unlock now |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
777 sender_locked = nil; |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
778 end |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
779 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5664
diff
changeset
|
780 |
3003
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
781 local _readbuffer = sender.readbuffer; |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
782 function sender.readbuffer() |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
783 _readbuffer(); |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
784 if not sender_locked and receiver.bufferlen() >= buffersize then |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
785 sender_locked = true; |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
786 sender:lock_read(true); |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
787 end |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
788 end |
6314
8029b8a5f37a
net.server_select/event: Switch sender mode to *a when reading, to make sure we get all available data
Matthew Wild <mwild1@gmail.com>
parents:
6047
diff
changeset
|
789 sender:set_mode("*a"); |
3003
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
790 end |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
791 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
792 ----------------------------------// PUBLIC //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
793 |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
794 listen = function ( addr, port, listeners, config ) |
6812
7af63377a1cf
net/server_select: Check arguments to add_server correctly
daurnimator <quae@daurnimator.com>
parents:
6782
diff
changeset
|
795 addr = addr or "*" |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
796 config = config or {} |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
797 local err |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
798 local sslctx = config.tls_ctx; |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
799 local ssldirect = config.tls_direct; |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
800 local pattern = config.read_size; |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
801 if type( listeners ) ~= "table" then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
802 err = "invalid listener table" |
6812
7af63377a1cf
net/server_select: Check arguments to add_server correctly
daurnimator <quae@daurnimator.com>
parents:
6782
diff
changeset
|
803 elseif type ( addr ) ~= "string" then |
7af63377a1cf
net/server_select: Check arguments to add_server correctly
daurnimator <quae@daurnimator.com>
parents:
6782
diff
changeset
|
804 err = "invalid address" |
7af63377a1cf
net/server_select: Check arguments to add_server correctly
daurnimator <quae@daurnimator.com>
parents:
6782
diff
changeset
|
805 elseif type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
806 err = "invalid port" |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
807 elseif _server[ addr..":"..port ] then |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
808 err = "listeners on '[" .. addr .. "]:" .. port .. "' already exist" |
6782
ec172dbe9d14
net.server_{select,event}: Don't rely on LuaSocket and LuaSec being present in the globals table
Kim Alvefur <zash@zash.se>
parents:
6459
diff
changeset
|
809 elseif sslctx and not has_luasec then |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
810 err = "luasec not found" |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
811 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
812 if err then |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
813 out_error( "server.lua, [", addr, "]:", port, ": ", err ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
814 return nil, err |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
815 end |
5531
483f795f6f99
net.server*: Allow the TCP backlog parameter to be set in the config
Kim Alvefur <zash@zash.se>
parents:
5492
diff
changeset
|
816 local server, err = socket_bind( addr, port, _tcpbacklog ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
817 if err then |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
818 out_error( "server.lua, [", addr, "]:", port, ": ", err ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
819 return nil, err |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
820 end |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
821 local handler, err = wrapserver( listeners, server, addr, port, pattern, sslctx, ssldirect ) -- wrap new server socket |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
822 if not handler then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
823 server:close( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
824 return nil, err |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
825 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
826 server:settimeout( 0 ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
827 _readlistlen = addsocket(_readlist, server, _readlistlen) |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
828 _server[ addr..":"..port ] = handler |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
829 _socketlist[ server ] = handler |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
830 out_put( "server.lua: new "..(sslctx and "ssl " or "").."server listener on '[", addr, "]:", port, "'" ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
831 return handler |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
832 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
833 |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
834 addserver = function( addr, port, listeners, pattern, sslctx ) -- this function provides a way for other scripts to reg a server |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
835 return listen(addr, port, listeners, { |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
836 read_size = pattern; |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
837 tls_ctx = sslctx; |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
838 tls_direct = sslctx and true or false; |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
839 }); |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
840 end |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
841 |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
842 getserver = function ( addr, port ) |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
843 return _server[ addr..":"..port ]; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
844 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
845 |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
846 removeserver = function( addr, port ) |
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
847 local handler = _server[ addr..":"..port ] |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
848 if not handler then |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
849 return nil, "no server found on '[" .. addr .. "]:" .. tostring( port ) .. "'" |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
850 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
851 handler:close( ) |
4322
aff627b1ce95
connlistener, server_select, prosody: Add support for binding to multiple addresses
Florian Zeitz <florob@babelmonkeys.de>
parents:
4145
diff
changeset
|
852 _server[ addr..":"..port ] = nil |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
853 return true |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
854 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
855 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
856 closeall = function( ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
857 for _, handler in pairs( _socketlist ) do |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
858 handler:close( ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
859 _socketlist[ _ ] = nil |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
860 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
861 _readlistlen = 0 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
862 _sendlistlen = 0 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
863 _timerlistlen = 0 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
864 _server = { } |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
865 _readlist = { } |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
866 _sendlist = { } |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
867 _timerlist = { } |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
868 _socketlist = { } |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
869 --mem_free( ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
870 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
871 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
872 getsettings = function( ) |
5385
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
873 return { |
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
874 select_timeout = _selecttimeout; |
5531
483f795f6f99
net.server*: Allow the TCP backlog parameter to be set in the config
Kim Alvefur <zash@zash.se>
parents:
5492
diff
changeset
|
875 tcp_backlog = _tcpbacklog; |
5385
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
876 max_send_buffer_size = _maxsendlen; |
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
877 max_receive_buffer_size = _maxreadlen; |
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
878 select_idle_check_interval = _checkinterval; |
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
879 send_timeout = _sendtimeout; |
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
880 read_timeout = _readtimeout; |
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
881 max_connections = _maxselectlen; |
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
882 max_ssl_handshake_roundtrips = _maxsslhandshake; |
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
883 highest_allowed_fd = _maxfd; |
7095
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
884 accept_retry_interval = _accepretry; |
5385
d663483036db
net.server, _select: Reorganise configuration of server_select to be more like server_event
Kim Alvefur <zash@zash.se>
parents:
5338
diff
changeset
|
885 } |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
886 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
887 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
888 changesettings = function( new ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
889 if type( new ) ~= "table" then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
890 return nil, "invalid settings table" |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
891 end |
5197
beffe931b3b6
net.server_select: Better names for config options (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents:
4983
diff
changeset
|
892 _selecttimeout = tonumber( new.select_timeout ) or _selecttimeout |
beffe931b3b6
net.server_select: Better names for config options (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents:
4983
diff
changeset
|
893 _maxsendlen = tonumber( new.max_send_buffer_size ) or _maxsendlen |
beffe931b3b6
net.server_select: Better names for config options (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents:
4983
diff
changeset
|
894 _maxreadlen = tonumber( new.max_receive_buffer_size ) or _maxreadlen |
beffe931b3b6
net.server_select: Better names for config options (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents:
4983
diff
changeset
|
895 _checkinterval = tonumber( new.select_idle_check_interval ) or _checkinterval |
5531
483f795f6f99
net.server*: Allow the TCP backlog parameter to be set in the config
Kim Alvefur <zash@zash.se>
parents:
5492
diff
changeset
|
896 _tcpbacklog = tonumber( new.tcp_backlog ) or _tcpbacklog |
5197
beffe931b3b6
net.server_select: Better names for config options (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents:
4983
diff
changeset
|
897 _sendtimeout = tonumber( new.send_timeout ) or _sendtimeout |
beffe931b3b6
net.server_select: Better names for config options (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents:
4983
diff
changeset
|
898 _readtimeout = tonumber( new.read_timeout ) or _readtimeout |
7095
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
899 _accepretry = tonumber( new.accept_retry_interval ) or _accepretry |
5331
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
900 _maxselectlen = new.max_connections or _maxselectlen |
5197
beffe931b3b6
net.server_select: Better names for config options (thanks MattJ)
Kim Alvefur <zash@zash.se>
parents:
4983
diff
changeset
|
901 _maxsslhandshake = new.max_ssl_handshake_roundtrips or _maxsslhandshake |
5331
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
902 _maxfd = new.highest_allowed_fd or _maxfd |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
903 return true |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
904 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
905 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
906 addtimer = function( listener ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
907 if type( listener ) ~= "function" then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
908 return nil, "invalid listener function" |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
909 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
910 _timerlistlen = _timerlistlen + 1 |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
911 _timerlist[ _timerlistlen ] = listener |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
912 return true |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
913 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
914 |
6481
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
915 local add_task do |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
916 local data = {}; |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
917 local new_data = {}; |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
918 |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
919 function add_task(delay, callback) |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
920 local current_time = luasocket_gettime(); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
921 delay = delay + current_time; |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
922 if delay >= current_time then |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
923 table_insert(new_data, {delay, callback}); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
924 else |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
925 local r = callback(current_time); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
926 if r and type(r) == "number" then |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
927 return add_task(r, callback); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
928 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
929 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
930 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
931 |
6540
31a8b3bfb31d
net.server_select: In add_task timer callback, use passed in time rather than re-fetching
daurnimator <quae@daurnimator.com>
parents:
6539
diff
changeset
|
932 addtimer(function(current_time) |
6481
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
933 if #new_data > 0 then |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
934 for _, d in pairs(new_data) do |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
935 table_insert(data, d); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
936 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
937 new_data = {}; |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
938 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
939 |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
940 local next_time = math_huge; |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
941 for i, d in pairs(data) do |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
942 local t, callback = d[1], d[2]; |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
943 if t <= current_time then |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
944 data[i] = nil; |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
945 local r = callback(current_time); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
946 if type(r) == "number" then |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
947 add_task(r, callback); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
948 next_time = math_min(next_time, r); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
949 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
950 else |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
951 next_time = math_min(next_time, t - current_time); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
952 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
953 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
954 return next_time; |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
955 end); |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
956 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
957 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
958 stats = function( ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
959 return _readtraffic, _sendtraffic, _readlistlen, _sendlistlen, _timerlistlen |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
960 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
961 |
3491
e8c06d20a18b
net.server_select: Add server.step() to run through a single iteration of the event loop
Matthew Wild <mwild1@gmail.com>
parents:
3458
diff
changeset
|
962 local quitting; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
963 |
4399
1b4161970842
net.server_event, net.server_select: Fixed some global pollution.
Waqas Hussain <waqas20@gmail.com>
parents:
4393
diff
changeset
|
964 local function setquitting(quit) |
8749
fb3cd6b2263b
net.server_select: Allow setquitting("once") to allow resuming the loop later
Matthew Wild <mwild1@gmail.com>
parents:
8728
diff
changeset
|
965 quitting = quit; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
966 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
967 |
3491
e8c06d20a18b
net.server_select: Add server.step() to run through a single iteration of the event loop
Matthew Wild <mwild1@gmail.com>
parents:
3458
diff
changeset
|
968 loop = function(once) -- this is the main loop of the program |
e8c06d20a18b
net.server_select: Add server.step() to run through a single iteration of the event loop
Matthew Wild <mwild1@gmail.com>
parents:
3458
diff
changeset
|
969 if quitting then return "quitting"; end |
e8c06d20a18b
net.server_select: Add server.step() to run through a single iteration of the event loop
Matthew Wild <mwild1@gmail.com>
parents:
3458
diff
changeset
|
970 if once then quitting = "once"; end |
6539
f923140ee7c5
net.server_select: Fix timers not being fired until another timer fixes (or 1 second passes)
daurnimator <quae@daurnimator.com>
parents:
6481
diff
changeset
|
971 _currenttime = luasocket_gettime( ) |
3491
e8c06d20a18b
net.server_select: Add server.step() to run through a single iteration of the event loop
Matthew Wild <mwild1@gmail.com>
parents:
3458
diff
changeset
|
972 repeat |
6539
f923140ee7c5
net.server_select: Fix timers not being fired until another timer fixes (or 1 second passes)
daurnimator <quae@daurnimator.com>
parents:
6481
diff
changeset
|
973 -- Fire timers |
3682
d12141cbbaa0
net.server_select: Make changes required for sub-second timer precision.
Waqas Hussain <waqas20@gmail.com>
parents:
3543
diff
changeset
|
974 local next_timer_time = math_huge; |
6539
f923140ee7c5
net.server_select: Fix timers not being fired until another timer fixes (or 1 second passes)
daurnimator <quae@daurnimator.com>
parents:
6481
diff
changeset
|
975 for i = 1, _timerlistlen do |
f923140ee7c5
net.server_select: Fix timers not being fired until another timer fixes (or 1 second passes)
daurnimator <quae@daurnimator.com>
parents:
6481
diff
changeset
|
976 local t = _timerlist[ i ]( _currenttime ) -- fire timers |
f923140ee7c5
net.server_select: Fix timers not being fired until another timer fixes (or 1 second passes)
daurnimator <quae@daurnimator.com>
parents:
6481
diff
changeset
|
977 if t then next_timer_time = math_min(next_timer_time, t); end |
f923140ee7c5
net.server_select: Fix timers not being fired until another timer fixes (or 1 second passes)
daurnimator <quae@daurnimator.com>
parents:
6481
diff
changeset
|
978 end |
f923140ee7c5
net.server_select: Fix timers not being fired until another timer fixes (or 1 second passes)
daurnimator <quae@daurnimator.com>
parents:
6481
diff
changeset
|
979 |
3682
d12141cbbaa0
net.server_select: Make changes required for sub-second timer precision.
Waqas Hussain <waqas20@gmail.com>
parents:
3543
diff
changeset
|
980 local read, write, err = socket_select( _readlist, _sendlist, math_min(_selecttimeout, next_timer_time) ) |
8528
67311cda0625
net.server_select: Better detection of errors for outgoing connections
Matthew Wild <mwild1@gmail.com>
parents:
7745
diff
changeset
|
981 for _, socket in ipairs( read ) do -- receive data |
67311cda0625
net.server_select: Better detection of errors for outgoing connections
Matthew Wild <mwild1@gmail.com>
parents:
7745
diff
changeset
|
982 local handler = _socketlist[ socket ] |
67311cda0625
net.server_select: Better detection of errors for outgoing connections
Matthew Wild <mwild1@gmail.com>
parents:
7745
diff
changeset
|
983 if handler then |
10852
2e48aebdb915
net.server_select: Pass conn/handler to readbuffer/sendbuffer
Matthew Wild <mwild1@gmail.com>
parents:
10851
diff
changeset
|
984 handler:readbuffer( ) |
8528
67311cda0625
net.server_select: Better detection of errors for outgoing connections
Matthew Wild <mwild1@gmail.com>
parents:
7745
diff
changeset
|
985 else |
67311cda0625
net.server_select: Better detection of errors for outgoing connections
Matthew Wild <mwild1@gmail.com>
parents:
7745
diff
changeset
|
986 closesocket( socket ) |
67311cda0625
net.server_select: Better detection of errors for outgoing connections
Matthew Wild <mwild1@gmail.com>
parents:
7745
diff
changeset
|
987 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen |
67311cda0625
net.server_select: Better detection of errors for outgoing connections
Matthew Wild <mwild1@gmail.com>
parents:
7745
diff
changeset
|
988 end |
67311cda0625
net.server_select: Better detection of errors for outgoing connections
Matthew Wild <mwild1@gmail.com>
parents:
7745
diff
changeset
|
989 end |
7468
2d38a2519d09
net.server_select: remove unused one-letter loop variables [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7323
diff
changeset
|
990 for _, socket in ipairs( write ) do -- send data waiting in writequeues |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
991 local handler = _socketlist[ socket ] |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
992 if handler then |
10852
2e48aebdb915
net.server_select: Pass conn/handler to readbuffer/sendbuffer
Matthew Wild <mwild1@gmail.com>
parents:
10851
diff
changeset
|
993 handler:sendbuffer( ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
994 else |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
995 closesocket( socket ) |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
996 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
997 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
998 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
999 for handler, err in pairs( _closelist ) do |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1000 handler.disconnect( )( handler, err ) |
4962
1d8fb12a9b2d
net.server_select, net.server_event: Change semantics of conn:close() - always call ondisconnect (with err == nil), and never close with data in send buffer (i.e. 'now' parameter removed)
Matthew Wild <mwild1@gmail.com>
parents:
4904
diff
changeset
|
1001 handler:force_close() -- forced disconnect |
5338
3df649ec43ea
net.server_select: Optimization, clean bufferqueue when it makes sense.
Kim Alvefur <zash@zash.se>
parents:
5337
diff
changeset
|
1002 _closelist[ handler ] = nil; |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1003 end |
3682
d12141cbbaa0
net.server_select: Make changes required for sub-second timer precision.
Waqas Hussain <waqas20@gmail.com>
parents:
3543
diff
changeset
|
1004 _currenttime = luasocket_gettime( ) |
5485
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1005 |
5486
bcf27dbef6c6
net.server_select: Add and improve some comments.
Waqas Hussain <waqas20@gmail.com>
parents:
5485
diff
changeset
|
1006 -- Check for socket timeouts |
7323
a1570219b865
net.server_select: Remove do-nothing os_difftime calls [backported from trunk]
daurnimator <quae@daurnimator.com>
parents:
7098
diff
changeset
|
1007 if _currenttime - _starttime > _checkinterval then |
5485
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1008 _starttime = _currenttime |
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1009 for handler, timestamp in pairs( _writetimes ) do |
7323
a1570219b865
net.server_select: Remove do-nothing os_difftime calls [backported from trunk]
daurnimator <quae@daurnimator.com>
parents:
7098
diff
changeset
|
1010 if _currenttime - timestamp > _sendtimeout then |
5485
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1011 handler.disconnect( )( handler, "send timeout" ) |
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1012 handler:force_close() -- forced disconnect |
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1013 end |
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1014 end |
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1015 for handler, timestamp in pairs( _readtimes ) do |
7323
a1570219b865
net.server_select: Remove do-nothing os_difftime calls [backported from trunk]
daurnimator <quae@daurnimator.com>
parents:
7098
diff
changeset
|
1016 if _currenttime - timestamp > _readtimeout then |
5632
8a2456f1f117
net.server_select: Support for listener.onreadtimeout() [see also e67891ad18d6]
Matthew Wild <mwild1@gmail.com>
parents:
5548
diff
changeset
|
1017 if not(handler.onreadtimeout) or handler:onreadtimeout() ~= true then |
8a2456f1f117
net.server_select: Support for listener.onreadtimeout() [see also e67891ad18d6]
Matthew Wild <mwild1@gmail.com>
parents:
5548
diff
changeset
|
1018 handler.disconnect( )( handler, "read timeout" ) |
8a2456f1f117
net.server_select: Support for listener.onreadtimeout() [see also e67891ad18d6]
Matthew Wild <mwild1@gmail.com>
parents:
5548
diff
changeset
|
1019 handler:close( ) -- forced disconnect? |
6459
ba421af3dfd3
net.server_select: Reset read timeout timer if the event is handled
Kim Alvefur <zash@zash.se>
parents:
6433
diff
changeset
|
1020 else |
ba421af3dfd3
net.server_select: Reset read timeout timer if the event is handled
Kim Alvefur <zash@zash.se>
parents:
6433
diff
changeset
|
1021 _readtimes[ handler ] = _currenttime -- reset timer |
5632
8a2456f1f117
net.server_select: Support for listener.onreadtimeout() [see also e67891ad18d6]
Matthew Wild <mwild1@gmail.com>
parents:
5548
diff
changeset
|
1022 end |
5485
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1023 end |
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1024 end |
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1025 end |
5147eed0a42e
net.server_select: Move socket timeout cleanup code out of a timer, into the select loop (which makes util.timer the only timer using server_select._addtimer).
Waqas Hussain <waqas20@gmail.com>
parents:
5484
diff
changeset
|
1026 |
7095
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
1027 for server, paused_time in pairs( _fullservers ) do |
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
1028 if _currenttime - paused_time > _accepretry then |
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
1029 _fullservers[ server ] = nil; |
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
1030 server.resume(); |
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
1031 end |
3e1dac35af87
server_select: Retry accepting clients from full servers after 10s (matches libevent behaviour)
Kim Alvefur <zash@zash.se>
parents:
7094
diff
changeset
|
1032 end |
3491
e8c06d20a18b
net.server_select: Add server.step() to run through a single iteration of the event loop
Matthew Wild <mwild1@gmail.com>
parents:
3458
diff
changeset
|
1033 until quitting; |
8749
fb3cd6b2263b
net.server_select: Allow setquitting("once") to allow resuming the loop later
Matthew Wild <mwild1@gmail.com>
parents:
8728
diff
changeset
|
1034 if quitting == "once" then quitting = nil; return; end |
7041
aff786e7b4ce
net.server_select: Close all connections when quitting (and not just stepping), matches server_event
Matthew Wild <mwild1@gmail.com>
parents:
6819
diff
changeset
|
1035 closeall(); |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1036 return "quitting" |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1037 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1038 |
4399
1b4161970842
net.server_event, net.server_select: Fixed some global pollution.
Waqas Hussain <waqas20@gmail.com>
parents:
4393
diff
changeset
|
1039 local function step() |
3491
e8c06d20a18b
net.server_select: Add server.step() to run through a single iteration of the event loop
Matthew Wild <mwild1@gmail.com>
parents:
3458
diff
changeset
|
1040 return loop(true); |
e8c06d20a18b
net.server_select: Add server.step() to run through a single iteration of the event loop
Matthew Wild <mwild1@gmail.com>
parents:
3458
diff
changeset
|
1041 end |
e8c06d20a18b
net.server_select: Add server.step() to run through a single iteration of the event loop
Matthew Wild <mwild1@gmail.com>
parents:
3458
diff
changeset
|
1042 |
2339
b04014f42fce
net.server_select: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2318
diff
changeset
|
1043 local function get_backend() |
b04014f42fce
net.server_select: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2318
diff
changeset
|
1044 return "select"; |
b04014f42fce
net.server_select: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2318
diff
changeset
|
1045 end |
b04014f42fce
net.server_select: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2318
diff
changeset
|
1046 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1047 --// EXPERIMENTAL //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1048 |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
1049 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx, extra ) |
10233
600eee3c4752
net.server: Accept and save an 'extra' field for client connections
Kim Alvefur <zash@zash.se>
parents:
9993
diff
changeset
|
1050 local handler, socket, err = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx, sslctx, extra) |
5331
ffa740b4f08d
net.server_select: Limit global number of sockets passed to select.
Kim Alvefur <zash@zash.se>
parents:
5330
diff
changeset
|
1051 if not handler then return nil, err end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1052 _socketlist[ socket ] = handler |
4349
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
1053 if not sslctx then |
10851
6cf16abd0976
net.server_select: Ensure onconnect is always called before onincoming
Matthew Wild <mwild1@gmail.com>
parents:
10474
diff
changeset
|
1054 handler._setpending() |
8528
67311cda0625
net.server_select: Better detection of errors for outgoing connections
Matthew Wild <mwild1@gmail.com>
parents:
7745
diff
changeset
|
1055 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
4349
16fd8061964e
net.server_select: Merge straight-SSL and starttls code paths, also fixes onconnect being called before handshake completion for straight-SSL
Matthew Wild <mwild1@gmail.com>
parents:
4348
diff
changeset
|
1056 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) |
3382
fea0f8e19e4c
net.server_select: Ensure that onconnect is called for server sockets as well as clients, rather than onincoming. Fixes mod_console traceback, issue #197
Matthew Wild <mwild1@gmail.com>
parents:
3312
diff
changeset
|
1057 end |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1058 return handler, socket |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1059 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1060 |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
1061 local addclient = function( address, port, listeners, pattern, sslctx, typ, extra ) |
6814
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1062 local err |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1063 if type( listeners ) ~= "table" then |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1064 err = "invalid listener table" |
6815
46546d57f639
net/server_select: Fix typo
daurnimator <quae@daurnimator.com>
parents:
6814
diff
changeset
|
1065 elseif type ( address ) ~= "string" then |
6814
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1066 err = "invalid address" |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1067 elseif type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1068 err = "invalid port" |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1069 elseif sslctx and not has_luasec then |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1070 err = "luasec not found" |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1071 end |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
1072 if not typ then |
9634
f457edaab510
net.server_select: Fix IP validation to use correct variable (thanks quest)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
1073 local n = inet_pton(address); |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
1074 if not n then return nil, "invalid-ip"; end |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
1075 if #n == 16 then |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
1076 typ = "tcp6"; |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
1077 elseif #n == 4 then |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
1078 typ = "tcp4"; |
6819
ffb2b5e31456
net.server_{select,event}: addclient: Use getaddrinfo to detect IP address type if no socket type argument given. (Argument must be given for non-TCP)
Kim Alvefur <zash@zash.se>
parents:
6818
diff
changeset
|
1079 end |
6818
ae9d1289a868
net.server_{select,event}: addclient: Add argument for overriding socket type
Kim Alvefur <zash@zash.se>
parents:
6815
diff
changeset
|
1080 end |
9495
89e05b118f6e
net.server: Remove socket constructor fallback
Kim Alvefur <zash@zash.se>
parents:
9473
diff
changeset
|
1081 local create = luasocket[typ]; |
6818
ae9d1289a868
net.server_{select,event}: addclient: Add argument for overriding socket type
Kim Alvefur <zash@zash.se>
parents:
6815
diff
changeset
|
1082 if type( create ) ~= "function" then |
ae9d1289a868
net.server_{select,event}: addclient: Add argument for overriding socket type
Kim Alvefur <zash@zash.se>
parents:
6815
diff
changeset
|
1083 err = "invalid socket type" |
ae9d1289a868
net.server_{select,event}: addclient: Add argument for overriding socket type
Kim Alvefur <zash@zash.se>
parents:
6815
diff
changeset
|
1084 end |
6819
ffb2b5e31456
net.server_{select,event}: addclient: Use getaddrinfo to detect IP address type if no socket type argument given. (Argument must be given for non-TCP)
Kim Alvefur <zash@zash.se>
parents:
6818
diff
changeset
|
1085 |
6814
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1086 if err then |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1087 out_error( "server.lua, addclient: ", err ) |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1088 return nil, err |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1089 end |
936f969bf783
net/server_select: addclient: Check arguments
daurnimator <quae@daurnimator.com>
parents:
6813
diff
changeset
|
1090 |
6818
ae9d1289a868
net.server_{select,event}: addclient: Add argument for overriding socket type
Kim Alvefur <zash@zash.se>
parents:
6815
diff
changeset
|
1091 local client, err = create( ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1092 if err then |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1093 return nil, err |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1094 end |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1095 client:settimeout( 0 ) |
9499
36e379f058c8
net.server: Swich method for connecting sockets with remotes
Kim Alvefur <zash@zash.se>
parents:
9495
diff
changeset
|
1096 local ok, err = client:setpeername( address, port ) |
6286
ec6e8d5a3fd3
net/server_*: Fix addclient: LuaSocket 3.0-rc1 sometimes returns EALREADY instead of EINPROGRESS when the dns lookup has multiple results
daurnimator <quae@daurnimator.com>
parents:
6055
diff
changeset
|
1097 if ok or err == "timeout" or err == "Operation already in progress" then |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9634
diff
changeset
|
1098 return wrapclient( client, address, port, listeners, pattern, sslctx, extra ) |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1099 else |
6813
b1615fa62e59
net/server_select: addclient: Check for failure correctly; remove wrapconnection call on failure
daurnimator <quae@daurnimator.com>
parents:
6812
diff
changeset
|
1100 return nil, err |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1101 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1102 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1103 |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1104 local closewatcher = function (handler) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1105 local socket = handler.conn; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1106 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1107 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1108 _socketlist[ socket ] = nil |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1109 end; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1110 |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1111 local addremove = function (handler, read, send) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1112 local socket = handler.conn |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1113 _socketlist[ socket ] = handler |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1114 if read ~= nil then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1115 if read then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1116 _readlistlen = addsocket( _readlist, socket, _readlistlen ) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1117 else |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1118 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1119 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1120 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1121 if send ~= nil then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1122 if send then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1123 _sendlistlen = addsocket( _sendlist, socket, _sendlistlen ) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1124 else |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1125 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1126 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1127 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1128 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1129 |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1130 local watchfd = function ( fd, onreadable, onwriteable ) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1131 local socket = fd |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1132 if type(fd) == "number" then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1133 socket = { getfd = function () return fd; end } |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1134 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1135 local handler = { |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1136 conn = socket; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1137 readbuffer = onreadable or id; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1138 sendbuffer = onwriteable or id; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1139 close = closewatcher; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1140 setflags = addremove; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1141 }; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1142 addremove( handler, onreadable, onwriteable ) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1143 return handler |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1144 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1145 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1146 ----------------------------------// BEGIN //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1147 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1148 use "setmetatable" ( _socketlist, { __mode = "k" } ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1149 use "setmetatable" ( _readtimes, { __mode = "k" } ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1150 use "setmetatable" ( _writetimes, { __mode = "k" } ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1151 |
3682
d12141cbbaa0
net.server_select: Make changes required for sub-second timer precision.
Waqas Hussain <waqas20@gmail.com>
parents:
3543
diff
changeset
|
1152 _starttime = luasocket_gettime( ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1153 |
2424
7d3b071e3b5b
net.server_select: Fix oops in last commit
Matthew Wild <mwild1@gmail.com>
parents:
2423
diff
changeset
|
1154 local function setlogger(new_logger) |
2423
0597c553acf2
net.server_select: Allow setting the logger using server.setlogger
Matthew Wild <mwild1@gmail.com>
parents:
2339
diff
changeset
|
1155 local old_logger = log; |
0597c553acf2
net.server_select: Allow setting the logger using server.setlogger
Matthew Wild <mwild1@gmail.com>
parents:
2339
diff
changeset
|
1156 if new_logger then |
0597c553acf2
net.server_select: Allow setting the logger using server.setlogger
Matthew Wild <mwild1@gmail.com>
parents:
2339
diff
changeset
|
1157 log = new_logger; |
0597c553acf2
net.server_select: Allow setting the logger using server.setlogger
Matthew Wild <mwild1@gmail.com>
parents:
2339
diff
changeset
|
1158 end |
0597c553acf2
net.server_select: Allow setting the logger using server.setlogger
Matthew Wild <mwild1@gmail.com>
parents:
2339
diff
changeset
|
1159 return old_logger; |
0597c553acf2
net.server_select: Allow setting the logger using server.setlogger
Matthew Wild <mwild1@gmail.com>
parents:
2339
diff
changeset
|
1160 end |
0597c553acf2
net.server_select: Allow setting the logger using server.setlogger
Matthew Wild <mwild1@gmail.com>
parents:
2339
diff
changeset
|
1161 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1162 ----------------------------------// PUBLIC INTERFACE //-- |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1163 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1164 return { |
4808
07d0a3a75c8a
net.server, net.timer, net.server_select: Rearrange dependencies between these three modules. server.addtimer() is no longer a public function (renamed to _addtimer) and is not available at all from server_event (compat code removed to prevent traceback) (thanks Nulani)
Matthew Wild <mwild1@gmail.com>
parents:
4675
diff
changeset
|
1165 _addtimer = addtimer, |
6481
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6465
diff
changeset
|
1166 add_task = add_task; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1167 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1168 addclient = addclient, |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1169 wrapclient = wrapclient, |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8749
diff
changeset
|
1170 watchfd = watchfd, |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5664
diff
changeset
|
1171 |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1172 loop = loop, |
3003
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
1173 link = link, |
4145
e992650185c8
net.server_select: Expose step() function (thanks Łukasz)
Matthew Wild <mwild1@gmail.com>
parents:
4111
diff
changeset
|
1174 step = step, |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1175 stats = stats, |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1176 closeall = closeall, |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1177 addserver = addserver, |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9635
diff
changeset
|
1178 listen = listen, |
2581
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1179 getserver = getserver, |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1180 setlogger = setlogger, |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1181 getsettings = getsettings, |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1182 setquitting = setquitting, |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1183 removeserver = removeserver, |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1184 get_backend = get_backend, |
1b9f424e695e
net.server_select: *Major* whitespace refactoring to meet Prosody's coding style guidelines, we now have tabs
Matthew Wild <mwild1@gmail.com>
parents:
2559
diff
changeset
|
1185 changesettings = changesettings, |
12481
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
12480
diff
changeset
|
1186 |
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
12480
diff
changeset
|
1187 tls_builder = function(basedir) |
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
12480
diff
changeset
|
1188 return sslconfig._new(tls_impl.new_context, basedir) |
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
12480
diff
changeset
|
1189 end, |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2312
diff
changeset
|
1190 } |