Skip to content

Commit ab72360

Browse files
committed
add support to connect on socket.unix
1 parent 776095b commit ab72360

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

lib/socket/unix.lua

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--
22
-- SPDX-FileCopyrightText: (c) 2025 Ring Zero Desenvolvimento de Software LTDA
3-
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
3+
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
44
--
55

66
---
@@ -17,20 +17,34 @@ local sock = socket.sock
1717

1818
local unix = {}
1919

20+
local function new(method)
21+
unix[method] = function (path, type)
22+
local type = type and string.upper(type) or "STREAM"
23+
local sock = socket.new(af.UNIX, sock[type], 0)
24+
sock[method](sock, path)
25+
return sock
26+
end
27+
end
28+
2029
---
21-
-- Binds a new UNIX domain socket to a specific path.
30+
-- Creates and binds a new UNIX domain socket to a specific path.
2231
-- @param path (string) The UNIX domain socket path.
2332
-- @param type (string) [optional] "STREAM" or "DGRAM".
2433
-- @return A new UNIX domain socket object (stream by default) bound to path.
2534
-- @raise Error if socket.new() or socket.bind() fail.
2635
-- @see socket.new
2736
-- @see socket.bind
28-
function unix.bind(path, _type)
29-
local t = _type and string.upper(_type) or "STREAM"
30-
local s = socket.new(af.UNIX, sock[t], 0)
31-
s:bind(path)
32-
return s
33-
end
37+
new("bind")
38+
39+
---
40+
-- Creates and connects a new UNIX domain socket to a specific path.
41+
-- @param path (string) The UNIX domain socket path.
42+
-- @param type (string) [optional] "STREAM" or "DGRAM".
43+
-- @return A new UNIX domain socket object (stream by default) connected to path.
44+
-- @raise Error if socket.new() or socket.connect() fail.
45+
-- @see socket.new
46+
-- @see socket.connect
47+
new("connect")
3448

3549
return unix
3650

0 commit comments

Comments
 (0)