Skip to content

TransportAddress

Eugene Kabanov edited this page May 29, 2018 · 4 revisions

Transport Address

Usage of SockAddr structure in all public API in current asyncdispatch is very inconvenient for developers, so asyncdispatch2 uses new object for IP/PORT address representation TransportAddress.

 TransportAddress* = object
    ## Transport network address
    address*: IpAddress           # IP Address
    port*: Port                   # IP port

Also there two additional helper functions for work with TransportAddress:

proc strAddress*(address: string): TransportAddress =
  ## Parses string representation of ``address``.
  ## 
  ## IPv4 transport address format is ``a.b.c.d:port``.
  ## IPv6 transport address format is ``[::]:port``.
proc `$`*(address: TransportAddress): string =
  ## Returns string representation of ``address``.

Examples

  var taddr4 = strAddress("127.0.0.1:80") # IPv4 address
  var taddr6 = strAddress("[::1]:80")     # Note, that IPv6 address
                                          # must be covered with `[]`
  echo $taddr4                            # Echo "127.0.0.1:80"
  echo $taddr6                            # Echo "[::1]:80"
Clone this wiki locally