diff options
author | alaviss <leorize+oss@disroot.org> | 2020-08-12 06:04:54 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-12 08:04:54 +0200 |
commit | 957bf15a08d5443a50452909743747d19b5f29f8 (patch) | |
tree | cd91e0bb130b1f41d14c7b51c0812089f9ccc7b8 /lib/pure | |
parent | ce2da87ecae09b952cb9b247662703577723bb02 (diff) | |
download | Nim-957bf15a08d5443a50452909743747d19b5f29f8.tar.gz |
asyncnet: don't try to close the socket again [backport] (#15174)
The closed flag isn't a good design by any means, but let's have this working first before I get rid of the flag and potentially create a non-backportable commit.
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/asyncnet.nim | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/lib/pure/asyncnet.nim b/lib/pure/asyncnet.nim index 9846f8999..455efe1c1 100644 --- a/lib/pure/asyncnet.nim +++ b/lib/pure/asyncnet.nim @@ -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 = @@ -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) = @@ -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") @@ -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: @@ -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: @@ -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 @@ -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()) |