Skip to content

Commit 8d554a3

Browse files
committed
fix addrinfo from udp and add more tests
1 parent 1e9d9db commit 8d554a3

3 files changed

Lines changed: 31 additions & 18 deletions

File tree

ljsocket.lua

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,16 +1168,15 @@ do -- addrinfo
11681168
return addrinfos[1]
11691169
end
11701170

1171-
function M.addrinfo_for_receive(sockaddr_ptr, src_address, len, family)
1172-
return {
1173-
addrinfo = {
1174-
ai_addr = ffi.cast(sockaddr_ptr, src_address),
1175-
ai_addrlen = len,
1176-
},
1177-
family = family,
1178-
get_port = addrinfo_get_port,
1179-
get_ip = addrinfo_get_ip,
1180-
}
1171+
function M.addrinfo_for_receive(ai_addr, len, family)
1172+
-- Create a minimal addrinfo structure with the received address
1173+
local ai = addrinfo_out()
1174+
ai.ai_addr = ai_addr
1175+
ai.ai_addrlen = len
1176+
ai.ai_family = AF.strict_lookup(family)
1177+
1178+
-- Use the existing addressinfo constructor which properly sets up the metatable
1179+
return M.addressinfo(ai, nil, nil)
11811180
end
11821181
end
11831182

@@ -1483,7 +1482,7 @@ do
14831482

14841483
if src_address then
14851484
return ffi.string(buff, len),
1486-
M.addrinfo_for_receive(sockaddr_ptr, src_address, len_res[0], self.family)
1485+
M.addrinfo_for_receive(ffi.cast(sockaddr_ptr, src_address), len_res[0], self.family)
14871486
end
14881487

14891488
return ffi.string(buff, len)

test/run.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
require("test.dns_lookup_test")
22
require("test.tcp_client_test")
33
require("test.tcp_client_server")
4+
require("test.udp_client_server")
45

56
require("examples.dns_lookup")
67
require("examples.tcp_client_blocking")
78
require("examples.tcp_client_nonblocking")
8-
--require("examples.udp_client_server")
9+
require("examples.tcp_client_blocking_tls")
10+
require("examples.tcp_client_nonblocking_tls")
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ do -- server
1313

1414
if data then
1515
print(data)
16-
assert(server:send_to(addr, "hello from server " .. os.clock()))
16+
assert(data == "hello from client")
17+
assert(addr:get_port())
18+
assert(server:send_to(addr, "hello from server"))
1719
elseif addr ~= "timeout" then
1820
error(addr)
1921
end
@@ -26,23 +28,33 @@ do -- client
2628
assert(client:set_blocking(false))
2729
local next_send = 0
2830

31+
local sent = false
32+
local times = 0
2933
function update_client()
30-
if next_send < os.clock() then
31-
assert(client:send_to(send_address, "hello from client " .. os.clock()))
32-
next_send = os.clock() + math.random() + 0.5
34+
35+
if not sent then
36+
assert(client:send_to(send_address, "hello from client"))
37+
sent = true
3338
end
3439

3540
local data, addr = client:receive_from()
3641

3742
if data then
38-
print(data, addr:get_ip(), addr:get_port())
43+
print(data)
44+
assert(data == "hello from server")
45+
assert(addr:get_port())
46+
assert(client:send_to(send_address, "hello from client"))
47+
times = times + 1
48+
if times > 5 then
49+
return true
50+
end
3951
elseif addr ~= "timeout" then
4052
error(addr)
4153
end
4254
end
4355
end
4456

4557
while true do
58+
if update_client() then break end
4659
update_server()
47-
update_client()
4860
end

0 commit comments

Comments
 (0)