Module streams

This module provides an abstraction of a duplex communication based on libuv. DuplexStream is an abstract interface which provides reading and writing for non-blocking I/O.

There are 3 duplex implementations in the form of TcpStream, TtyStream, PipeStream.

Types

DuplexStream = ref object of RootObj
  handle: Stream
  readBuf: array[BufSize, char]
  readBufLen: int
  readCb: proc (data: pointer; size: int) {.
closure, gcsafe
.} readEndCb: proc () {.
closure, gcsafe
.} writeBuf: seq[tuple[base: pointer, length: int, cb: proc (err: ref NodeError) {.
closure, gcsafe
.}]] writeBufLen: int writeQueueSize: int writeDrainCb: proc () {.
closure, gcsafe
.} writeEndCb: proc () {.
closure, gcsafe
.} writingReq: Write writingSize: int writingCb: proc (err: ref NodeError) {.
closure, gcsafe
.} shutdown: Shutdown closeCb: proc (err: ref NodeError) {.
closure, gcsafe
.} closeHookCb: proc (err: ref NodeError) {.
closure, gcsafe
.} error: ref NodeError stats: set[StreamStat]
Abstract interface are both readable and writable.   Source Edit
TcpStream = ref object of DuplexStream
  connectCb: proc () {.
closure, gcsafe
.}
Abstraction of TCP communication based on stream IO manner.   Source Edit

Consts

BufSize = 4096
  Source Edit
WriteOverLevel = 16384
  Source Edit

Procs

proc onRead=(S: DuplexStream;
            cb: proc (data: pointer; size: int) {.
closure, gcsafe
.}) {.
raises: [], tags: []
.}
Sets the callback which will be invoked when reading a chunk of data.   Source Edit
proc onReadEnd=(S: DuplexStream; cb: proc () {.
closure, gcsafe
.}) {.
raises: [], tags: []
.}
Sets the callback which will be invoked when reading the end of file.   Source Edit
proc onWriteDrain=(S: DuplexStream; cb: proc () {.
closure, gcsafe
.}) {.
raises: [], tags: []
.}
Sets the callback which will be invoked when writing buffers is become empty from large than WriteOverLevel.   Source Edit
proc onWriteEnd=(S: DuplexStream; cb: proc () {.
closure, gcsafe
.}) {.
raises: [], tags: []
.}
Sets the callback which will be invoked when the write side is shutdowned.   Source Edit
proc onClose=(S: DuplexStream; cb: proc (err: ref NodeError) {.
closure, gcsafe
.}) {.
raises: [], tags: []
.}
Sets the callback which will be invoked when the stream is closed. If an error occurs, the callback will
be called with the error as its first argument.
  Source Edit
proc close(S: DuplexStream) {.
raises: [Exception, NodeError], tags: [RootEffect]
.}
Close S. Handles that wrap file descriptors are closed immediately.   Source Edit
proc writeEnd(S: DuplexStream) {.
raises: [Exception, NodeError], tags: [RootEffect]
.}
Shutdown the outgoing (write) side and waits for all pending write requests to complete.   Source Edit
proc closeSoon(S: DuplexStream) {.
raises: [Exception, NodeError], tags: [RootEffect]
.}
Shutdown the outgoing (write) side and waits for all pending write requests to complete. After that, close S.   Source Edit
proc write(S: DuplexStream; buf: pointer; size: int;
          cb: proc (err: ref NodeError) {.
closure, gcsafe
.} = nil) {.
raises: [Exception, NodeError], tags: [RootEffect]
.}
Writes data to the underlying system, and calls the supplied callback once the data has been fully handled. If an error occurs, the callback will be called with the error as its first argument.   Source Edit
proc write(S: DuplexStream; buf: string;
          cb: proc (err: ref NodeError) {.
closure, gcsafe
.} = nil) {.
raises: [Exception, NodeError], tags: [RootEffect]
.}
Writes data to the underlying system, and calls the supplied callback once the data has been fully handled. If an error occurs, the callback will be called with the error as its first argument.   Source Edit
proc writeCork(S: DuplexStream) {.
raises: [], tags: []
.}

Forces buffering of all writes.

Buffered data will be flushed either at writeUncork() or at writeEnd() call.

  Source Edit
proc writeUncork(S: DuplexStream) {.
raises: [Exception, NodeError], tags: [RootEffect]
.}
Flush all data, buffered since writeCork() call.   Source Edit
proc readResume(S: DuplexStream) {.
raises: [Exception, NodeError], tags: [RootEffect]
.}
Switchs S into flowing mode, data is read from the underlying system and provided to your program as fast as possible.   Source Edit
proc readPause(S: DuplexStream) {.
raises: [], tags: []
.}
Switchs S into paused mode, any data that becomes available will remain in the internal buffer.   Source Edit
proc isFlowing(S: DuplexStream): bool {.
raises: [], tags: []
.}
  Source Edit
proc isNeedDrain(S: DuplexStream): bool {.
raises: [], tags: []
.}
  Source Edit
proc failed(S: DuplexStream): bool {.
raises: [], tags: []
.}
  Source Edit
proc error(S: DuplexStream): ref NodeError {.
raises: [], tags: []
.}
  Source Edit
proc onConnect=(S: TcpStream; cb: proc () {.
closure, gcsafe
.}) {.
raises: [], tags: []
.}
Sets the callback which will be invoked when connecting successfuly.   Source Edit
proc connect(port: Port; hostname = "127.0.0.1"; domain = Domain.AF_INET): TcpStream {.
raises: [Exception, NodeError], tags: [RootEffect]
.}
Establishs an IPv4 or IPv6 TCP connection and returns a fresh TcpStream.   Source Edit
proc accept(server: ptr TCP;
           closeCb: proc (err: ref NodeError) {.
closure, gcsafe
.} = nil): TcpStream {.
raises: [Exception, NodeError], tags: [RootEffect]
.}
Accept incoming connections, returns a new TcpStream which is a wrapper of a connection.   Source Edit