Skip to content

Commit 824032f

Browse files
committed
fix timeout options for blocking sockets
1 parent 98635cf commit 824032f

10 files changed

Lines changed: 64 additions & 59 deletions

examples/tcp_client_nonblocking.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ while true do
3737
print(str)
3838
return
3939
end
40-
elseif err ~= "timeout" then
40+
elseif err ~= "tryagain" then
4141
error(err)
4242
end
4343
end

examples/tcp_client_nonblocking_tls.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ while true do
5454
print(str)
5555
return
5656
end
57-
elseif err ~= "timeout" then
57+
elseif err ~= "tryagain" then
5858
error(err)
5959
end
6060
end

examples/tcp_server_nonblocking.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ do -- server
3737
client:close()
3838
elseif err == "closed" then
3939
client:close()
40-
elseif err ~= "timeout" then
40+
elseif err ~= "tryagain" then
4141
error(err)
4242
end
43-
elseif err ~= "timeout" then
43+
elseif err ~= "tryagain" then
4444
error(err)
4545
end
4646
end

ljsocket.lua

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,13 +1284,14 @@ do
12841284
return true
12851285
end
12861286

1287-
if key:lower() == "rcvtimeo" then
1287+
if key:lower() == "rcvtimeo" or key:lower() == "sndtimeo" then
12881288
if ffi.os == "Windows" then
1289-
val = ffi.new("int[1]", val)
1289+
val = ffi.new("int[1]", val * 1000)
12901290
else
1291-
local usec = val * 1000
1292-
val = timeval()
1293-
val.tv_usec = usec
1291+
local tv = timeval()
1292+
tv.tv_sec = math.floor(val)
1293+
tv.tv_usec = (val - math.floor(val)) * 1000000
1294+
val = tv
12941295
end
12951296
else
12961297
if type(val) == "boolean" then
@@ -1369,10 +1370,6 @@ do
13691370
end
13701371
end
13711372

1372-
function meta:settimeout(t)
1373-
self:set_option("rcvtimeo", t)
1374-
end
1375-
13761373
function meta:connect(host, service)
13771374
local res, err = M.find_first_address_info(host, service, nil, self.family, self.socket_type, self.protocol)
13781375

@@ -1386,6 +1383,8 @@ do
13861383
return true
13871384
end
13881385

1386+
if timeout_messages[num] then return nil, "timeout", num end
1387+
13891388
return ok, err, num
13901389
end
13911390

@@ -1401,7 +1400,7 @@ do
14011400
return ok, err, num
14021401
end
14031402

1404-
return nil, "timeout"
1403+
return nil, "tryagain"
14051404
end
14061405

14071406
function meta:bind(host, service)
@@ -1428,9 +1427,11 @@ do
14281427
)
14291428

14301429
if not self.blocking and timeout_messages[num] then
1431-
return nil, "timeout", num
1430+
return nil, "tryagain", num
14321431
end
14331432

1433+
if timeout_messages[num] then return nil, "timeout", num end
1434+
14341435
if fd ~= socket.INVALID_SOCKET then
14351436
local client = setmetatable(
14361437
{
@@ -1577,6 +1578,12 @@ do
15771578

15781579
if not len then
15791580
if not self.blocking and timeout_messages[num] then
1581+
return nil, "tryagain", num
1582+
end
1583+
1584+
if timeout_messages[num] then
1585+
if self.debug then print(tostring(self), ": timeout") end
1586+
15801587
return nil, "timeout", num
15811588
end
15821589

ljtls.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ local loaders = {
338338
elseif bytes == 0 then
339339
return ""
340340
else
341-
return nil, "timeout"
341+
return nil, "tryagain"
342342
end
343343
end
344344

@@ -906,14 +906,14 @@ local loaders = {
906906
local err = lib_ssl.SSL_get_error(ssl_conn, ret)
907907

908908
-- SSL_ERROR_WANT_READ = 2, SSL_ERROR_WANT_WRITE = 3
909-
if err == 2 or err == 3 then return nil, "timeout", err end
909+
if err == 2 or err == 3 then return nil, "tryagain", err end
910910

911911
return nil, get_error_string()
912912
end
913913

914914
if state == "connected" then return true end
915915

916-
return nil, "timeout"
916+
return nil, "tryagain"
917917
end
918918

919919
local function send(data_str)
@@ -925,7 +925,7 @@ local loaders = {
925925

926926
local err = lib_ssl.SSL_get_error(ssl_conn, ret)
927927

928-
if err == 2 or err == 3 then return nil, "timeout", err end
928+
if err == 2 or err == 3 then return nil, "tryagain", err end
929929

930930
return nil, get_error_string()
931931
end
@@ -941,7 +941,7 @@ local loaders = {
941941

942942
local err = lib_ssl.SSL_get_error(ssl_conn, ret)
943943

944-
if err == 2 or err == 3 then return nil, "timeout", err end
944+
if err == 2 or err == 3 then return nil, "tryagain", err end
945945

946946
return nil, get_error_string()
947947
end
@@ -1122,7 +1122,7 @@ local loaders = {
11221122
local status = lib.SSLHandshake(ctx)
11231123

11241124
if status == errSSLWouldBlock then
1125-
return nil, "timeout", status
1125+
return nil, "tryagain", status
11261126
elseif status ~= 0 then
11271127
return nil, string.format("SSLHandshake: %s", status_to_msg(status))
11281128
end
@@ -1132,7 +1132,7 @@ local loaders = {
11321132

11331133
if state == "connected" then return true end
11341134

1135-
return nil, "timeout", status
1135+
return nil, "tryagain", status
11361136
end
11371137

11381138
local function send(data_str)
@@ -1144,7 +1144,7 @@ local loaders = {
11441144

11451145
if status == 0 then return tonumber(processed[0]) end
11461146

1147-
if status == errSSLWouldBlock then return nil, "timeout", status end
1147+
if status == errSSLWouldBlock then return nil, "tryagain", status end
11481148

11491149
return nil, string.format("SSLWrite: %s", status_to_msg(status))
11501150
end
@@ -1161,7 +1161,7 @@ local loaders = {
11611161
return ffi.string(buffer_ptr, len)
11621162
end
11631163

1164-
if status == errSSLWouldBlock then return nil, "timeout", status end
1164+
if status == errSSLWouldBlock then return nil, "tryagain", status end
11651165

11661166
return nil, string.format("SSLRead: %s", status_to_msg(status))
11671167
end

test/run.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require("test.tcp_client_server")
66
require("test.udp_client_server")
77
require("test.options")
88
require("test.poll")
9+
require("test.timeout_blocking")
910

1011
local old = _G.print
1112
_G.print = function() end

test/tcp_client_server.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test('TCP client-server communication', function()
2323
if client then
2424
current_client = client
2525
assert(client:set_blocking(false))
26-
elseif err ~= "timeout" then
26+
elseif err ~= "tryagain" then
2727
error("server accept error: " .. tostring(err))
2828
end
2929

@@ -36,7 +36,7 @@ test('TCP client-server communication', function()
3636
assert(current_client:close())
3737
current_client = nil
3838
return true
39-
elseif err ~= "timeout" then
39+
elseif err ~= "tryagain" then
4040
error("server receive error: " .. tostring(err))
4141
end
4242
end
@@ -60,7 +60,7 @@ test('TCP client-server communication', function()
6060
if str then
6161
ok(str == "hello", "client should receive echo 'hello'")
6262
assert(client:close())
63-
elseif err ~= "timeout" then
63+
elseif err ~= "tryagain" then
6464
error("client receive error: " .. tostring(err))
6565
end
6666
end

test/tcp_client_test.lua

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ local test = require("test.gambarina")
33

44
test('TCP client blocking test', function()
55
local host = "www.freebsd.no"
6-
local sock = socket.create("inet", "stream", "tcp")
7-
ok(sock ~= nil, "socket creation should succeed")
6+
local sock = assert(socket.create("inet", "stream", "tcp"))
7+
assert(sock:connect(host, "http"))
88

9-
local connected = sock:connect(host, "http")
10-
ok(connected, "connection should succeed")
11-
12-
local sent = sock:send(
9+
local sent = assert(sock:send(
1310
"GET / HTTP/1.1\r\n"..
1411
"Host: " .. host .. "\r\n"..
1512
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n"..
@@ -19,21 +16,13 @@ test('TCP client blocking test', function()
1916
"Connection: keep-alive\r\n"..
2017
"Upgrade-Insecure-Requests: 1\r\n"..
2118
"\r\n"
22-
)
23-
ok(sent, "sending HTTP request should succeed")
19+
))
2420

2521
local total_length
2622
local str = ""
2723

2824
while true do
29-
local chunk = sock:receive()
30-
ok(chunk ~= nil or chunk == false, "receive should not error")
31-
32-
if not chunk then
33-
break
34-
end
35-
36-
str = str .. chunk
25+
str = str .. assert(sock:receive())
3726

3827
if not total_length then
3928
total_length = tonumber(str:match("Content%-Length: (%d+)"))
@@ -52,21 +41,16 @@ end)
5241

5342
test('TCP client non-blocking test', function()
5443
local host = "www.freebsd.no"
55-
local sock = socket.create("inet", "stream", "tcp")
56-
ok(sock ~= nil, "socket creation should succeed")
57-
58-
local connected = sock:connect(host, "http")
59-
ok(connected, "connection should succeed")
60-
61-
local blocking_set = sock:set_blocking(false)
62-
ok(blocking_set, "setting non-blocking mode should succeed")
44+
local sock = assert(socket.create("inet", "stream", "tcp"))
45+
assert(sock:connect(host, "http"))
46+
assert(sock:set_blocking(false))
6347

6448
local str = ""
6549
local total_length
6650

6751
while true do
6852
if sock:is_connected() then
69-
local sent = sock:send(
53+
assert(sock:send(
7054
"GET / HTTP/1.1\r\n"..
7155
"Host: "..host.."\r\n"..
7256
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n"..
@@ -76,11 +60,10 @@ test('TCP client non-blocking test', function()
7660
"Connection: keep-alive\r\n"..
7761
"Upgrade-Insecure-Requests: 1\r\n"..
7862
"\r\n"
79-
)
80-
ok(sent, "sending HTTP request should succeed")
63+
))
8164

8265
while true do
83-
local chunk, err, num = sock:receive()
66+
local chunk, err = sock:receive()
8467

8568
if chunk then
8669
str = str .. chunk
@@ -95,8 +78,8 @@ test('TCP client non-blocking test', function()
9578
ok(string.find(str, "</html>", nil, true) ~= nil, "response should contain closing html tag")
9679
return
9780
end
98-
elseif err ~= "timeout" then
99-
ok(false, "receive error: " .. tostring(err))
81+
elseif err ~= "tryagain" then
82+
error("receive error: " .. tostring(err))
10083
return
10184
end
10285
end

test/timeout_blocking.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local socket = require("ljsocket")
2+
local test = require("test.gambarina")
3+
4+
test('TCP client timeout blocking test', function()
5+
local sock = assert(socket.create("inet", "stream", "tcp"))
6+
assert(sock:connect("httpbin.org", "http"))
7+
assert(sock:send("GET /delay/10 HTTP/1.1\r\nHost: httpbin.org\r\n\r\n"))
8+
assert(sock:set_option("rcvtimeo", 0.25))
9+
local res, err = sock:receive()
10+
if res then
11+
error("expected timeout error, got data")
12+
end
13+
eq(err, "timeout")
14+
end)

test/udp_client_server.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('UDP client-server communication', function()
2525
ok(addr:get_port() ~= nil, "should have sender port")
2626
local sent = server:send_to(addr, "hello from server")
2727
ok(sent, "server should send response")
28-
elseif addr ~= "timeout" then
28+
elseif addr ~= "tryagain" then
2929
ok(false, "server receive error: " .. tostring(addr))
3030
end
3131
end
@@ -61,7 +61,7 @@ test('UDP client-server communication', function()
6161
if times > 5 then
6262
return true
6363
end
64-
elseif addr ~= "timeout" then
64+
elseif addr ~= "tryagain" then
6565
ok(false, "client receive error: " .. tostring(addr))
6666
end
6767
end

0 commit comments

Comments
 (0)