Module tch.socket.unix
Lua C module exposing Unix domain sockets.
Currently implemented:
- Datagram and stream sockets.
- The sockets are created with
SO_PASSCRED
option. - All addresses are in the abstract namespace but the leading
\0
will be added automatically. - Maximum size of datagrams is
MAX_DGRAM_SIZE
bytes.
See unix(7) for more information on Unix domain sockets.
See also:
Usage:
Example server code: local unix = require("tch.socket.unix") local sk = unix.dgram() assert(sk:bind("myserveraddress")) while true do local data, from = sk:recvfrom() // process data and formulate response local response = process(data) sk:sendto(response, from) end sk:close() Example client code: local unix = require("tch.socket.unix") local sk = unix.dgram() assert(sk:connect("myserveraddress)) local data = produce() sk:send(data) local response = sk:recv() process(response) sk:close()
Functions
dgram(flags) | Create a new datagram socket. |
stream(flags) | Create a new stream socket. |
Class sk
sk:accept(flags) | Accept a new connection. |
sk:bind(path) | Bind the socket to the given path in the abstract namespace. |
sk:close() | Close the socket. |
sk:connect(path) | Connect the socket to the given path in the abstract namespace. |
sk:fd() | Return the underlying file descriptor of this socket. |
sk:listen() | Mark the socket as a listening socket. |
sk:recv() | Receive data from the socket. |
sk:recvfrom() | Receive data from the socket and the address of the sender. |
sk:send(data) | Send the given data to the peer to which the socket is connected. |
sk:sendto(data, path) | Send the given data to the given path in the abstract namespace. |
tch.socket.unix | Useful constants. |
Functions
- dgram(flags)
-
Create a new datagram socket.
The
SO_PASSCRED
socket option is always set.Parameters:
- flags
int
Bitwise OR of the
SOCK_*
constants.
Returns:
-
sk
The newly created datagram socket.
Or
- nil
- string Error message.
- flags
int
Bitwise OR of the
- stream(flags)
-
Create a new stream socket.
The
SO_PASSCRED
socket option is always set.Parameters:
- flags
int
Bitwise OR of the
SOCK_*
constants.
Returns:
-
sk
The newly created stream socket.
Or
- nil
- string Error message.
- flags
int
Bitwise OR of the
Class sk
Unixsocket methods.
- sk:accept(flags)
-
Accept a new connection.
Parameters:
- flags
int
Bitwise OR of the
SOCK_*
constants.
Returns:
-
sk
New socket representing the accepted connection.
Or
- nil
-
string
Error message. In particular, if the socket is marked non-blocking
and the operation would block then the message
"WOULDBLOCK"
is returned.
- flags
int
Bitwise OR of the
- sk:bind(path)
-
Bind the socket to the given path in the abstract namespace.
The leading
\0
will be added automatically. Can only be done once and before any data is sent.Parameters:
- path string The path in the abstract namespace to bind to.
Returns:
-
boolean
true
Or
- nil
- string Error message.
- sk:close()
-
Close the socket.
Calling socket methods on the closed socket will result in errors. It's not an error to close a socket more than once. Closing a socket will automatically remove it from any event loop it was added to but not removed yet.
- sk:connect(path)
-
Connect the socket to the given path in the abstract namespace.
The leading
\0
will be added automatically. Can be called multiple times.Parameters:
- path string The path in the abstract namespace to connect to.
Returns:
-
boolean
true
Or
- nil
- string Error message.
- sk:fd()
-
Return the underlying file descriptor of this socket.
Returns:
-
int
The file descriptor.
- sk:listen()
-
Mark the socket as a listening socket.
Returns:
-
boolean
true
Or
- nil
- string Error message.
- sk:recv()
-
Receive data from the socket.
Returns:
-
string
The data read from the socket.
Or
- nil
-
string
Error message. In particular, if the socket is marked non-blocking
and the operation would block then the message
"WOULDBLOCK"
is returned.
- sk:recvfrom()
-
Receive data from the socket and the address of the sender.
Returns:
- string The data read from the socket.
-
string
The path in the abstract namespace (without leading
\0
) of the sender. Note that this can be an empty string if the sender didn't bind its socket and the kernel didn't automatically generate an address (like it does when e.g. theSO_PASSCRED
option is set).
Or
- nil
-
string
Error message. In particular, if the socket is marked non-blocking
and the operation would block then the message
"WOULDBLOCK"
is returned.
- sk:send(data)
-
Send the given data to the peer to which the socket is connected.
This means sk:connect must have been called first.
The data can be provided as a string or as an array of strings.
If the total size of the data is more than
MAX_DGRAM_SIZE
an error will be returned.Parameters:
- data string or {string,...} The data to send.
Returns:
-
boolean
true
Or
- nil
-
string
Error message. In particular, if the socket is marked non-blocking
and the operation would block then the message
"WOULDBLOCK"
is returned.
- sk:sendto(data, path)
-
Send the given data to the given path in the abstract namespace.
The leading
\0
will be added automatically. The data can be provided as a string or as an array of strings. If the total size of the data is more thanMAX_DGRAM_SIZE
an error will be returned.Parameters:
- data string or {string,...} The data to send.
- path string The path in the abstract namespace to send the data to.
Returns:
-
boolean
true
Or
- nil
-
string
Error message. In particular, if the socket is marked non-blocking
and the operation would block then the message
"WOULDBLOCK"
is returned.
- tch.socket.unix
-
Useful constants.
Fields: