Provide access to getsockname()/getpeername()
Port of #3323 with added tests
This commit is contained in:
parent
a0fb77dfd5
commit
d8ff25f032
2 changed files with 16 additions and 0 deletions
|
|
@ -166,6 +166,18 @@ proc newAsyncSocket*(domain: Domain = AF_INET, sockType: SockType = SOCK_STREAM,
|
|||
raiseOSError(osLastError())
|
||||
result = newAsyncSocket(fd, domain, sockType, protocol, buffered)
|
||||
|
||||
proc getLocalAddr*(socket: AsyncSocket): (string, Port) =
|
||||
## Get the socket's local address and port number.
|
||||
##
|
||||
## This is high-level interface for `getsockname`:idx:.
|
||||
getLocalAddr(socket.fd, socket.domain)
|
||||
|
||||
proc getPeerAddr*(socket: AsyncSocket): (string, Port) =
|
||||
## Get the socket's peer address and port number.
|
||||
##
|
||||
## This is high-level interface for `getpeername`:idx:.
|
||||
getPeerAddr(socket.fd, socket.domain)
|
||||
|
||||
proc newAsyncSocket*(domain, sockType, protocol: cint,
|
||||
buffered = true): AsyncSocket =
|
||||
## Creates a new asynchronous socket.
|
||||
|
|
|
|||
|
|
@ -26,6 +26,10 @@ proc launchSwarm(port: Port) {.async.} =
|
|||
proc readMessages(client: AsyncFD) {.async.} =
|
||||
# wrapping the AsyncFd into a AsyncSocket object
|
||||
var sockObj = newAsyncSocket(client)
|
||||
var (ipaddr, port) = sockObj.getPeerAddr()
|
||||
doAssert ipaddr == "127.0.0.1"
|
||||
(ipaddr, port) = sockObj.getLocalAddr()
|
||||
doAssert ipaddr == "127.0.0.1"
|
||||
while true:
|
||||
var line = await recvLine(sockObj)
|
||||
if line == "":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue