uv_stream_t.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ---@meta
  2. -- luacheck: no unused args
  3. error('Cannot require a meta file')
  4. --- Stream handles provide an abstraction of a duplex communication channel.
  5. --- `uv_stream_t` is an abstract type, libuv provides 3 stream implementations
  6. --- in the form of `uv_tcp_t`, `uv_pipe_t` and `uv_tty_t`.
  7. ---
  8. ---@class uv.uv_stream_t : uv.uv_handle_t
  9. ---
  10. local stream = {} -- luacheck: no unused
  11. --- This call is used in conjunction with `uv.listen()` to accept incoming
  12. --- connections. Call this function after receiving a callback to accept the
  13. --- connection.
  14. ---
  15. --- When the connection callback is called it is guaranteed that this function
  16. --- will complete successfully the first time. If you attempt to use it more than
  17. --- once, it may fail. It is suggested to only call this function once per
  18. --- connection call.
  19. ---
  20. --- ```lua
  21. --- server:listen(128, function (err)
  22. --- local client = uv.new_tcp()
  23. --- server:accept(client)
  24. --- end)
  25. --- ```
  26. ---
  27. ---@param client_stream uv.uv_stream_t
  28. ---@return 0|nil success
  29. ---@return uv.error.message|nil err
  30. ---@return uv.error.name|nil err_name
  31. function stream:accept(client_stream) end
  32. --- Returns the stream's write queue size.
  33. ---
  34. ---@return integer size
  35. function stream:get_write_queue_size() end
  36. --- Returns `true` if the stream is readable, `false` otherwise.
  37. ---
  38. ---@return boolean readable
  39. function stream:is_readable() end
  40. --- Returns `true` if the stream is writable, `false` otherwise.
  41. ---
  42. ---@return boolean writable
  43. function stream:is_writable() end
  44. --- Start listening for incoming connections.
  45. ---
  46. --- `backlog` indicates the number of connections the kernel might queue, same as `listen(2)`.
  47. ---
  48. --- When a new incoming connection is received the callback is called.
  49. ---
  50. ---@param backlog integer
  51. ---@param callback uv.listen.callback
  52. ---@return 0|nil success
  53. ---@return uv.error.message|nil err
  54. ---@return uv.error.name|nil err_name
  55. function stream:listen(backlog, callback) end
  56. --- Read data from an incoming stream.
  57. ---
  58. --- The callback will be made several times until there is no more data to read or `stream:read_stop()` is called.
  59. ---
  60. --- When we've reached EOF, `data` will be `nil`.
  61. ---
  62. --- ```lua
  63. --- stream:read_start(function (err, chunk)
  64. --- if err then
  65. --- -- handle read error
  66. --- elseif chunk then
  67. --- -- handle data
  68. --- else
  69. --- -- handle disconnect
  70. --- end
  71. --- end)
  72. --- ```
  73. ---
  74. ---@param callback uv.read_start.callback
  75. ---@return 0|nil success
  76. ---@return uv.error.message|nil err
  77. ---@return uv.error.name|nil err_name
  78. function stream:read_start(callback) end
  79. --- Stop reading data from the stream.
  80. ---
  81. --- The read callback will no longer be called.
  82. ---
  83. --- This function is idempotent and may be safely called on a stopped stream.
  84. ---
  85. ---@return 0|nil success
  86. ---@return uv.error.message|nil err
  87. ---@return uv.error.name|nil err_name
  88. function stream:read_stop() end
  89. --- Enable or disable blocking mode for a stream.
  90. ---
  91. --- When blocking mode is enabled all writes complete synchronously. The interface
  92. --- remains unchanged otherwise, e.g. completion or failure of the operation will
  93. --- still be reported through a callback which is made asynchronously.
  94. ---
  95. --- **Warning**: Relying too much on this API is not recommended. It is likely to
  96. --- change significantly in the future. Currently this only works on Windows and
  97. --- only for `uv_pipe_t` handles. Also libuv currently makes no ordering guarantee
  98. --- when the blocking mode is changed after write requests have already been
  99. --- submitted. Therefore it is recommended to set the blocking mode immediately
  100. --- after opening or creating the stream.
  101. ---
  102. ---@param blocking boolean
  103. ---@return 0|nil success
  104. ---@return uv.error.message|nil err
  105. ---@return uv.error.name|nil err_name
  106. function stream:set_blocking(blocking) end
  107. --- Shutdown the outgoing (write) side of a duplex stream. It waits for pending
  108. --- write requests to complete. The callback is called after shutdown is complete.
  109. ---
  110. ---@param callback? uv.shutdown.callback
  111. ---@return uv.uv_shutdown_t|nil shutdown
  112. ---@return uv.error.message|nil err
  113. ---@return uv.error.name|nil err_name
  114. function stream:shutdown(callback) end
  115. --- Same as `stream:write()`, but won't queue a write request if it can't be completed
  116. --- immediately.
  117. ---
  118. --- Will return number of bytes written (can be less than the supplied buffer size).
  119. ---
  120. ---@param data uv.buffer
  121. ---@return integer|nil bytes
  122. ---@return uv.error.message|nil err
  123. ---@return uv.error.name|nil err_name
  124. function stream:try_write(data) end
  125. --- Like `stream:write2()`, but with the properties of `stream:try_write()`. Not supported on Windows, where it returns `UV_EAGAIN`.
  126. ---
  127. --- Will return number of bytes written (can be less than the supplied buffer size).
  128. ---
  129. ---@param data uv.buffer
  130. ---@param send_handle uv.uv_stream_t
  131. ---@return integer|nil bytes
  132. ---@return uv.error.message|nil err
  133. ---@return uv.error.name|nil err_name
  134. function stream:try_write2(data, send_handle) end
  135. --- Write data to stream.
  136. ---
  137. --- `data` can either be a Lua string or a table of strings. If a table is passed
  138. --- in, the C backend will use writev to send all strings in a single system call.
  139. ---
  140. --- The optional `callback` is for knowing when the write is complete.
  141. ---
  142. ---@param data uv.buffer
  143. ---@param callback? uv.write.callback
  144. ---@return uv.uv_write_t|nil bytes
  145. ---@return uv.error.message|nil err
  146. ---@return uv.error.name|nil err_name
  147. function stream:write(data, callback) end
  148. --- Extended write function for sending handles over a pipe. The pipe must be
  149. --- initialized with `ipc` option `true`.
  150. ---
  151. --- **Note:** `send_handle` must be a TCP socket or pipe, which is a server or a
  152. --- connection (listening or connected state). Bound sockets or pipes will be
  153. --- assumed to be servers.
  154. ---
  155. ---@param data uv.buffer
  156. ---@param send_handle uv.uv_stream_t
  157. ---@param callback? uv.write2.callback
  158. ---@return uv.uv_write_t|nil write
  159. ---@return uv.error.message|nil err
  160. ---@return uv.error.name|nil err_name
  161. function stream:write2(data, send_handle, callback) end