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