1 StreamTransport API
Eugene Kabanov edited this page 2018-05-30 09:29:41 +03:00
proc readExactly*(transp: StreamTransport, pbytes: pointer,
                  nbytes: int) {.async.} =
  ## Read exactly ``nbytes`` bytes from transport ``transp`` and store it to
  ## ``pbytes``.
  ## 
  ## If EOF is received and ``nbytes`` is not yet readed, the procedure
  ## will raise ``TransportIncompleteError``.
proc readOnce*(transp: StreamTransport, pbytes: pointer,
               nbytes: int): Future[int] {.async.} =
  ## Perform one read operation on transport ``transp``.
  ## 
  ## If internal buffer is not empty, ``nbytes`` bytes will be transferred from
  ## internal buffer, otherwise it will wait until some bytes will be received.
proc readUntil*(transp: StreamTransport, pbytes: pointer, nbytes: int,
                sep: seq[byte]): Future[int] {.async.} =
  ## Read data from the transport ``transp`` until separator ``sep`` is found.
  ## 
  ## On success, the data and separator will be removed from the internal
  ## buffer (consumed). Returned data will NOT include the separator at the end.
  ## 
  ## If EOF is received, and `sep` was not found, procedure will raise
  ## ``TransportIncompleteError``.
  ## 
  ## If ``nbytes`` bytes has been received and `sep` was not found, procedure
  ## will raise ``TransportLimitError``.
  ## 
  ## Procedure returns actual number of bytes read.
proc readLine*(transp: StreamTransport, limit = 0,
               sep = "\r\n"): Future[string] {.async.} =
  ## Read one line from transport ``transp``, where "line" is a sequence of
  ## bytes ending with ``sep`` (default is "\r\n").
  ## 
  ## If EOF is received, and ``sep`` was not found, the method will return the
  ## partial read bytes.
  ## 
  ## If the EOF was received and the internal buffer is empty, return an
  ## empty string.
  ## 
  ## If ``limit`` more then 0, then read is limited to ``limit`` bytes.
proc read*(transp: StreamTransport, n = -1): Future[seq[byte]] {.async.} =
  ## Read all bytes (n == -1) or `n` bytes from transport ``transp``.
  ## 
  ## This procedure allocates buffer seq[byte] and return it as result.
proc atEof*(transp: StreamTransport): bool {.inline.} =
  ## Returns ``true`` if ``transp`` is at EOF.
proc join*(transp: StreamTransport) {.async.} =
  ## Wait until ``transp`` will not be closed.
proc close*(transp: StreamTransport) =
  ## Closes and frees resources of transport ``transp``.
proc write*(transp: StreamTransport, pbytes: pointer,
            nbytes: int): Future[int] {.async.} =
  ## Write data from buffer ``pbytes`` with size ``nbytes`` using transport
  ## ``transp``.
proc writeFile*(transp: StreamTransport, handle: int,
                offset: uint = 0,
                size: int = 0): Future[void] {.async.} =
  ## Write data from file descriptor ``handle`` to transport ``transp``.
  ## 
  ## You can specify starting ``offset`` in opened file and number of bytes
  ## to transfer from file to transport via ``size``.