---@meta socket.unix

--------------------------------------------------------------------------------
-- LuaSocket Unix domain socket C module
--------------------------------------------------------------------------------

---@type socket_core*
local socket_core = require("socket.core")

---@class socket_unix_stream*: socket_core_tcp*
local socket_unix_stream = {}

---@class socket_unix_dgram*: socket_core_udp*
local socket_unix_dgram = {}

---@class socket_unix*
---@type socket_unix*
local M = {}

--- Create a Unix stream socket.
---@return socket_unix_stream*? sock Unix stream socket
---@return string? errMsg error message
function M.stream() end

--- Create a Unix datagram socket.
---@return socket_unix_dgram*? sock Unix datagram socket
---@return string? errMsg error message
function M.dgram() end

--- Compatible with aliases, equivalent to stream.
---@return socket_unix_stream*? sock Unix stream socket
---@return string? errMsg error message
function M.tcp() end

--- Compatible with aliases, equivalent to dgram.
---@return socket_unix_dgram*? sock Unix datagram socket
---@return string? errMsg error message
function M.udp() end

--- Connect to Unix socket.
---@param path string socket path
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_stream:connect(path) end

--- Bind Unix socket.
---@param path string socket path
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_stream:bind(path) end

--- Start monitoring.
---@param backlog? integer backlog
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_stream:listen(backlog) end

--- Accept the connection.
---@return socket_unix_stream*? client client socket
---@return string? errMsg error message
function socket_unix_stream:accept() end

--- Send data.
---@param data string data
---@param from? integer starting position
---@param to? integer end position
---@return integer? sent Number of bytes sent
---@return string? errMsg error message
---@return integer? lastIndex last sent position
function socket_unix_stream:send(data, from, to) end

--- Receive data.
---@param pattern? '*l'|'*a'|integer receive mode
---@param prefix? string prefix
---@return string? data data
---@return string? errMsg error message
---@return string? partial partial data
function socket_unix_stream:receive(pattern, prefix) end

--- Close the connection.
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_stream:close() end

--- Set timeout.
---@param timeout socket_core_timeout Timeout seconds
---@param mode? 'b'|'r'|'s' mode
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_stream:settimeout(timeout, mode) end

--- Get the local socket path.
---@return string? path socket path
---@return string? errMsg error message
function socket_unix_stream:getsockname() end

--- Set socket options.
---@param option string option name
---@param value any option value
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_stream:setoption(option, value) end

--- Turn off one-way or two-way transmission.
---@param mode? socket_shutdown_mode Close direction
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_stream:shutdown(mode) end

--- Get the underlying fd.
---@return integer fd file descriptor
function socket_unix_stream:getfd() end

--- Replace underlying fd.
---@param fd integer file descriptor
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_stream:setfd(fd) end

--- Check whether there is unread data in the buffer.
---@return boolean dirty Is there any unread data?
function socket_unix_stream:dirty() end

--- Get transfer statistics.
---@return integer received Number of bytes received
---@return integer sent Number of bytes sent
---@return number age seconds after creation
function socket_unix_stream:getstats() end

--- Set transfer statistics.
---@param received? integer Number of bytes received
---@param sent? integer Number of bytes sent
---@param age? number seconds after creation
---@return integer? ok Returns 1 on success
function socket_unix_stream:setstats(received, sent, age) end

--- Connect to Unix datagram socket.
---@param path string socket path
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_dgram:connect(path) end

--- Bind the Unix datagram socket.
---@param path string socket path
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_dgram:bind(path) end

--- Send data.
---@param data string data
---@return integer? sent Number of bytes sent
---@return string? errMsg error message
function socket_unix_dgram:send(data) end

--- Send data to the specified Unix socket.
---@param data string data
---@param path string socket path
---@return integer? sent Number of bytes sent
---@return string? errMsg error message
function socket_unix_dgram:sendto(data, path) end

--- Receive data.
---@param size? integer maximum length
---@return string? data data
---@return string? errMsg error message
function socket_unix_dgram:receive(size) end

--- Receive data and sources.
---@param size? integer maximum length
---@return string? data data
---@return string? path Source socket path
function socket_unix_dgram:receivefrom(size) end

--- Get the local socket path.
---@return string? path socket path
---@return string? errMsg error message
function socket_unix_dgram:getsockname() end

--- Set timeout.
---@param timeout socket_core_timeout Timeout seconds
---@param mode? 'b'|'r'|'s' mode
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_dgram:settimeout(timeout, mode) end

--- Get the timeout setting.
---@return number? block Single operation timeout
---@return number? total total timeout
function socket_unix_dgram:gettimeout() end

--- Set socket options.
---@param option string option name
---@param value any option value
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_dgram:setoption(option, value) end

--- Get the underlying fd.
---@return integer fd file descriptor
function socket_unix_dgram:getfd() end

--- Replace underlying fd.
---@param fd integer file descriptor
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_dgram:setfd(fd) end

--- Check whether the socket has unread data.
---@return boolean dirty Is there any unread data?
function socket_unix_dgram:dirty() end

--- Close the socket.
---@return integer? ok Returns 1 on success
---@return string? errMsg error message
function socket_unix_dgram:close() end

return M
