From b59e759e87d15598fe3c8e455c029d4726860725 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Wed, 23 Oct 2019 14:13:23 +0300 Subject: [PATCH] Fix #53 --- chronos/transports/common.nim | 6 ++++++ chronos/transports/stream.nim | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/chronos/transports/common.nim b/chronos/transports/common.nim index 78cae403..f8fb55ff 100644 --- a/chronos/transports/common.nim +++ b/chronos/transports/common.nim @@ -457,6 +457,12 @@ template checkClosed*(t: untyped, future: untyped) = future.fail(newException(TransportError, "Transport is already closed!")) return future +template checkWriteEof*(t: untyped, future: untyped) = + if (WriteEof in (t).state): + future.fail(newException(TransportError, + "Transport connection is already dropped!")) + return future + template getError*(t: untyped): ref Exception = var err = (t).error (t).error = nil diff --git a/chronos/transports/stream.nim b/chronos/transports/stream.nim index 604bb924..3348d0f2 100644 --- a/chronos/transports/stream.nim +++ b/chronos/transports/stream.nim @@ -1614,6 +1614,7 @@ proc write*(transp: StreamTransport, pbytes: pointer, ## ``transp``. var retFuture = newFuture[int]("stream.transport.write(pointer)") transp.checkClosed(retFuture) + transp.checkWriteEof(retFuture) var vector = StreamVector(kind: DataBuffer, writer: retFuture, buf: pbytes, buflen: nbytes) transp.queue.addLast(vector) @@ -1625,6 +1626,7 @@ proc write*(transp: StreamTransport, msg: string, msglen = -1): Future[int] = ## Write data from string ``msg`` using transport ``transp``. var retFuture = newFutureStr[int]("stream.transport.write(string)") transp.checkClosed(retFuture) + transp.checkWriteEof(retFuture) if not isLiteral(msg): shallowCopy(retFuture.gcholder, msg) else: @@ -1642,6 +1644,7 @@ proc write*[T](transp: StreamTransport, msg: seq[T], msglen = -1): Future[int] = ## Write sequence ``msg`` using transport ``transp``. var retFuture = newFutureSeq[int, T]("stream.transport.write(seq)") transp.checkClosed(retFuture) + transp.checkWriteEof(retFuture) if not isLiteral(msg): shallowCopy(retFuture.gcholder, msg) else: @@ -1667,6 +1670,7 @@ proc writeFile*(transp: StreamTransport, handle: int, raise newException(TransportNoSupport, "writeFile() is not supported!") var retFuture = newFuture[int]("stream.transport.writeFile") transp.checkClosed(retFuture) + transp.checkWriteEof(retFuture) var vector = StreamVector(kind: DataFile, writer: retFuture, buf: cast[pointer](size), offset: offset, buflen: handle)