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.
This commit is contained in:
alaviss 2020-08-12 06:04:54 +00:00 • committed by GitHub
commit 957bf15a08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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())