Module tcp

Types

TcpServer* = ref object
  incomingConnections*: Input[TcpConnection]
  incomingConnectionsProvider: Output[TcpConnection]
  sockAddr: tuple[address: IpAddress, port: int]
  Source Edit
TcpConnection* = ref object of uvstream.UvPipe
  Source Edit
TcpConnectionData* = object
  host*: IpAddress
  port*: int                   ## TCP port
  boundSocket*: TcpBoundSocket ## if not nil, use pre-existing bound TCP socket
  
IP address   Source Edit
TcpBoundSocket* = ref object
  stream: ptr uv_tcp_t
  Source Edit

Procs

proc getPeerAddr*(conn: TcpConnection): tuple[address: IpAddress, port: int]
Get address of a remote peer (similar to POSIX getpeername).   Source Edit
proc getSockAddr*(conn: TcpBoundSocket | TcpConnection): tuple[address: IpAddress,
    port: int]
  Source Edit
proc getSockAddr*(conn: TcpServer): auto
  Source Edit
proc createTcpServer*(port: int; addresses: seq[IpAddress]; reusePort = false): Future[
    TcpServer]
  Source Edit
proc createTcpServer*(port: int; host = "localhost"; reusePort = false): Future[TcpServer]

Create TcpServer listening on host:port.

Example:

let server = await createTcpServer(5000)
asyncFor conn in server.incomingConnections:
  # handle incoming connection
  await conn.output.write("hello")
  Source Edit
proc bindSocketForConnect*(bindHost: IpAddress; bindPort: int): Future[TcpBoundSocket]
  Source Edit
proc connectTcp*(info: TcpConnectionData): Future[TcpConnection]
  Source Edit
proc connectTcp*(host: IpAddress; port: int): Future[TcpConnection]
  Source Edit
proc connectTcpAsFd*(info: TcpConnectionData): Future[cint]
Connect to TCP server running on host:port.   Source Edit
proc connectTcp*(host: string; port: int): Future[TcpConnection] {.
async
.}
Connect to TCP server running on host:port.   Source Edit
proc close*(t: TcpConnection; err: ref Exception)
Close TCP connection.   Source Edit
proc connectTcp*(address: string): Future[TcpConnection]
  Source Edit