Software /
code /
prosody
Annotate
net/server_event.lua @ 7042:20ac6972b9aa
Merge 0.10->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 02 Jan 2016 20:42:53 +0000 |
parent | 6929:03bc9e197fcc |
parent | 7040:bdcea2dd33b8 |
child | 7225:a74f0b7be6bf |
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 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
8 -- you cant even register a new EV_READ/EV_WRITE callback inside another one |
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 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
10 -- dont let garbagecollect eventcallbacks, as long they are running |
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 |
2939
dc73cbc69bd5
net.server_event: Bump timeout values up somewhat
Matthew Wild <mwild1@gmail.com>
parents:
2731
diff
changeset
|
29 READ_TIMEOUT = 60 * 60 * 6, -- timeout in seconds for read data from socket |
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) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
33 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
|
34 } |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
35 |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 |
5286
0cbe57fbe2cd
net.server_event: Optimize socket write buffers (don't do buf=buf..newdata).
Waqas Hussain <waqas20@gmail.com>
parents:
5277
diff
changeset
|
42 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
|
43 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
|
44 local s_sub = string.sub |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
45 |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
46 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
|
47 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
|
48 |
6782
ec172dbe9d14
net.server_{select,event}: Don't rely on LuaSocket and LuaSec being present in the globals table
Kim Alvefur <zash@zash.se>
parents:
6382
diff
changeset
|
49 local has_luasec, ssl = 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
|
50 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
|
51 local levent = require "luaevent.core" |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
52 |
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
53 local socket_gettime = socket.gettime |
6819
ffb2b5e31456
net.server_{select,event}: addclient: Use getaddrinfo to detect IP address type if no socket type argument given. (Argument must be given for non-TCP)
Kim Alvefur <zash@zash.se>
parents:
6818
diff
changeset
|
54 local getaddrinfo = socket.dns.getaddrinfo |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
55 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
56 local log = require ("util.logger").init("socket") |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
57 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
58 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
|
59 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
|
60 end |
6855
40236602ab5b
server_event: Comment out the unused vdebug function [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6854
diff
changeset
|
61 -- 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
|
62 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 end |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 local limit = x > y and x or y |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
71 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
|
72 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
|
73 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
|
74 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
75 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
|
76 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
77 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
|
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 end )( ) |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
80 |
6853
a2cc477eb19b
server_event: Rename luaevent module variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6852
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
88 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
|
89 |
6849
0455b9686e16
server_event: Replace implementation of ordered set with a simple table set
Kim Alvefur <zash@zash.se>
parents:
6819
diff
changeset
|
90 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
|
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 -- Client interface methods |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
93 local interface_mt = {}; interface_mt.__index = interface_mt; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
94 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
95 -- Private methods |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
96 function interface_mt:_close() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
97 return self:_destroy(); |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
98 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
99 |
6861 | 100 function interface_mt:_start_connection(plainssl) -- called from wrapclient |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
101 local callback = function( event ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
102 if EV_TIMEOUT == event then -- timeout during connection |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
103 self.fatalerror = "connection timeout" |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
104 self:ontimeout() -- call timeout listener |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
105 self:_close() |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
106 debug( "new connection failed. id:", self.id, "error:", self.fatalerror ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
107 else |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
108 if plainssl and has_luasec then -- start ssl session |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
109 self:starttls(self._sslctx, true) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
110 else -- normal connection |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
111 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
|
112 end |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
113 debug( "new connection established. id:", self.id ) |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
114 end |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
115 self.eventconnect = nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
116 return -1 |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
117 end |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
118 self.eventconnect = addevent( base, self.conn, EV_WRITE, callback, cfg.CONNECT_TIMEOUT ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
119 return true |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
120 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
121 function interface_mt:_start_session(call_onconnect) -- new session, for example after startssl |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
122 if self.type == "client" then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
123 local callback = function( ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
124 self:_lock( false, false, false ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
125 --vdebug( "start listening on client socket with id:", self.id ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
126 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback, cfg.READ_TIMEOUT ); -- register callback |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
127 if call_onconnect then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
128 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
|
129 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
130 self.eventsession = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
131 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
|
132 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
133 self.eventsession = addevent( base, nil, EV_TIMEOUT, callback, 0 ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
134 else |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
135 self:_lock( false ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
136 --vdebug( "start listening on server socket with id:", self.id ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
137 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
|
138 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
139 return true |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
140 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
141 function interface_mt:_start_ssl(call_onconnect) -- old socket will be destroyed, therefore we have to close read/write events first |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
142 --vdebug( "starting ssl session with client id:", self.id ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
143 local _ |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
144 _ = self.eventread and self.eventread:close( ) -- close events; this must be called outside of the event callbacks! |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
145 _ = self.eventwrite and self.eventwrite:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
146 self.eventread, self.eventwrite = nil, nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
147 local err |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
148 self.conn, err = ssl.wrap( self.conn, self._sslctx ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
149 if err then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
150 self.fatalerror = err |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
151 self.conn = nil -- cannot be used anymore |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
152 if call_onconnect then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
153 self.ondisconnect = nil -- dont call this when client isnt really connected |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
154 end |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
155 self:_close() |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
156 debug( "fatal error while ssl wrapping:", err ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
157 return false |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
158 end |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
159 self.conn:settimeout( 0 ) -- set non blocking |
6859
88a997e08b70
server_event: Put function keyword on same line as the coroutine.wrap it's being passed to
Kim Alvefur <zash@zash.se>
parents:
6858
diff
changeset
|
160 local handshakecallback = coroutine_wrap(function( event ) |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
161 local _, err |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
162 local attempt = 0 |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
163 local maxattempt = cfg.MAX_HANDSHAKE_ATTEMPTS |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
164 while attempt < maxattempt do -- no endless loop |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
165 attempt = attempt + 1 |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
166 debug( "ssl handshake of client with id:"..tostring(self)..", attempt:"..attempt ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
167 if attempt > maxattempt then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
168 self.fatalerror = "max handshake attempts exceeded" |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
169 elseif EV_TIMEOUT == event then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
170 self.fatalerror = "timeout during handshake" |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
171 else |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
172 _, err = self.conn:dohandshake( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
173 if not err then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
174 self:_lock( false, false, false ) -- unlock the interface; sending, closing etc allowed |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
175 self.send = self.conn.send -- caching table lookups with new client object |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
176 self.receive = self.conn.receive |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
177 if not call_onconnect then -- trigger listener |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
178 self:onstatus("ssl-handshake-complete"); |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
179 end |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
180 self:_start_session( call_onconnect ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
181 debug( "ssl handshake done" ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
182 self.eventhandshake = nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
183 return -1 |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
184 end |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
185 if err == "wantwrite" then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
186 event = EV_WRITE |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
187 elseif err == "wantread" then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
188 event = EV_READ |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
189 else |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
190 debug( "ssl handshake error:", err ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
191 self.fatalerror = 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
|
192 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
193 end |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
194 if self.fatalerror then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
195 if call_onconnect then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
196 self.ondisconnect = nil -- dont call this when client isnt really connected |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
197 end |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
198 self:_close() |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
199 debug( "handshake failed because:", self.fatalerror ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
200 self.eventhandshake = nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
201 return -1 |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
202 end |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
203 event = coroutine_yield( event, cfg.HANDSHAKE_TIMEOUT ) -- yield this monster... |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
204 end |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
205 end |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
206 ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
207 debug "starting handshake..." |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
208 self:_lock( false, true, true ) -- unlock read/write events, but keep interface locked |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
209 self.eventhandshake = addevent( base, self.conn, EV_READWRITE, handshakecallback, cfg.HANDSHAKE_TIMEOUT ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
210 return true |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
211 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
212 function interface_mt:_destroy() -- close this interface + events and call last listener |
6858
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
213 debug( "closing client with id:", self.id, self.fatalerror ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
214 self:_lock( true, true, true ) -- first of all, lock the interface to avoid further actions |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
215 local _ |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
216 _ = self.eventread and self.eventread:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
217 if self.type == "client" then |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
218 _ = self.eventwrite and self.eventwrite:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
219 _ = self.eventhandshake and self.eventhandshake:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
220 _ = self.eventstarthandshake and self.eventstarthandshake:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
221 _ = self.eventconnect and self.eventconnect:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
222 _ = self.eventsession and self.eventsession:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
223 _ = self.eventwritetimeout and self.eventwritetimeout:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
224 _ = self.eventreadtimeout and self.eventreadtimeout:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
225 _ = self.ondisconnect and self:ondisconnect( self.fatalerror ~= "client to close" and self.fatalerror) -- call ondisconnect listener (wont be the case if handshake failed on connect) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
226 _ = self.conn and self.conn:close( ) -- close connection |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
227 _ = self._server and self._server:counter(-1); |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
228 self.eventread, self.eventwrite = nil, nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
229 self.eventstarthandshake, self.eventhandshake, self.eventclose = nil, nil, nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
230 self.readcallback, self.writecallback = nil, nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
231 else |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
232 self.conn:close( ) |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
233 self.eventread, self.eventclose = nil, nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
234 self.interface, self.readcallback = nil, nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
235 end |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
236 interfacelist[ self ] = nil |
d8f8c0b2fda8
server_event: Normalize indentation
Kim Alvefur <zash@zash.se>
parents:
6856
diff
changeset
|
237 return true |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
238 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
239 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
240 function interface_mt:_lock(nointerface, noreading, nowriting) -- lock or unlock this interface or events |
6861 | 241 self.nointerface, self.noreading, self.nowriting = nointerface, noreading, nowriting |
242 return nointerface, noreading, nowriting | |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
243 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
|
244 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
245 --TODO: Deprecate |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
246 function interface_mt:lock_read(switch) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
247 if switch then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
248 return self:pause(); |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
249 else |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
250 return self:resume(); |
3000
43a4cb707fe4
net.server_select, net.server_event: Support for :pause() and :resume() on connections
Matthew Wild <mwild1@gmail.com>
parents:
2988
diff
changeset
|
251 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
252 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
|
253 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
254 function interface_mt:pause() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
255 return self:_lock(self.nointerface, true, self.nowriting); |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
256 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
257 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
258 function interface_mt:resume() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
259 self:_lock(self.nointerface, false, self.nowriting); |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
260 if not self.eventread then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
261 self.eventread = addevent( base, self.conn, EV_READ, self.readcallback, cfg.READ_TIMEOUT ); -- register callback |
2708
d9c4ade80ae9
net.server_event: Add lock_read() method to connections
Matthew Wild <mwild1@gmail.com>
parents:
2660
diff
changeset
|
262 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
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 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
265 function interface_mt:counter(c) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
266 if c then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
267 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
|
268 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
269 return self._connections |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
270 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
271 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
272 -- Public methods |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
273 function interface_mt:write(data) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
274 if self.nowriting then return nil, "locked" end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
275 --vdebug( "try to send data to client, id/data:", self.id, data ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
276 data = tostring( data ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
277 local len = #data |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
278 local total = len + self.writebufferlen |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
279 if total > cfg.MAX_SEND_LENGTH then -- check buffer length |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
280 local err = "send buffer exceeded" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
281 debug( "error:", err ) -- to much, check your app |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
282 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
|
283 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
284 t_insert(self.writebuffer, data) -- new buffer |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
285 self.writebufferlen = total |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
286 if not self.eventwrite then -- register new write event |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
287 --vdebug( "register new write event" ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
288 self.eventwrite = addevent( base, self.conn, EV_WRITE, self.writecallback, cfg.WRITE_TIMEOUT ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
289 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
290 return true |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
291 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
292 function interface_mt:close() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
293 if self.nointerface then return nil, "locked"; end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
294 debug( "try to close client connection with id:", self.id ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
295 if self.type == "client" then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
296 self.fatalerror = "client to close" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
297 if self.eventwrite then -- wait for incomplete write request |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
298 self:_lock( true, true, false ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
299 debug "closing delayed until writebuffer is empty" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
300 return nil, "writebuffer not empty, waiting" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
301 else -- close now |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
302 self:_lock( true, true, true ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
303 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
|
304 return 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
|
305 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
306 else |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
307 debug( "try to close server with id:", tostring(self.id)) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
308 self.fatalerror = "server to close" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
309 self:_lock( true ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
310 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
|
311 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
|
312 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
313 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
314 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
315 function interface_mt:socket() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
316 return self.conn |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
317 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
318 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
319 function interface_mt:server() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
320 return self._server or self; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
321 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
322 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
323 function interface_mt:port() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
324 return self._port |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
325 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
326 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
327 function interface_mt:serverport() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
328 return self._serverport |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
329 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
330 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
331 function interface_mt:ip() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
332 return self._ip |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
333 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
334 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
335 function interface_mt:ssl() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
336 return self._usingssl |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
337 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
338 interface_mt.clientport = interface_mt.port -- COMPAT server_select |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
339 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
340 function interface_mt:type() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
341 return self._type or "client" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
342 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
343 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
344 function interface_mt:connections() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
345 return self._connections |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
346 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
347 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
348 function interface_mt:address() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
349 return self.addr |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
350 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
351 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
352 function interface_mt:set_sslctx(sslctx) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
353 self._sslctx = sslctx; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
354 if sslctx then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
355 self.starttls = nil; -- use starttls() of interface_mt |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
356 else |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
357 self.starttls = false; -- prevent starttls() |
2534
1f46a9d9b16a
net.server_event: Support :setlistener() method on connections to work with port multiplexing
Matthew Wild <mwild1@gmail.com>
parents:
2478
diff
changeset
|
358 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
359 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
|
360 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
361 function interface_mt:set_mode(pattern) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
362 if pattern then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
363 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
|
364 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
365 return self._pattern; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
366 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
367 |
6852
626d8152b1ad
server_event: Add luacheck annotations
Kim Alvefur <zash@zash.se>
parents:
6851
diff
changeset
|
368 function interface_mt:set_send(new_send) -- luacheck: ignore 212 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
369 -- No-op, we always use the underlying connection's send |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
370 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
371 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
372 function interface_mt:starttls(sslctx, call_onconnect) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
373 debug( "try to start ssl at client id:", self.id ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
374 local err |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
375 self._sslctx = sslctx; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
376 if self._usingssl then -- startssl was already called |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
377 err = "ssl already active" |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
378 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
379 if err then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
380 debug( "error:", err ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
381 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
|
382 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
383 self._usingssl = true |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
384 self.startsslcallback = function( ) -- we have to start the handshake outside of a read/write event |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
385 self.startsslcallback = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
386 self:_start_ssl(call_onconnect); |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
387 self.eventstarthandshake = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
388 return -1 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
389 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
390 if not self.eventwrite then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
391 self:_lock( true, true, true ) -- lock the interface, to not disturb the handshake |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
392 self.eventstarthandshake = addevent( base, nil, EV_TIMEOUT, self.startsslcallback, 0 ) -- add event to start handshake |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
393 else -- wait until writebuffer is empty |
6860 | 394 self:_lock( true, true, false ) |
395 debug "ssl session delayed until writebuffer is empty..." | |
396 end | |
397 self.starttls = false; | |
398 return true | |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
399 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
400 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
401 function interface_mt:setoption(option, value) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
402 if self.conn.setoption then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
403 return self.conn:setoption(option, value); |
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
|
404 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
405 return false, "setoption not implemented"; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
406 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
407 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
408 function interface_mt:setlistener(listener) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
409 self:ondetach(); -- Notify listener that it is no longer responsible for this connection |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
410 self.onconnect, self.ondisconnect, self.onincoming, self.ontimeout, |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
411 self.onreadtimeout, self.onstatus, self.ondetach |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
412 = listener.onconnect, listener.ondisconnect, listener.onincoming, listener.ontimeout, |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
413 listener.onreadtimeout, listener.onstatus, listener.ondetach; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
414 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
415 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
416 -- Stub handlers |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
417 function interface_mt:onconnect() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
418 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
419 function interface_mt:onincoming() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
420 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
421 function interface_mt:ondisconnect() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
422 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
423 function interface_mt:ontimeout() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
424 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
425 function interface_mt:onreadtimeout() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
426 self.fatalerror = "timeout during receiving" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
427 debug( "connection failed:", self.fatalerror ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
428 self:_close() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
429 self.eventread = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
430 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
431 function interface_mt:ondrain() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
432 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
433 function interface_mt:ondetach() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
434 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
435 function interface_mt:onstatus() |
2535
75bb31b6c53f
net.server_event: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2534
diff
changeset
|
436 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
|
437 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
438 -- 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
|
439 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
440 local function handleclient( client, ip, port, server, pattern, listener, sslctx ) -- creates an client interface |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
441 --vdebug("creating client interfacce...") |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
442 local interface = { |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
443 type = "client"; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
444 conn = client; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
445 currenttime = socket_gettime( ); -- safe the origin |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
446 writebuffer = {}; -- writebuffer |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
447 writebufferlen = 0; -- length of writebuffer |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
448 send = client.send; -- caching table lookups |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
449 receive = client.receive; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
450 onconnect = listener.onconnect; -- will be called when client disconnects |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
451 ondisconnect = listener.ondisconnect; -- will be called when client disconnects |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
452 onincoming = listener.onincoming; -- will be called when client sends data |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
453 ontimeout = listener.ontimeout; -- called when fatal socket timeout occurs |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
454 onreadtimeout = listener.onreadtimeout; -- called when socket inactivity timeout occurs |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
455 ondrain = listener.ondrain; -- called when writebuffer is empty |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
456 ondetach = listener.ondetach; -- called when disassociating this listener from this connection |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
457 onstatus = listener.onstatus; -- called for status changes (e.g. of SSL/TLS) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
458 eventread = false, eventwrite = false, eventclose = false, |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
459 eventhandshake = false, eventstarthandshake = false; -- event handler |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
460 eventconnect = false, eventsession = false; -- more event handler... |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
461 eventwritetimeout = false; -- even more event handler... |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
462 eventreadtimeout = false; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
463 fatalerror = false; -- error message |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
464 writecallback = false; -- will be called on write events |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
465 readcallback = false; -- will be called on read events |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
466 nointerface = true; -- lock/unlock parameter of this interface |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
467 noreading = false, nowriting = false; -- locks of the read/writecallback |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
468 startsslcallback = false; -- starting handshake callback |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
469 position = false; -- position of client in interfacelist |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
470 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
471 -- Properties |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
472 _ip = ip, _port = port, _server = server, _pattern = pattern, |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
473 _serverport = (server and server:port() or nil), |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
474 _sslctx = sslctx; -- parameters |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
475 _usingssl = false; -- client is using ssl; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
476 } |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
477 if not has_luasec then interface.starttls = false; end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
478 interface.id = tostring(interface):match("%x+$"); |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
479 interface.writecallback = function( event ) -- called on write events |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
480 --vdebug( "new client write event, id/ip/port:", interface, ip, port ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
481 if interface.nowriting or ( interface.fatalerror and ( "client to close" ~= interface.fatalerror ) ) then -- leave this event |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
482 --vdebug( "leaving this event because:", interface.nowriting or interface.fatalerror ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
483 interface.eventwrite = false |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
484 return -1 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
485 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
486 if EV_TIMEOUT == event then -- took too long to write some data to socket -> disconnect |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
487 interface.fatalerror = "timeout during writing" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
488 debug( "writing failed:", interface.fatalerror ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
489 interface:_close() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
490 interface.eventwrite = false |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
491 return -1 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
492 else -- can write :) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
493 if interface._usingssl then -- handle luasec |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
494 if interface.eventreadtimeout then -- we have to read first |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
495 local ret = interface.readcallback( ) -- call readcallback |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
496 --vdebug( "tried to read in writecallback, result:", ret ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
497 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
498 if interface.eventwritetimeout then -- luasec only |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
499 interface.eventwritetimeout:close( ) -- first we have to close timeout event which where regged after a wantread error |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
500 interface.eventwritetimeout = false |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
501 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
502 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
503 interface.writebuffer = { t_concat(interface.writebuffer) } |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
504 local succ, err, byte = interface.conn:send( interface.writebuffer[1], 1, interface.writebufferlen ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
505 --vdebug( "write data:", interface.writebuffer, "error:", err, "part:", byte ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
506 if succ then -- writing succesful |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
507 interface.writebuffer[1] = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
508 interface.writebufferlen = 0 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
509 interface:ondrain(); |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
510 if interface.fatalerror then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
511 debug "closing client after writing" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
512 interface:_close() -- close interface if needed |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
513 elseif interface.startsslcallback then -- start ssl connection if needed |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
514 debug "starting ssl handshake after writing" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
515 interface.eventstarthandshake = addevent( base, nil, EV_TIMEOUT, interface.startsslcallback, 0 ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
516 elseif interface.eventreadtimeout then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
517 return EV_WRITE, EV_TIMEOUT |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
518 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
519 interface.eventwrite = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
520 return -1 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
521 elseif byte and (err == "timeout" or err == "wantwrite") then -- want write again |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
522 --vdebug( "writebuffer is not empty:", err ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
523 interface.writebuffer[1] = s_sub( interface.writebuffer[1], byte + 1, interface.writebufferlen ) -- new buffer |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
524 interface.writebufferlen = interface.writebufferlen - byte |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
525 if "wantread" == err then -- happens only with luasec |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
526 local callback = function( ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
527 interface:_close() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
528 interface.eventwritetimeout = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
529 return -1; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
530 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
531 interface.eventwritetimeout = addevent( base, nil, EV_TIMEOUT, callback, cfg.WRITE_TIMEOUT ) -- reg a new timeout event |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
532 debug( "wantread during write attempt, reg it in readcallback but dont know what really happens next..." ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
533 -- hopefully this works with luasec; its simply not possible to use 2 different write events on a socket in luaevent |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
534 return -1 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
535 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
536 return EV_WRITE, cfg.WRITE_TIMEOUT |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
537 else -- connection was closed during writing or fatal error |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
538 interface.fatalerror = err or "fatal error" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
539 debug( "connection failed in write event:", interface.fatalerror ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
540 interface:_close() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
541 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
|
542 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
|
543 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
544 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
545 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
546 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
547 interface.readcallback = function( event ) -- called on read events |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
548 --vdebug( "new client read event, id/ip/port:", tostring(interface.id), tostring(ip), tostring(port) ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
549 if interface.noreading or interface.fatalerror then -- leave this event |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
550 --vdebug( "leaving this event because:", tostring(interface.noreading or interface.fatalerror) ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
551 interface.eventread = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
552 return -1 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
553 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
554 if EV_TIMEOUT == event and interface:onreadtimeout() ~= true then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
555 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
|
556 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
557 if interface._usingssl then -- handle luasec |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
558 if interface.eventwritetimeout then -- ok, in the past writecallback was regged |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
559 local ret = interface.writecallback( ) -- call it |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
560 --vdebug( "tried to write in readcallback, result:", tostring(ret) ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
561 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
562 if interface.eventreadtimeout then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
563 interface.eventreadtimeout:close( ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
564 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
|
565 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
566 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
567 local buffer, err, part = interface.conn:receive( interface._pattern ) -- receive buffer with "pattern" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
568 --vdebug( "read data:", tostring(buffer), "error:", tostring(err), "part:", tostring(part) ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
569 buffer = buffer or part |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
570 if buffer and #buffer > cfg.MAX_READ_LENGTH then -- check buffer length |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
571 interface.fatalerror = "receive buffer exceeded" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
572 debug( "fatal error:", interface.fatalerror ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
573 interface:_close() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
574 interface.eventread = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
575 return -1 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
576 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
577 if err and ( err ~= "timeout" and err ~= "wantread" ) then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
578 if "wantwrite" == err then -- need to read on write event |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
579 if not interface.eventwrite then -- register new write event if needed |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
580 interface.eventwrite = addevent( base, interface.conn, EV_WRITE, interface.writecallback, cfg.WRITE_TIMEOUT ) |
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
|
581 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
582 interface.eventreadtimeout = addevent( base, nil, EV_TIMEOUT, |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
583 function( ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
584 interface:_close() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
585 end, cfg.READ_TIMEOUT |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
586 ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
587 debug( "wantwrite during read attempt, reg it in writecallback but dont know what really happens next..." ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
588 -- to be honest i dont know what happens next, if it is allowed to first read, the write etc... |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
589 else -- connection was closed or fatal error |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
590 interface.fatalerror = err |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
591 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
|
592 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
|
593 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
|
594 return -1 |
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
|
595 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
596 else |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
597 interface.onincoming( interface, buffer, err ) -- send new data to listener |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
598 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
599 if interface.noreading then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
600 interface.eventread = nil; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
601 return -1; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
602 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
603 return EV_READ, cfg.READ_TIMEOUT |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
604 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
605 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
606 client:settimeout( 0 ) -- set non blocking |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
607 setmetatable(interface, interface_mt) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
608 interfacelist[ interface ] = true -- add to interfacelist |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
609 return interface |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
610 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
611 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
612 local function handleserver( server, addr, port, pattern, listener, sslctx ) -- creates an server interface |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
613 debug "creating server interface..." |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
614 local interface = { |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
615 _connections = 0; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
616 |
7040
bdcea2dd33b8
net.server_event: Add conn.type == 'server' for server sockets (fixes closeallservers() )
Matthew Wild <mwild1@gmail.com>
parents:
6860
diff
changeset
|
617 type = "server"; |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
618 conn = server; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
619 onconnect = listener.onconnect; -- will be called when new client connected |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
620 eventread = false; -- read event handler |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
621 eventclose = false; -- close event handler |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
622 readcallback = false; -- read event callback |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
623 fatalerror = false; -- error message |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
624 nointerface = true; -- lock/unlock parameter |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
625 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
626 _ip = addr, _port = port, _pattern = pattern, |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
627 _sslctx = sslctx; |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
628 } |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
629 interface.id = tostring(interface):match("%x+$"); |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
630 interface.readcallback = function( event ) -- server handler, called on incoming connections |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
631 --vdebug( "server can accept, id/addr/port:", interface, addr, port ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
632 if interface.fatalerror then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
633 --vdebug( "leaving this event because:", self.fatalerror ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
634 interface.eventread = nil |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
635 return -1 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
636 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
637 local delay = cfg.ACCEPT_DELAY |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
638 if EV_TIMEOUT == event then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
639 if interface._connections >= cfg.MAX_CONNECTIONS then -- check connection count |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
640 debug( "to many connections, seconds to wait for next accept:", delay ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
641 return EV_TIMEOUT, delay -- timeout... |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
642 else |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
643 return EV_READ -- accept again |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
644 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
645 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
646 --vdebug("max connection check ok, accepting...") |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
647 local client, err = server:accept() -- try to accept; TODO: check err |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
648 while client do |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
649 if interface._connections >= cfg.MAX_CONNECTIONS then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
650 client:close( ) -- refuse connection |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
651 debug( "maximal connections reached, refuse client connection; accept delay:", delay ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
652 return EV_TIMEOUT, delay -- delay for next accept attempt |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
653 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
654 local client_ip, client_port = client:getpeername( ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
655 interface._connections = interface._connections + 1 -- increase connection count |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
656 local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, sslctx ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
657 --vdebug( "client id:", clientinterface, "startssl:", startssl ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
658 if has_luasec and sslctx then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
659 clientinterface:starttls(sslctx, true) |
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
|
660 else |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
661 clientinterface:_start_session( true ) |
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
|
662 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
663 debug( "accepted incoming client connection from:", client_ip or "<unknown IP>", client_port or "<unknown port>", "to", port or "<unknown port>"); |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
664 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
665 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
|
666 end |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
667 return EV_READ |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
668 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
669 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
670 server:settimeout( 0 ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
671 setmetatable(interface, interface_mt) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
672 interfacelist[ interface ] = true |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
673 interface:_start_session() |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
674 return interface |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
675 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
676 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
677 local function addserver( addr, port, listener, pattern, sslctx, startssl ) -- TODO: check arguments |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
678 --vdebug( "creating new tcp server with following parameters:", addr or "nil", port or "nil", sslctx or "nil", startssl or "nil") |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
679 if sslctx and not has_luasec then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
680 debug "fatal error: luasec not found" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
681 return nil, "luasec not found" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
682 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
683 local server, err = socket.bind( addr, port, cfg.ACCEPT_QUEUE ) -- create server socket |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
684 if not server then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
685 debug( "creating server socket on "..addr.." port "..port.." failed:", err ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
686 return nil, err |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
687 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
688 local interface = handleserver( server, addr, port, pattern, listener, sslctx, startssl ) -- new server handler |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
689 debug( "new server created with id:", tostring(interface)) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
690 return interface |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
691 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
|
692 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
693 local function wrapclient( client, ip, port, listeners, pattern, sslctx ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
694 local interface = handleclient( client, ip, port, nil, pattern, listeners, sslctx ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
695 interface:_start_connection(sslctx) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
696 return interface, client |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
697 --function handleclient( client, ip, port, server, pattern, listener, _, sslctx ) -- creates an client interface |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
698 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
699 |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
700 local function addclient( addr, serverport, listener, pattern, sslctx, typ ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
701 if sslctx and not has_luasec then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
702 debug "need luasec, but not available" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
703 return nil, "luasec not found" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
704 end |
6861 | 705 if getaddrinfo and not typ then |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
706 local addrinfo, err = getaddrinfo(addr) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
707 if not addrinfo then return nil, err end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
708 if addrinfo[1] and addrinfo[1].family == "inet6" then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
709 typ = "tcp6" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
710 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
711 end |
6861 | 712 local create = socket[typ or "tcp"] |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
713 if type( create ) ~= "function" then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
714 return nil, "invalid socket type" |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
715 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
716 local client, err = create() -- creating new socket |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
717 if not client then |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
718 debug( "cannot create socket:", err ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
719 return nil, err |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
720 end |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
721 client:settimeout( 0 ) -- set nonblocking |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
722 local res, err = client:connect( addr, serverport ) -- connect |
6861 | 723 if res or ( err == "timeout" or err == "Operation already in progress" ) then |
724 if client.getsockname then | |
725 addr = client:getsockname( ) | |
726 end | |
727 local interface = wrapclient( client, addr, serverport, listener, pattern, sslctx ) | |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
728 debug( "new connection id:", interface.id ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
729 return interface, err |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
730 else |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
731 debug( "new connection failed:", err ) |
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
732 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
|
733 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
734 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
735 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
736 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
|
737 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
|
738 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
|
739 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
740 |
6850
41de00647ad3
server_event: Move local references to various functions to top of file
Kim Alvefur <zash@zash.se>
parents:
6849
diff
changeset
|
741 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
|
742 return addevent( base, ... ) |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
743 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
744 |
6851
1f1bed8ebc41
server_event: Remove needless scoping and indentation
Kim Alvefur <zash@zash.se>
parents:
6850
diff
changeset
|
745 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
|
746 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
|
747 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
|
748 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
|
749 end |
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 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
751 end |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
752 |
2335
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
753 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
|
754 if yes then |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
755 -- Quit now |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
756 closeallservers(); |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
757 base:loopexit(); |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
758 end |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
759 end |
060fb67cefd5
net.server_event: Add setquitting() function to be compatible with net.server_select
Matthew Wild <mwild1@gmail.com>
parents:
2334
diff
changeset
|
760 |
4399
1b4161970842
net.server_event, net.server_select: Fixed some global pollution.
Waqas Hussain <waqas20@gmail.com>
parents:
4377
diff
changeset
|
761 local function get_backend() |
2340
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
762 return base:method(); |
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
763 end |
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
764 |
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
|
765 -- 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
|
766 -- 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
|
767 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
|
768 local function hook_signal(signal_num, handler) |
6856
489f4ae291bf
server_event: Remove unused argument [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6855
diff
changeset
|
769 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
|
770 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
|
771 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
|
772 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
|
773 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
|
774 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
|
775 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
|
776 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
|
777 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
|
778 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
|
779 |
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
|
780 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
|
781 local sender_locked; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
782 |
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
|
783 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
|
784 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
|
785 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
|
786 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
|
787 end |
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
788 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5756
diff
changeset
|
789 |
3003
48871a549b4c
net.server_select, net.server_event: Add server.link() to link 2 connections with an intermediate buffer of the specified size
Matthew Wild <mwild1@gmail.com>
parents:
3002
diff
changeset
|
790 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
|
791 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
|
792 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
|
793 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
|
794 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
|
795 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
|
796 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
|
797 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
|
798 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
|
799 |
6862
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
800 local function add_task(delay, callback) |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
801 local event_handle; |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
802 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
|
803 local ret = callback(socket_gettime()); |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
804 if ret then |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
805 return 0, ret; |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
806 elseif event_handle then |
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
807 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
|
808 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6383
diff
changeset
|
809 end |
6862
7a81fb23de5d
server_event: Remove needless scoping around add_task
Kim Alvefur <zash@zash.se>
parents:
6861
diff
changeset
|
810 , 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
|
811 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
|
812 end |
dbc72cd1332e
Move timer code out of util.timer and into relevant net.server backends
daurnimator <quae@daurnimator.com>
parents:
6383
diff
changeset
|
813 |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
814 return { |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
815 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
|
816 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
|
817 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
|
818 link = link, |
6853
a2cc477eb19b
server_event: Rename luaevent module variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6852
diff
changeset
|
819 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
|
820 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
|
821 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
|
822 addserver = addserver, |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
823 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
|
824 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
|
825 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
|
826 closeall = closeallservers, |
2340
b286d60cb4fc
net.server_event: Add get_backend() method
Matthew Wild <mwild1@gmail.com>
parents:
2336
diff
changeset
|
827 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
|
828 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
|
829 add_task = add_task, |
2318
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
830 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
831 __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
|
832 __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
|
833 __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
|
834 __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
|
835 |
a831de056de3
net.server_select, net.server_event: Convert to unix line endings from Windows
Matthew Wild <mwild1@gmail.com>
parents:
2303
diff
changeset
|
836 } |