mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-02 11:12:37 +00:00
Provide access to getsockname()/getpeername()
Port of #3323 with added tests
This commit is contained in:
committed by
Dominik Picheta
parent
a0fb77dfd5
commit
d8ff25f032
@@ -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 == "":
|
||||
|
||||
Reference in New Issue
Block a user