
---@class copas_debug*
local copas_debug = {}

---@class copas*
---@field _COPYRIGHT string Copyright information.
---@field _DESCRIPTION string Module description.
---@field _VERSION string Module version.
---@field autoclose boolean handler Whether to automatically close the socket after completion.
---@field running boolean loop is running.
---@field receivePartial function deprecated， Equivalent to receivepartial.
---@field setErrorHandler function deprecated， Equivalent to seterrorhandler.
---@field debug copas_debug* Debugging tools.
---@type copas*
local M = {}

M._COPYRIGHT = nil
M._DESCRIPTION = nil
M._VERSION = nil
M.autoclose = true
M.running = false
M.receivePartial = nil
M.setErrorHandler = nil
M.debug = copas_debug

--- Close the socket.
---@param skt any socket。
---@param ... any parameter.
---@return any result Close results.
function M.close(skt, ...) end

--- Set socket timeout.
---@param skt any socket。
---@param timeout? number timeout period.
---@return any result result.
function M.settimeout(skt, timeout) end

--- Set three types of connect/send/read timeouts.
---@param skt any socket。
---@param connect? number connect time out.
---@param send? number send time out.
---@param read? number read time out.
function M.settimeouts(skt, connect, send, read) end

--- Receive data.
---@param client any socket。
---@param pattern? string|integer receive mode.
---@param part? string Part received.
---@return string|nil data data.
---@return string? errMsg error message.
---@return string? partial Partial data.
function M.receive(client, pattern, part) end

--- UDP receive.
---@param client any socket。
---@param size? integer Number of bytes.
---@return string|nil data data.
---@return string? ip Source IP.
---@return integer|string? portOrErr Source port or error.
function M.receivefrom(client, size) end

--- Receive partial data.
---@param client any socket。
---@param pattern? string|integer receive mode.
---@param part? string Part received.
---@return string|nil data data.
---@return string? errMsg error message.
---@return string? partial Partial data.
function M.receivepartial(client, pattern, part) end

--- Send data.
---@param client any socket。
---@param data string data.
---@param from? integer starting position.
---@param to? integer end position.
---@return integer|nil sent Location sent.
---@return string? errMsg error message.
function M.send(client, data, from, to) end

--- Send via UDP.
---@param client any socket。
---@param data string data.
---@param ip string Destination IP.
---@param port integer Destination port.
---@return integer|nil sent Number of bytes sent.
---@return string? errMsg error message.
function M.sendto(client, data, ip, port) end

--- Connect socket.
---@param skt any socket。
---@param host string host.
---@param port integer port.
---@return boolean|nil ok Whether it is successful or not.
---@return string? errMsg error message.
function M.connect(skt, host, port) end

--- Perform TLS handshake.
---@param skt any socket。
---@param wrap_params? table TLS parameter.
---@return boolean|nil ok Whether it is successful or not.
---@return string? errMsg error message.
function M.dohandshake(skt, wrap_params) end

--- flush the current socket.
---@return boolean|nil ok Whether it is successful or not.
---@return string? errMsg error message.
function M.flush() end

--- Wrap a normal socket into a Copas socket.
---@param skt any original socket.
---@param sslt? table TLS parameter.
---@return any wrapped Copas socket。
function M.wrap(skt, sslt) end

--- Create server handler.
---@param handler function handler function.
---@param sslparams? table TLS parameter.
---@return function handler The wrapped handler.
function M.handler(handler, sslparams) end

--- Get traceback.
---@param msg any error message.
---@param co? thread coroutine。
---@param skt? any socket。
---@return string traceback traceback。
function M.gettraceback(msg, co, skt) end

--- Set error handler.
---@param err function Error handler.
---@param default? boolean Whether to set the default processor.
function M.seterrorhandler(err, default) end

--- Get the error handler.
---@param co? thread coroutine。
---@return function handler Error handler.
function M.geterrorhandler(co) end

--- Toggle socket timeout error behavior.
---@param enabled boolean Whether to enable.
function M.useSocketTimeoutErrors(enabled) end

--- Add server.
---@param server any server socket。
---@param handler function handler function.
---@param timeout? number time out.
---@param name? string name.
---@return any server server。
function M.addserver(server, handler, timeout, name) end

--- Remove server.
---@param server any server socket。
---@param keep_open? boolean Whether to keep it open.
---@return any result result.
function M.removeserver(server, keep_open) end

--- Add named coroutine.
---@param name string name.
---@param handler function function.
---@param ... any parameter.
---@return thread co coroutine。
function M.addnamedthread(name, handler, ...) end

--- Add coroutines.
---@param handler function function.
---@param ... any parameter.
---@return thread co coroutine。
function M.addthread(handler, ...) end

--- Remove coroutine.
---@param thread thread coroutine。
function M.removethread(thread) end

--- Sleep for the specified number of seconds.
---@param seconds number Number of seconds.
function M.sleep(seconds) end

--- Pause the current coroutine.
---@param seconds? number Number of seconds.
function M.pause(seconds) end

--- Permanently suspends the current coroutine until wakeup.
function M.pauseforever() end

--- Wake up the coroutine.
---@param co thread coroutine。
function M.wakeup(co) end

--- Get the current timeout configuration.
---@return table timeouts Timeout configuration.
function M.gettimeouts() end

--- Create a one-time timer.
---@param delay number Delay in seconds.
---@param callback function callback.
---@return any timer timer.
function M.timeout(delay, callback) end

--- Execute a loop.
---@param timeout? number time out.
---@return boolean running Is it still running?
function M.step(timeout) end

--- Determine whether the loop ends.
---@return boolean finished Is it over?
function M.finished() end

--- Get status.
---@param enable_stats? boolean Whether to enable statistics.
---@return table|string status state.
function M.status(enable_stats) end

--- Run Copas loop.
---@param initializer? function Initialization function.
---@param timeout? number time out.
function M.loop(initializer, timeout) end

--- Set socket name.
---@param name string name.
---@param skt any socket。
function M.setsocketname(name, skt) end

--- Get the socket name.
---@param skt any socket。
---@return string name name.
function M.getsocketname(skt) end

--- Set the coroutine name.
---@param name string name.
---@param coro thread coroutine。
function M.setthreadname(name, coro) end

--- Get coroutine name.
---@param coro thread coroutine。
---@return string name name.
function M.getthreadname(coro) end

--- Start debugging.
---@param logger? function Log function.
---@param core? table Debugging core.
function copas_debug.start(logger, core) end

--- Stop debugging.
function copas_debug.stop() end

--- Wrap the debug socket.
---@param original_skt any original socket.
---@return any socket Debug socket.
function copas_debug.socket(original_skt) end

return M
