Software /
code /
prosody
Annotate
net/server.lua @ 2766:5fe6ea49cf70
net.server_select: Restore checks for wantread/timeout errors on receive
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 03 Dec 2009 19:18:18 +0000 |
parent | 2765:3cb9df793269 |
child | 2923:b7049746bd29 |
rev | line source |
---|---|
1544
c7f0938bc33e
net.server: Fix copyright header line endings and text
Matthew Wild <mwild1@gmail.com>
parents:
1543
diff
changeset
|
1 -- |
c7f0938bc33e
net.server: Fix copyright header line endings and text
Matthew Wild <mwild1@gmail.com>
parents:
1543
diff
changeset
|
2 -- server.lua by blastbeat of the luadch project |
c7f0938bc33e
net.server: Fix copyright header line endings and text
Matthew Wild <mwild1@gmail.com>
parents:
1543
diff
changeset
|
3 -- Re-used here under the MIT/X Consortium License |
c7f0938bc33e
net.server: Fix copyright header line endings and text
Matthew Wild <mwild1@gmail.com>
parents:
1543
diff
changeset
|
4 -- |
c7f0938bc33e
net.server: Fix copyright header line endings and text
Matthew Wild <mwild1@gmail.com>
parents:
1543
diff
changeset
|
5 -- Modifications (C) 2008-2009 Matthew Wild, Waqas Hussain |
c7f0938bc33e
net.server: Fix copyright header line endings and text
Matthew Wild <mwild1@gmail.com>
parents:
1543
diff
changeset
|
6 -- |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
7 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
8 -- // wrapping luadch stuff // -- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
9 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
10 local use = function( what ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
11 return _G[ what ] |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
12 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
13 local clean = function( tbl ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
14 for i, k in pairs( tbl ) do |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
15 tbl[ i ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
16 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
17 end |
740
cc25d75b4027
server.lua: Use Prosody logger for errors, silence non-errors
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
18 |
cc25d75b4027
server.lua: Use Prosody logger for errors, silence non-errors
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
19 local log, table_concat = require ("util.logger").init("socket"), table.concat; |
741
97577b6c07ca
server.lua: Comment out unnecessary or very verbose logging, and log the rest as debug level
Matthew Wild <mwild1@gmail.com>
parents:
740
diff
changeset
|
20 local out_put = function (...) return log("debug", table_concat{...}); end |
740
cc25d75b4027
server.lua: Use Prosody logger for errors, silence non-errors
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
21 local out_error = function (...) return log("warn", table_concat{...}); end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
22 local mem_free = collectgarbage |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
23 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
24 ----------------------------------// DECLARATION //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
25 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
26 --// constants //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
27 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
28 local STAT_UNIT = 1 -- byte |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
29 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
30 --// lua functions //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
31 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
32 local type = use "type" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
33 local pairs = use "pairs" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
34 local ipairs = use "ipairs" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
35 local tostring = use "tostring" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
36 local collectgarbage = use "collectgarbage" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
37 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
38 --// lua libs //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
39 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
40 local os = use "os" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
41 local table = use "table" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
42 local string = use "string" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
43 local coroutine = use "coroutine" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
44 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
45 --// lua lib methods //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
46 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
47 local os_time = os.time |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
48 local os_difftime = os.difftime |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
49 local table_concat = table.concat |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
50 local table_remove = table.remove |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
51 local string_len = string.len |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
52 local string_sub = string.sub |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
53 local coroutine_wrap = coroutine.wrap |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
54 local coroutine_yield = coroutine.yield |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
55 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
56 --// extern libs //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
57 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
58 local luasec = select( 2, pcall( require, "ssl" ) ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
59 local luasocket = require "socket" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
60 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
61 --// extern lib methods //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
62 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
63 local ssl_wrap = ( luasec and luasec.wrap ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
64 local socket_bind = luasocket.bind |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
65 local socket_sleep = luasocket.sleep |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
66 local socket_select = luasocket.select |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
67 local ssl_newcontext = ( luasec and luasec.newcontext ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
68 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
69 --// functions //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
70 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
71 local id |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
72 local loop |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
73 local stats |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
74 local idfalse |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
75 local addtimer |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
76 local closeall |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
77 local addserver |
1098
37ea264e133d
net.server: Add getserver() method to find the server listening on the specified port
Matthew Wild <mwild1@gmail.com>
parents:
999
diff
changeset
|
78 local getserver |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
79 local wrapserver |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
80 local getsettings |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
81 local closesocket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
82 local removesocket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
83 local removeserver |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
84 local changetimeout |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
85 local wrapconnection |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
86 local changesettings |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
87 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
88 --// tables //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
89 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
90 local _server |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
91 local _readlist |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
92 local _timerlist |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
93 local _sendlist |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
94 local _socketlist |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
95 local _closelist |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
96 local _readtimes |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
97 local _writetimes |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
98 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
99 --// simple data types //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
100 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
101 local _ |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
102 local _readlistlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
103 local _sendlistlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
104 local _timerlistlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
105 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
106 local _sendtraffic |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
107 local _readtraffic |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
108 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
109 local _selecttimeout |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
110 local _sleeptime |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
111 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
112 local _starttime |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
113 local _currenttime |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
114 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
115 local _maxsendlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
116 local _maxreadlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
117 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
118 local _checkinterval |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
119 local _sendtimeout |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
120 local _readtimeout |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
121 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
122 local _cleanqueue |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
123 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
124 local _timer |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
125 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
126 local _maxclientsperserver |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
127 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
128 ----------------------------------// DEFINITION //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
129 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
130 _server = { } -- key = port, value = table; list of listening servers |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
131 _readlist = { } -- array with sockets to read from |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
132 _sendlist = { } -- arrary with sockets to write to |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
133 _timerlist = { } -- array of timer functions |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
134 _socketlist = { } -- key = socket, value = wrapped socket (handlers) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
135 _readtimes = { } -- key = handler, value = timestamp of last data reading |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
136 _writetimes = { } -- key = handler, value = timestamp of last data writing/sending |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
137 _closelist = { } -- handlers to close |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
138 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
139 _readlistlen = 0 -- length of readlist |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
140 _sendlistlen = 0 -- length of sendlist |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
141 _timerlistlen = 0 -- lenght of timerlist |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
142 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
143 _sendtraffic = 0 -- some stats |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
144 _readtraffic = 0 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
145 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
146 _selecttimeout = 1 -- timeout of socket.select |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
147 _sleeptime = 0 -- time to wait at the end of every loop |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
148 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
149 _maxsendlen = 51000 * 1024 -- max len of send buffer |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
150 _maxreadlen = 25000 * 1024 -- max len of read buffer |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
151 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
152 _checkinterval = 1200000 -- interval in secs to check idle clients |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
153 _sendtimeout = 60000 -- allowed send idle time in secs |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
154 _readtimeout = 6 * 60 * 60 -- allowed read idle time in secs |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
155 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
156 _cleanqueue = false -- clean bufferqueue after using |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
157 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
158 _maxclientsperserver = 1000 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
159 |
1884
6eaa440bc9fc
net.server: Increase SSL handshake round-trip limit, and make it configurable
Matthew Wild <mwild1@gmail.com>
parents:
1883
diff
changeset
|
160 _maxsslhandshake = 30 -- max handshake round-trips |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
161 ----------------------------------// PRIVATE //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
162 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
163 wrapserver = function( listeners, socket, ip, serverport, pattern, sslctx, maxconnections, startssl ) -- this function wraps a server |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
164 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
165 maxconnections = maxconnections or _maxclientsperserver |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
166 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
167 local connections = 0 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
168 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
169 local dispatch, disconnect = listeners.incoming or listeners.listener, listeners.disconnect |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
170 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
171 local err |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
172 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
173 local ssl = false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
174 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
175 if sslctx then |
1570
a46453758631
net.server: Continue to initialise port listener even when SSL errors occur
Matthias Diener
parents:
1546
diff
changeset
|
176 ssl = true |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
177 if not ssl_newcontext then |
1570
a46453758631
net.server: Continue to initialise port listener even when SSL errors occur
Matthias Diener
parents:
1546
diff
changeset
|
178 out_error "luasec not found" |
a46453758631
net.server: Continue to initialise port listener even when SSL errors occur
Matthias Diener
parents:
1546
diff
changeset
|
179 ssl = false |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
180 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
181 if type( sslctx ) ~= "table" then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
182 out_error "server.lua: wrong server sslctx" |
1570
a46453758631
net.server: Continue to initialise port listener even when SSL errors occur
Matthias Diener
parents:
1546
diff
changeset
|
183 ssl = false |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
184 end |
1612
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
185 local ctx; |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
186 ctx, err = ssl_newcontext( sslctx ) |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
187 if not ctx then |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
188 err = err or "wrong sslctx parameters" |
1612
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
189 local file; |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
190 file = err:match("^error loading (.-) %("); |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
191 if file then |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
192 if file == "private key" then |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
193 file = sslctx.key or "your private key"; |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
194 elseif file == "certificate" then |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
195 file = sslctx.certificate or "your certificate file"; |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
196 end |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
197 local reason = err:match("%((.+)%)$") or "some reason"; |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
198 if reason == "Permission denied" then |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
199 reason = "Check that the permissions allow Prosody to read this file."; |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
200 elseif reason == "No such file or directory" then |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
201 reason = "Check that the path is correct, and the file exists."; |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
202 elseif reason == "system lib" then |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
203 reason = "Previous error (see logs), or other system error."; |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
204 else |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
205 reason = "Reason: "..tostring(reason or "unknown"):lower(); |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
206 end |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
207 log("error", "SSL/TLS: Failed to load %s: %s", file, reason); |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
208 else |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
209 log("error", "SSL/TLS: Error initialising for port %d: %s", serverport, err ); |
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
210 end |
1570
a46453758631
net.server: Continue to initialise port listener even when SSL errors occur
Matthias Diener
parents:
1546
diff
changeset
|
211 ssl = false |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
212 end |
1612
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
213 sslctx = ctx; |
1570
a46453758631
net.server: Continue to initialise port listener even when SSL errors occur
Matthias Diener
parents:
1546
diff
changeset
|
214 end |
a46453758631
net.server: Continue to initialise port listener even when SSL errors occur
Matthias Diener
parents:
1546
diff
changeset
|
215 if not ssl then |
1574
3692706f78e3
net.server: Set sslctx to false when SSL wrapping fails, to avoid attempting to wrap clients with a broken context
Matthew Wild <mwild1@gmail.com>
parents:
1570
diff
changeset
|
216 sslctx = false; |
1581
4cdf9cefa0bc
net.server: Fail to open port if legacy SSL requested, but SSL not available
Matthias Diene
parents:
1577
diff
changeset
|
217 if startssl then |
1612
0413aaf9edae
net.server: Much improve SSL/TLS error reporting, do our best to understand and hide OpenSSL's ridiculously unfriendly error messages
Matthew Wild <mwild1@gmail.com>
parents:
1581
diff
changeset
|
218 log("error", "Failed to listen on port %d due to SSL/TLS to SSL/TLS initialisation errors (see logs)", serverport ) |
1581
4cdf9cefa0bc
net.server: Fail to open port if legacy SSL requested, but SSL not available
Matthias Diene
parents:
1577
diff
changeset
|
219 return nil, "Cannot start ssl, see log for details" |
4cdf9cefa0bc
net.server: Fail to open port if legacy SSL requested, but SSL not available
Matthias Diene
parents:
1577
diff
changeset
|
220 end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
221 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
222 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
223 local accept = socket.accept |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
224 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
225 --// public methods of the object //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
226 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
227 local handler = { } |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
228 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
229 handler.shutdown = function( ) end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
230 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
231 handler.ssl = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
232 return ssl |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
233 end |
1881
35e05b77f6ce
net.server: Provide a way to get sslctx of a server
Matthew Wild <mwild1@gmail.com>
parents:
1865
diff
changeset
|
234 handler.sslctx = function( ) |
35e05b77f6ce
net.server: Provide a way to get sslctx of a server
Matthew Wild <mwild1@gmail.com>
parents:
1865
diff
changeset
|
235 return sslctx |
35e05b77f6ce
net.server: Provide a way to get sslctx of a server
Matthew Wild <mwild1@gmail.com>
parents:
1865
diff
changeset
|
236 end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
237 handler.remove = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
238 connections = connections - 1 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
239 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
240 handler.close = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
241 for _, handler in pairs( _socketlist ) do |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
242 if handler.serverport == serverport then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
243 handler.disconnect( handler, "server closed" ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
244 handler.close( true ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
245 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
246 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
247 socket:close( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
248 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
249 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
250 _socketlist[ socket ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
251 handler = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
252 socket = nil |
1683
a73b0557d87a
net.server: Comment all calls to mem_free/collectgarbage... not really suitable for a realtime server, let's trust Lua's GC - it's good.
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
253 --mem_free( ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
254 out_put "server.lua: closed server handler and removed sockets from list" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
255 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
256 handler.ip = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
257 return ip |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
258 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
259 handler.serverport = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
260 return serverport |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
261 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
262 handler.socket = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
263 return socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
264 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
265 handler.readbuffer = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
266 if connections > maxconnections then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
267 out_put( "server.lua: refused new client connection: server full" ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
268 return false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
269 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
270 local client, err = accept( socket ) -- try to accept |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
271 if client then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
272 local ip, clientport = client:getpeername( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
273 client:settimeout( 0 ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
274 local handler, client, err = wrapconnection( handler, listeners, client, ip, serverport, clientport, pattern, sslctx, startssl ) -- wrap new client socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
275 if err then -- error while wrapping ssl socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
276 return false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
277 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
278 connections = connections + 1 |
962
872f5d85636e
net.server: Ensure some logging parameters are strings
Matthew Wild <mwild1@gmail.com>
parents:
885
diff
changeset
|
279 out_put( "server.lua: accepted new client connection from ", tostring(ip), ":", tostring(clientport), " to ", tostring(serverport)) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
280 return dispatch( handler ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
281 elseif err then -- maybe timeout or something else |
962
872f5d85636e
net.server: Ensure some logging parameters are strings
Matthew Wild <mwild1@gmail.com>
parents:
885
diff
changeset
|
282 out_put( "server.lua: error with new client connection: ", tostring(err) ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
283 return false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
284 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
285 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
286 return handler |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
287 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
288 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
289 wrapconnection = function( server, listeners, socket, ip, serverport, clientport, pattern, sslctx, startssl ) -- this function wraps a client to a handler object |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
290 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
291 socket:settimeout( 0 ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
292 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
293 --// local import of socket methods //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
294 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
295 local send |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
296 local receive |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
297 local shutdown |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
298 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
299 --// private closures of the object //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
300 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
301 local ssl |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
302 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
303 local dispatch = listeners.incoming or listeners.listener |
1883
6866cf298701
net.server: Call handler's status() to notify it of ssl handshake success
Matthew Wild <mwild1@gmail.com>
parents:
1882
diff
changeset
|
304 local status = listeners.status |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
305 local disconnect = listeners.disconnect |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
306 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
307 local bufferqueue = { } -- buffer array |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
308 local bufferqueuelen = 0 -- end of buffer array |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
309 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
310 local toclose |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
311 local fatalerror |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
312 local needtls |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
313 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
314 local bufferlen = 0 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
315 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
316 local noread = false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
317 local nosend = false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
318 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
319 local sendtraffic, readtraffic = 0, 0 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
320 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
321 local maxsendlen = _maxsendlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
322 local maxreadlen = _maxreadlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
323 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
324 --// public methods of the object //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
325 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
326 local handler = bufferqueue -- saves a table ^_^ |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
327 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
328 handler.dispatch = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
329 return dispatch |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
330 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
331 handler.disconnect = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
332 return disconnect |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
333 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
334 handler.setlistener = function( listeners ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
335 dispatch = listeners.incoming |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
336 disconnect = listeners.disconnect |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
337 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
338 handler.getstats = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
339 return readtraffic, sendtraffic |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
340 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
341 handler.ssl = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
342 return ssl |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
343 end |
1882
ad04cac04354
net.server: Provide a way to get sslctx of a client connection
Matthew Wild <mwild1@gmail.com>
parents:
1881
diff
changeset
|
344 handler.sslctx = function ( ) |
ad04cac04354
net.server: Provide a way to get sslctx of a client connection
Matthew Wild <mwild1@gmail.com>
parents:
1881
diff
changeset
|
345 return sslctx |
ad04cac04354
net.server: Provide a way to get sslctx of a client connection
Matthew Wild <mwild1@gmail.com>
parents:
1881
diff
changeset
|
346 end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
347 handler.send = function( _, data, i, j ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
348 return send( socket, data, i, j ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
349 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
350 handler.receive = function( pattern, prefix ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
351 return receive( socket, pattern, prefix ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
352 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
353 handler.shutdown = function( pattern ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
354 return shutdown( socket, pattern ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
355 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
356 handler.close = function( forced ) |
867
21347a87e185
net.server: Check for potential nil handler on close
Matthew Wild <mwild1@gmail.com>
parents:
855
diff
changeset
|
357 if not handler then return true; end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
358 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
359 _readtimes[ handler ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
360 if bufferqueuelen ~= 0 then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
361 if not ( forced or fatalerror ) then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
362 handler.sendbuffer( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
363 if bufferqueuelen ~= 0 then -- try again... |
999
c088139d8f05
net.server: Fix potential nil handler usage
Matthew Wild <mwild1@gmail.com>
parents:
996
diff
changeset
|
364 if handler then |
c088139d8f05
net.server: Fix potential nil handler usage
Matthew Wild <mwild1@gmail.com>
parents:
996
diff
changeset
|
365 handler.write = nil -- ... but no further writing allowed |
c088139d8f05
net.server: Fix potential nil handler usage
Matthew Wild <mwild1@gmail.com>
parents:
996
diff
changeset
|
366 end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
367 toclose = true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
368 return false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
369 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
370 else |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
371 send( socket, table_concat( bufferqueue, "", 1, bufferqueuelen ), 1, bufferlen ) -- forced send |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
372 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
373 end |
1952
4a15b176fca8
net.server: Better handling of closing sockets during send
Matthew Wild <mwild1@gmail.com>
parents:
1863
diff
changeset
|
374 if socket then |
4a15b176fca8
net.server: Better handling of closing sockets during send
Matthew Wild <mwild1@gmail.com>
parents:
1863
diff
changeset
|
375 _ = shutdown and shutdown( socket ) |
4a15b176fca8
net.server: Better handling of closing sockets during send
Matthew Wild <mwild1@gmail.com>
parents:
1863
diff
changeset
|
376 socket:close( ) |
4a15b176fca8
net.server: Better handling of closing sockets during send
Matthew Wild <mwild1@gmail.com>
parents:
1863
diff
changeset
|
377 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
4a15b176fca8
net.server: Better handling of closing sockets during send
Matthew Wild <mwild1@gmail.com>
parents:
1863
diff
changeset
|
378 _socketlist[ socket ] = nil |
4a15b176fca8
net.server: Better handling of closing sockets during send
Matthew Wild <mwild1@gmail.com>
parents:
1863
diff
changeset
|
379 socket = nil |
4a15b176fca8
net.server: Better handling of closing sockets during send
Matthew Wild <mwild1@gmail.com>
parents:
1863
diff
changeset
|
380 else |
4a15b176fca8
net.server: Better handling of closing sockets during send
Matthew Wild <mwild1@gmail.com>
parents:
1863
diff
changeset
|
381 out_put "server.lua: socket already closed" |
4a15b176fca8
net.server: Better handling of closing sockets during send
Matthew Wild <mwild1@gmail.com>
parents:
1863
diff
changeset
|
382 end |
999
c088139d8f05
net.server: Fix potential nil handler usage
Matthew Wild <mwild1@gmail.com>
parents:
996
diff
changeset
|
383 if handler then |
c088139d8f05
net.server: Fix potential nil handler usage
Matthew Wild <mwild1@gmail.com>
parents:
996
diff
changeset
|
384 _writetimes[ handler ] = nil |
c088139d8f05
net.server: Fix potential nil handler usage
Matthew Wild <mwild1@gmail.com>
parents:
996
diff
changeset
|
385 _closelist[ handler ] = nil |
c088139d8f05
net.server: Fix potential nil handler usage
Matthew Wild <mwild1@gmail.com>
parents:
996
diff
changeset
|
386 handler = nil |
c088139d8f05
net.server: Fix potential nil handler usage
Matthew Wild <mwild1@gmail.com>
parents:
996
diff
changeset
|
387 end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
388 if server then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
389 server.remove( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
390 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
391 out_put "server.lua: closed client handler and removed socket from list" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
392 return true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
393 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
394 handler.ip = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
395 return ip |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
396 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
397 handler.serverport = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
398 return serverport |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
399 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
400 handler.clientport = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
401 return clientport |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
402 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
403 local write = function( data ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
404 bufferlen = bufferlen + string_len( data ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
405 if bufferlen > maxsendlen then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
406 _closelist[ handler ] = "send buffer exceeded" -- cannot close the client at the moment, have to wait to the end of the cycle |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
407 handler.write = idfalse -- dont write anymore |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
408 return false |
775
d4b6714fc829
net.server: Fix nil table index assignment
Matthew Wild <mwild1@gmail.com>
parents:
773
diff
changeset
|
409 elseif socket and not _sendlist[ socket ] then |
2057
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
410 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
411 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
412 bufferqueuelen = bufferqueuelen + 1 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
413 bufferqueue[ bufferqueuelen ] = data |
815
cf8392613f46
net.server: Fix some more potential nil handler accesses
Matthew Wild <mwild1@gmail.com>
parents:
775
diff
changeset
|
414 if handler then |
cf8392613f46
net.server: Fix some more potential nil handler accesses
Matthew Wild <mwild1@gmail.com>
parents:
775
diff
changeset
|
415 _writetimes[ handler ] = _writetimes[ handler ] or _currenttime |
cf8392613f46
net.server: Fix some more potential nil handler accesses
Matthew Wild <mwild1@gmail.com>
parents:
775
diff
changeset
|
416 end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
417 return true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
418 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
419 handler.write = write |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
420 handler.bufferqueue = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
421 return bufferqueue |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
422 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
423 handler.socket = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
424 return socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
425 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
426 handler.pattern = function( new ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
427 pattern = new or pattern |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
428 return pattern |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
429 end |
885
38ed2db19a55
net.server: Allow replacement of send function on an open socket
Matthew Wild <mwild1@gmail.com>
parents:
868
diff
changeset
|
430 handler.setsend = function ( newsend ) |
38ed2db19a55
net.server: Allow replacement of send function on an open socket
Matthew Wild <mwild1@gmail.com>
parents:
868
diff
changeset
|
431 send = newsend or send |
38ed2db19a55
net.server: Allow replacement of send function on an open socket
Matthew Wild <mwild1@gmail.com>
parents:
868
diff
changeset
|
432 return send |
38ed2db19a55
net.server: Allow replacement of send function on an open socket
Matthew Wild <mwild1@gmail.com>
parents:
868
diff
changeset
|
433 end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
434 handler.bufferlen = function( readlen, sendlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
435 maxsendlen = sendlen or maxsendlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
436 maxreadlen = readlen or maxreadlen |
2765
3cb9df793269
net.server_select: Make bufferlen() method return, of all things, the buffer length
Matthew Wild <mwild1@gmail.com>
parents:
2764
diff
changeset
|
437 return bufferlen, maxreadlen, maxsendlen |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
438 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
439 handler.lock = function( switch ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
440 if switch == true then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
441 handler.write = idfalse |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
442 local tmp = _sendlistlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
443 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
444 _writetimes[ handler ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
445 if _sendlistlen ~= tmp then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
446 nosend = true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
447 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
448 tmp = _readlistlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
449 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
450 _readtimes[ handler ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
451 if _readlistlen ~= tmp then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
452 noread = true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
453 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
454 elseif switch == false then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
455 handler.write = write |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
456 if noread then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
457 noread = false |
2057
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
458 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
459 _readtimes[ handler ] = _currenttime |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
460 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
461 if nosend then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
462 nosend = false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
463 write( "" ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
464 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
465 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
466 return noread, nosend |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
467 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
468 local _readbuffer = function( ) -- this function reads data |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
469 local buffer, err, part = receive( socket, pattern ) -- receive buffer with "pattern" |
2766
5fe6ea49cf70
net.server_select: Restore checks for wantread/timeout errors on receive
Matthew Wild <mwild1@gmail.com>
parents:
2765
diff
changeset
|
470 if not err or (err == "wantread" or err == "timeout") or string_len(part) > 0 then -- received something |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
471 local buffer = buffer or part or "" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
472 local len = string_len( buffer ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
473 if len > maxreadlen then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
474 disconnect( handler, "receive buffer exceeded" ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
475 handler.close( true ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
476 return false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
477 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
478 local count = len * STAT_UNIT |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
479 readtraffic = readtraffic + count |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
480 _readtraffic = _readtraffic + count |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
481 _readtimes[ handler ] = _currenttime |
1886
d19c0a21bfb6
net.server: Small logging improvements
Matthew Wild <mwild1@gmail.com>
parents:
1885
diff
changeset
|
482 --out_put( "server.lua: read data '", buffer:gsub("[^%w%p ]", "."), "', error: ", err ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
483 return dispatch( handler, buffer, err ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
484 else -- connections was closed or fatal error |
1886
d19c0a21bfb6
net.server: Small logging improvements
Matthew Wild <mwild1@gmail.com>
parents:
1885
diff
changeset
|
485 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " read error: ", tostring(err) ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
486 fatalerror = true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
487 disconnect( handler, err ) |
773
c36edc1934c5
net.server: Fix potential fatal error in server.lua
Matthew Wild <mwild1@gmail.com>
parents:
770
diff
changeset
|
488 _ = handler and handler.close( ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
489 return false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
490 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
491 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
492 local _sendbuffer = function( ) -- this function sends data |
1785
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
493 local succ, err, byte, buffer, count; |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
494 local count; |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
495 if socket then |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
496 buffer = table_concat( bufferqueue, "", 1, bufferqueuelen ) |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
497 succ, err, byte = send( socket, buffer, 1, bufferlen ) |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
498 count = ( succ or byte or 0 ) * STAT_UNIT |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
499 sendtraffic = sendtraffic + count |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
500 _sendtraffic = _sendtraffic + count |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
501 _ = _cleanqueue and clean( bufferqueue ) |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
502 --out_put( "server.lua: sended '", buffer, "', bytes: ", tostring(succ), ", error: ", tostring(err), ", part: ", tostring(byte), ", to: ", tostring(ip), ":", tostring(clientport) ) |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
503 else |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
504 succ, err, count = false, "closed", 0; |
17c0fb598c97
net.server: Add checking for nil sockets to prevent traceback
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
505 end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
506 if succ then -- sending succesful |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
507 bufferqueuelen = 0 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
508 bufferlen = 0 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
509 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) -- delete socket from writelist |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
510 _ = needtls and handler.starttls(true) |
770
bddf52121908
Fix for nil handler error after close
Matthew Wild <mwild1@gmail.com>
parents:
741
diff
changeset
|
511 _writetimes[ handler ] = nil |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
512 _ = toclose and handler.close( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
513 return true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
514 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
515 buffer = string_sub( buffer, byte + 1, bufferlen ) -- new buffer |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
516 bufferqueue[ 1 ] = buffer -- insert new buffer in queue |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
517 bufferqueuelen = 1 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
518 bufferlen = bufferlen - byte |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
519 _writetimes[ handler ] = _currenttime |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
520 return true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
521 else -- connection was closed during sending or fatal error |
1886
d19c0a21bfb6
net.server: Small logging improvements
Matthew Wild <mwild1@gmail.com>
parents:
1885
diff
changeset
|
522 out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
523 fatalerror = true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
524 disconnect( handler, err ) |
815
cf8392613f46
net.server: Fix some more potential nil handler accesses
Matthew Wild <mwild1@gmail.com>
parents:
775
diff
changeset
|
525 _ = handler and handler.close( ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
526 return false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
527 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
528 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
529 |
1979
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
530 -- Set the sslctx |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
531 local handshake; |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
532 function handler.set_sslctx(new_sslctx) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
533 ssl = true |
1979
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
534 sslctx = new_sslctx; |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
535 local wrote |
846
77244ba297ac
net.server: Some fixes for SSL/TLS handshake handling
Matthew Wild <mwild1@gmail.com>
parents:
815
diff
changeset
|
536 local read |
1979
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
537 handshake = coroutine_wrap( function( client ) -- create handshake coroutine |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
538 local err |
1884
6eaa440bc9fc
net.server: Increase SSL handshake round-trip limit, and make it configurable
Matthew Wild <mwild1@gmail.com>
parents:
1883
diff
changeset
|
539 for i = 1, _maxsslhandshake do |
2059
d4fb80b60c65
server.lua: Standardise on 'client' variable to refer to the socket, makes the code a bit cleaner
Matthew Wild <mwild1@gmail.com>
parents:
2057
diff
changeset
|
540 _sendlistlen = ( wrote and removesocket( _sendlist, client, _sendlistlen ) ) or _sendlistlen |
d4fb80b60c65
server.lua: Standardise on 'client' variable to refer to the socket, makes the code a bit cleaner
Matthew Wild <mwild1@gmail.com>
parents:
2057
diff
changeset
|
541 _readlistlen = ( read and removesocket( _readlist, client, _readlistlen ) ) or _readlistlen |
846
77244ba297ac
net.server: Some fixes for SSL/TLS handshake handling
Matthew Wild <mwild1@gmail.com>
parents:
815
diff
changeset
|
542 read, wrote = nil, nil |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
543 _, err = client:dohandshake( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
544 if not err then |
850
edbd0d1fce93
net.server: Don't retry if client closes socket during SSL handshaking
Matthew Wild <mwild1@gmail.com>
parents:
846
diff
changeset
|
545 out_put( "server.lua: ssl handshake done" ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
546 handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
547 handler.sendbuffer = _sendbuffer |
1883
6866cf298701
net.server: Call handler's status() to notify it of ssl handshake success
Matthew Wild <mwild1@gmail.com>
parents:
1882
diff
changeset
|
548 _ = status and status( handler, "ssl-handshake-complete" ) |
2059
d4fb80b60c65
server.lua: Standardise on 'client' variable to refer to the socket, makes the code a bit cleaner
Matthew Wild <mwild1@gmail.com>
parents:
2057
diff
changeset
|
549 _readlistlen = addsocket(_readlist, client, _readlistlen) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
550 return true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
551 else |
1979
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
552 out_put( "server.lua: error during ssl handshake: ", tostring(err) ) |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
553 if err == "wantwrite" and not wrote then |
2057
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
554 _sendlistlen = addsocket(_sendlist, client, _sendlistlen) |
1979
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
555 wrote = true |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
556 elseif err == "wantread" and not read then |
2057
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
557 _readlistlen = addsocket(_readlist, client, _readlistlen) |
1979
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
558 read = true |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
559 else |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
560 break; |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
561 end |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
562 --coroutine_yield( handler, nil, err ) -- handshake not finished |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
563 coroutine_yield( ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
564 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
565 end |
850
edbd0d1fce93
net.server: Don't retry if client closes socket during SSL handshaking
Matthew Wild <mwild1@gmail.com>
parents:
846
diff
changeset
|
566 disconnect( handler, "ssl handshake failed" ) |
1533
8c13be7aa9f0
net.server: Don't close handler if it is already nil when SSL handshake fails
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
567 _ = handler and handler.close( true ) -- forced disconnect |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
568 return false -- handshake failed |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
569 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
570 ) |
1979
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
571 end |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
572 if sslctx then -- ssl? |
52ddaa2e6ab8
net.server: Allow changing the sslctx of a connection after it has been established
Matthew Wild <mwild1@gmail.com>
parents:
1953
diff
changeset
|
573 handler.set_sslctx(sslctx); |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
574 if startssl then -- ssl now? |
741
97577b6c07ca
server.lua: Comment out unnecessary or very verbose logging, and log the rest as debug level
Matthew Wild <mwild1@gmail.com>
parents:
740
diff
changeset
|
575 --out_put("server.lua: ", "starting ssl handshake") |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
576 local err |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
577 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
578 if err then |
962
872f5d85636e
net.server: Ensure some logging parameters are strings
Matthew Wild <mwild1@gmail.com>
parents:
885
diff
changeset
|
579 out_put( "server.lua: ssl error: ", tostring(err) ) |
1683
a73b0557d87a
net.server: Comment all calls to mem_free/collectgarbage... not really suitable for a realtime server, let's trust Lua's GC - it's good.
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
580 --mem_free( ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
581 return nil, nil, err -- fatal error |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
582 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
583 socket:settimeout( 0 ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
584 handler.readbuffer = handshake |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
585 handler.sendbuffer = handshake |
1546
7d7bf705675d
net.server: Initiate the handshake when we should, the more correcter fix for the previous correct fix
Matthew Wild <mwild1@gmail.com>
parents:
1544
diff
changeset
|
586 handshake( socket ) -- do handshake |
7d7bf705675d
net.server: Initiate the handshake when we should, the more correcter fix for the previous correct fix
Matthew Wild <mwild1@gmail.com>
parents:
1544
diff
changeset
|
587 if not socket then |
1535
0afc8ae01515
net.server: Don't continue with socket initialisation if SSL handshake failed, fixes nil socket traceback
Matthew Wild <mwild1@gmail.com>
parents:
1533
diff
changeset
|
588 return nil, nil, "ssl handshake failed"; |
0afc8ae01515
net.server: Don't continue with socket initialisation if SSL handshake failed, fixes nil socket traceback
Matthew Wild <mwild1@gmail.com>
parents:
1533
diff
changeset
|
589 end |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
590 else |
1214
27d4cfb8fb7a
net.server: A connection is not secure if we delay the TLS handshake
Matthew Wild <mwild1@gmail.com>
parents:
1100
diff
changeset
|
591 -- We're not automatically doing SSL, so we're not secure (yet) |
27d4cfb8fb7a
net.server: A connection is not secure if we delay the TLS handshake
Matthew Wild <mwild1@gmail.com>
parents:
1100
diff
changeset
|
592 ssl = false |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
593 handler.starttls = function( now ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
594 if not now then |
741
97577b6c07ca
server.lua: Comment out unnecessary or very verbose logging, and log the rest as debug level
Matthew Wild <mwild1@gmail.com>
parents:
740
diff
changeset
|
595 --out_put "server.lua: we need to do tls, but delaying until later" |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
596 needtls = true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
597 return |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
598 end |
741
97577b6c07ca
server.lua: Comment out unnecessary or very verbose logging, and log the rest as debug level
Matthew Wild <mwild1@gmail.com>
parents:
740
diff
changeset
|
599 --out_put( "server.lua: attempting to start tls on " .. tostring( socket ) ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
600 local oldsocket, err = socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
601 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket |
741
97577b6c07ca
server.lua: Comment out unnecessary or very verbose logging, and log the rest as debug level
Matthew Wild <mwild1@gmail.com>
parents:
740
diff
changeset
|
602 --out_put( "server.lua: sslwrapped socket is " .. tostring( socket ) ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
603 if err then |
962
872f5d85636e
net.server: Ensure some logging parameters are strings
Matthew Wild <mwild1@gmail.com>
parents:
885
diff
changeset
|
604 out_put( "server.lua: error while starting tls on client: ", tostring(err) ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
605 return nil, err -- fatal error |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
606 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
607 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
608 socket:settimeout( 0 ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
609 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
610 -- add the new socket to our system |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
611 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
612 send = socket.send |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
613 receive = socket.receive |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
614 shutdown = id |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
615 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
616 _socketlist[ socket ] = handler |
2057
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
617 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
618 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
619 -- remove traces of the old socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
620 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
621 _readlistlen = removesocket( _readlist, oldsocket, _readlistlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
622 _sendlistlen = removesocket( _sendlist, oldsocket, _sendlistlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
623 _socketlist[ oldsocket ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
624 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
625 handler.starttls = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
626 needtls = nil |
1214
27d4cfb8fb7a
net.server: A connection is not secure if we delay the TLS handshake
Matthew Wild <mwild1@gmail.com>
parents:
1100
diff
changeset
|
627 |
27d4cfb8fb7a
net.server: A connection is not secure if we delay the TLS handshake
Matthew Wild <mwild1@gmail.com>
parents:
1100
diff
changeset
|
628 -- Secure now |
27d4cfb8fb7a
net.server: A connection is not secure if we delay the TLS handshake
Matthew Wild <mwild1@gmail.com>
parents:
1100
diff
changeset
|
629 ssl = true |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
630 |
846
77244ba297ac
net.server: Some fixes for SSL/TLS handshake handling
Matthew Wild <mwild1@gmail.com>
parents:
815
diff
changeset
|
631 handler.readbuffer = handshake |
77244ba297ac
net.server: Some fixes for SSL/TLS handshake handling
Matthew Wild <mwild1@gmail.com>
parents:
815
diff
changeset
|
632 handler.sendbuffer = handshake |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
633 handshake( socket ) -- do handshake |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
634 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
635 handler.readbuffer = _readbuffer |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
636 handler.sendbuffer = _sendbuffer |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
637 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
638 else -- normal connection |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
639 ssl = false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
640 handler.readbuffer = _readbuffer |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
641 handler.sendbuffer = _sendbuffer |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
642 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
643 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
644 send = socket.send |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
645 receive = socket.receive |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
646 shutdown = ( ssl and id ) or socket.shutdown |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
647 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
648 _socketlist[ socket ] = handler |
2057
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
649 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
650 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
651 return handler, socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
652 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
653 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
654 id = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
655 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
656 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
657 idfalse = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
658 return false |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
659 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
660 |
2057
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
661 addsocket = function( list, socket, len ) |
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
662 if not list[ socket ] then |
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
663 len = len + 1 |
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
664 list[ len ] = socket |
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
665 list[ socket ] = len |
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
666 end |
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
667 return len; |
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
668 end |
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
669 |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
670 removesocket = function( list, socket, len ) -- this function removes sockets from a list ( copied from copas ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
671 local pos = list[ socket ] |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
672 if pos then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
673 list[ socket ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
674 local last = list[ len ] |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
675 list[ len ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
676 if last ~= socket then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
677 list[ last ] = pos |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
678 list[ pos ] = last |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
679 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
680 return len - 1 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
681 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
682 return len |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
683 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
684 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
685 closesocket = function( socket ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
686 _sendlistlen = removesocket( _sendlist, socket, _sendlistlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
687 _readlistlen = removesocket( _readlist, socket, _readlistlen ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
688 _socketlist[ socket ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
689 socket:close( ) |
1683
a73b0557d87a
net.server: Comment all calls to mem_free/collectgarbage... not really suitable for a realtime server, let's trust Lua's GC - it's good.
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
690 --mem_free( ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
691 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
692 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
693 ----------------------------------// PUBLIC //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
694 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
695 addserver = function( listeners, port, addr, pattern, sslctx, maxconnections, startssl ) -- this function provides a way for other scripts to reg a server |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
696 local err |
741
97577b6c07ca
server.lua: Comment out unnecessary or very verbose logging, and log the rest as debug level
Matthew Wild <mwild1@gmail.com>
parents:
740
diff
changeset
|
697 --out_put("server.lua: autossl on ", port, " is ", startssl) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
698 if type( listeners ) ~= "table" then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
699 err = "invalid listener table" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
700 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
701 if not type( port ) == "number" or not ( port >= 0 and port <= 65535 ) then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
702 err = "invalid port" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
703 elseif _server[ port ] then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
704 err = "listeners on port '" .. port .. "' already exist" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
705 elseif sslctx and not luasec then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
706 err = "luasec not found" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
707 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
708 if err then |
1297
9c8f86a3878d
net.server: Display server port in error messages
Matthew Wild <mwild1@gmail.com>
parents:
1214
diff
changeset
|
709 out_error( "server.lua, port ", port, ": ", err ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
710 return nil, err |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
711 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
712 addr = addr or "*" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
713 local server, err = socket_bind( addr, port ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
714 if err then |
1297
9c8f86a3878d
net.server: Display server port in error messages
Matthew Wild <mwild1@gmail.com>
parents:
1214
diff
changeset
|
715 out_error( "server.lua, port ", port, ": ", err ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
716 return nil, err |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
717 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
718 local handler, err = wrapserver( listeners, server, addr, port, pattern, sslctx, maxconnections, startssl ) -- wrap new server socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
719 if not handler then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
720 server:close( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
721 return nil, err |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
722 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
723 server:settimeout( 0 ) |
2057
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
724 _readlistlen = addsocket(_readlist, server, _readlistlen) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
725 _server[ port ] = handler |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
726 _socketlist[ server ] = handler |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
727 out_put( "server.lua: new server listener on '", addr, ":", port, "'" ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
728 return handler |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
729 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
730 |
1098
37ea264e133d
net.server: Add getserver() method to find the server listening on the specified port
Matthew Wild <mwild1@gmail.com>
parents:
999
diff
changeset
|
731 getserver = function ( port ) |
37ea264e133d
net.server: Add getserver() method to find the server listening on the specified port
Matthew Wild <mwild1@gmail.com>
parents:
999
diff
changeset
|
732 return _server[ port ]; |
37ea264e133d
net.server: Add getserver() method to find the server listening on the specified port
Matthew Wild <mwild1@gmail.com>
parents:
999
diff
changeset
|
733 end |
37ea264e133d
net.server: Add getserver() method to find the server listening on the specified port
Matthew Wild <mwild1@gmail.com>
parents:
999
diff
changeset
|
734 |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
735 removeserver = function( port ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
736 local handler = _server[ port ] |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
737 if not handler then |
1863
51b1612325e6
net.server: Fix missing concatenation operator in error message
Matthew Wild <mwild1@gmail.com>
parents:
1804
diff
changeset
|
738 return nil, "no server found on port '" .. tostring( port ) .. "'" |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
739 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
740 handler.close( ) |
1577
c0e6d11e35ce
net.server: Remove listener from listeners table when calling removeserver
Matthew Wild <mwild1@gmail.com>
parents:
1574
diff
changeset
|
741 _server[ port ] = nil |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
742 return true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
743 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
744 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
745 closeall = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
746 for _, handler in pairs( _socketlist ) do |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
747 handler.close( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
748 _socketlist[ _ ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
749 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
750 _readlistlen = 0 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
751 _sendlistlen = 0 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
752 _timerlistlen = 0 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
753 _server = { } |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
754 _readlist = { } |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
755 _sendlist = { } |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
756 _timerlist = { } |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
757 _socketlist = { } |
1683
a73b0557d87a
net.server: Comment all calls to mem_free/collectgarbage... not really suitable for a realtime server, let's trust Lua's GC - it's good.
Matthew Wild <mwild1@gmail.com>
parents:
1612
diff
changeset
|
758 --mem_free( ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
759 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
760 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
761 getsettings = function( ) |
1884
6eaa440bc9fc
net.server: Increase SSL handshake round-trip limit, and make it configurable
Matthew Wild <mwild1@gmail.com>
parents:
1883
diff
changeset
|
762 return _selecttimeout, _sleeptime, _maxsendlen, _maxreadlen, _checkinterval, _sendtimeout, _readtimeout, _cleanqueue, _maxclientsperserver, _maxsslhandshake |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
763 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
764 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
765 changesettings = function( new ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
766 if type( new ) ~= "table" then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
767 return nil, "invalid settings table" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
768 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
769 _selecttimeout = tonumber( new.timeout ) or _selecttimeout |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
770 _sleeptime = tonumber( new.sleeptime ) or _sleeptime |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
771 _maxsendlen = tonumber( new.maxsendlen ) or _maxsendlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
772 _maxreadlen = tonumber( new.maxreadlen ) or _maxreadlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
773 _checkinterval = tonumber( new.checkinterval ) or _checkinterval |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
774 _sendtimeout = tonumber( new.sendtimeout ) or _sendtimeout |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
775 _readtimeout = tonumber( new.readtimeout ) or _readtimeout |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
776 _cleanqueue = new.cleanqueue |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
777 _maxclientsperserver = new._maxclientsperserver or _maxclientsperserver |
1884
6eaa440bc9fc
net.server: Increase SSL handshake round-trip limit, and make it configurable
Matthew Wild <mwild1@gmail.com>
parents:
1883
diff
changeset
|
778 _maxsslhandshake = new._maxsslhandshake or _maxsslhandshake |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
779 return true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
780 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
781 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
782 addtimer = function( listener ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
783 if type( listener ) ~= "function" then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
784 return nil, "invalid listener function" |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
785 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
786 _timerlistlen = _timerlistlen + 1 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
787 _timerlist[ _timerlistlen ] = listener |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
788 return true |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
789 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
790 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
791 stats = function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
792 return _readtraffic, _sendtraffic, _readlistlen, _sendlistlen, _timerlistlen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
793 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
794 |
1098
37ea264e133d
net.server: Add getserver() method to find the server listening on the specified port
Matthew Wild <mwild1@gmail.com>
parents:
999
diff
changeset
|
795 local dontstop = true; -- thinking about tomorrow, ... |
989
ec239c044f31
net.server: Add server.setquitting() to gracefully quit the socket loop
Matthew Wild <mwild1@gmail.com>
parents:
962
diff
changeset
|
796 |
ec239c044f31
net.server: Add server.setquitting() to gracefully quit the socket loop
Matthew Wild <mwild1@gmail.com>
parents:
962
diff
changeset
|
797 setquitting = function (quit) |
ec239c044f31
net.server: Add server.setquitting() to gracefully quit the socket loop
Matthew Wild <mwild1@gmail.com>
parents:
962
diff
changeset
|
798 dontstop = not quit; |
ec239c044f31
net.server: Add server.setquitting() to gracefully quit the socket loop
Matthew Wild <mwild1@gmail.com>
parents:
962
diff
changeset
|
799 return; |
ec239c044f31
net.server: Add server.setquitting() to gracefully quit the socket loop
Matthew Wild <mwild1@gmail.com>
parents:
962
diff
changeset
|
800 end |
ec239c044f31
net.server: Add server.setquitting() to gracefully quit the socket loop
Matthew Wild <mwild1@gmail.com>
parents:
962
diff
changeset
|
801 |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
802 loop = function( ) -- this is the main loop of the program |
989
ec239c044f31
net.server: Add server.setquitting() to gracefully quit the socket loop
Matthew Wild <mwild1@gmail.com>
parents:
962
diff
changeset
|
803 while dontstop do |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
804 local read, write, err = socket_select( _readlist, _sendlist, _selecttimeout ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
805 for i, socket in ipairs( write ) do -- send data waiting in writequeues |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
806 local handler = _socketlist[ socket ] |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
807 if handler then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
808 handler.sendbuffer( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
809 else |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
810 closesocket( socket ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
811 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
812 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
813 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
814 for i, socket in ipairs( read ) do -- receive data |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
815 local handler = _socketlist[ socket ] |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
816 if handler then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
817 handler.readbuffer( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
818 else |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
819 closesocket( socket ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
820 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
821 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
822 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
823 for handler, err in pairs( _closelist ) do |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
824 handler.disconnect( )( handler, err ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
825 handler.close( true ) -- forced disconnect |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
826 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
827 clean( _closelist ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
828 _currenttime = os_time( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
829 if os_difftime( _currenttime - _timer ) >= 1 then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
830 for i = 1, _timerlistlen do |
1885
1fde15a415e6
net.server: Pass current time to timer callbacks
Matthew Wild <mwild1@gmail.com>
parents:
1884
diff
changeset
|
831 _timerlist[ i ]( _currenttime ) -- fire timers |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
832 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
833 _timer = _currenttime |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
834 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
835 socket_sleep( _sleeptime ) -- wait some time |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
836 --collectgarbage( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
837 end |
989
ec239c044f31
net.server: Add server.setquitting() to gracefully quit the socket loop
Matthew Wild <mwild1@gmail.com>
parents:
962
diff
changeset
|
838 return "quitting" |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
839 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
840 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
841 --// EXPERIMENTAL //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
842 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
843 local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx, startssl ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
844 local handler = wrapconnection( nil, listeners, socket, ip, serverport, "clientport", pattern, sslctx, startssl ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
845 _socketlist[ socket ] = handler |
2057
ee2929e620bb
server.lua: Add addsocket(), refactor all code to use it, fixes nasty socket object leak - moral of the story is to not duplicate code, and not prematurely optimise. But you knew that already didn't you?
Matthew Wild <mwild1@gmail.com>
parents:
1979
diff
changeset
|
846 _sendlistlen = addsocket(_sendlist, socket, _sendlistlen) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
847 return handler, socket |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
848 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
849 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
850 local addclient = function( address, port, listeners, pattern, sslctx, startssl ) |
1035
8cc7e4452b2b
net.server: Fix global access
Matthew Wild <mwild1@gmail.com>
parents:
999
diff
changeset
|
851 local client, err = luasocket.tcp( ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
852 if err then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
853 return nil, err |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
854 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
855 client:settimeout( 0 ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
856 _, err = client:connect( address, port ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
857 if err then -- try again |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
858 local handler = wrapclient( client, address, port, listeners ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
859 else |
1039
cc4280f45d0e
net.server: Fix global access/incorrect parameter
Matthew Wild <mwild1@gmail.com>
parents:
1036
diff
changeset
|
860 wrapconnection( nil, listeners, client, address, port, "clientport", pattern, sslctx, startssl ) |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
861 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
862 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
863 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
864 --// EXPERIMENTAL //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
865 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
866 ----------------------------------// BEGIN //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
867 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
868 use "setmetatable" ( _socketlist, { __mode = "k" } ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
869 use "setmetatable" ( _readtimes, { __mode = "k" } ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
870 use "setmetatable" ( _writetimes, { __mode = "k" } ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
871 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
872 _timer = os_time( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
873 _starttime = os_time( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
874 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
875 addtimer( function( ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
876 local difftime = os_difftime( _currenttime - _starttime ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
877 if difftime > _checkinterval then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
878 _starttime = _currenttime |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
879 for handler, timestamp in pairs( _writetimes ) do |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
880 if os_difftime( _currenttime - timestamp ) > _sendtimeout then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
881 --_writetimes[ handler ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
882 handler.disconnect( )( handler, "send timeout" ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
883 handler.close( true ) -- forced disconnect |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
884 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
885 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
886 for handler, timestamp in pairs( _readtimes ) do |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
887 if os_difftime( _currenttime - timestamp ) > _readtimeout then |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
888 --_readtimes[ handler ] = nil |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
889 handler.disconnect( )( handler, "read timeout" ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
890 handler.close( ) -- forced disconnect? |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
891 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
892 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
893 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
894 end |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
895 ) |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
896 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
897 ----------------------------------// PUBLIC INTERFACE //-- |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
898 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
899 return { |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
900 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
901 addclient = addclient, |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
902 wrapclient = wrapclient, |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
903 |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
904 loop = loop, |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
905 stats = stats, |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
906 closeall = closeall, |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
907 addtimer = addtimer, |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
908 addserver = addserver, |
1098
37ea264e133d
net.server: Add getserver() method to find the server listening on the specified port
Matthew Wild <mwild1@gmail.com>
parents:
999
diff
changeset
|
909 getserver = getserver, |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
910 getsettings = getsettings, |
989
ec239c044f31
net.server: Add server.setquitting() to gracefully quit the socket loop
Matthew Wild <mwild1@gmail.com>
parents:
962
diff
changeset
|
911 setquitting = setquitting, |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
912 removeserver = removeserver, |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
913 changesettings = changesettings, |
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
731
diff
changeset
|
914 } |