Annotate

net/server.lua @ 172:71247788c7c7

Inbound subscription cancellation
author Waqas Hussain <waqas20@gmail.com>
date Sat, 25 Oct 2008 02:29:58 +0500
parent 127:93f3c6b94c75
child 319:4a8a949c3870
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
3 server.lua by blastbeat of the luadch project
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
4
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
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
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
7 - this script contains the server loop of the program
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
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
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
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
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
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
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
103 local wrapclient, err
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
104
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
105 if sslctx then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
106 if not ssl_newcontext then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
107 return nil, "luasec not found"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
108 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
109 if type( sslctx ) ~= "table" then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
110 out_error "server.lua: wrong server sslctx"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
111 return nil, "wrong server sslctx"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
112 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
113 sslctx, err = ssl_newcontext( sslctx )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
114 if not sslctx then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
115 err = err or "wrong sslctx parameters"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
116 out_error( "server.lua: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
117 return nil, err
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
118 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
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
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
121 else
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
122 wrapclient = wraptcpclient
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
123 end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
124
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
125 local accept = socket.accept
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
126 local close = socket.close
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
127
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
128 --// public methods of the object //--
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
129
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
130 local handler = { }
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
131
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
132 handler.shutdown = function( ) end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
133
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
134 --[[handler.listener = function( data, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
135 return ondata( handler, data, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
136 end]]
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
137 handler.ssl = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
138 return sslctx and true or false
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
139 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
140 handler.close = function( closed )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
141 _ = not closed and close( socket )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
142 writelen = removesocket( writelist, socket, writelen )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
143 readlen = removesocket( readlist, socket, readlen )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
144 socketlist[ socket ] = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
145 handler = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
146 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
147 handler.ip = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
148 return ip
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
149 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
150 handler.serverport = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
151 return serverport
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
152 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
153 handler.socket = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
154 return socket
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
155 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
156 handler.receivedata = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
157 local client, err = accept( socket ) -- try to accept
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
158 if client then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
159 local ip, clientport = client:getpeername( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
160 client:settimeout( 0 )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
161 local handler, client, err = wrapclient( listener, client, ip, serverport, clientport, mode, sslctx ) -- wrap new client socket
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
162 if err then -- error while wrapping ssl socket
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
163 return false
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
164 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
165 out_put( "server.lua: accepted new client connection from ", ip, ":", clientport )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
166 return dispatch( handler )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
167 elseif err then -- maybe timeout or something else
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
168 out_put( "server.lua: error with new client connection: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
169 return false
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
170 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
171 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
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
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
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
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
179 --// transform socket to ssl object //--
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
180
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
181 local err
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
182 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
183 if err then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
184 out_put( "server.lua: ssl error: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
185 return nil, nil, err -- fatal error
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
186 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
187 socket:settimeout( 0 )
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
188
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
189 --// private closures of the object //--
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
190
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
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
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
193 local eol -- end of buffer
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
194
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
195 local sstat, rstat = 0, 0
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
196
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
197 --// local import of socket methods //--
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
198
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
199 local send = socket.send
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
200 local receive = socket.receive
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
201 local close = socket.close
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
202 --local shutdown = socket.shutdown
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
203
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
204 --// public methods of the object //--
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
205
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
206 local handler = { }
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
207
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
208 handler.getstats = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
209 return rstat, sstat
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
210 end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
211
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
212 handler.listener = function( data, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
213 return listener( handler, data, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
214 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
215 handler.ssl = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
216 return true
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
217 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
218 handler.send = function( _, data, i, j )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
219 return send( socket, data, i, j )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
220 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
221 handler.receive = function( pattern, prefix )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
222 return receive( socket, pattern, prefix )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
223 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
224 handler.shutdown = function( pattern )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
225 --return shutdown( socket, pattern )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
226 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
227 handler.close = function( closed )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
228 close( socket )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
229 writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
230 readlen = removesocket( readlist, socket, readlen )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
231 socketlist[ socket ] = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
232 out_put "server.lua: closed handler and removed socket from list"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
233 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
234 handler.ip = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
235 return ip
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
236 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
237 handler.serverport = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
238 return serverport
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
239 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
240 handler.clientport = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
241 return clientport
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
242 end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
243
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
244 handler.write = function( data )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
245 if not eol then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
246 writelen = writelen + 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
247 writelist[ writelen ] = socket
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
248 eol = 0
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
249 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
250 eol = eol + 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
251 writequeue[ eol ] = data
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
252 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
253 handler.writequeue = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
254 return writequeue
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
255 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
256 handler.socket = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
257 return socket
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
258 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
259 handler.mode = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
260 return mode
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
261 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
262 handler._receivedata = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
263 local data, err, part = receive( socket, mode ) -- receive data in "mode"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
264 if not err or ( err == "timeout" or err == "wantread" ) then -- received something
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
265 local data = data or part or ""
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
266 local count = #data * STAT_UNIT
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
267 rstat = rstat + count
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
268 receivestat = receivestat + count
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
269 out_put( "server.lua: read data '", data, "', error: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
270 return dispatch( handler, data, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
271 else -- connections was closed or fatal error
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
272 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
273 handler.close( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
274 disconnect( handler, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
275 writequeue = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
276 handler = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
277 return false
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
278 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
279 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
280 handler._dispatchdata = function( ) -- this function writes data to handlers
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
281 local buffer = table_concat( writequeue, "", 1, eol )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
282 local succ, err, byte = send( socket, buffer )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
283 local count = ( succ or 0 ) * STAT_UNIT
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
284 sstat = sstat + count
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
285 sendstat = sendstat + count
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
286 out_put( "server.lua: sended '", buffer, "', bytes: ", succ, ", error: ", err, ", part: ", byte, ", to: ", ip, ":", clientport )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
287 if succ then -- sending succesful
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
288 --writequeue = { }
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
289 eol = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
290 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
291 return true
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
292 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
293 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
294 writequeue[ 1 ] = buffer -- insert new buffer in queue
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
295 eol = 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
296 return true
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
297 else -- connection was closed during sending or fatal error
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
298 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
299 handler.close( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
300 disconnect( handler, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
301 writequeue = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
302 handler = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
303 return false
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
304 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
305 end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
306
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
307 -- // COMPAT // --
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
308
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
309 handler.getIp = handler.ip
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
310 handler.getPort = handler.clientport
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
311
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
312 --// handshake //--
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
313
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
314 local wrote
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
315
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
316 handler.handshake = coroutine_wrap( function( client )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
317 local err
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
318 for i = 1, 10 do -- 10 handshake attemps
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
319 _, err = client:dohandshake( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
320 if not err then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
321 out_put( "server.lua: ssl handshake done" )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
322 writelen = ( wrote and removesocket( writelist, socket, writelen ) ) or writelen
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
323 handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
324 handler.dispatchdata = handler._dispatchdata
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
325 return dispatch( handler )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
326 else
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
327 out_put( "server.lua: error during ssl handshake: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
328 if err == "wantwrite" then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
329 if wrote == nil then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
330 writelen = writelen + 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
331 writelist[ writelen ] = client
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
332 wrote = true
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
333 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
334 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
335 coroutine_yield( handler, nil, err ) -- handshake not finished
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
336 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
337 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
338 _ = err ~= "closed" and close( socket )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
339 handler.close( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
340 disconnect( handler, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
341 writequeue = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
342 handler = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
343 return false -- handshake failed
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
344 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
345 )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
346 handler.receivedata = handler.handshake
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
347 handler.dispatchdata = handler.handshake
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
348
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
349 handler.handshake( socket ) -- do handshake
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
350
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
351 socketlist[ socket ] = handler
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
352 readlen = readlen + 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
353 readlist[ readlen ] = socket
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
354
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
355 return handler, socket
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
356 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
357
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
358 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
359
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
360 local dispatch, disconnect = listener.listener, listener.disconnect
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 --// transform socket to ssl object //--
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 local err
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 socket:settimeout( 0 )
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 --// private closures of the object //--
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
369
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
370 local writequeue = { } -- buffer for messages to send
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 eol -- end of buffer
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 sstat, rstat = 0, 0
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 import of socket methods //--
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 send = socket.send
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
379 local receive = socket.receive
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
380 local close = socket.close
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
381 --local shutdown = socket.shutdown
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
382
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
383 --// public methods of the object //--
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 local handler = { }
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 handler.getstats = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
388 return rstat, sstat
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
389 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
390
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
391 handler.listener = function( data, err )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
392 return listener( handler, data, err )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
393 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
394 handler.ssl = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
395 return false
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.send = function( _, data, i, j )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
398 return send( socket, data, i, j )
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.receive = function( pattern, prefix )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
401 return receive( socket, pattern, prefix )
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.shutdown = function( pattern )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
404 --return shutdown( socket, pattern )
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.close = function( closed )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
407 close( socket )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
408 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
409 readlen = removesocket( readlist, socket, readlen )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
410 socketlist[ socket ] = nil
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
411 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
412 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
413 handler.ip = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
414 return ip
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.serverport = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
417 return serverport
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.clientport = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
420 return clientport
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
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
423 handler.write = function( data )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
424 if not eol then
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
425 writelen = writelen + 1
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
426 writelist[ writelen ] = socket
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
427 eol = 0
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
428 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
429 eol = eol + 1
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
430 writequeue[ eol ] = data
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 handler.writequeue = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
433 return writequeue
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.socket = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
436 return socket
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.mode = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
439 return mode
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._receivedata = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
442 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
443 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
444 local data = data or part or ""
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
445 local count = #data * STAT_UNIT
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
446 rstat = rstat + count
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
447 receivestat = receivestat + count
66
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
448 --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
449 return dispatch( handler, data, err )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
450 else -- connections was closed or fatal error
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
451 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
452 handler.close( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
453 disconnect( handler, err )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
454 writequeue = nil
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
455 handler = nil
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
456 return false
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
457 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
458 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
459 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
460 local buffer = table_concat( writequeue, "", 1, eol )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
461 local succ, err, byte = send( socket, buffer )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
462 local count = ( succ or 0 ) * STAT_UNIT
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
463 sstat = sstat + count
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
464 sendstat = sendstat + count
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
465 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
466 if succ then -- sending succesful
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
467 --writequeue = { }
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
468 eol = nil
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
469 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
470 if handler.need_tls then
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
471 out_put("server.lua: connection is ready for tls handshake");
66
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
472 handler.starttls(true);
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
473 if handler.need_tls then
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
474 out_put("server.lua: uh-oh... we still want tls, something must be wrong");
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
475 end
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
476 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
477 return true
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
478 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
479 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
480 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
481 eol = 1
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
482 return true
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
483 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
484 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
485 handler.close( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
486 disconnect( handler, err )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
487 writequeue = nil
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
488 handler = nil
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
489 return false
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
490 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
491 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
492
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
493 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
494 -- // COMPAT // --
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.getIp = handler.ip
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
497 handler.getPort = handler.clientport
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 --// handshake //--
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 local wrote, read
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
502
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
503 handler.starttls = function (now)
66
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
504 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
505 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
506 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
507 out_put("sslwrapped socket is "..tostring(socket));
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
508 if err then
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
509 out_put( "server.lua: ssl error: ", err )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
510 return nil, nil, err -- fatal error
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
511 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
512 socket:settimeout( 1 )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
513 send = socket.send
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
514 receive = socket.receive
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
515 close = socket.close
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
516 handler.ssl = function( )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
517 return true
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
518 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
519 handler.send = function( _, data, i, j )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
520 return send( socket, data, i, j )
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.receive = function( pattern, prefix )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
523 return receive( socket, pattern, prefix )
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
524 end
66
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
525
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
526 handler.handshake = coroutine_wrap( function( client )
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
527 local err
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
528 for i = 1, 10 do -- 10 handshake attemps
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
529 _, err = client:dohandshake( )
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
530 if not err then
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
531 out_put( "server.lua: ssl handshake done" )
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
532 writelen = ( wrote and removesocket( writelist, socket, writelen ) ) or writelen
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
533 handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
534 handler.dispatchdata = handler._dispatchdata
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
535 handler.need_tls = nil
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
536 socketlist[ client ] = handler
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
537 readlen = readlen + 1
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
538 readlist[ readlen ] = client
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
539 return true;
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
540 else
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
541 out_put( "server.lua: error during ssl handshake: ", err )
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
542 if err == "wantwrite" then
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
543 if wrote == nil then
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
544 writelen = writelen + 1
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
545 writelist[ writelen ] = client
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
546 wrote = true
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
547 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
548 end
66
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
549 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
550 end
66
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
551 end
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
552 _ = err ~= "closed" and close( socket )
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
553 handler.close( )
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
554 disconnect( handler, err )
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
555 writequeue = nil
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
556 handler = nil
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
557 return false -- handshake failed
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
558 end
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
559 )
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
560 handler.receivedata = handler.handshake
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
561 handler.dispatchdata = handler.handshake
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
562
66
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
563 handler.handshake( socket ) -- do handshake
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
564 end
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
565 socketlist[ socket ] = handler
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
566 readlen = readlen + 1
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
567 readlist[ readlen ] = socket
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
568
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
569 return handler, socket
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
570 end
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
571
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
572 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
573
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
574 local dispatch, disconnect = listener.listener, listener.disconnect
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
575
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
576 --// private closures of the object //--
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
577
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
578 local writequeue = { } -- list for messages to send
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
579
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
580 local eol
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
581
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
582 local rstat, sstat = 0, 0
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
583
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
584 --// local import of socket methods //--
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
585
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
586 local send = socket.send
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
587 local receive = socket.receive
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
588 local close = socket.close
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
589 local shutdown = socket.shutdown
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
590
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
591 --// public methods of the object //--
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
592
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
593 local handler = { }
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
594
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
595 handler.getstats = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
596 return rstat, sstat
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
597 end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
598
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
599 handler.listener = function( data, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
600 return listener( handler, data, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
601 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
602 handler.ssl = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
603 return false
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
604 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
605 handler.send = function( _, data, i, j )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
606 return send( socket, data, i, j )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
607 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
608 handler.receive = function( pattern, prefix )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
609 return receive( socket, pattern, prefix )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
610 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
611 handler.shutdown = function( pattern )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
612 return shutdown( socket, pattern )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
613 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
614 handler.close = function( closed )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
615 _ = not closed and shutdown( socket )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
616 _ = not closed and close( socket )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
617 writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
618 readlen = removesocket( readlist, socket, readlen )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
619 socketlist[ socket ] = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
620 out_put "server.lua: closed handler and removed socket from list"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
621 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
622 handler.ip = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
623 return ip
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
624 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
625 handler.serverport = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
626 return serverport
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
627 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
628 handler.clientport = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
629 return clientport
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
630 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
631 handler.write = function( data )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
632 if not eol then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
633 writelen = writelen + 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
634 writelist[ writelen ] = socket
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
635 eol = 0
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
636 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
637 eol = eol + 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
638 writequeue[ eol ] = data
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
639 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
640 handler.writequeue = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
641 return writequeue
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
642 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
643 handler.socket = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
644 return socket
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
645 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
646 handler.mode = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
647 return mode
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
648 end
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
649
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
650 handler.receivedata = function( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
651 local data, err, part = receive( socket, mode ) -- receive data in "mode"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
652 if not err or ( err == "timeout" or err == "wantread" ) then -- received something
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
653 local data = data or part or ""
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
654 local count = #data * STAT_UNIT
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
655 rstat = rstat + count
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
656 receivestat = receivestat + count
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
657 out_put( "server.lua: read data '", data, "', error: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
658 return dispatch( handler, data, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
659 else -- connections was closed or fatal error
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
660 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
661 handler.close( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
662 disconnect( handler, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
663 writequeue = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
664 handler = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
665 return false
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
666 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
667 end
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
668
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
669 handler.dispatchdata = function( ) -- this function writes data to handlers
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
670 local buffer = table_concat( writequeue, "", 1, eol )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
671 local succ, err, byte = send( socket, buffer )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
672 local count = ( succ or 0 ) * STAT_UNIT
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
673 sstat = sstat + count
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
674 sendstat = sendstat + count
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
675 out_put( "server.lua: sended '", buffer, "', bytes: ", succ, ", error: ", err, ", part: ", byte, ", to: ", ip, ":", clientport )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
676 if succ then -- sending succesful
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
677 --writequeue = { }
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
678 eol = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
679 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
680 return true
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
681 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
682 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
683 writequeue[ 1 ] = buffer -- insert new buffer in queue
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
684 eol = 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
685 return true
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
686 else -- connection was closed during sending or fatal error
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
687 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
688 handler.close( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
689 disconnect( handler, err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
690 writequeue = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
691 handler = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
692 return false
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
693 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
694 end
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
695
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
696 -- // COMPAT // --
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
697
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
698 handler.getIp = handler.ip
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
699 handler.getPort = handler.clientport
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
700
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
701 socketlist[ socket ] = handler
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
702 readlen = readlen + 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
703 readlist[ readlen ] = socket
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
704
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
705 return handler, socket
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
706 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
707
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
708 addtimer = function( listener )
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
709 timelistener[ #timelistener + 1 ] = listener
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
710 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
711
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
712 firetimer = function( listener )
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
713 for i, listener in ipairs( timelistener ) do
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
714 listener( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
715 end
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 addserver = function( listeners, port, addr, mode, sslctx ) -- this function provides a way for other scripts to reg a server
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
719 local err
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
720 if type( listeners ) ~= "table" then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
721 err = "invalid listener table"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
722 else
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
723 for name, func in pairs( listeners ) do
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
724 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
725 --err = "invalid listener function"
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
726 break
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
727 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
728 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
729 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
730 if not type( port ) == "number" or not ( port >= 0 and port <= 65535 ) then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
731 err = "invalid port"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
732 elseif listener[ port ] then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
733 err= "listeners on port '" .. port .. "' already exist"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
734 elseif sslctx and not luasec then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
735 err = "luasec not found"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
736 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
737 if err then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
738 out_error( "server.lua: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
739 return nil, err
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
740 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
741 addr = addr or "*"
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
742 local server, err = socket_bind( addr, port )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
743 if err then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
744 out_error( "server.lua: ", err )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
745 return nil, err
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
746 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
747 local handler, err = wrapserver( listeners, server, addr, port, mode, sslctx ) -- wrap new server socket
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
748 if not handler then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
749 server:close( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
750 return nil, err
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
751 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
752 server:settimeout( 0 )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
753 readlen = readlen + 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
754 readlist[ readlen ] = server
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
755 listener[ port ] = listeners
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
756 socketlist[ server ] = handler
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
757 out_put( "server.lua: new server listener on ", addr, ":", port )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
758 return true
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
759 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
760
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
761 removesocket = function( tbl, socket, len ) -- this function removes sockets from a list
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
762 for i, target in ipairs( tbl ) do
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
763 if target == socket then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
764 len = len - 1
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
765 table_remove( tbl, i )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
766 return len
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
767 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
768 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
769 return len
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
770 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
771
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
772 closeall = function( )
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
773 for sock, handler in pairs( socketlist ) do
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
774 handler.shutdown( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
775 handler.close( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
776 socketlist[ sock ] = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
777 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
778 writelist, readlist, socketlist = { }, { }, { }
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 closesocket = function( socket )
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
782 writelen = removesocket( writelist, socket, writelen )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
783 readlen = removesocket( readlist, socket, readlen )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
784 socketlist[ socket ] = nil
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
785 socket:close( )
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
786 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
787
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
788 loop = function( ) -- this is the main loop of the program
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
789 --signal_set( "hub", "run" )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
790 repeat
66
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
791 --[[print(readlen, writelen)
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
792 for _, s in ipairs(readlist) do print("R:", tostring(s)) end
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
793 for _, s in ipairs(writelist) do print("W:", tostring(s)) end
018705d57f09 Working TLS!
Matthew Wild <mwild1@gmail.com>
parents: 65
diff changeset
794 out_put("select()"..os.time())]]
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
795 local read, write, err = socket_select( readlist, writelist, 1 ) -- 1 sec timeout, nice for timers
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
796 for i, socket in ipairs( write ) do -- send data waiting in writequeues
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
797 local handler = socketlist[ socket ]
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
798 if handler then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
799 handler.dispatchdata( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
800 else
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
801 closesocket( socket )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
802 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
803 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
804 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
805 for i, socket in ipairs( read ) do -- receive data
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
806 local handler = socketlist[ socket ]
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
807 if handler then
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
808 handler.receivedata( )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
809 else
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
810 closesocket( socket )
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
811 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
812 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
813 end
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
814 firetimer( )
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
815 until false
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 64
diff changeset
816 return
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
817 end
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
818
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
819 ----------------------------------// BEGIN //--
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
820
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
821 ----------------------------------// PUBLIC INTERFACE //--
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
822
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
823 return {
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
824
64
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
825 add = addserver,
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
826 loop = loop,
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
827 stats = stats,
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
828 closeall = closeall,
bcd0a3975580 Convert spaces->tabs
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
829 addtimer = addtimer,
127
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 98
diff changeset
830 wraptlsclient = wraptlsclient,
1
b8787e859fd2 Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff changeset
831 }