Software /
code /
prosody
Annotate
net/server.lua @ 440:dee02bf4656a
Some mod_console updates
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 27 Nov 2008 17:09:21 +0000 |
parent | 374:ff502dada372 |
child | 519:cccd610a0ef9 |
rev | line source |
---|---|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
1 --[[ |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
2 |
64 | 3 server.lua by blastbeat of the luadch project |
4 | |
5 re-used here under the MIT/X Consortium License | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
6 |
64 | 7 - this script contains the server loop of the program |
8 - other scripts can reg a server here | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
9 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
10 ]]-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
11 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
12 ----------------------------------// DECLARATION //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
13 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
14 --// constants //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
15 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
16 local STAT_UNIT = 1 / ( 1024 * 1024 ) -- mb |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
17 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
18 --// lua functions //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
19 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
20 local function use( what ) return _G[ what ] end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
21 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
22 local type = use "type" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
23 local pairs = use "pairs" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
24 local ipairs = use "ipairs" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
25 local tostring = use "tostring" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
26 local collectgarbage = use "collectgarbage" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
27 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
28 --// lua libs //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
29 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
30 local table = use "table" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
31 local coroutine = use "coroutine" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
32 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
33 --// lua lib methods //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
34 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
35 local table_concat = table.concat |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
36 local table_remove = table.remove |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
37 local string_sub = use'string'.sub |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
38 local coroutine_wrap = coroutine.wrap |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
39 local coroutine_yield = coroutine.yield |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
40 local print = print; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
41 local out_put = function () end --print; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
42 local out_error = print; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
43 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
44 --// extern libs //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
45 |
98
3a2d327c4856
server.lua should degrade gracefully when LuaSec not present
Matthew Wild <mwild1@gmail.com>
parents:
74
diff
changeset
|
46 local luasec = select(2, pcall(require, "ssl")) |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
47 local luasocket = require "socket" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
48 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
49 --// extern lib methods //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
50 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
51 local ssl_wrap = ( luasec and luasec.wrap ) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
52 local socket_bind = luasocket.bind |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
53 local socket_select = luasocket.select |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
54 local ssl_newcontext = ( luasec and luasec.newcontext ) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
55 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
56 --// functions //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
57 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
58 local loop |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
59 local stats |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
60 local addtimer |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
61 local closeall |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
62 local addserver |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
63 local firetimer |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
64 local closesocket |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
65 local removesocket |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
66 local wrapserver |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
67 local wraptcpclient |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
68 local wrapsslclient |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
69 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
70 --// tables //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
71 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
72 local listener |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
73 local readlist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
74 local writelist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
75 local socketlist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
76 local timelistener |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
77 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
78 --// simple data types //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
79 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
80 local _ |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
81 local readlen = 0 -- length of readlist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
82 local writelen = 0 -- lenght of writelist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
83 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
84 local sendstat= 0 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
85 local receivestat = 0 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
86 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
87 ----------------------------------// DEFINITION //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
88 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
89 listener = { } -- key = port, value = table |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
90 readlist = { } -- array with sockets to read from |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
91 writelist = { } -- arrary with sockets to write to |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
92 socketlist = { } -- key = socket, value = wrapped socket |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
93 timelistener = { } |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
94 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
95 stats = function( ) |
64 | 96 return receivestat, sendstat |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
97 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
98 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
99 wrapserver = function( listener, socket, ip, serverport, mode, sslctx ) -- this function wraps a server |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
100 |
64 | 101 local dispatch, disconnect = listener.listener, listener.disconnect -- dangerous |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
102 |
64 | 103 local wrapclient, err |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
104 |
64 | 105 if sslctx then |
106 if not ssl_newcontext then | |
107 return nil, "luasec not found" | |
108 end | |
109 if type( sslctx ) ~= "table" then | |
110 out_error "server.lua: wrong server sslctx" | |
111 return nil, "wrong server sslctx" | |
112 end | |
113 sslctx, err = ssl_newcontext( sslctx ) | |
114 if not sslctx then | |
115 err = err or "wrong sslctx parameters" | |
116 out_error( "server.lua: ", err ) | |
117 return nil, err | |
118 end | |
119 wrapclient = wrapsslclient | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
120 wrapclient = wraptlsclient |
64 | 121 else |
122 wrapclient = wraptcpclient | |
123 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
124 |
64 | 125 local accept = socket.accept |
126 local close = socket.close | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
127 |
64 | 128 --// public methods of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
129 |
64 | 130 local handler = { } |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
131 |
64 | 132 handler.shutdown = function( ) end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
133 |
64 | 134 --[[handler.listener = function( data, err ) |
135 return ondata( handler, data, err ) | |
136 end]] | |
137 handler.ssl = function( ) | |
138 return sslctx and true or false | |
139 end | |
140 handler.close = function( closed ) | |
141 _ = not closed and close( socket ) | |
142 writelen = removesocket( writelist, socket, writelen ) | |
143 readlen = removesocket( readlist, socket, readlen ) | |
144 socketlist[ socket ] = nil | |
145 handler = nil | |
146 end | |
147 handler.ip = function( ) | |
148 return ip | |
149 end | |
150 handler.serverport = function( ) | |
151 return serverport | |
152 end | |
153 handler.socket = function( ) | |
154 return socket | |
155 end | |
156 handler.receivedata = function( ) | |
157 local client, err = accept( socket ) -- try to accept | |
158 if client then | |
159 local ip, clientport = client:getpeername( ) | |
160 client:settimeout( 0 ) | |
161 local handler, client, err = wrapclient( listener, client, ip, serverport, clientport, mode, sslctx ) -- wrap new client socket | |
162 if err then -- error while wrapping ssl socket | |
163 return false | |
164 end | |
165 out_put( "server.lua: accepted new client connection from ", ip, ":", clientport ) | |
166 return dispatch( handler ) | |
167 elseif err then -- maybe timeout or something else | |
168 out_put( "server.lua: error with new client connection: ", err ) | |
169 return false | |
170 end | |
171 end | |
172 return handler | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
173 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
174 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
175 wrapsslclient = function( listener, socket, ip, serverport, clientport, mode, sslctx ) -- this function wraps a ssl cleint |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
176 |
64 | 177 local dispatch, disconnect = listener.listener, listener.disconnect |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
178 |
64 | 179 --// transform socket to ssl object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
180 |
64 | 181 local err |
182 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket | |
183 if err then | |
184 out_put( "server.lua: ssl error: ", err ) | |
185 return nil, nil, err -- fatal error | |
186 end | |
187 socket:settimeout( 0 ) | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
188 |
64 | 189 --// private closures of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
190 |
64 | 191 local writequeue = { } -- buffer for messages to send |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
192 |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
193 local eol, fatal_send_error -- end of buffer |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
194 |
64 | 195 local sstat, rstat = 0, 0 |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
196 |
64 | 197 --// local import of socket methods //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
198 |
64 | 199 local send = socket.send |
200 local receive = socket.receive | |
201 local close = socket.close | |
202 --local shutdown = socket.shutdown | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
203 |
64 | 204 --// public methods of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
205 |
64 | 206 local handler = { } |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
207 |
64 | 208 handler.getstats = function( ) |
209 return rstat, sstat | |
210 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
211 |
64 | 212 handler.listener = function( data, err ) |
213 return listener( handler, data, err ) | |
214 end | |
215 handler.ssl = function( ) | |
216 return true | |
217 end | |
218 handler.send = function( _, data, i, j ) | |
219 return send( socket, data, i, j ) | |
220 end | |
221 handler.receive = function( pattern, prefix ) | |
222 return receive( socket, pattern, prefix ) | |
223 end | |
224 handler.shutdown = function( pattern ) | |
225 --return shutdown( socket, pattern ) | |
226 end | |
227 handler.close = function( closed ) | |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
228 if eol and not fatal_send_error then handler._dispatchdata(); end |
64 | 229 close( socket ) |
230 writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen | |
231 readlen = removesocket( readlist, socket, readlen ) | |
232 socketlist[ socket ] = nil | |
233 out_put "server.lua: closed handler and removed socket from list" | |
234 end | |
235 handler.ip = function( ) | |
236 return ip | |
237 end | |
238 handler.serverport = function( ) | |
239 return serverport | |
240 end | |
241 handler.clientport = function( ) | |
242 return clientport | |
243 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
244 |
64 | 245 handler.write = function( data ) |
246 if not eol then | |
247 writelen = writelen + 1 | |
248 writelist[ writelen ] = socket | |
249 eol = 0 | |
250 end | |
251 eol = eol + 1 | |
252 writequeue[ eol ] = data | |
253 end | |
254 handler.writequeue = function( ) | |
255 return writequeue | |
256 end | |
257 handler.socket = function( ) | |
258 return socket | |
259 end | |
260 handler.mode = function( ) | |
261 return mode | |
262 end | |
263 handler._receivedata = function( ) | |
264 local data, err, part = receive( socket, mode ) -- receive data in "mode" | |
265 if not err or ( err == "timeout" or err == "wantread" ) then -- received something | |
266 local data = data or part or "" | |
267 local count = #data * STAT_UNIT | |
268 rstat = rstat + count | |
269 receivestat = receivestat + count | |
270 out_put( "server.lua: read data '", data, "', error: ", err ) | |
271 return dispatch( handler, data, err ) | |
272 else -- connections was closed or fatal error | |
273 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) | |
274 handler.close( ) | |
275 disconnect( handler, err ) | |
276 writequeue = nil | |
277 handler = nil | |
278 return false | |
279 end | |
280 end | |
281 handler._dispatchdata = function( ) -- this function writes data to handlers | |
282 local buffer = table_concat( writequeue, "", 1, eol ) | |
283 local succ, err, byte = send( socket, buffer ) | |
284 local count = ( succ or 0 ) * STAT_UNIT | |
285 sstat = sstat + count | |
286 sendstat = sendstat + count | |
287 out_put( "server.lua: sended '", buffer, "', bytes: ", succ, ", error: ", err, ", part: ", byte, ", to: ", ip, ":", clientport ) | |
288 if succ then -- sending succesful | |
289 --writequeue = { } | |
290 eol = nil | |
291 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist | |
292 return true | |
293 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write | |
294 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer | |
295 writequeue[ 1 ] = buffer -- insert new buffer in queue | |
296 eol = 1 | |
297 return true | |
298 else -- connection was closed during sending or fatal error | |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
299 fatal_send_error = true; |
64 | 300 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) |
301 handler.close( ) | |
302 disconnect( handler, err ) | |
303 writequeue = nil | |
304 handler = nil | |
305 return false | |
306 end | |
307 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
308 |
64 | 309 -- // COMPAT // -- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
310 |
64 | 311 handler.getIp = handler.ip |
312 handler.getPort = handler.clientport | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
313 |
64 | 314 --// handshake //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
315 |
64 | 316 local wrote |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
317 |
64 | 318 handler.handshake = coroutine_wrap( function( client ) |
319 local err | |
320 for i = 1, 10 do -- 10 handshake attemps | |
321 _, err = client:dohandshake( ) | |
322 if not err then | |
323 out_put( "server.lua: ssl handshake done" ) | |
324 writelen = ( wrote and removesocket( writelist, socket, writelen ) ) or writelen | |
325 handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions | |
326 handler.dispatchdata = handler._dispatchdata | |
327 return dispatch( handler ) | |
328 else | |
329 out_put( "server.lua: error during ssl handshake: ", err ) | |
330 if err == "wantwrite" then | |
331 if wrote == nil then | |
332 writelen = writelen + 1 | |
333 writelist[ writelen ] = client | |
334 wrote = true | |
335 end | |
336 end | |
337 coroutine_yield( handler, nil, err ) -- handshake not finished | |
338 end | |
339 end | |
340 _ = err ~= "closed" and close( socket ) | |
341 handler.close( ) | |
342 disconnect( handler, err ) | |
343 writequeue = nil | |
344 handler = nil | |
345 return false -- handshake failed | |
346 end | |
347 ) | |
348 handler.receivedata = handler.handshake | |
349 handler.dispatchdata = handler.handshake | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
350 |
64 | 351 handler.handshake( socket ) -- do handshake |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
352 |
64 | 353 socketlist[ socket ] = handler |
354 readlen = readlen + 1 | |
355 readlist[ readlen ] = socket | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
356 |
64 | 357 return handler, socket |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
358 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
359 |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
360 wraptlsclient = function( listener, socket, ip, serverport, clientport, mode, sslctx ) -- this function wraps a tls cleint |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
361 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
362 local dispatch, disconnect = listener.listener, listener.disconnect |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
363 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
364 --// transform socket to ssl object //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
365 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
366 local err |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
367 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
368 socket:settimeout( 0 ) |
328
a1d25769970f
Flush write queue before closing socket
Matthew Wild <mwild1@gmail.com>
parents:
319
diff
changeset
|
369 out_put("setting linger on "..tostring(socket)) |
a1d25769970f
Flush write queue before closing socket
Matthew Wild <mwild1@gmail.com>
parents:
319
diff
changeset
|
370 socket:setoption("linger", { on = true, timeout = 10 }); |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
371 --// private closures of the object //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
372 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
373 local writequeue = { } -- buffer for messages to send |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
374 |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
375 local eol, fatal_send_error -- end of buffer |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
376 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
377 local sstat, rstat = 0, 0 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
378 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
379 --// local import of socket methods //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
380 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
381 local send = socket.send |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
382 local receive = socket.receive |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
383 local close = socket.close |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
384 --local shutdown = socket.shutdown |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
385 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
386 --// public methods of the object //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
387 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
388 local handler = { } |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
389 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
390 handler.getstats = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
391 return rstat, sstat |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
392 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
393 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
394 handler.listener = function( data, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
395 return listener( handler, data, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
396 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
397 handler.ssl = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
398 return false |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
399 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
400 handler.send = function( _, data, i, j ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
401 return send( socket, data, i, j ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
402 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
403 handler.receive = function( pattern, prefix ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
404 return receive( socket, pattern, prefix ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
405 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
406 handler.shutdown = function( pattern ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
407 --return shutdown( socket, pattern ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
408 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
409 handler.close = function( closed ) |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
410 if eol and not fatal_send_error then handler._dispatchdata(); end |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
411 close( socket ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
412 writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
413 readlen = removesocket( readlist, socket, readlen ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
414 socketlist[ socket ] = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
415 out_put "server.lua: closed handler and removed socket from list" |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
416 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
417 handler.ip = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
418 return ip |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
419 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
420 handler.serverport = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
421 return serverport |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
422 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
423 handler.clientport = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
424 return clientport |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
425 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
426 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
427 handler.write = function( data ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
428 if not eol then |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
429 writelen = writelen + 1 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
430 writelist[ writelen ] = socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
431 eol = 0 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
432 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
433 eol = eol + 1 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
434 writequeue[ eol ] = data |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
435 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
436 handler.writequeue = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
437 return writequeue |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
438 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
439 handler.socket = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
440 return socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
441 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
442 handler.mode = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
443 return mode |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
444 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
445 handler._receivedata = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
446 local data, err, part = receive( socket, mode ) -- receive data in "mode" |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
447 if not err or ( err == "timeout" or err == "wantread" ) then -- received something |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
448 local data = data or part or "" |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
449 local count = #data * STAT_UNIT |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
450 rstat = rstat + count |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
451 receivestat = receivestat + count |
66 | 452 --out_put( "server.lua: read data '", data, "', error: ", err ) |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
453 return dispatch( handler, data, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
454 else -- connections was closed or fatal error |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
455 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
456 handler.close( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
457 disconnect( handler, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
458 writequeue = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
459 handler = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
460 return false |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
461 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
462 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
463 handler._dispatchdata = function( ) -- this function writes data to handlers |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
464 local buffer = table_concat( writequeue, "", 1, eol ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
465 local succ, err, byte = send( socket, buffer ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
466 local count = ( succ or 0 ) * STAT_UNIT |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
467 sstat = sstat + count |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
468 sendstat = sendstat + count |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
469 out_put( "server.lua: sended '", buffer, "', bytes: ", succ, ", error: ", err, ", part: ", byte, ", to: ", ip, ":", clientport ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
470 if succ then -- sending succesful |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
471 --writequeue = { } |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
472 eol = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
473 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
474 if handler.need_tls then |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
475 out_put("server.lua: connection is ready for tls handshake"); |
66 | 476 handler.starttls(true); |
477 if handler.need_tls then | |
478 out_put("server.lua: uh-oh... we still want tls, something must be wrong"); | |
479 end | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
480 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
481 return true |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
482 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
483 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
484 writequeue[ 1 ] = buffer -- insert new buffer in queue |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
485 eol = 1 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
486 return true |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
487 else -- connection was closed during sending or fatal error |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
488 fatal_send_error = true; -- :( |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
489 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
490 handler.close( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
491 disconnect( handler, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
492 writequeue = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
493 handler = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
494 return false |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
495 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
496 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
497 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
498 handler.receivedata, handler.dispatchdata = handler._receivedata, handler._dispatchdata; |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
499 -- // COMPAT // -- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
500 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
501 handler.getIp = handler.ip |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
502 handler.getPort = handler.clientport |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
503 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
504 --// handshake //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
505 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
506 local wrote, read |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
507 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
508 handler.starttls = function (now) |
66 | 509 if not now then out_put("server.lua: we need to do tls, but delaying until later"); handler.need_tls = true; return; end |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
510 out_put( "server.lua: attempting to start tls on "..tostring(socket) ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
511 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
512 out_put("sslwrapped socket is "..tostring(socket)); |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
513 if err then |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
514 out_put( "server.lua: ssl error: ", err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
515 return nil, nil, err -- fatal error |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
516 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
517 socket:settimeout( 1 ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
518 send = socket.send |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
519 receive = socket.receive |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
520 close = socket.close |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
521 handler.ssl = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
522 return true |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
523 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
524 handler.send = function( _, data, i, j ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
525 return send( socket, data, i, j ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
526 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
527 handler.receive = function( pattern, prefix ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
528 return receive( socket, pattern, prefix ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
529 end |
66 | 530 |
319
4a8a949c3870
Fix for putting TLS in stream:features for connections already using TLS. Thanks to albert for spotting.
Matthew Wild <mwild1@gmail.com>
parents:
127
diff
changeset
|
531 handler.starttls = nil; |
4a8a949c3870
Fix for putting TLS in stream:features for connections already using TLS. Thanks to albert for spotting.
Matthew Wild <mwild1@gmail.com>
parents:
127
diff
changeset
|
532 |
66 | 533 handler.handshake = coroutine_wrap( function( client ) |
534 local err | |
535 for i = 1, 10 do -- 10 handshake attemps | |
536 _, err = client:dohandshake( ) | |
537 if not err then | |
538 out_put( "server.lua: ssl handshake done" ) | |
539 writelen = ( wrote and removesocket( writelist, socket, writelen ) ) or writelen | |
540 handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions | |
541 handler.dispatchdata = handler._dispatchdata | |
542 handler.need_tls = nil | |
543 socketlist[ client ] = handler | |
544 readlen = readlen + 1 | |
545 readlist[ readlen ] = client | |
546 return true; | |
547 else | |
548 out_put( "server.lua: error during ssl handshake: ", err ) | |
549 if err == "wantwrite" then | |
550 if wrote == nil then | |
551 writelen = writelen + 1 | |
552 writelist[ writelen ] = client | |
553 wrote = true | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
554 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
555 end |
66 | 556 coroutine_yield( handler, nil, err ) -- handshake not finished |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
557 end |
66 | 558 end |
559 _ = err ~= "closed" and close( socket ) | |
560 handler.close( ) | |
561 disconnect( handler, err ) | |
562 writequeue = nil | |
563 handler = nil | |
564 return false -- handshake failed | |
565 end | |
566 ) | |
567 handler.receivedata = handler.handshake | |
568 handler.dispatchdata = handler.handshake | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
569 |
66 | 570 handler.handshake( socket ) -- do handshake |
571 end | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
572 socketlist[ socket ] = handler |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
573 readlen = readlen + 1 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
574 readlist[ readlen ] = socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
575 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
576 return handler, socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
577 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
578 |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
579 wraptcpclient = function( listener, socket, ip, serverport, clientport, mode ) -- this function wraps a socket |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
580 |
64 | 581 local dispatch, disconnect = listener.listener, listener.disconnect |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
582 |
64 | 583 --// private closures of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
584 |
64 | 585 local writequeue = { } -- list for messages to send |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
586 |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
587 local eol, fatal_send_error |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
588 |
64 | 589 local rstat, sstat = 0, 0 |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
590 |
64 | 591 --// local import of socket methods //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
592 |
64 | 593 local send = socket.send |
594 local receive = socket.receive | |
595 local close = socket.close | |
596 local shutdown = socket.shutdown | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
597 |
64 | 598 --// public methods of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
599 |
64 | 600 local handler = { } |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
601 |
64 | 602 handler.getstats = function( ) |
603 return rstat, sstat | |
604 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
605 |
64 | 606 handler.listener = function( data, err ) |
607 return listener( handler, data, err ) | |
608 end | |
609 handler.ssl = function( ) | |
610 return false | |
611 end | |
612 handler.send = function( _, data, i, j ) | |
613 return send( socket, data, i, j ) | |
614 end | |
615 handler.receive = function( pattern, prefix ) | |
616 return receive( socket, pattern, prefix ) | |
617 end | |
618 handler.shutdown = function( pattern ) | |
619 return shutdown( socket, pattern ) | |
620 end | |
621 handler.close = function( closed ) | |
374
ff502dada372
All the people who don't use TLS, this one's for you.
Matthew Wild <mwild1@gmail.com>
parents:
335
diff
changeset
|
622 if eol and not fatal_send_error then handler.dispatchdata(); end |
64 | 623 _ = not closed and shutdown( socket ) |
624 _ = not closed and close( socket ) | |
625 writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen | |
626 readlen = removesocket( readlist, socket, readlen ) | |
627 socketlist[ socket ] = nil | |
628 out_put "server.lua: closed handler and removed socket from list" | |
629 end | |
630 handler.ip = function( ) | |
631 return ip | |
632 end | |
633 handler.serverport = function( ) | |
634 return serverport | |
635 end | |
636 handler.clientport = function( ) | |
637 return clientport | |
638 end | |
639 handler.write = function( data ) | |
640 if not eol then | |
641 writelen = writelen + 1 | |
642 writelist[ writelen ] = socket | |
643 eol = 0 | |
644 end | |
645 eol = eol + 1 | |
646 writequeue[ eol ] = data | |
647 end | |
648 handler.writequeue = function( ) | |
649 return writequeue | |
650 end | |
651 handler.socket = function( ) | |
652 return socket | |
653 end | |
654 handler.mode = function( ) | |
655 return mode | |
656 end | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
657 |
64 | 658 handler.receivedata = function( ) |
659 local data, err, part = receive( socket, mode ) -- receive data in "mode" | |
660 if not err or ( err == "timeout" or err == "wantread" ) then -- received something | |
661 local data = data or part or "" | |
662 local count = #data * STAT_UNIT | |
663 rstat = rstat + count | |
664 receivestat = receivestat + count | |
665 out_put( "server.lua: read data '", data, "', error: ", err ) | |
666 return dispatch( handler, data, err ) | |
667 else -- connections was closed or fatal error | |
668 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) | |
669 handler.close( ) | |
670 disconnect( handler, err ) | |
671 writequeue = nil | |
672 handler = nil | |
673 return false | |
674 end | |
675 end | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
676 |
64 | 677 handler.dispatchdata = function( ) -- this function writes data to handlers |
678 local buffer = table_concat( writequeue, "", 1, eol ) | |
679 local succ, err, byte = send( socket, buffer ) | |
680 local count = ( succ or 0 ) * STAT_UNIT | |
681 sstat = sstat + count | |
682 sendstat = sendstat + count | |
683 out_put( "server.lua: sended '", buffer, "', bytes: ", succ, ", error: ", err, ", part: ", byte, ", to: ", ip, ":", clientport ) | |
684 if succ then -- sending succesful | |
685 --writequeue = { } | |
686 eol = nil | |
687 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist | |
688 return true | |
689 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write | |
690 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer | |
691 writequeue[ 1 ] = buffer -- insert new buffer in queue | |
692 eol = 1 | |
693 return true | |
694 else -- connection was closed during sending or fatal error | |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
695 fatal_send_error = true; -- :'-( |
64 | 696 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) |
697 handler.close( ) | |
698 disconnect( handler, err ) | |
699 writequeue = nil | |
700 handler = nil | |
701 return false | |
702 end | |
703 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
704 |
64 | 705 -- // COMPAT // -- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
706 |
64 | 707 handler.getIp = handler.ip |
708 handler.getPort = handler.clientport | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
709 |
64 | 710 socketlist[ socket ] = handler |
711 readlen = readlen + 1 | |
712 readlist[ readlen ] = socket | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
713 |
64 | 714 return handler, socket |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
715 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
716 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
717 addtimer = function( listener ) |
64 | 718 timelistener[ #timelistener + 1 ] = listener |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
719 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
720 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
721 firetimer = function( listener ) |
64 | 722 for i, listener in ipairs( timelistener ) do |
723 listener( ) | |
724 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
725 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
726 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
727 addserver = function( listeners, port, addr, mode, sslctx ) -- this function provides a way for other scripts to reg a server |
64 | 728 local err |
729 if type( listeners ) ~= "table" then | |
730 err = "invalid listener table" | |
731 else | |
732 for name, func in pairs( listeners ) do | |
733 if type( func ) ~= "function" then | |
98
3a2d327c4856
server.lua should degrade gracefully when LuaSec not present
Matthew Wild <mwild1@gmail.com>
parents:
74
diff
changeset
|
734 --err = "invalid listener function" |
64 | 735 break |
736 end | |
737 end | |
738 end | |
739 if not type( port ) == "number" or not ( port >= 0 and port <= 65535 ) then | |
740 err = "invalid port" | |
741 elseif listener[ port ] then | |
742 err= "listeners on port '" .. port .. "' already exist" | |
743 elseif sslctx and not luasec then | |
744 err = "luasec not found" | |
745 end | |
746 if err then | |
747 out_error( "server.lua: ", err ) | |
748 return nil, err | |
749 end | |
750 addr = addr or "*" | |
751 local server, err = socket_bind( addr, port ) | |
752 if err then | |
753 out_error( "server.lua: ", err ) | |
754 return nil, err | |
755 end | |
756 local handler, err = wrapserver( listeners, server, addr, port, mode, sslctx ) -- wrap new server socket | |
757 if not handler then | |
758 server:close( ) | |
759 return nil, err | |
760 end | |
761 server:settimeout( 0 ) | |
762 readlen = readlen + 1 | |
763 readlist[ readlen ] = server | |
764 listener[ port ] = listeners | |
765 socketlist[ server ] = handler | |
766 out_put( "server.lua: new server listener on ", addr, ":", port ) | |
767 return true | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
768 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
769 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
770 removesocket = function( tbl, socket, len ) -- this function removes sockets from a list |
64 | 771 for i, target in ipairs( tbl ) do |
772 if target == socket then | |
773 len = len - 1 | |
774 table_remove( tbl, i ) | |
775 return len | |
776 end | |
777 end | |
778 return len | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
779 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
780 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
781 closeall = function( ) |
64 | 782 for sock, handler in pairs( socketlist ) do |
783 handler.shutdown( ) | |
784 handler.close( ) | |
785 socketlist[ sock ] = nil | |
786 end | |
787 writelist, readlist, socketlist = { }, { }, { } | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
788 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
789 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
790 closesocket = function( socket ) |
64 | 791 writelen = removesocket( writelist, socket, writelen ) |
792 readlen = removesocket( readlist, socket, readlen ) | |
793 socketlist[ socket ] = nil | |
794 socket:close( ) | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
795 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
796 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
797 loop = function( ) -- this is the main loop of the program |
64 | 798 --signal_set( "hub", "run" ) |
799 repeat | |
66 | 800 --[[print(readlen, writelen) |
801 for _, s in ipairs(readlist) do print("R:", tostring(s)) end | |
802 for _, s in ipairs(writelist) do print("W:", tostring(s)) end | |
803 out_put("select()"..os.time())]] | |
64 | 804 local read, write, err = socket_select( readlist, writelist, 1 ) -- 1 sec timeout, nice for timers |
805 for i, socket in ipairs( write ) do -- send data waiting in writequeues | |
806 local handler = socketlist[ socket ] | |
807 if handler then | |
808 handler.dispatchdata( ) | |
809 else | |
810 closesocket( socket ) | |
811 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen | |
812 end | |
813 end | |
814 for i, socket in ipairs( read ) do -- receive data | |
815 local handler = socketlist[ socket ] | |
816 if handler then | |
817 handler.receivedata( ) | |
818 else | |
819 closesocket( socket ) | |
820 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen | |
821 end | |
822 end | |
823 firetimer( ) | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
824 until false |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
825 return |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
826 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
827 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
828 ----------------------------------// BEGIN //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
829 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
830 ----------------------------------// PUBLIC INTERFACE //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
831 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
832 return { |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
833 |
64 | 834 add = addserver, |
835 loop = loop, | |
836 stats = stats, | |
837 closeall = closeall, | |
838 addtimer = addtimer, | |
127 | 839 wraptlsclient = wraptlsclient, |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
840 } |