Software /
code /
prosody
Annotate
net/server_event.lua @ 13164:1aa83a5667f9
util.jsonschema: Remove wrapper function
This was to silence some Teal warning that seems to have gone away.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 17 Jun 2023 17:19:40 +0200 |
parent | 12974:ba409c67353b |
rev | line source |
---|---|
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
1 --[[ |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
2 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
3 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
4 server.lua based on lua/libevent by blastbeat |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
5 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
6 notes: |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
7 -- when using luaevent, never register 2 or more EV_READ at one socket, same for EV_WRITE |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8530
diff
changeset
|
8 -- you can't even register a new EV_READ/EV_WRITE callback inside another one |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
9 -- to do some of the above, use timeout events or something what will called from outside |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8530
diff
changeset
|
10 -- don't let garbagecollect eventcallbacks, as long they are running |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
11 -- when using luasec, there are 4 cases of timeout errors: wantread or wantwrite during reading or writing |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
12 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
13 --]] |
6852
626d8152b1ad
server_event: Add luacheck annotations
Kim Alvefur <zash@zash.se>
parents:
6851
diff
changeset
|
14 -- luacheck: ignore 212/self 431/err 211/ret |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
15 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
16 local SCRIPT_NAME = "server_event.lua" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
17 local SCRIPT_VERSION = "0.05" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
18 local SCRIPT_AUTHOR = "blastbeat" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
19 local LAST_MODIFIED = "2009/11/20" |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
20 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
21 local cfg = { |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
22 MAX_CONNECTIONS = 100000, -- max per server connections (use "ulimit -n" on *nix) |
3237
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
23 MAX_HANDSHAKE_ATTEMPTS= 1000, -- attempts to finish ssl handshake |
721a83a3beb9
net.server_event: attemp to fix more spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
3149
diff
changeset
|
24 HANDSHAKE_TIMEOUT = 60, -- timeout in seconds per handshake attempt |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
25 MAX_READ_LENGTH = 1024 * 1024 * 1024 * 1024, -- max bytes allowed to read from sockets |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
26 MAX_SEND_LENGTH = 1024 * 1024 * 1024 * 1024, -- max bytes size of write buffer (for writing on sockets) |
5531
483f795f6f99
net.server*: Allow the TCP backlog parameter to be set in the config
Kim Alvefur <zash@zash.se>
parents:
5339
diff
changeset
|
27 ACCEPT_QUEUE = 128, -- might influence the length of the pending sockets queue |
2952
5069534f7f36
net.server_event: Fixed spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
2941
diff
changeset
|
28 ACCEPT_DELAY = 10, -- seconds to wait until the next attempt of a full server to accept |
8983
23f66f04e216
net.server: Set default read timeout to 14 minutes (fixes #971)
Kim Alvefur <zash@zash.se>
parents:
8781
diff
changeset
|
29 READ_TIMEOUT = 14 * 60, -- timeout in seconds for read data from socket |
2939
dc73cbc69bd5
net.server_event: Bump timeout values up somewhat
Matthew Wild <mwild1@gmail.com>
parents:
2731
diff
changeset
|
30 WRITE_TIMEOUT = 180, -- timeout in seconds for write data on socket |
2952
5069534f7f36
net.server_event: Fixed spelling fail.
Waqas Hussain <waqas20@gmail.com>
parents:
2941
diff
changeset
|
31 CONNECT_TIMEOUT = 20, -- timeout in seconds for connection attempts |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
32 CLEAR_DELAY = 5, -- seconds to wait for clearing interface list (and calling ondisconnect listeners) |
7420
3fc5560557a5
net.server_event: Schedule another read callback if there is still data left in buffer after reading (fixes #583 for real)
Kim Alvefur <zash@zash.se>
parents:
7419
diff
changeset
|
33 READ_RETRY_DELAY = 1e-06, -- if, after reading, there is still data in buffer, wait this long and continue reading |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
34 DEBUG = true, -- show debug messages |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
35 } |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
36 |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
37 local pairs = pairs |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
38 local select = select |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
39 local require = require |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
40 local tostring = tostring |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
41 local setmetatable = setmetatable |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
42 |
5286
0cbe57fbe2cd
net.server_event: Optimize socket write buffers (don't do buf=buf..newdata).
Waqas Hussain <waqas20@gmail.com>
parents:
5277
diff
changeset
|
43 local t_insert = table.insert |
0cbe57fbe2cd
net.server_event: Optimize socket write buffers (don't do buf=buf..newdata).
Waqas Hussain <waqas20@gmail.com>
parents:
5277
diff
changeset
|
44 local t_concat = table.concat |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
45 local s_sub = string.sub |
5286
0cbe57fbe2cd
net.server_event: Optimize socket write buffers (don't do buf=buf..newdata).
Waqas Hussain <waqas20@gmail.com>
parents:
5277
diff
changeset
|
46 |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
47 local coroutine_wrap = coroutine.wrap |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
48 local coroutine_yield = coroutine.yield |
5286
0cbe57fbe2cd
net.server_event: Optimize socket write buffers (don't do buf=buf..newdata).
Waqas Hussain <waqas20@gmail.com>
parents:
5277
diff
changeset
|
49 |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
50 local has_luasec = pcall ( require , "ssl" ) |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
51 local socket = require "socket" |
6853
a2cc477eb19b
server_event: Rename luaevent module variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6852
diff
changeset
|
52 local levent = require "luaevent.core" |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12542
diff
changeset
|
53 local inet = require "prosody.util.net"; |
9473
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; |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12542
diff
changeset
|
55 local sslconfig = require "prosody.util.sslconfig"; |
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12542
diff
changeset
|
56 local tls_impl = require "prosody.net.tls_luasec"; |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
57 |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
58 local socket_gettime = socket.gettime |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
59 |
12974
ba409c67353b
net: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12542
diff
changeset
|
60 local log = require ("prosody.util.logger").init("socket") |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
61 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
62 local function debug(...) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
63 return log("debug", ("%s "):rep(select('#', ...)), ...) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
64 end |
6855
40236602ab5b
server_event: Comment out the unused vdebug function [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6854
diff
changeset
|
65 -- local vdebug = debug; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
66 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
67 local bitor = ( function( ) -- thx Rici Lake |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
68 local hasbit = function( x, p ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
69 return x % ( p + p ) >= p |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
70 end |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
71 return function( x, y ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
72 local p = 1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
73 local z = 0 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
74 local limit = x > y and x or y |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
75 while p <= limit do |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
76 if hasbit( x, p ) or hasbit( y, p ) then |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
77 z = z + p |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
78 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
79 p = p + p |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
80 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
81 return z |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
82 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
83 end )( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
84 |
6853
a2cc477eb19b
server_event: Rename luaevent module variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6852
diff
changeset
|
85 local base = levent.new( ) |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
86 local addevent = base.addevent |
6853
a2cc477eb19b
server_event: Rename luaevent module variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6852
diff
changeset
|
87 local EV_READ = levent.EV_READ |
a2cc477eb19b
server_event: Rename luaevent module variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6852
diff
changeset
|
88 local EV_WRITE = levent.EV_WRITE |
a2cc477eb19b
server_event: Rename luaevent module variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6852
diff
changeset
|
89 local EV_TIMEOUT = levent.EV_TIMEOUT |
a2cc477eb19b
server_event: Rename luaevent module variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6852
diff
changeset
|
90 local EV_SIGNAL = levent.EV_SIGNAL |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
91 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
92 local EV_READWRITE = bitor( EV_READ, EV_WRITE ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
93 |
6849
0455b9686e16
server_event: Replace implementation of ordered set with a simple table set
Kim Alvefur <zash@zash.se>
parents:
6819
diff
changeset
|
94 local interfacelist = { } |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
95 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
96 -- Client interface methods |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
97 local interface_mt = {}; interface_mt.__index = interface_mt; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
98 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
99 -- Private methods |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
100 function interface_mt:_close() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
101 return self:_destroy(); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
102 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
103 |
7339
b5565715fce8
server_event: addclient: wrapclient already calls startconnection for us [backported from trunk]
daurnimator <quae@daurnimator.com>
parents:
7337
diff
changeset
|
104 function interface_mt:_start_connection(plainssl) -- called from wrapclient |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
105 local callback = function( event ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
106 if EV_TIMEOUT == event then -- timeout during connection |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
107 self.fatalerror = "connection timeout" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
108 self:ontimeout() -- call timeout listener |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
109 self:_close() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
110 debug( "new connection failed. id:", self.id, "error:", self.fatalerror ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
111 else |
8530
075df839c110
net.server_event: Not every instantly-readable connection is an error, continue if possible
Matthew Wild <mwild1@gmail.com>
parents:
8529
diff
changeset
|
112 if EV_READWRITE == event then |
075df839c110
net.server_event: Not every instantly-readable connection is an error, continue if possible
Matthew Wild <mwild1@gmail.com>
parents:
8529
diff
changeset
|
113 if self.readcallback(event) == -1 then |
075df839c110
net.server_event: Not every instantly-readable connection is an error, continue if possible
Matthew Wild <mwild1@gmail.com>
parents:
8529
diff
changeset
|
114 -- Fatal error occurred |
075df839c110
net.server_event: Not every instantly-readable connection is an error, continue if possible
Matthew Wild <mwild1@gmail.com>
parents:
8529
diff
changeset
|
115 return -1; |
075df839c110
net.server_event: Not every instantly-readable connection is an error, continue if possible
Matthew Wild <mwild1@gmail.com>
parents:
8529
diff
changeset
|
116 end |
075df839c110
net.server_event: Not every instantly-readable connection is an error, continue if possible
Matthew Wild <mwild1@gmail.com>
parents:
8529
diff
changeset
|
117 end |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
118 if plainssl and has_luasec then -- start ssl session |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
119 self:starttls(self._sslctx, true) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
120 else -- normal connection |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
121 self:_start_session(true) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
122 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
123 debug( "new connection established. id:", self.id ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
124 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
125 self.eventconnect = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
126 return -1 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
127 end |
8529
257fd322fc9f
net.server_event: Better outgoing connection error handling
Matthew Wild <mwild1@gmail.com>
parents:
8384
diff
changeset
|
128 self.eventconnect = addevent( base, self.conn, EV_READWRITE, callback, cfg.CONNECT_TIMEOUT ) |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
129 return true |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
130 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
131 function interface_mt:_start_session(call_onconnect) -- new session, for example after startssl |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
132 if self.type == "client" then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
133 local callback = function( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
134 self:_lock( false, false, false ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
135 --vdebug( "start listening on client socket with id:", self.id ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
136 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback, cfg.READ_TIMEOUT ); -- register callback |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
137 if call_onconnect then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
138 self:onconnect() |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
139 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
140 self.eventsession = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
141 return -1 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
142 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
143 self.eventsession = addevent( base, nil, EV_TIMEOUT, callback, 0 ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
144 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
145 self:_lock( false ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
146 --vdebug( "start listening on server socket with id:", self.id ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
147 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback ) -- register callback |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
148 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
149 return true |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
150 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
151 function interface_mt:_start_ssl(call_onconnect) -- old socket will be destroyed, therefore we have to close read/write events first |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
152 --vdebug( "starting ssl session with client id:", self.id ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
153 local _ |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
154 _ = self.eventread and self.eventread:close( ) -- close events; this must be called outside of the event callbacks! |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
155 _ = self.eventwrite and self.eventwrite:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
156 self.eventread, self.eventwrite = nil, nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
157 local err |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
158 self.conn, err = self._sslctx:wrap(self.conn) |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
159 if err then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
160 self.fatalerror = err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
161 self.conn = nil -- cannot be used anymore |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
162 if call_onconnect then |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8530
diff
changeset
|
163 self.ondisconnect = nil -- don't call this when client isn't really connected |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
164 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
165 self:_close() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
166 debug( "fatal error while ssl wrapping:", err ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
167 return false |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
168 end |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
169 |
9850
9a905888b96c
net.server_event: Add SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
170 if self.conn.sni then |
9a905888b96c
net.server_event: Add SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
171 if self.servername then |
9a905888b96c
net.server_event: Add SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
172 self.conn:sni(self.servername); |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
173 elseif next(self._sslctx._sni_contexts) ~= nil then |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
174 self.conn:sni(self._sslctx._sni_contexts, true); |
9850
9a905888b96c
net.server_event: Add SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
175 end |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
176 end |
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
177 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
178 self.conn:settimeout( 0 ) -- set non blocking |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
179 local handshakecallback = coroutine_wrap(function( event ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
180 local _, err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
181 local attempt = 0 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
182 local maxattempt = cfg.MAX_HANDSHAKE_ATTEMPTS |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
183 while attempt < maxattempt do -- no endless loop |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
184 attempt = attempt + 1 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
185 debug( "ssl handshake of client with id:"..tostring(self)..", attempt:"..attempt ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
186 if attempt > maxattempt then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
187 self.fatalerror = "max handshake attempts exceeded" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
188 elseif EV_TIMEOUT == event then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
189 self.fatalerror = "timeout during handshake" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
190 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
191 _, err = self.conn:dohandshake( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
192 if not err then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
193 self:_lock( false, false, false ) -- unlock the interface; sending, closing etc allowed |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
194 self.send = self.conn.send -- caching table lookups with new client object |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
195 self.receive = self.conn.receive |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
196 if not call_onconnect then -- trigger listener |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
197 self:onstatus("ssl-handshake-complete"); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
198 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
199 self:_start_session( call_onconnect ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
200 debug( "ssl handshake done" ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
201 self.eventhandshake = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
202 return -1 |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
203 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
204 if err == "wantwrite" then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
205 event = EV_WRITE |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
206 elseif err == "wantread" then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
207 event = EV_READ |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
208 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
209 debug( "ssl handshake error:", err ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
210 self.fatalerror = err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
211 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
212 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
213 if self.fatalerror then |
3396
23cf369ed1c3
net.server_event: Fix to not call onconnect a second time after the SSL handshake for starttls connections (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents:
3387
diff
changeset
|
214 if call_onconnect then |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8530
diff
changeset
|
215 self.ondisconnect = nil -- don't call this when client isn't really connected |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
216 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
217 self:_close() |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
218 debug( "handshake failed because:", self.fatalerror ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
219 self.eventhandshake = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
220 return -1 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
221 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
222 event = coroutine_yield( event, cfg.HANDSHAKE_TIMEOUT ) -- yield this monster... |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
223 end |
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
224 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
225 ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
226 debug "starting handshake..." |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
227 self:_lock( false, true, true ) -- unlock read/write events, but keep interface locked |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
228 self.eventhandshake = addevent( base, self.conn, EV_READWRITE, handshakecallback, cfg.HANDSHAKE_TIMEOUT ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
229 return true |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
230 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
231 function interface_mt:_destroy() -- close this interface + events and call last listener |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
232 debug( "closing client with id:", self.id, self.fatalerror ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
233 self:_lock( true, true, true ) -- first of all, lock the interface to avoid further actions |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
234 local _ |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
235 _ = self.eventread and self.eventread:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
236 if self.type == "client" then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
237 _ = self.eventwrite and self.eventwrite:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
238 _ = self.eventhandshake and self.eventhandshake:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
239 _ = self.eventstarthandshake and self.eventstarthandshake:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
240 _ = self.eventconnect and self.eventconnect:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
241 _ = self.eventsession and self.eventsession:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
242 _ = self.eventwritetimeout and self.eventwritetimeout:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
243 _ = self.eventreadtimeout and self.eventreadtimeout:close( ) |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8530
diff
changeset
|
244 -- call ondisconnect listener (won't be the case if handshake failed on connect) |
8384
4a5c6f6214ee
net.server_event: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8314
diff
changeset
|
245 _ = self.ondisconnect and self:ondisconnect( self.fatalerror ~= "client to close" and self.fatalerror) |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
246 _ = self.conn and self.conn:close( ) -- close connection |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
247 _ = self._server and self._server:counter(-1); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
248 self.eventread, self.eventwrite = nil, nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
249 self.eventstarthandshake, self.eventhandshake, self.eventclose = nil, nil, nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
250 self.readcallback, self.writecallback = nil, nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
251 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
252 self.conn:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
253 self.eventread, self.eventclose = nil, nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
254 self.interface, self.readcallback = nil, nil |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
255 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
256 interfacelist[ self ] = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
257 return true |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
258 end |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
259 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
260 function interface_mt:_lock(nointerface, noreading, nowriting) -- lock or unlock this interface or events |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
261 self.nointerface, self.noreading, self.nowriting = nointerface, noreading, nowriting |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
262 return nointerface, noreading, nowriting |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
263 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
264 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
265 --TODO: Deprecate |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
266 function interface_mt:lock_read(switch) |
12387
05c250fa335a
Spelling: Fix various spelling mistakes (thanks timeless)
Kim Alvefur <zash@zash.se>
parents:
11741
diff
changeset
|
267 log("warn", ":lock_read is deprecated, use :pause() and :resume()"); |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
268 if switch then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
269 return self:pause(); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
270 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
271 return self:resume(); |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
272 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
273 end |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
274 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
275 function interface_mt:pause() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
276 return self:_lock(self.nointerface, true, self.nowriting); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
277 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
278 |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
279 function interface_mt:sslctx() |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
280 return self._sslctx |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
281 end |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
282 |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
283 function interface_mt:ssl_info() |
12542
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
284 local sock = self.conn; |
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
285 if not sock.info then return nil, "not-implemented"; end |
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
286 return sock:info(); |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
287 end |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
288 |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
289 function interface_mt:ssl_peercertificate() |
12542
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
290 local sock = self.conn; |
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
291 if not sock.getpeercertificate then return nil, "not-implemented"; end |
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
292 return sock:getpeercertificate(); |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
293 end |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
294 |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
295 function interface_mt:ssl_peerverification() |
12542
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
296 local sock = self.conn; |
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
297 if not sock.getpeerverification then return nil, { { "Chain verification not supported" } }; end |
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
298 return sock:getpeerverification(); |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
299 end |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
300 |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
301 function interface_mt:ssl_peerfinished() |
12542
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
302 local sock = self.conn; |
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
303 if not sock.getpeerfinished then return nil, "not-implemented"; end |
5ec9d6913162
net.server: Fix multiple return values
Kim Alvefur <zash@zash.se>
parents:
12481
diff
changeset
|
304 return sock:getpeerfinished(); |
12480
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
305 end |
7e9ebdc75ce4
net: isolate LuaSec-specifics
Jonas Schäfer <jonas@wielicki.name>
parents:
12387
diff
changeset
|
306 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
307 function interface_mt:resume() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
308 self:_lock(self.nointerface, false, self.nowriting); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
309 if self.readcallback and not self.eventread then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
310 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback, cfg.READ_TIMEOUT ); -- register callback |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
311 return true; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
312 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
313 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
314 |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
315 function interface_mt:pause_writes() |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
316 return self:_lock(self.nointerface, self.noreading, true); |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
317 end |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
318 |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
319 function interface_mt:resume_writes() |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
320 self:_lock(self.nointerface, self.noreading, false); |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
321 if self.writecallback and not self.eventwrite then |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
322 self.eventwrite = addevent( base, self.conn, EV_WRITE, self.writecallback, cfg.WRITE_TIMEOUT ); -- register callback |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
323 return true; |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
324 end |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
325 end |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
326 |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9575
diff
changeset
|
327 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
328 function interface_mt:counter(c) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
329 if c then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
330 self._connections = self._connections + c |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
331 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
332 return self._connections |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
333 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
334 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
335 -- Public methods |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
336 function interface_mt:write(data) |
9915
9fe7b18bca15
net.server_event: Allow writing into buffer of write-locked connections
Kim Alvefur <zash@zash.se>
parents:
9897
diff
changeset
|
337 if self.nointerface then return nil, "locked"; end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
338 --vdebug( "try to send data to client, id/data:", self.id, data ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
339 data = tostring( data ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
340 local len = #data |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
341 local total = len + self.writebufferlen |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
342 if total > cfg.MAX_SEND_LENGTH then -- check buffer length |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
343 local err = "send buffer exceeded" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
344 debug( "error:", err ) -- to much, check your app |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
345 return nil, err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
346 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
347 t_insert(self.writebuffer, data) -- new buffer |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
348 self.writebufferlen = total |
9915
9fe7b18bca15
net.server_event: Allow writing into buffer of write-locked connections
Kim Alvefur <zash@zash.se>
parents:
9897
diff
changeset
|
349 if not self.eventwrite and not self.nowriting then -- register new write event |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
350 --vdebug( "register new write event" ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
351 self.eventwrite = addevent( base, self.conn, EV_WRITE, self.writecallback, cfg.WRITE_TIMEOUT ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
352 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
353 return true |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
354 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
355 function interface_mt:close() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
356 if self.nointerface then return nil, "locked"; end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
357 debug( "try to close client connection with id:", self.id ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
358 if self.type == "client" then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
359 self.fatalerror = "client to close" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
360 if self.eventwrite then -- wait for incomplete write request |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
361 self:_lock( true, true, false ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
362 debug "closing delayed until writebuffer is empty" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
363 return nil, "writebuffer not empty, waiting" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
364 else -- close now |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
365 self:_lock( true, true, true ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
366 self:_close() |
4676
1ce2ff9ec313
net.server_event: Do not automatically close clients when closing a server port
Matthew Wild <mwild1@gmail.com>
parents:
4468
diff
changeset
|
367 return true |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
368 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
369 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
370 debug( "try to close server with id:", tostring(self.id)) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
371 self.fatalerror = "server to close" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
372 self:_lock( true ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
373 self:_close( 0 ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
374 return true |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
375 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
376 end |
7224 | 377 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
378 function interface_mt:socket() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
379 return self.conn |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
380 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
381 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
382 function interface_mt:server() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
383 return self._server or self; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
384 end |
7224 | 385 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
386 function interface_mt:port() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
387 return self._port |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
388 end |
7224 | 389 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
390 function interface_mt:serverport() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
391 return self._serverport |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
392 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
393 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
394 function interface_mt:ip() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
395 return self._ip |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
396 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
397 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
398 function interface_mt:ssl() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
399 return self._usingssl |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
400 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
401 interface_mt.clientport = interface_mt.port -- COMPAT server_select |
7224 | 402 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
403 function interface_mt:type() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
404 return self._type or "client" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
405 end |
7224 | 406 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
407 function interface_mt:connections() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
408 return self._connections |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
409 end |
7224 | 410 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
411 function interface_mt:address() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
412 return self.addr |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
413 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
414 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
415 function interface_mt:set_sslctx(sslctx) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
416 self._sslctx = sslctx; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
417 if sslctx then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
418 self.starttls = nil; -- use starttls() of interface_mt |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
419 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
420 self.starttls = false; -- prevent starttls() |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
421 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
422 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
423 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
424 function interface_mt:set_mode(pattern) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
425 if pattern then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
426 self._pattern = pattern; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
427 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
428 return self._pattern; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
429 end |
7224 | 430 |
431 function interface_mt:set_send(new_send) -- luacheck: ignore 212 | |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
432 -- No-op, we always use the underlying connection's send |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
433 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
|
434 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
435 function interface_mt:starttls(sslctx, call_onconnect) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
436 debug( "try to start ssl at client id:", self.id ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
437 local err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
438 self._sslctx = sslctx; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
439 if self._usingssl then -- startssl was already called |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
440 err = "ssl already active" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
441 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
442 if err then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
443 debug( "error:", err ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
444 return nil, err |
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
|
445 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
446 self._usingssl = true |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
447 self.startsslcallback = function( ) -- we have to start the handshake outside of a read/write event |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
448 self.startsslcallback = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
449 self:_start_ssl(call_onconnect); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
450 self.eventstarthandshake = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
451 return -1 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
452 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
453 if not self.eventwrite then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
454 self:_lock( true, true, true ) -- lock the interface, to not disturb the handshake |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
455 self.eventstarthandshake = addevent( base, nil, EV_TIMEOUT, self.startsslcallback, 0 ) -- add event to start handshake |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
456 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
457 -- wait until writebuffer is empty |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
458 self:_lock( true, true, false ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
459 debug "ssl session delayed until writebuffer is empty..." |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
460 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
461 self.starttls = false; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
462 return true |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
463 end |
5625
e67891ad18d6
net.server_event: Add support for listener.onreadtimeout(conn), which can return true to prevent the connection from being closed when a read timeout occurs
Matthew Wild <mwild1@gmail.com>
parents:
5531
diff
changeset
|
464 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
465 function interface_mt:setoption(option, value) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
466 if self.conn.setoption then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
467 return self.conn:setoption(option, value); |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
468 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
469 return false, "setoption not implemented"; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
470 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
471 |
9387
33e52f727f0f
net.connect: Fix passing request table to new listener
Kim Alvefur <zash@zash.se>
parents:
9385
diff
changeset
|
472 function interface_mt:setlistener(listener, data) |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
473 self:ondetach(); -- Notify listener that it is no longer responsible for this connection |
7337
8ec935687bae
server_event: Split long list of assignments into multiple lines for readability
Kim Alvefur <zash@zash.se>
parents:
7336
diff
changeset
|
474 self.onconnect = listener.onconnect; |
8ec935687bae
server_event: Split long list of assignments into multiple lines for readability
Kim Alvefur <zash@zash.se>
parents:
7336
diff
changeset
|
475 self.ondisconnect = listener.ondisconnect; |
8ec935687bae
server_event: Split long list of assignments into multiple lines for readability
Kim Alvefur <zash@zash.se>
parents:
7336
diff
changeset
|
476 self.onincoming = listener.onincoming; |
8ec935687bae
server_event: Split long list of assignments into multiple lines for readability
Kim Alvefur <zash@zash.se>
parents:
7336
diff
changeset
|
477 self.ontimeout = listener.ontimeout; |
8ec935687bae
server_event: Split long list of assignments into multiple lines for readability
Kim Alvefur <zash@zash.se>
parents:
7336
diff
changeset
|
478 self.onreadtimeout = listener.onreadtimeout; |
8ec935687bae
server_event: Split long list of assignments into multiple lines for readability
Kim Alvefur <zash@zash.se>
parents:
7336
diff
changeset
|
479 self.onstatus = listener.onstatus; |
8ec935687bae
server_event: Split long list of assignments into multiple lines for readability
Kim Alvefur <zash@zash.se>
parents:
7336
diff
changeset
|
480 self.ondetach = listener.ondetach; |
9385
58fe099043ed
net.server: Call a 'onattach' callback when replacing listeners
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
481 self.onattach = listener.onattach; |
11741
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
482 self.onpredrain = listener.onpredrain; |
7809
34ca48325ce7
net.server_event: Also replace 'ondrain' when setting new listeners (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
7688
diff
changeset
|
483 self.ondrain = listener.ondrain; |
9387
33e52f727f0f
net.connect: Fix passing request table to new listener
Kim Alvefur <zash@zash.se>
parents:
9385
diff
changeset
|
484 self:onattach(data); |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
485 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
486 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
487 -- Stub handlers |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
488 function interface_mt:onconnect() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
489 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
490 function interface_mt:onincoming() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
491 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
492 function interface_mt:ondisconnect() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
493 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
494 function interface_mt:ontimeout() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
495 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
496 function interface_mt:onreadtimeout() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
497 end |
11741
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
498 function interface_mt:onpredrain() |
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
499 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
500 function interface_mt:ondrain() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
501 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
502 function interface_mt:ondetach() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
503 end |
9385
58fe099043ed
net.server: Call a 'onattach' callback when replacing listeners
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
504 function interface_mt:onattach() |
58fe099043ed
net.server: Call a 'onattach' callback when replacing listeners
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
505 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
506 function interface_mt:onstatus() |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
507 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
508 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
509 -- End of client interface methods |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
510 |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
511 local function handleclient( client, ip, port, server, pattern, listener, sslctx, extra ) -- creates an client interface |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
512 --vdebug("creating client interfacce...") |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
513 local interface = { |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
514 type = "client"; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
515 conn = client; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
516 currenttime = socket_gettime( ); -- safe the origin |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
517 writebuffer = {}; -- writebuffer |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
518 writebufferlen = 0; -- length of writebuffer |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
519 send = client.send; -- caching table lookups |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
520 receive = client.receive; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
521 onconnect = listener.onconnect; -- will be called when client disconnects |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
522 ondisconnect = listener.ondisconnect; -- will be called when client disconnects |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
523 onincoming = listener.onincoming; -- will be called when client sends data |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
524 ontimeout = listener.ontimeout; -- called when fatal socket timeout occurs |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
525 onreadtimeout = listener.onreadtimeout; -- called when socket inactivity timeout occurs |
11741
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
526 onpredrain = listener.onpredrain; -- called before writes |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
527 ondrain = listener.ondrain; -- called when writebuffer is empty |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
528 ondetach = listener.ondetach; -- called when disassociating this listener from this connection |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
529 onstatus = listener.onstatus; -- called for status changes (e.g. of SSL/TLS) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
530 eventread = false, eventwrite = false, eventclose = false, |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
531 eventhandshake = false, eventstarthandshake = false; -- event handler |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
532 eventconnect = false, eventsession = false; -- more event handler... |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
533 eventwritetimeout = false; -- even more event handler... |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
534 eventreadtimeout = false; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
535 fatalerror = false; -- error message |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
536 writecallback = false; -- will be called on write events |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
537 readcallback = false; -- will be called on read events |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
538 nointerface = true; -- lock/unlock parameter of this interface |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
539 noreading = false, nowriting = false; -- locks of the read/writecallback |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
540 startsslcallback = false; -- starting handshake callback |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
541 position = false; -- position of client in interfacelist |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
542 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
543 -- Properties |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
544 _ip = ip, _port = port, _server = server, _pattern = pattern, |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
545 _serverport = (server and server:port() or nil), |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
546 _sslctx = sslctx; -- parameters |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
547 _usingssl = false; -- client is using ssl; |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
548 extra = extra; |
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
549 servername = extra and extra.servername; |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
550 } |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
551 if not has_luasec then interface.starttls = false; end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
552 interface.id = tostring(interface):match("%x+$"); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
553 interface.writecallback = function( event ) -- called on write events |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
554 --vdebug( "new client write event, id/ip/port:", interface, ip, port ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
555 if interface.nowriting or ( interface.fatalerror and ( "client to close" ~= interface.fatalerror ) ) then -- leave this event |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
556 --vdebug( "leaving this event because:", interface.nowriting or interface.fatalerror ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
557 interface.eventwrite = false |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
558 return -1 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
559 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
560 if EV_TIMEOUT == event then -- took too long to write some data to socket -> disconnect |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
561 interface.fatalerror = "timeout during writing" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
562 debug( "writing failed:", interface.fatalerror ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
563 interface:_close() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
564 interface.eventwrite = false |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
565 return -1 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
566 else -- can write :) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
567 if interface._usingssl then -- handle luasec |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
568 if interface.eventreadtimeout then -- we have to read first |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
569 local ret = interface.readcallback( ) -- call readcallback |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
570 --vdebug( "tried to read in writecallback, result:", ret ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
571 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
572 if interface.eventwritetimeout then -- luasec only |
12387
05c250fa335a
Spelling: Fix various spelling mistakes (thanks timeless)
Kim Alvefur <zash@zash.se>
parents:
11741
diff
changeset
|
573 interface.eventwritetimeout:close( ) -- first we have to close timeout event which where registered after a wantread error |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
574 interface.eventwritetimeout = false |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
575 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
576 end |
11741
dcf38ac6a38c
net.server: Add a predrain callaback just before writes
Kim Alvefur <zash@zash.se>
parents:
11068
diff
changeset
|
577 interface:onpredrain(); |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
578 interface.writebuffer = { t_concat(interface.writebuffer) } |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
579 local succ, err, byte = interface.conn:send( interface.writebuffer[1], 1, interface.writebufferlen ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
580 --vdebug( "write data:", interface.writebuffer, "error:", err, "part:", byte ) |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8530
diff
changeset
|
581 if succ then -- writing successful |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
582 interface.writebuffer[1] = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
583 interface.writebufferlen = 0 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
584 interface:ondrain(); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
585 if interface.fatalerror then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
586 debug "closing client after writing" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
587 interface:_close() -- close interface if needed |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
588 elseif interface.startsslcallback then -- start ssl connection if needed |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
589 debug "starting ssl handshake after writing" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
590 interface.eventstarthandshake = addevent( base, nil, EV_TIMEOUT, interface.startsslcallback, 0 ) |
7399 | 591 elseif interface.writebufferlen ~= 0 then |
7394 | 592 -- data possibly written from ondrain |
593 return EV_WRITE, cfg.WRITE_TIMEOUT | |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
594 elseif interface.eventreadtimeout then |
7385 | 595 return EV_WRITE, cfg.WRITE_TIMEOUT |
596 end | |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
597 interface.eventwrite = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
598 return -1 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
599 elseif byte and (err == "timeout" or err == "wantwrite") then -- want write again |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
600 --vdebug( "writebuffer is not empty:", err ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
601 interface.writebuffer[1] = s_sub( interface.writebuffer[1], byte + 1, interface.writebufferlen ) -- new buffer |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
602 interface.writebufferlen = interface.writebufferlen - byte |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
603 if "wantread" == err then -- happens only with luasec |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
604 local callback = function( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
605 interface:_close() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
606 interface.eventwritetimeout = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
607 return -1; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
608 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
609 interface.eventwritetimeout = addevent( base, nil, EV_TIMEOUT, callback, cfg.WRITE_TIMEOUT ) -- reg a new timeout event |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8530
diff
changeset
|
610 debug( "wantread during write attempt, reg it in readcallback but don't know what really happens next..." ) |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
611 -- hopefully this works with luasec; its simply not possible to use 2 different write events on a socket in luaevent |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
612 return -1 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
613 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
614 return EV_WRITE, cfg.WRITE_TIMEOUT |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
615 else -- connection was closed during writing or fatal error |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
616 interface.fatalerror = err or "fatal error" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
617 debug( "connection failed in write event:", interface.fatalerror ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
618 interface:_close() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
619 interface.eventwrite = nil |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
620 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
621 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
622 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
623 end |
7224 | 624 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
625 interface.readcallback = function( event ) -- called on read events |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
626 --vdebug( "new client read event, id/ip/port:", tostring(interface.id), tostring(ip), tostring(port) ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
627 if interface.noreading or interface.fatalerror then -- leave this event |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
628 --vdebug( "leaving this event because:", tostring(interface.noreading or interface.fatalerror) ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
629 interface.eventread = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
630 return -1 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
631 end |
7420
3fc5560557a5
net.server_event: Schedule another read callback if there is still data left in buffer after reading (fixes #583 for real)
Kim Alvefur <zash@zash.se>
parents:
7419
diff
changeset
|
632 if EV_TIMEOUT == event and not interface.conn:dirty() and interface:onreadtimeout() ~= true then |
7688
88ad2d5bcf80
net.server_event: Restore destruction of socket on read timeout (lost since e67891ad18d6) (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
7420
diff
changeset
|
633 interface.fatalerror = "timeout during receiving" |
88ad2d5bcf80
net.server_event: Restore destruction of socket on read timeout (lost since e67891ad18d6) (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
7420
diff
changeset
|
634 debug( "connection failed:", interface.fatalerror ) |
88ad2d5bcf80
net.server_event: Restore destruction of socket on read timeout (lost since e67891ad18d6) (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
7420
diff
changeset
|
635 interface:_close() |
88ad2d5bcf80
net.server_event: Restore destruction of socket on read timeout (lost since e67891ad18d6) (thanks Ge0rG)
Kim Alvefur <zash@zash.se>
parents:
7420
diff
changeset
|
636 interface.eventread = nil |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
637 return -1 -- took too long to get some data from client -> disconnect |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
638 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
639 if interface._usingssl then -- handle luasec |
12387
05c250fa335a
Spelling: Fix various spelling mistakes (thanks timeless)
Kim Alvefur <zash@zash.se>
parents:
11741
diff
changeset
|
640 if interface.eventwritetimeout then -- ok, in the past writecallback was registered |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
641 local ret = interface.writecallback( ) -- call it |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
642 --vdebug( "tried to write in readcallback, result:", tostring(ret) ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
643 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
644 if interface.eventreadtimeout then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
645 interface.eventreadtimeout:close( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
646 interface.eventreadtimeout = nil |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
647 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
648 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
649 local buffer, err, part = interface.conn:receive( interface._pattern ) -- receive buffer with "pattern" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
650 --vdebug( "read data:", tostring(buffer), "error:", tostring(err), "part:", tostring(part) ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
651 buffer = buffer or part |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
652 if buffer and #buffer > cfg.MAX_READ_LENGTH then -- check buffer length |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
653 interface.fatalerror = "receive buffer exceeded" |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
654 debug( "fatal error:", interface.fatalerror ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
655 interface:_close() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
656 interface.eventread = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
657 return -1 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
658 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
659 if err and ( err ~= "timeout" and err ~= "wantread" ) then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
660 if "wantwrite" == err then -- need to read on write event |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
661 if not interface.eventwrite then -- register new write event if needed |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
662 interface.eventwrite = addevent( base, interface.conn, EV_WRITE, interface.writecallback, cfg.WRITE_TIMEOUT ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
663 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
664 interface.eventreadtimeout = addevent( base, nil, EV_TIMEOUT, |
7419
325c03f5481b
net.server_event: Collect entire one line function on one line
Kim Alvefur <zash@zash.se>
parents:
7399
diff
changeset
|
665 function( ) interface:_close() end, cfg.READ_TIMEOUT) |
8728
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8530
diff
changeset
|
666 debug( "wantwrite during read attempt, reg it in writecallback but don't know what really happens next..." ) |
41c959c5c84b
Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents:
8530
diff
changeset
|
667 -- to be honest i don't know what happens next, if it is allowed to first read, the write etc... |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
668 else -- connection was closed or fatal error |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
669 interface.fatalerror = err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
670 debug( "connection failed in read event:", interface.fatalerror ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
671 interface:_close() |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
672 interface.eventread = nil |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
673 return -1 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
674 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
675 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
676 interface.onincoming( interface, buffer, err ) -- send new data to listener |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
677 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
678 if interface.noreading then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
679 interface.eventread = nil; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
680 return -1; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
681 end |
7420
3fc5560557a5
net.server_event: Schedule another read callback if there is still data left in buffer after reading (fixes #583 for real)
Kim Alvefur <zash@zash.se>
parents:
7419
diff
changeset
|
682 if interface.conn:dirty() then -- still data left in buffer |
3fc5560557a5
net.server_event: Schedule another read callback if there is still data left in buffer after reading (fixes #583 for real)
Kim Alvefur <zash@zash.se>
parents:
7419
diff
changeset
|
683 return EV_TIMEOUT, cfg.READ_RETRY_DELAY; |
3fc5560557a5
net.server_event: Schedule another read callback if there is still data left in buffer after reading (fixes #583 for real)
Kim Alvefur <zash@zash.se>
parents:
7419
diff
changeset
|
684 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
685 return EV_READ, cfg.READ_TIMEOUT |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
686 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
687 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
688 client:settimeout( 0 ) -- set non blocking |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
689 setmetatable(interface, interface_mt) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
690 interfacelist[ interface ] = true -- add to interfacelist |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
691 return interface |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
692 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
693 |
9897
a471d4cfec5e
net/server_event: fix typo in comment
Maxime “pep” Buquet <pep@bouah.net>
parents:
9853
diff
changeset
|
694 local function handleserver( server, addr, port, pattern, listener, sslctx, startssl ) -- creates a server interface |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
695 debug "creating server interface..." |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
696 local interface = { |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
697 _connections = 0; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
698 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
699 type = "server"; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
700 conn = server; |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
701 onconnect = listener.onconnect; -- will be called when new client connected |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
702 eventread = false; -- read event handler |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
703 eventclose = false; -- close event handler |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
704 readcallback = false; -- read event callback |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
705 fatalerror = false; -- error message |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
706 nointerface = true; -- lock/unlock parameter |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
707 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
708 _ip = addr, _port = port, _pattern = pattern, |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
709 _sslctx = sslctx; |
9850
9a905888b96c
net.server_event: Add SNI support (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
710 hosts = {}; |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
711 } |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
712 interface.id = tostring(interface):match("%x+$"); |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
713 interface.readcallback = function( event ) -- server handler, called on incoming connections |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
714 --vdebug( "server can accept, id/addr/port:", interface, addr, port ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
715 if interface.fatalerror then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
716 --vdebug( "leaving this event because:", self.fatalerror ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
717 interface.eventread = nil |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
718 return -1 |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
719 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
720 local delay = cfg.ACCEPT_DELAY |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
721 if EV_TIMEOUT == event then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
722 if interface._connections >= cfg.MAX_CONNECTIONS then -- check connection count |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
723 debug( "to many connections, seconds to wait for next accept:", delay ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
724 return EV_TIMEOUT, delay -- timeout... |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
725 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
726 return EV_READ -- accept again |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
727 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
728 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
729 --vdebug("max connection check ok, accepting...") |
10547
c77471c180ea
net.server_event: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents:
10407
diff
changeset
|
730 -- luacheck: ignore 231/err |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
731 local client, err = server:accept() -- try to accept; TODO: check err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
732 while client do |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
733 if interface._connections >= cfg.MAX_CONNECTIONS then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
734 client:close( ) -- refuse connection |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
735 debug( "maximal connections reached, refuse client connection; accept delay:", delay ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
736 return EV_TIMEOUT, delay -- delay for next accept attempt |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
737 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
738 local client_ip, client_port = client:getpeername( ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
739 interface._connections = interface._connections + 1 -- increase connection count |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
740 local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, sslctx ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
741 --vdebug( "client id:", clientinterface, "startssl:", startssl ) |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
742 if has_luasec and startssl then |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
743 clientinterface:starttls(sslctx, true) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
744 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
745 clientinterface:_start_session( true ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
746 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
747 debug( "accepted incoming client connection from:", client_ip or "<unknown IP>", client_port or "<unknown port>", "to", port or "<unknown port>"); |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
748 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
749 client, err = server:accept() -- try to accept again |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
750 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
751 return EV_READ |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
752 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
753 |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
754 server:settimeout( 0 ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
755 setmetatable(interface, interface_mt) |
7224 | 756 interfacelist[ interface ] = true |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
757 interface:_start_session() |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
758 return interface |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
759 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
760 |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
761 local function listen(addr, port, listener, config) |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
762 config = config or {} |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
763 if config.sslctx and not has_luasec then |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
764 debug "fatal error: luasec not found" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
765 return nil, "luasec not found" |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
766 end |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
767 local server, err = socket.bind( addr, port, cfg.ACCEPT_QUEUE ) -- create server socket |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
768 if not server then |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
769 debug( "creating server socket on "..addr.." port "..port.." failed:", err ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
770 return nil, err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
771 end |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
772 local interface = handleserver( server, addr, port, config.read_size, listener, config.tls_ctx, config.tls_direct) -- new server handler |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
773 debug( "new server created with id:", tostring(interface)) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
774 return interface |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
775 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
776 |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
777 local function addserver( addr, port, listener, pattern, sslctx ) -- TODO: check arguments |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
778 --vdebug( "creating new tcp server with following parameters:", addr or "nil", port or "nil", sslctx or "nil", startssl or "nil") |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
779 return listen( addr, port, listener, { |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
780 read_size = pattern, |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
781 tls_ctx = sslctx, |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
782 tls_direct = not not sslctx, |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
783 }); |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
784 end |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
785 |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
786 local function wrapclient( client, ip, port, listeners, pattern, sslctx, extra ) |
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
787 local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx, extra ) |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
788 interface:_start_connection(sslctx) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
789 return interface, client |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
790 --function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
791 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
792 |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
793 local function addclient( addr, serverport, listener, pattern, sslctx, typ, extra ) |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
794 if sslctx and not has_luasec then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
795 debug "need luasec, but not available" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
796 return nil, "luasec not found" |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
797 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
798 if not typ then |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
799 local n = inet_pton(addr); |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
800 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
|
801 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
|
802 typ = "tcp6"; |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
803 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
|
804 typ = "tcp4"; |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
805 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
806 end |
9495
89e05b118f6e
net.server: Remove socket constructor fallback
Kim Alvefur <zash@zash.se>
parents:
9473
diff
changeset
|
807 local create = socket[typ]; |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
808 if type( create ) ~= "function" then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
809 return nil, "invalid socket type" |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
810 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
811 local client, err = create() -- creating new socket |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
812 if not client then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
813 debug( "cannot create socket:", err ) |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
814 return nil, err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
815 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
816 client:settimeout( 0 ) -- set nonblocking |
9499
36e379f058c8
net.server: Swich method for connecting sockets with remotes
Kim Alvefur <zash@zash.se>
parents:
9495
diff
changeset
|
817 local res, err = client:setpeername( addr, serverport ) -- connect |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
818 if res or ( err == "timeout" ) then |
10547
c77471c180ea
net.server_event: Silence luacheck warnings
Kim Alvefur <zash@zash.se>
parents:
10407
diff
changeset
|
819 -- luacheck: ignore 211/port |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
820 local ip, port = client:getsockname( ) |
11063
30d3f6f85eb8
net.server: Backport client parts of SNI support from trunk (#409)
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
821 local interface = wrapclient( client, ip, serverport, listener, pattern, sslctx, extra ) |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
822 debug( "new connection id:", interface.id ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
823 return interface, err |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
824 else |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
825 debug( "new connection failed:", err ) |
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
826 return nil, err |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
827 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
828 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
829 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
830 local function loop( ) -- starts the event loop |
2333
ed824115becf
net.server_event: Return "quitting" on loop exit to be compatible with net.server_select, and give Prosody a way to know why the loop broke
Matthew Wild <mwild1@gmail.com>
parents:
2318
diff
changeset
|
831 base:loop( ) |
ed824115becf
net.server_event: Return "quitting" on loop exit to be compatible with net.server_select, and give Prosody a way to know why the loop broke
Matthew Wild <mwild1@gmail.com>
parents:
2318
diff
changeset
|
832 return "quitting"; |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
833 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
834 |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
835 local function newevent( ... ) |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
836 return addevent( base, ... ) |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
837 end |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
838 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
839 local function closeallservers ( arg ) |
6849
0455b9686e16
server_event: Replace implementation of ordered set with a simple table set
Kim Alvefur <zash@zash.se>
parents:
6819
diff
changeset
|
840 for item in pairs( interfacelist ) do |
2334
4cedfeeaaed4
net.server_event: Fix closeallservers() to work correctly after the giant refactorizification
Matthew Wild <mwild1@gmail.com>
parents:
2333
diff
changeset
|
841 if item.type == "server" then |
4cedfeeaaed4
net.server_event: Fix closeallservers() to work correctly after the giant refactorizification
Matthew Wild <mwild1@gmail.com>
parents:
2333
diff
changeset
|
842 item:close( arg ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
843 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
844 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
845 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
846 |
2335
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
847 local function setquitting(yes) |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
848 if yes then |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
849 -- Quit now |
8751
469f78fe1c3e
net.server_event: Don't close servers if just quitting the loop once
Matthew Wild <mwild1@gmail.com>
parents:
8728
diff
changeset
|
850 if yes ~= "once" then |
469f78fe1c3e
net.server_event: Don't close servers if just quitting the loop once
Matthew Wild <mwild1@gmail.com>
parents:
8728
diff
changeset
|
851 closeallservers(); |
469f78fe1c3e
net.server_event: Don't close servers if just quitting the loop once
Matthew Wild <mwild1@gmail.com>
parents:
8728
diff
changeset
|
852 end |
7336
0d9ac472e58c
server_event: Fix indentation (again)
Kim Alvefur <zash@zash.se>
parents:
7224
diff
changeset
|
853 base:loopexit(); |
2335
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
854 end |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
855 end |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
856 |
4399
1b4161970842
net.server_event, net.server_select: Fixed some global pollution.
Waqas Hussain <waqas20@gmail.com>
parents:
4377
diff
changeset
|
857 local function get_backend() |
8314
1d2149d84c72
net.server_event: Add "libevent" to backend description
Kim Alvefur <zash@zash.se>
parents:
7810
diff
changeset
|
858 return "libevent " .. base:method(); |
2340
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
859 end |
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
860 |
2434
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
861 -- We need to hold onto the events to stop them |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
862 -- being garbage-collected |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
863 local signal_events = {}; -- [signal_num] -> event object |
4399
1b4161970842
net.server_event, net.server_select: Fixed some global pollution.
Waqas Hussain <waqas20@gmail.com>
parents:
4377
diff
changeset
|
864 local function hook_signal(signal_num, handler) |
6856
489f4ae291bf
server_event: Remove unused argument [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6855
diff
changeset
|
865 local function _handler() |
2434
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
866 local ret = handler(); |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
867 if ret ~= false then -- Continue handling this signal? |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
868 return EV_SIGNAL; -- Yes |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
869 end |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
870 return -1; -- Close this event |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
871 end |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
872 signal_events[signal_num] = base:addevent(signal_num, EV_SIGNAL, _handler); |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
873 return signal_events[signal_num]; |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
874 end |
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
875 |
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
|
876 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
|
877 local sender_locked; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
878 |
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
|
879 function receiver:ondrain() |
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
|
880 if sender_locked 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
|
881 sender:resume(); |
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
|
882 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
|
883 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
|
884 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
885 |
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
|
886 function sender:onincoming(data) |
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
|
887 receiver:write(data); |
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
|
888 if receiver.writebufferlen >= 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
|
889 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
|
890 sender:pause(); |
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
|
891 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
|
892 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
|
893 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
|
894 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
|
895 |
6862
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
896 local function add_task(delay, callback) |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
897 local event_handle; |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
898 event_handle = base:addevent(nil, 0, function () |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
899 local ret = callback(socket_gettime()); |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
900 if ret then |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
901 return 0, ret; |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
902 elseif event_handle then |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
903 return -1; |
6481
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6383
diff
changeset
|
904 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6383
diff
changeset
|
905 end |
6862
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
906 , delay); |
6929
03bc9e197fcc
net.server_event: Return event handle from add_task so it can be closed by eg util.timer
Kim Alvefur <zash@zash.se>
parents:
6862
diff
changeset
|
907 return event_handle; |
6481
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6383
diff
changeset
|
908 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6383
diff
changeset
|
909 |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
910 local function watchfd(fd, onreadable, onwriteable) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
911 local handle = {}; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
912 function handle:setflags(r,w) |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
913 if r ~= nil then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
914 if r and not self.wantread then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
915 self.wantread = base:addevent(fd, EV_READ, function () |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
916 onreadable(self); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
917 end); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
918 elseif not r and self.wantread then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
919 self.wantread:close(); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
920 self.wantread = nil; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
921 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
922 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
923 if w ~= nil then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
924 if w and not self.wantwrite then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
925 self.wantwrite = base:addevent(fd, EV_WRITE, function () |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
926 onwriteable(self); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
927 end); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
928 elseif not r and self.wantread then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
929 self.wantwrite:close(); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
930 self.wantwrite = nil; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
931 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
932 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
933 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
934 handle:setflags(onreadable, onwriteable); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
935 return handle; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
936 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
937 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
938 return { |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
939 cfg = cfg, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
940 base = base, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
941 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
|
942 link = link, |
6853
a2cc477eb19b
server_event: Rename luaevent module variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6852
diff
changeset
|
943 event = levent, |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
944 event_base = base, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
945 addevent = newevent, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
946 addserver = addserver, |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9581
diff
changeset
|
947 listen = listen, |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
948 addclient = addclient, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
949 wrapclient = wrapclient, |
2336
fb1d9d2444df
Rename closeallservers to closeall in the public API (same as net.server_select) and expose setquitting
Matthew Wild <mwild1@gmail.com>
parents:
2335
diff
changeset
|
950 setquitting = setquitting, |
fb1d9d2444df
Rename closeallservers to closeall in the public API (same as net.server_select) and expose setquitting
Matthew Wild <mwild1@gmail.com>
parents:
2335
diff
changeset
|
951 closeall = closeallservers, |
2340
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
952 get_backend = get_backend, |
2434
4403d4473783
net.server_event: Add server.hook_signal(signal_num, handler) to wrap libevent's signal-catching interface
Matthew Wild <mwild1@gmail.com>
parents:
2375
diff
changeset
|
953 hook_signal = hook_signal, |
6481
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6383
diff
changeset
|
954 add_task = add_task, |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8751
diff
changeset
|
955 watchfd = watchfd, |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
956 |
12481
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
12480
diff
changeset
|
957 tls_builder = function(basedir) |
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
12480
diff
changeset
|
958 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
|
959 end, |
2ee27587fec7
net: refactor sslconfig to not depend on LuaSec
Jonas Schäfer <jonas@wielicki.name>
parents:
12480
diff
changeset
|
960 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
961 __NAME = SCRIPT_NAME, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
962 __DATE = LAST_MODIFIED, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
963 __AUTHOR = SCRIPT_AUTHOR, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
964 __VERSION = SCRIPT_VERSION, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
965 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
966 } |