Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions lib/pure/asyncnet.nim
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,12 @@ elif defined(nimdoc):

proc close*(socket: AsyncSocket) =
## Closes the socket.
if socket.closed: return

defer:
socket.fd.AsyncFD.closeSocket()
socket.closed = true # TODO: Add extra debugging checks for this.

when defineSsl:
if socket.isSsl:
let res =
Expand All @@ -731,7 +735,6 @@ proc close*(socket: AsyncSocket) =
discard
elif res != 1:
raiseSSLError()
socket.closed = true # TODO: Add extra debugging checks for this.

when defineSsl:
proc wrapSocket*(ctx: SslContext, socket: AsyncSocket) =
Expand Down Expand Up @@ -819,9 +822,9 @@ proc sendTo*(socket: AsyncSocket, address: string, port: Port, data: string,
## address or a hostname. If a hostname is specified this function will try
## each IP of that hostname. The returned future will complete once all data
## has been sent.
##
##
## If an error occurs an OSError exception will be raised.
##
##
## This proc is normally used with connectionless sockets (UDP sockets).
assert(socket.protocol != IPPROTO_TCP,
"Cannot `sendTo` on a TCP socket. Use `send` instead")
Expand All @@ -834,22 +837,22 @@ proc sendTo*(socket: AsyncSocket, address: string, port: Port, data: string,
it = aiList
success = false
lastException: ref Exception

while it != nil:
let fut = sendTo(socket.fd.AsyncFD, cstring(data), len(data), it.ai_addr,
it.ai_addrlen.SockLen, flags)

yield fut

if not fut.failed:
success = true

break

lastException = fut.readError()

it = it.ai_next

freeaddrinfo(aiList)

if not success:
Expand All @@ -869,24 +872,24 @@ proc recvFrom*(socket: AsyncSocket, data: FutureVar[string], size: int,
## packet received.
##
## If an error occurs an OSError exception will be raised.
##
##
## This proc is normally used with connectionless sockets (UDP sockets).
##
##
## **Notes**
## * ``data`` must be initialized to the length of ``size``.
## * ``address`` must be initialized to 46 in length.
template adaptRecvFromToDomain(domain: Domain) =
var lAddr = sizeof(sAddr).SockLen

result = await recvFromInto(AsyncFD(getFd(socket)), cstring(data.mget()), size,
cast[ptr SockAddr](addr sAddr), addr lAddr,
flags)

data.mget().setLen(result)
data.complete()

getAddrString(cast[ptr SockAddr](addr sAddr), address.mget())

address.complete()

when domain == AF_INET6:
Expand All @@ -901,7 +904,7 @@ proc recvFrom*(socket: AsyncSocket, data: FutureVar[string], size: int,
"`date` was not initialized correctly. `size` != `len(data.mget())`")
assert(46 == len(address.mget()),
"`address` was not initialized correctly. 46 != `len(address.mget())`")

case socket.domain
of AF_INET6:
var sAddr: Sockaddr_in6
Expand All @@ -920,18 +923,18 @@ proc recvFrom*(socket: AsyncSocket, size: int,
## ``size``. Returned future will complete once one datagram has been received
## and will return tuple with: data of packet received; and address and port
## of datagram's sender.
##
##
## If an error occurs an OSError exception will be raised.
##
##
## This proc is normally used with connectionless sockets (UDP sockets).
var
data = newFutureVar[string]()
address = newFutureVar[string]()
port = newFutureVar[Port]()

data.mget().setLen(size)
address.mget().setLen(46)

let read = await recvFrom(socket, data, size, address, port, flags)

result = (data.mget(), address.mget(), port.mget())
Expand Down