net/server.lua

Sun, 01 Aug 2010 12:17:31 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 01 Aug 2010 12:17:31 +0100
changeset 14
578214a34ded
parent 0
73bc20975514
permissions
-rw-r--r--

Implement initial expiry support

0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 --
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- server.lua by blastbeat of the luadch project
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Re-used here under the MIT/X Consortium License
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- Modifications (C) 2008-2010 Matthew Wild, Waqas Hussain
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- // wrapping luadch stuff // --
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local use = function( what )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 return _G[ what ]
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local clean = function( tbl )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 for i, k in pairs( tbl ) do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 tbl[ i ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local log, table_concat = require ("util.logger").init("socket"), table.concat;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local out_put = function (...) return log("debug", table_concat{...}); end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local out_error = function (...) return log("warn", table_concat{...}); end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local mem_free = collectgarbage
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 ----------------------------------// DECLARATION //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 --// constants //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local STAT_UNIT = 1 -- byte
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 --// lua functions //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local type = use "type"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local pairs = use "pairs"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local ipairs = use "ipairs"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local tostring = use "tostring"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local collectgarbage = use "collectgarbage"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 --// lua libs //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local os = use "os"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local table = use "table"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local string = use "string"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local coroutine = use "coroutine"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 --// lua lib methods //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local os_time = os.time
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local os_difftime = os.difftime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local table_concat = table.concat
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local table_remove = table.remove
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 local string_len = string.len
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local string_sub = string.sub
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local coroutine_wrap = coroutine.wrap
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local coroutine_yield = coroutine.yield
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 --// extern libs //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local luasec = use "ssl"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local luasocket = use "socket" or require "socket"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 --// extern lib methods //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local ssl_wrap = ( luasec and luasec.wrap )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local socket_bind = luasocket.bind
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local socket_sleep = luasocket.sleep
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local socket_select = luasocket.select
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 local ssl_newcontext = ( luasec and luasec.newcontext )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 --// functions //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 local id
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local loop
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 local stats
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local idfalse
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local addtimer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local closeall
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local addserver
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local getserver
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local wrapserver
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 local getsettings
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 local closesocket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local removesocket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local removeserver
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 local changetimeout
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 local wrapconnection
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local changesettings
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 --// tables //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local _server
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local _readlist
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 local _timerlist
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 local _sendlist
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local _socketlist
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 local _closelist
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 local _readtimes
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local _writetimes
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 --// simple data types //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local _
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local _readlistlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local _sendlistlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local _timerlistlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 local _sendtraffic
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 local _readtraffic
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local _selecttimeout
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 local _sleeptime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 local _starttime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 local _currenttime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 local _maxsendlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 local _maxreadlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 local _checkinterval
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 local _sendtimeout
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local _readtimeout
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 local _cleanqueue
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 local _timer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 local _maxclientsperserver
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 ----------------------------------// DEFINITION //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 _server = { } -- key = port, value = table; list of listening servers
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 _readlist = { } -- array with sockets to read from
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 _sendlist = { } -- arrary with sockets to write to
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 _timerlist = { } -- array of timer functions
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 _socketlist = { } -- key = socket, value = wrapped socket (handlers)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 _readtimes = { } -- key = handler, value = timestamp of last data reading
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 _writetimes = { } -- key = handler, value = timestamp of last data writing/sending
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 _closelist = { } -- handlers to close
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 _readlistlen = 0 -- length of readlist
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 _sendlistlen = 0 -- length of sendlist
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 _timerlistlen = 0 -- lenght of timerlist
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 _sendtraffic = 0 -- some stats
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 _readtraffic = 0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 _selecttimeout = 1 -- timeout of socket.select
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 _sleeptime = 0 -- time to wait at the end of every loop
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 _maxsendlen = 51000 * 1024 -- max len of send buffer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 _maxreadlen = 25000 * 1024 -- max len of read buffer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 _checkinterval = 1200000 -- interval in secs to check idle clients
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 _sendtimeout = 60000 -- allowed send idle time in secs
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 _readtimeout = 6 * 60 * 60 -- allowed read idle time in secs
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 _cleanqueue = false -- clean bufferqueue after using
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 _maxclientsperserver = 1000
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 _maxsslhandshake = 30 -- max handshake round-trips
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 ----------------------------------// PRIVATE //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 wrapserver = function( listeners, socket, ip, serverport, pattern, sslctx, maxconnections ) -- this function wraps a server
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 maxconnections = maxconnections or _maxclientsperserver
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 local connections = 0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 local dispatch, disconnect = listeners.onconnect or listeners.onincoming, listeners.ondisconnect
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 local accept = socket.accept
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 --// public methods of the object //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 local handler = { }
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 handler.shutdown = function( ) end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 handler.ssl = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 return sslctx ~= nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 handler.sslctx = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 return sslctx
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 handler.remove = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 connections = connections - 1
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 handler.close = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 for _, handler in pairs( _socketlist ) do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 if handler.serverport == serverport then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 handler.disconnect( handler, "server closed" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 handler:close( true )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 socket:close( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 _readlistlen = removesocket( _readlist, socket, _readlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 _socketlist[ socket ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 handler = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 socket = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 --mem_free( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 out_put "server.lua: closed server handler and removed sockets from list"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 handler.ip = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 return ip
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 handler.serverport = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 return serverport
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 handler.socket = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 return socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 handler.readbuffer = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 if connections > maxconnections then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 out_put( "server.lua: refused new client connection: server full" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 return false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 local client, err = accept( socket ) -- try to accept
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 if client then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 local ip, clientport = client:getpeername( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 client:settimeout( 0 )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 local handler, client, err = wrapconnection( handler, listeners, client, ip, serverport, clientport, pattern, sslctx ) -- wrap new client socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 if err then -- error while wrapping ssl socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 return false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 connections = connections + 1
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 out_put( "server.lua: accepted new client connection from ", tostring(ip), ":", tostring(clientport), " to ", tostring(serverport))
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 return dispatch( handler )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 elseif err then -- maybe timeout or something else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 out_put( "server.lua: error with new client connection: ", tostring(err) )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 return false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 return handler
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 wrapconnection = function( server, listeners, socket, ip, serverport, clientport, pattern, sslctx ) -- this function wraps a client to a handler object
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 socket:settimeout( 0 )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 --// local import of socket methods //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 local send
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 local receive
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 local shutdown
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 --// private closures of the object //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 local ssl
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 local dispatch = listeners.onincoming
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 local status = listeners.onstatus
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 local disconnect = listeners.ondisconnect
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 local drain = listeners.ondrain
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 local bufferqueue = { } -- buffer array
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 local bufferqueuelen = 0 -- end of buffer array
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 local toclose
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 local fatalerror
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 local needtls
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 local bufferlen = 0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 local noread = false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 local nosend = false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 local sendtraffic, readtraffic = 0, 0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 local maxsendlen = _maxsendlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 local maxreadlen = _maxreadlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 --// public methods of the object //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 local handler = bufferqueue -- saves a table ^_^
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 handler.dispatch = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 return dispatch
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 handler.disconnect = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 return disconnect
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 handler.setlistener = function( self, listeners )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 dispatch = listeners.onincoming
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 disconnect = listeners.ondisconnect
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 status = listeners.onstatus
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 drain = listeners.ondrain
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 handler.getstats = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 return readtraffic, sendtraffic
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 handler.ssl = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 return ssl
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 handler.sslctx = function ( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 return sslctx
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 handler.send = function( _, data, i, j )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 return send( socket, data, i, j )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 handler.receive = function( pattern, prefix )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 return receive( socket, pattern, prefix )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 handler.shutdown = function( pattern )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 return shutdown( socket, pattern )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 handler.setoption = function (self, option, value)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 if socket.setoption then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 return socket:setoption(option, value);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 return false, "setoption not implemented";
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 handler.close = function( self, forced )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 if not handler then return true; end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 _readlistlen = removesocket( _readlist, socket, _readlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 _readtimes[ handler ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 if bufferqueuelen ~= 0 then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 if not ( forced or fatalerror ) then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 handler.sendbuffer( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 if bufferqueuelen ~= 0 then -- try again...
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 if handler then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 handler.write = nil -- ... but no further writing allowed
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 toclose = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 return false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 send( socket, table_concat( bufferqueue, "", 1, bufferqueuelen ), 1, bufferlen ) -- forced send
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 if socket then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 _ = shutdown and shutdown( socket )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 socket:close( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 _socketlist[ socket ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 socket = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 out_put "server.lua: socket already closed"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 if handler then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 _writetimes[ handler ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 _closelist[ handler ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 handler = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 if server then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 server.remove( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 out_put "server.lua: closed client handler and removed socket from list"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 return true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 handler.ip = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 return ip
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 handler.serverport = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 return serverport
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 handler.clientport = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 return clientport
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 local write = function( self, data )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 bufferlen = bufferlen + string_len( data )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 if bufferlen > maxsendlen then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 _closelist[ handler ] = "send buffer exceeded" -- cannot close the client at the moment, have to wait to the end of the cycle
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 handler.write = idfalse -- dont write anymore
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 return false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 elseif socket and not _sendlist[ socket ] then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 bufferqueuelen = bufferqueuelen + 1
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 bufferqueue[ bufferqueuelen ] = data
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 if handler then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 _writetimes[ handler ] = _writetimes[ handler ] or _currenttime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 return true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 handler.write = write
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 handler.bufferqueue = function( self )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 return bufferqueue
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 handler.socket = function( self )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 return socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 handler.set_mode = function( self, new )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 pattern = new or pattern
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 return pattern
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 handler.set_send = function ( self, newsend )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389 send = newsend or send
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390 return send
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 handler.bufferlen = function( self, readlen, sendlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 maxsendlen = sendlen or maxsendlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 maxreadlen = readlen or maxreadlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 return bufferlen, maxreadlen, maxsendlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 --TODO: Deprecate
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398 handler.lock_read = function (self, switch)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 if switch == true then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 local tmp = _readlistlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 _readlistlen = removesocket( _readlist, socket, _readlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 _readtimes[ handler ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 if _readlistlen ~= tmp then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 noread = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 elseif switch == false then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 if noread then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 noread = false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409 _readlistlen = addsocket(_readlist, socket, _readlistlen)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
410 _readtimes[ handler ] = _currenttime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
412 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413 return noread
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415 handler.pause = function (self)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 return self:lock_read(true);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418 handler.resume = function (self)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
419 return self:lock_read(false);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
420 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 handler.lock = function( self, switch )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 handler.lock_read (switch)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 if switch == true then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 handler.write = idfalse
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425 local tmp = _sendlistlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
426 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
427 _writetimes[ handler ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 if _sendlistlen ~= tmp then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 nosend = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
430 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431 elseif switch == false then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432 handler.write = write
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
433 if nosend then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
434 nosend = false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435 write( "" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
436 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438 return noread, nosend
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
439 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 local _readbuffer = function( ) -- this function reads data
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
442 if not err or (err == "wantread" or err == "timeout") then -- received something
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
443 local buffer = buffer or part or ""
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
444 local len = string_len( buffer )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445 if len > maxreadlen then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446 disconnect( handler, "receive buffer exceeded" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
447 handler:close( true )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
448 return false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
449 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450 local count = len * STAT_UNIT
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451 readtraffic = readtraffic + count
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 _readtraffic = _readtraffic + count
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453 _readtimes[ handler ] = _currenttime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 --out_put( "server.lua: read data '", buffer:gsub("[^%w%p ]", "."), "', error: ", err )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
455 return dispatch( handler, buffer, err )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456 else -- connections was closed or fatal error
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " read error: ", tostring(err) )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458 fatalerror = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459 disconnect( handler, err )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 _ = handler and handler:close( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 return false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 local _sendbuffer = function( ) -- this function sends data
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465 local succ, err, byte, buffer, count;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466 local count;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 if socket then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
468 buffer = table_concat( bufferqueue, "", 1, bufferqueuelen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469 succ, err, byte = send( socket, buffer, 1, bufferlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470 count = ( succ or byte or 0 ) * STAT_UNIT
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
471 sendtraffic = sendtraffic + count
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 _sendtraffic = _sendtraffic + count
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473 _ = _cleanqueue and clean( bufferqueue )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474 --out_put( "server.lua: sended '", buffer, "', bytes: ", tostring(succ), ", error: ", tostring(err), ", part: ", tostring(byte), ", to: ", tostring(ip), ":", tostring(clientport) )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 succ, err, count = false, "closed", 0;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478 if succ then -- sending succesful
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479 bufferqueuelen = 0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
480 bufferlen = 0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
481 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
482 _writetimes[ handler ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
483 if drain then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
484 drain(handler)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
485 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
486 _ = needtls and handler:starttls(nil)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
487 _ = toclose and handler:close( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
488 return true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
489 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
490 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
491 bufferqueue[ 1 ] = buffer -- insert new buffer in queue
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
492 bufferqueuelen = 1
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
493 bufferlen = bufferlen - byte
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
494 _writetimes[ handler ] = _currenttime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
495 return true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496 else -- connection was closed during sending or fatal error
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498 fatalerror = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499 disconnect( handler, err )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
500 _ = handler and handler:close( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501 return false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
505 -- Set the sslctx
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
506 local handshake;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
507 function handler.set_sslctx(self, new_sslctx)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
508 ssl = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
509 sslctx = new_sslctx;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
510 local wrote
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
511 local read
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
512 handshake = coroutine_wrap( function( client ) -- create handshake coroutine
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
513 local err
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
514 for i = 1, _maxsslhandshake do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
515 _sendlistlen = ( wrote and removesocket( _sendlist, client, _sendlistlen ) ) or _sendlistlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
516 _readlistlen = ( read and removesocket( _readlist, client, _readlistlen ) ) or _readlistlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
517 read, wrote = nil, nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
518 _, err = client:dohandshake( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
519 if not err then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
520 out_put( "server.lua: ssl handshake done" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
521 handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
522 handler.sendbuffer = _sendbuffer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
523 _ = status and status( handler, "ssl-handshake-complete" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
524 _readlistlen = addsocket(_readlist, client, _readlistlen)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
525 return true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
526 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
527 out_put( "server.lua: error during ssl handshake: ", tostring(err) )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
528 if err == "wantwrite" and not wrote then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
529 _sendlistlen = addsocket(_sendlist, client, _sendlistlen)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
530 wrote = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
531 elseif err == "wantread" and not read then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
532 _readlistlen = addsocket(_readlist, client, _readlistlen)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
533 read = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
534 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
535 break;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
536 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
537 --coroutine_yield( handler, nil, err ) -- handshake not finished
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
538 coroutine_yield( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
539 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
540 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
541 disconnect( handler, "ssl handshake failed" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
542 _ = handler and handler:close( true ) -- forced disconnect
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
543 return false -- handshake failed
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
544 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
545 )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
546 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
547 if luasec then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
548 if sslctx then -- ssl?
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
549 handler:set_sslctx(sslctx);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
550 out_put("server.lua: ", "starting ssl handshake")
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
551 local err
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
552 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
553 if err then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
554 out_put( "server.lua: ssl error: ", tostring(err) )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
555 --mem_free( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
556 return nil, nil, err -- fatal error
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
557 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
558 socket:settimeout( 0 )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
559 handler.readbuffer = handshake
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
560 handler.sendbuffer = handshake
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
561 handshake( socket ) -- do handshake
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
562 if not socket then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
563 return nil, nil, "ssl handshake failed";
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
564 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
565 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
566 local sslctx;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
567 handler.starttls = function( self, _sslctx)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
568 if _sslctx then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
569 sslctx = _sslctx;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
570 handler:set_sslctx(sslctx);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
571 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
572 if bufferqueuelen > 0 then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
573 out_put "server.lua: we need to do tls, but delaying until send buffer empty"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
574 needtls = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
575 return
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
576 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
577 out_put( "server.lua: attempting to start tls on " .. tostring( socket ) )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
578 local oldsocket, err = socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
579 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
580 --out_put( "server.lua: sslwrapped socket is " .. tostring( socket ) )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
581 if err then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
582 out_put( "server.lua: error while starting tls on client: ", tostring(err) )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
583 return nil, err -- fatal error
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
584 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
585
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
586 socket:settimeout( 0 )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
587
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
588 -- add the new socket to our system
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
589
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
590 send = socket.send
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
591 receive = socket.receive
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
592 shutdown = id
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
593
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
594 _socketlist[ socket ] = handler
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
595 _readlistlen = addsocket(_readlist, socket, _readlistlen)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
596
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
597 -- remove traces of the old socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
598
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
599 _readlistlen = removesocket( _readlist, oldsocket, _readlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
600 _sendlistlen = removesocket( _sendlist, oldsocket, _sendlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
601 _socketlist[ oldsocket ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
602
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
603 handler.starttls = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
604 needtls = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
605
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
606 -- Secure now
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
607 ssl = true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
608
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
609 handler.readbuffer = handshake
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
610 handler.sendbuffer = handshake
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
611 handshake( socket ) -- do handshake
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
612 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
613 handler.readbuffer = _readbuffer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
614 handler.sendbuffer = _sendbuffer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
615 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
616 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
617 handler.readbuffer = _readbuffer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
618 handler.sendbuffer = _sendbuffer
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
619 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
620 send = socket.send
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
621 receive = socket.receive
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
622 shutdown = ( ssl and id ) or socket.shutdown
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
623
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
624 _socketlist[ socket ] = handler
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
625 _readlistlen = addsocket(_readlist, socket, _readlistlen)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
626 return handler, socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
627 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
628
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
629 id = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
630 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
631
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
632 idfalse = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
633 return false
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
634 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
635
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
636 addsocket = function( list, socket, len )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
637 if not list[ socket ] then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
638 len = len + 1
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
639 list[ len ] = socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
640 list[ socket ] = len
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
641 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
642 return len;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
643 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
644
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
645 removesocket = function( list, socket, len ) -- this function removes sockets from a list ( copied from copas )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
646 local pos = list[ socket ]
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
647 if pos then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
648 list[ socket ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
649 local last = list[ len ]
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
650 list[ len ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
651 if last ~= socket then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
652 list[ last ] = pos
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
653 list[ pos ] = last
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
654 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
655 return len - 1
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
656 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
657 return len
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
658 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
659
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
660 closesocket = function( socket )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
661 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
662 _readlistlen = removesocket( _readlist, socket, _readlistlen )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
663 _socketlist[ socket ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
664 socket:close( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
665 --mem_free( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
666 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
667
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
668 local function link(sender, receiver, buffersize)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
669 sender:set_mode(buffersize);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
670 local sender_locked;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
671 local _sendbuffer = receiver.sendbuffer;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
672 function receiver.sendbuffer()
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
673 _sendbuffer();
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
674 if sender_locked and receiver.bufferlen() < buffersize then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
675 sender:lock_read(false); -- Unlock now
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
676 sender_locked = nil;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
677 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
678 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
679
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
680 local _readbuffer = sender.readbuffer;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
681 function sender.readbuffer()
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
682 _readbuffer();
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
683 if not sender_locked and receiver.bufferlen() >= buffersize then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
684 sender_locked = true;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
685 sender:lock_read(true);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
686 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
687 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
688 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
689
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
690 ----------------------------------// PUBLIC //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
691
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
692 addserver = function( addr, port, listeners, pattern, sslctx ) -- this function provides a way for other scripts to reg a server
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
693 local err
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
694 if type( listeners ) ~= "table" then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
695 err = "invalid listener table"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
696 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
697 if type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
698 err = "invalid port"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
699 elseif _server[ port ] then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
700 err = "listeners on port '" .. port .. "' already exist"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
701 elseif sslctx and not luasec then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
702 err = "luasec not found"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
703 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
704 if err then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
705 out_error( "server.lua, port ", port, ": ", err )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
706 return nil, err
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
707 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
708 addr = addr or "*"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
709 local server, err = socket_bind( addr, port )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
710 if err then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
711 out_error( "server.lua, port ", port, ": ", err )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
712 return nil, err
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
713 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
714 local handler, err = wrapserver( listeners, server, addr, port, pattern, sslctx, _maxclientsperserver ) -- wrap new server socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
715 if not handler then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
716 server:close( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
717 return nil, err
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
718 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
719 server:settimeout( 0 )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
720 _readlistlen = addsocket(_readlist, server, _readlistlen)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
721 _server[ port ] = handler
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
722 _socketlist[ server ] = handler
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
723 out_put( "server.lua: new "..(sslctx and "ssl " or "").."server listener on '", addr, ":", port, "'" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
724 return handler
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
725 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
726
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
727 getserver = function ( port )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
728 return _server[ port ];
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
729 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
730
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
731 removeserver = function( port )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
732 local handler = _server[ port ]
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
733 if not handler then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
734 return nil, "no server found on port '" .. tostring( port ) .. "'"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
735 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
736 handler:close( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
737 _server[ port ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
738 return true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
739 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
740
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
741 closeall = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
742 for _, handler in pairs( _socketlist ) do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
743 handler:close( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
744 _socketlist[ _ ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
745 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
746 _readlistlen = 0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
747 _sendlistlen = 0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
748 _timerlistlen = 0
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
749 _server = { }
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
750 _readlist = { }
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
751 _sendlist = { }
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
752 _timerlist = { }
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
753 _socketlist = { }
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
754 --mem_free( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
755 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
756
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
757 getsettings = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
758 return _selecttimeout, _sleeptime, _maxsendlen, _maxreadlen, _checkinterval, _sendtimeout, _readtimeout, _cleanqueue, _maxclientsperserver, _maxsslhandshake
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
759 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
760
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
761 changesettings = function( new )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
762 if type( new ) ~= "table" then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
763 return nil, "invalid settings table"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
764 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
765 _selecttimeout = tonumber( new.timeout ) or _selecttimeout
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
766 _sleeptime = tonumber( new.sleeptime ) or _sleeptime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
767 _maxsendlen = tonumber( new.maxsendlen ) or _maxsendlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
768 _maxreadlen = tonumber( new.maxreadlen ) or _maxreadlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
769 _checkinterval = tonumber( new.checkinterval ) or _checkinterval
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
770 _sendtimeout = tonumber( new.sendtimeout ) or _sendtimeout
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
771 _readtimeout = tonumber( new.readtimeout ) or _readtimeout
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
772 _cleanqueue = new.cleanqueue
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
773 _maxclientsperserver = new._maxclientsperserver or _maxclientsperserver
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
774 _maxsslhandshake = new._maxsslhandshake or _maxsslhandshake
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
775 return true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
776 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
777
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
778 addtimer = function( listener )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
779 if type( listener ) ~= "function" then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
780 return nil, "invalid listener function"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
781 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
782 _timerlistlen = _timerlistlen + 1
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
783 _timerlist[ _timerlistlen ] = listener
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
784 return true
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
785 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
786
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
787 stats = function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
788 return _readtraffic, _sendtraffic, _readlistlen, _sendlistlen, _timerlistlen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
789 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
790
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
791 local dontstop = true; -- thinking about tomorrow, ...
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
792
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
793 setquitting = function (quit)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
794 dontstop = not quit;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
795 return;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
796 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
797
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
798 loop = function( ) -- this is the main loop of the program
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
799 while dontstop do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
800 local read, write, err = socket_select( _readlist, _sendlist, _selecttimeout )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
801 for i, socket in ipairs( write ) do -- send data waiting in writequeues
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
802 local handler = _socketlist[ socket ]
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
803 if handler then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
804 handler.sendbuffer( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
805 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
806 closesocket( socket )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
807 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
808 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
809 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
810 for i, socket in ipairs( read ) do -- receive data
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
811 local handler = _socketlist[ socket ]
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
812 if handler then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
813 handler.readbuffer( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
814 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
815 closesocket( socket )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
816 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
817 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
818 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
819 for handler, err in pairs( _closelist ) do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
820 handler.disconnect( )( handler, err )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
821 handler:close( true ) -- forced disconnect
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
822 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
823 clean( _closelist )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
824 _currenttime = os_time( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
825 if os_difftime( _currenttime - _timer ) >= 1 then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
826 for i = 1, _timerlistlen do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
827 _timerlist[ i ]( _currenttime ) -- fire timers
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
828 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
829 _timer = _currenttime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
830 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
831 socket_sleep( _sleeptime ) -- wait some time
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
832 --collectgarbage( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
833 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
834 return "quitting"
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
835 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
836
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
837 local function get_backend()
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
838 return "select";
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
839 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
840
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
841 --// EXPERIMENTAL //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
842
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
843 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
844 local handler = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
845 _socketlist[ socket ] = handler
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
846 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
847 if listeners.onconnect then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
848 -- When socket is writeable, call onconnect
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
849 local _sendbuffer = handler.sendbuffer;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
850 handler.sendbuffer = function ()
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
851 listeners.onconnect(handler);
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
852 handler.sendbuffer = _sendbuffer;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
853 -- If there was data with the incoming packet, handle it now.
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
854 if #handler:bufferqueue() > 0 then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
855 return _sendbuffer();
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
856 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
857 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
858 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
859 return handler, socket
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
860 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
861
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
862 local addclient = function( address, port, listeners, pattern, sslctx )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
863 local client, err = luasocket.tcp( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
864 if err then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
865 return nil, err
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
866 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
867 client:settimeout( 0 )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
868 _, err = client:connect( address, port )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
869 if err then -- try again
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
870 local handler = wrapclient( client, address, port, listeners )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
871 else
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
872 wrapconnection( nil, listeners, client, address, port, "clientport", pattern, sslctx )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
873 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
874 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
875
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
876 --// EXPERIMENTAL //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
877
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
878 ----------------------------------// BEGIN //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
879
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
880 use "setmetatable" ( _socketlist, { __mode = "k" } )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
881 use "setmetatable" ( _readtimes, { __mode = "k" } )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
882 use "setmetatable" ( _writetimes, { __mode = "k" } )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
883
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
884 _timer = os_time( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
885 _starttime = os_time( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
886
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
887 addtimer( function( )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
888 local difftime = os_difftime( _currenttime - _starttime )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
889 if difftime > _checkinterval then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
890 _starttime = _currenttime
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
891 for handler, timestamp in pairs( _writetimes ) do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
892 if os_difftime( _currenttime - timestamp ) > _sendtimeout then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
893 --_writetimes[ handler ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
894 handler.disconnect( )( handler, "send timeout" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
895 handler:close( true ) -- forced disconnect
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
896 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
897 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
898 for handler, timestamp in pairs( _readtimes ) do
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
899 if os_difftime( _currenttime - timestamp ) > _readtimeout then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
900 --_readtimes[ handler ] = nil
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
901 handler.disconnect( )( handler, "read timeout" )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
902 handler:close( ) -- forced disconnect?
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
903 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
904 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
905 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
906 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
907 )
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
908
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
909 local function setlogger(new_logger)
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
910 local old_logger = log;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
911 if new_logger then
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
912 log = new_logger;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
913 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
914 return old_logger;
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
915 end
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
916
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
917 ----------------------------------// PUBLIC INTERFACE //--
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
918
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
919 return {
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
920
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
921 addclient = addclient,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
922 wrapclient = wrapclient,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
923
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
924 loop = loop,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
925 link = link,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
926 stats = stats,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
927 closeall = closeall,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
928 addtimer = addtimer,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
929 addserver = addserver,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
930 getserver = getserver,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
931 setlogger = setlogger,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
932 getsettings = getsettings,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
933 setquitting = setquitting,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
934 removeserver = removeserver,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
935 get_backend = get_backend,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
936 changesettings = changesettings,
73bc20975514 Memory is the storehouse in which the substance of our knowledge is treasured up.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
937 }

mercurial