Software / code / prosody
Annotate
net/server.lua @ 328:a1d25769970f
Flush write queue before closing socket
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 18 Nov 2008 05:06:50 +0000 |
| parent | 319:4a8a949c3870 |
| child | 335:906311ff2170 |
| 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 |
| 64 | 193 local eol -- 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 ) | |
|
328
a1d25769970f
Flush write queue before closing socket
Matthew Wild <mwild1@gmail.com>
parents:
319
diff
changeset
|
228 if eol 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 | |
| 299 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) | |
| 300 handler.close( ) | |
| 301 disconnect( handler, err ) | |
| 302 writequeue = nil | |
| 303 handler = nil | |
| 304 return false | |
| 305 end | |
| 306 end | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
307 |
| 64 | 308 -- // COMPAT // -- |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
309 |
| 64 | 310 handler.getIp = handler.ip |
| 311 handler.getPort = handler.clientport | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
312 |
| 64 | 313 --// handshake //-- |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
314 |
| 64 | 315 local wrote |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
316 |
| 64 | 317 handler.handshake = coroutine_wrap( function( client ) |
| 318 local err | |
| 319 for i = 1, 10 do -- 10 handshake attemps | |
| 320 _, err = client:dohandshake( ) | |
| 321 if not err then | |
| 322 out_put( "server.lua: ssl handshake done" ) | |
| 323 writelen = ( wrote and removesocket( writelist, socket, writelen ) ) or writelen | |
| 324 handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions | |
| 325 handler.dispatchdata = handler._dispatchdata | |
| 326 return dispatch( handler ) | |
| 327 else | |
| 328 out_put( "server.lua: error during ssl handshake: ", err ) | |
| 329 if err == "wantwrite" then | |
| 330 if wrote == nil then | |
| 331 writelen = writelen + 1 | |
| 332 writelist[ writelen ] = client | |
| 333 wrote = true | |
| 334 end | |
| 335 end | |
| 336 coroutine_yield( handler, nil, err ) -- handshake not finished | |
| 337 end | |
| 338 end | |
| 339 _ = err ~= "closed" and close( socket ) | |
| 340 handler.close( ) | |
| 341 disconnect( handler, err ) | |
| 342 writequeue = nil | |
| 343 handler = nil | |
| 344 return false -- handshake failed | |
| 345 end | |
| 346 ) | |
| 347 handler.receivedata = handler.handshake | |
| 348 handler.dispatchdata = handler.handshake | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
349 |
| 64 | 350 handler.handshake( socket ) -- do handshake |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
351 |
| 64 | 352 socketlist[ socket ] = handler |
| 353 readlen = readlen + 1 | |
| 354 readlist[ readlen ] = socket | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
355 |
| 64 | 356 return handler, socket |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
357 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
358 |
|
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
359 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
|
360 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
361 local dispatch, disconnect = listener.listener, listener.disconnect |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
362 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
363 --// transform socket to ssl object //-- |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
364 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
365 local err |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
366 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
367 socket:settimeout( 0 ) |
|
328
a1d25769970f
Flush write queue before closing socket
Matthew Wild <mwild1@gmail.com>
parents:
319
diff
changeset
|
368 out_put("setting linger on "..tostring(socket)) |
|
a1d25769970f
Flush write queue before closing socket
Matthew Wild <mwild1@gmail.com>
parents:
319
diff
changeset
|
369 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
|
370 --// private closures of the object //-- |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
371 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
372 local writequeue = { } -- buffer for messages to send |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
373 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
374 local eol -- end of buffer |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
375 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
376 local sstat, rstat = 0, 0 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
377 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
378 --// local import of socket methods //-- |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
379 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
380 local send = socket.send |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
381 local receive = socket.receive |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
382 local close = socket.close |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
383 --local shutdown = socket.shutdown |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
384 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
385 --// public methods of the object //-- |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
386 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
387 local handler = { } |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
388 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
389 handler.getstats = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
390 return rstat, sstat |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
391 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
392 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
393 handler.listener = function( data, err ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
394 return listener( handler, data, err ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
395 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
396 handler.ssl = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
397 return false |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
398 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
399 handler.send = function( _, data, i, j ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
400 return send( socket, data, i, j ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
401 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
402 handler.receive = function( pattern, prefix ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
403 return receive( socket, pattern, prefix ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
404 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
405 handler.shutdown = function( pattern ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
406 --return shutdown( socket, pattern ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
407 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
408 handler.close = function( closed ) |
|
328
a1d25769970f
Flush write queue before closing socket
Matthew Wild <mwild1@gmail.com>
parents:
319
diff
changeset
|
409 if eol then handler._dispatchdata(); end |
|
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
410 close( socket ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
411 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
|
412 readlen = removesocket( readlist, socket, readlen ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
413 socketlist[ socket ] = nil |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
414 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
|
415 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
416 handler.ip = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
417 return ip |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
418 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
419 handler.serverport = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
420 return serverport |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
421 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
422 handler.clientport = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
423 return clientport |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
424 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
425 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
426 handler.write = function( data ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
427 if not eol then |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
428 writelen = writelen + 1 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
429 writelist[ writelen ] = socket |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
430 eol = 0 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
431 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
432 eol = eol + 1 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
433 writequeue[ eol ] = data |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
434 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
435 handler.writequeue = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
436 return writequeue |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
437 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
438 handler.socket = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
439 return socket |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
440 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
441 handler.mode = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
442 return mode |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
443 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
444 handler._receivedata = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
445 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
|
446 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
|
447 local data = data or part or "" |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
448 local count = #data * STAT_UNIT |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
449 rstat = rstat + count |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
450 receivestat = receivestat + count |
| 66 | 451 --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
|
452 return dispatch( handler, data, err ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
453 else -- connections was closed or fatal error |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
454 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
|
455 handler.close( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
456 disconnect( handler, err ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
457 writequeue = nil |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
458 handler = nil |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
459 return false |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
460 end |
|
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 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
|
463 local buffer = table_concat( writequeue, "", 1, eol ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
464 local succ, err, byte = send( socket, buffer ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
465 local count = ( succ or 0 ) * STAT_UNIT |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
466 sstat = sstat + count |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
467 sendstat = sendstat + count |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
468 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
|
469 if succ then -- sending succesful |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
470 --writequeue = { } |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
471 eol = nil |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
472 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
|
473 if handler.need_tls then |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
474 out_put("server.lua: connection is ready for tls handshake"); |
| 66 | 475 handler.starttls(true); |
| 476 if handler.need_tls then | |
| 477 out_put("server.lua: uh-oh... we still want tls, something must be wrong"); | |
| 478 end | |
|
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
479 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
480 return true |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
481 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
|
482 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
|
483 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
|
484 eol = 1 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
485 return true |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
486 else -- connection was closed during sending or fatal error |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
487 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
|
488 handler.close( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
489 disconnect( handler, err ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
490 writequeue = nil |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
491 handler = nil |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
492 return false |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
493 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
494 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
495 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
496 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
|
497 -- // COMPAT // -- |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
498 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
499 handler.getIp = handler.ip |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
500 handler.getPort = handler.clientport |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
501 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
502 --// handshake //-- |
|
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 local wrote, read |
|
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 handler.starttls = function (now) |
| 66 | 507 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
|
508 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
|
509 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
|
510 out_put("sslwrapped socket is "..tostring(socket)); |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
511 if err then |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
512 out_put( "server.lua: ssl error: ", err ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
513 return nil, nil, err -- fatal error |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
514 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
515 socket:settimeout( 1 ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
516 send = socket.send |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
517 receive = socket.receive |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
518 close = socket.close |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
519 handler.ssl = function( ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
520 return true |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
521 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
522 handler.send = function( _, data, i, j ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
523 return send( socket, data, i, j ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
524 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
525 handler.receive = function( pattern, prefix ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
526 return receive( socket, pattern, prefix ) |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
527 end |
| 66 | 528 |
|
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
|
529 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
|
530 |
| 66 | 531 handler.handshake = coroutine_wrap( function( client ) |
| 532 local err | |
| 533 for i = 1, 10 do -- 10 handshake attemps | |
| 534 _, err = client:dohandshake( ) | |
| 535 if not err then | |
| 536 out_put( "server.lua: ssl handshake done" ) | |
| 537 writelen = ( wrote and removesocket( writelist, socket, writelen ) ) or writelen | |
| 538 handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions | |
| 539 handler.dispatchdata = handler._dispatchdata | |
| 540 handler.need_tls = nil | |
| 541 socketlist[ client ] = handler | |
| 542 readlen = readlen + 1 | |
| 543 readlist[ readlen ] = client | |
| 544 return true; | |
| 545 else | |
| 546 out_put( "server.lua: error during ssl handshake: ", err ) | |
| 547 if err == "wantwrite" then | |
| 548 if wrote == nil then | |
| 549 writelen = writelen + 1 | |
| 550 writelist[ writelen ] = client | |
| 551 wrote = true | |
|
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
552 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
553 end |
| 66 | 554 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
|
555 end |
| 66 | 556 end |
| 557 _ = err ~= "closed" and close( socket ) | |
| 558 handler.close( ) | |
| 559 disconnect( handler, err ) | |
| 560 writequeue = nil | |
| 561 handler = nil | |
| 562 return false -- handshake failed | |
| 563 end | |
| 564 ) | |
| 565 handler.receivedata = handler.handshake | |
| 566 handler.dispatchdata = handler.handshake | |
|
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
567 |
| 66 | 568 handler.handshake( socket ) -- do handshake |
| 569 end | |
|
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
570 socketlist[ socket ] = handler |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
571 readlen = readlen + 1 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
572 readlist[ readlen ] = socket |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
573 |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
574 return handler, socket |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
575 end |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
576 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
577 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
|
578 |
| 64 | 579 local dispatch, disconnect = listener.listener, listener.disconnect |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
580 |
| 64 | 581 --// private closures of the object //-- |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
582 |
| 64 | 583 local writequeue = { } -- list for messages to send |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
584 |
| 64 | 585 local eol |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
586 |
| 64 | 587 local rstat, sstat = 0, 0 |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
588 |
| 64 | 589 --// local import of socket methods //-- |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
590 |
| 64 | 591 local send = socket.send |
| 592 local receive = socket.receive | |
| 593 local close = socket.close | |
| 594 local shutdown = socket.shutdown | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
595 |
| 64 | 596 --// public methods of the object //-- |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
597 |
| 64 | 598 local handler = { } |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
599 |
| 64 | 600 handler.getstats = function( ) |
| 601 return rstat, sstat | |
| 602 end | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
603 |
| 64 | 604 handler.listener = function( data, err ) |
| 605 return listener( handler, data, err ) | |
| 606 end | |
| 607 handler.ssl = function( ) | |
| 608 return false | |
| 609 end | |
| 610 handler.send = function( _, data, i, j ) | |
| 611 return send( socket, data, i, j ) | |
| 612 end | |
| 613 handler.receive = function( pattern, prefix ) | |
| 614 return receive( socket, pattern, prefix ) | |
| 615 end | |
| 616 handler.shutdown = function( pattern ) | |
| 617 return shutdown( socket, pattern ) | |
| 618 end | |
| 619 handler.close = function( closed ) | |
|
328
a1d25769970f
Flush write queue before closing socket
Matthew Wild <mwild1@gmail.com>
parents:
319
diff
changeset
|
620 if eol then handler._dispatchdata(); end |
| 64 | 621 _ = not closed and shutdown( socket ) |
| 622 _ = not closed and close( socket ) | |
| 623 writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen | |
| 624 readlen = removesocket( readlist, socket, readlen ) | |
| 625 socketlist[ socket ] = nil | |
| 626 out_put "server.lua: closed handler and removed socket from list" | |
| 627 end | |
| 628 handler.ip = function( ) | |
| 629 return ip | |
| 630 end | |
| 631 handler.serverport = function( ) | |
| 632 return serverport | |
| 633 end | |
| 634 handler.clientport = function( ) | |
| 635 return clientport | |
| 636 end | |
| 637 handler.write = function( data ) | |
| 638 if not eol then | |
| 639 writelen = writelen + 1 | |
| 640 writelist[ writelen ] = socket | |
| 641 eol = 0 | |
| 642 end | |
| 643 eol = eol + 1 | |
| 644 writequeue[ eol ] = data | |
| 645 end | |
| 646 handler.writequeue = function( ) | |
| 647 return writequeue | |
| 648 end | |
| 649 handler.socket = function( ) | |
| 650 return socket | |
| 651 end | |
| 652 handler.mode = function( ) | |
| 653 return mode | |
| 654 end | |
|
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
655 |
| 64 | 656 handler.receivedata = function( ) |
| 657 local data, err, part = receive( socket, mode ) -- receive data in "mode" | |
| 658 if not err or ( err == "timeout" or err == "wantread" ) then -- received something | |
| 659 local data = data or part or "" | |
| 660 local count = #data * STAT_UNIT | |
| 661 rstat = rstat + count | |
| 662 receivestat = receivestat + count | |
| 663 out_put( "server.lua: read data '", data, "', error: ", err ) | |
| 664 return dispatch( handler, data, err ) | |
| 665 else -- connections was closed or fatal error | |
| 666 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) | |
| 667 handler.close( ) | |
| 668 disconnect( handler, err ) | |
| 669 writequeue = nil | |
| 670 handler = nil | |
| 671 return false | |
| 672 end | |
| 673 end | |
|
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
674 |
| 64 | 675 handler.dispatchdata = function( ) -- this function writes data to handlers |
| 676 local buffer = table_concat( writequeue, "", 1, eol ) | |
| 677 local succ, err, byte = send( socket, buffer ) | |
| 678 local count = ( succ or 0 ) * STAT_UNIT | |
| 679 sstat = sstat + count | |
| 680 sendstat = sendstat + count | |
| 681 out_put( "server.lua: sended '", buffer, "', bytes: ", succ, ", error: ", err, ", part: ", byte, ", to: ", ip, ":", clientport ) | |
| 682 if succ then -- sending succesful | |
| 683 --writequeue = { } | |
| 684 eol = nil | |
| 685 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist | |
| 686 return true | |
| 687 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write | |
| 688 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer | |
| 689 writequeue[ 1 ] = buffer -- insert new buffer in queue | |
| 690 eol = 1 | |
| 691 return true | |
| 692 else -- connection was closed during sending or fatal error | |
| 693 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) | |
| 694 handler.close( ) | |
| 695 disconnect( handler, err ) | |
| 696 writequeue = nil | |
| 697 handler = nil | |
| 698 return false | |
| 699 end | |
| 700 end | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
701 |
| 64 | 702 -- // COMPAT // -- |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
703 |
| 64 | 704 handler.getIp = handler.ip |
| 705 handler.getPort = handler.clientport | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
706 |
| 64 | 707 socketlist[ socket ] = handler |
| 708 readlen = readlen + 1 | |
| 709 readlist[ readlen ] = socket | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
710 |
| 64 | 711 return handler, socket |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
712 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
713 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
714 addtimer = function( listener ) |
| 64 | 715 timelistener[ #timelistener + 1 ] = listener |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
716 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
717 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
718 firetimer = function( listener ) |
| 64 | 719 for i, listener in ipairs( timelistener ) do |
| 720 listener( ) | |
| 721 end | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
722 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
723 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
724 addserver = function( listeners, port, addr, mode, sslctx ) -- this function provides a way for other scripts to reg a server |
| 64 | 725 local err |
| 726 if type( listeners ) ~= "table" then | |
| 727 err = "invalid listener table" | |
| 728 else | |
| 729 for name, func in pairs( listeners ) do | |
| 730 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
|
731 --err = "invalid listener function" |
| 64 | 732 break |
| 733 end | |
| 734 end | |
| 735 end | |
| 736 if not type( port ) == "number" or not ( port >= 0 and port <= 65535 ) then | |
| 737 err = "invalid port" | |
| 738 elseif listener[ port ] then | |
| 739 err= "listeners on port '" .. port .. "' already exist" | |
| 740 elseif sslctx and not luasec then | |
| 741 err = "luasec not found" | |
| 742 end | |
| 743 if err then | |
| 744 out_error( "server.lua: ", err ) | |
| 745 return nil, err | |
| 746 end | |
| 747 addr = addr or "*" | |
| 748 local server, err = socket_bind( addr, port ) | |
| 749 if err then | |
| 750 out_error( "server.lua: ", err ) | |
| 751 return nil, err | |
| 752 end | |
| 753 local handler, err = wrapserver( listeners, server, addr, port, mode, sslctx ) -- wrap new server socket | |
| 754 if not handler then | |
| 755 server:close( ) | |
| 756 return nil, err | |
| 757 end | |
| 758 server:settimeout( 0 ) | |
| 759 readlen = readlen + 1 | |
| 760 readlist[ readlen ] = server | |
| 761 listener[ port ] = listeners | |
| 762 socketlist[ server ] = handler | |
| 763 out_put( "server.lua: new server listener on ", addr, ":", port ) | |
| 764 return true | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
765 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
766 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
767 removesocket = function( tbl, socket, len ) -- this function removes sockets from a list |
| 64 | 768 for i, target in ipairs( tbl ) do |
| 769 if target == socket then | |
| 770 len = len - 1 | |
| 771 table_remove( tbl, i ) | |
| 772 return len | |
| 773 end | |
| 774 end | |
| 775 return len | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
776 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
777 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
778 closeall = function( ) |
| 64 | 779 for sock, handler in pairs( socketlist ) do |
| 780 handler.shutdown( ) | |
| 781 handler.close( ) | |
| 782 socketlist[ sock ] = nil | |
| 783 end | |
| 784 writelist, readlist, socketlist = { }, { }, { } | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
785 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
786 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
787 closesocket = function( socket ) |
| 64 | 788 writelen = removesocket( writelist, socket, writelen ) |
| 789 readlen = removesocket( readlist, socket, readlen ) | |
| 790 socketlist[ socket ] = nil | |
| 791 socket:close( ) | |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
792 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
793 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
794 loop = function( ) -- this is the main loop of the program |
| 64 | 795 --signal_set( "hub", "run" ) |
| 796 repeat | |
| 66 | 797 --[[print(readlen, writelen) |
| 798 for _, s in ipairs(readlist) do print("R:", tostring(s)) end | |
| 799 for _, s in ipairs(writelist) do print("W:", tostring(s)) end | |
| 800 out_put("select()"..os.time())]] | |
| 64 | 801 local read, write, err = socket_select( readlist, writelist, 1 ) -- 1 sec timeout, nice for timers |
| 802 for i, socket in ipairs( write ) do -- send data waiting in writequeues | |
| 803 local handler = socketlist[ socket ] | |
| 804 if handler then | |
| 805 handler.dispatchdata( ) | |
| 806 else | |
| 807 closesocket( socket ) | |
| 808 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen | |
| 809 end | |
| 810 end | |
| 811 for i, socket in ipairs( read ) do -- receive data | |
| 812 local handler = socketlist[ socket ] | |
| 813 if handler then | |
| 814 handler.receivedata( ) | |
| 815 else | |
| 816 closesocket( socket ) | |
| 817 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen | |
| 818 end | |
| 819 end | |
| 820 firetimer( ) | |
|
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
821 until false |
|
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
822 return |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
823 end |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
824 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
825 ----------------------------------// BEGIN //-- |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
826 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
827 ----------------------------------// PUBLIC INTERFACE //-- |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
828 |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
829 return { |
|
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
830 |
| 64 | 831 add = addserver, |
| 832 loop = loop, | |
| 833 stats = stats, | |
| 834 closeall = closeall, | |
| 835 addtimer = addtimer, | |
| 127 | 836 wraptlsclient = wraptlsclient, |
|
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
837 } |