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