@@ -17,53 +17,56 @@ local _M = socket
1717-- Exported auxiliar functions
1818---- -------------------------------------------------------------------------
1919function _M .connect4 (address , port , laddress , lport )
20- return socket .connect (address , port , laddress , lport , " inet" )
20+ return socket .connect (address , port , laddress , lport , " inet" )
2121end
2222
2323function _M .connect6 (address , port , laddress , lport )
24- return socket .connect (address , port , laddress , lport , " inet6" )
24+ return socket .connect (address , port , laddress , lport , " inet6" )
2525end
2626
2727function _M .bind (host , port , backlog )
28- if host == " *" then host = " 0.0.0.0" end
29- local addrinfo , err = socket .dns .getaddrinfo (host );
30- if not addrinfo then return nil , err end
31- local sock , res
32- err = " no info on address"
33- for i , alt in base .ipairs (addrinfo ) do
34- if alt .family == " inet" then
35- sock , err = socket .tcp4 ()
36- else
37- sock , err = socket .tcp6 ()
38- end
39- if not sock then return nil , err end
40- sock :setoption (" reuseaddr" , true )
41- res , err = sock :bind (alt .addr , port )
42- if not res then
43- sock :close ()
44- else
45- res , err = sock :listen (backlog )
46- if not res then
47- sock :close ()
48- else
49- return sock
50- end
51- end
52- end
53- return nil , err
28+ if host == " *" then host = " 0.0.0.0" end
29+ local addrinfo , err = socket .dns .getaddrinfo (host );
30+ if not addrinfo then return nil , err end
31+ local sock , res
32+ err = " no info on address"
33+ for i , alt in base .ipairs (addrinfo ) do
34+ if alt .family == " inet" then
35+ sock , err = socket .tcp4 ()
36+ else
37+ sock , err = socket .tcp6 ()
38+ end
39+ if not sock then return nil , err end
40+ -- sock:setoption("reuseaddr", true)
41+ res , err = sock :bind (alt .addr , port )
42+ if not res then
43+ sock :close ()
44+ else
45+ res , err = sock :listen (backlog )
46+ if not res then
47+ sock :close ()
48+ else
49+ return sock
50+ end
51+ end
52+ end
53+ return nil , err
5454end
5555
5656_M .try = _M .newtry ()
5757
5858function _M .choose (table )
59- return function (name , opt1 , opt2 )
60- if base .type (name ) ~= " string" then
61- name , opt1 , opt2 = " default" , name , opt1
62- end
63- local f = table [name or " nil" ]
64- if not f then base .error (" unknown key (" .. base .tostring (name ) .. " )" , 3 )
65- else return f (opt1 , opt2 ) end
66- end
59+ return function (name , opt1 , opt2 )
60+ if base .type (name ) ~= " string" then
61+ name , opt1 , opt2 = " default" , name , opt1
62+ end
63+ local f = table [name or " nil" ]
64+ if not f then
65+ base .error (" unknown key (" .. base .tostring (name ) .. " )" , 3 )
66+ else
67+ return f (opt1 , opt2 )
68+ end
69+ end
6770end
6871
6972---- -------------------------------------------------------------------------
@@ -77,68 +80,76 @@ _M.sinkt = sinkt
7780_M .BLOCKSIZE = 2048
7881
7982sinkt [" close-when-done" ] = function (sock )
80- return base .setmetatable ({
81- getfd = function () return sock :getfd () end ,
82- dirty = function () return sock :dirty () end
83- }, {
84- __call = function (self , chunk , err )
85- if not chunk then
86- sock :close ()
87- return 1
88- else return sock :send (chunk ) end
89- end
90- })
83+ return base .setmetatable ({
84+ getfd = function () return sock :getfd () end ,
85+ dirty = function () return sock :dirty () end
86+ }, {
87+ __call = function (self , chunk , err )
88+ if not chunk then
89+ sock :close ()
90+ return 1
91+ else
92+ return sock :send (chunk )
93+ end
94+ end
95+ })
9196end
9297
9398sinkt [" keep-open" ] = function (sock )
94- return base .setmetatable ({
95- getfd = function () return sock :getfd () end ,
96- dirty = function () return sock :dirty () end
97- }, {
98- __call = function (self , chunk , err )
99- if chunk then return sock :send (chunk )
100- else return 1 end
101- end
102- })
99+ return base .setmetatable ({
100+ getfd = function () return sock :getfd () end ,
101+ dirty = function () return sock :dirty () end
102+ }, {
103+ __call = function (self , chunk , err )
104+ if chunk then
105+ return sock :send (chunk )
106+ else
107+ return 1
108+ end
109+ end
110+ })
103111end
104112
105113sinkt [" default" ] = sinkt [" keep-open" ]
106114
107115_M .sink = _M .choose (sinkt )
108116
109117sourcet [" by-length" ] = function (sock , length )
110- return base .setmetatable ({
111- getfd = function () return sock :getfd () end ,
112- dirty = function () return sock :dirty () end
113- }, {
114- __call = function ()
115- if length <= 0 then return nil end
116- local size = math.min (socket .BLOCKSIZE , length )
117- local chunk , err = sock :receive (size )
118- if err then return nil , err end
119- length = length - string.len (chunk )
120- return chunk
121- end
122- })
118+ return base .setmetatable ({
119+ getfd = function () return sock :getfd () end ,
120+ dirty = function () return sock :dirty () end
121+ }, {
122+ __call = function ()
123+ if length <= 0 then return nil end
124+ local size = math.min (socket .BLOCKSIZE , length )
125+ local chunk , err = sock :receive (size )
126+ if err then return nil , err end
127+ length = length - string.len (chunk )
128+ return chunk
129+ end
130+ })
123131end
124132
125133sourcet [" until-closed" ] = function (sock )
126- local done
127- return base .setmetatable ({
128- getfd = function () return sock :getfd () end ,
129- dirty = function () return sock :dirty () end
130- }, {
131- __call = function ()
132- if done then return nil end
133- local chunk , err , partial = sock :receive (socket .BLOCKSIZE )
134- if not err then return chunk
135- elseif err == " closed" then
136- sock :close ()
137- done = 1
138- return partial
139- else return nil , err end
140- end
141- })
134+ local done
135+ return base .setmetatable ({
136+ getfd = function () return sock :getfd () end ,
137+ dirty = function () return sock :dirty () end
138+ }, {
139+ __call = function ()
140+ if done then return nil end
141+ local chunk , err , partial = sock :receive (socket .BLOCKSIZE )
142+ if not err then
143+ return chunk
144+ elseif err == " closed" then
145+ sock :close ()
146+ done = 1
147+ return partial
148+ else
149+ return nil , err
150+ end
151+ end
152+ })
142153end
143154
144155
0 commit comments