Fix streams to check only for Closed state.

This commit is contained in:
cheatfate 2021-01-30 20:15:34 +02:00 committed by zah
parent 534df03a0b
commit c0472bd349
2 changed files with 17 additions and 17 deletions

View File

@ -206,9 +206,9 @@ proc newAsyncStreamIncorrectError*(m: string): ref AsyncStreamIncorrectError {.
inline.} = inline.} =
newException(AsyncStreamIncorrectError, m) newException(AsyncStreamIncorrectError, m)
template checkRunning*(t: untyped) = template checkStreamClosed*(t: untyped) =
if not(t.running()): if t.state == AsyncStreamState.Closed:
raise newAsyncStreamIncorrectError("Incorrect stream state") raise newAsyncStreamUseClosedError()
proc atEof*(rstream: AsyncStreamReader): bool = proc atEof*(rstream: AsyncStreamReader): bool =
## Returns ``true`` is reading stream is closed or finished and internal ## Returns ``true`` is reading stream is closed or finished and internal
@ -332,7 +332,7 @@ proc readExactly*(rstream: AsyncStreamReader, pbytes: pointer,
doAssert(not(isNil(pbytes)), "pbytes must not be nil") doAssert(not(isNil(pbytes)), "pbytes must not be nil")
doAssert(nbytes >= 0, "nbytes must be non-negative integer") doAssert(nbytes >= 0, "nbytes must be non-negative integer")
checkRunning(rstream) checkStreamClosed(rstream)
if nbytes == 0: if nbytes == 0:
return return
@ -370,7 +370,7 @@ proc readOnce*(rstream: AsyncStreamReader, pbytes: pointer,
## internal buffer, otherwise it will wait until some bytes will be available. ## internal buffer, otherwise it will wait until some bytes will be available.
doAssert(not(isNil(pbytes)), "pbytes must not be nil") doAssert(not(isNil(pbytes)), "pbytes must not be nil")
doAssert(nbytes > 0, "nbytes must be positive value") doAssert(nbytes > 0, "nbytes must be positive value")
checkRunning(rstream) checkStreamClosed(rstream)
if isNil(rstream.rsource): if isNil(rstream.rsource):
try: try:
@ -411,7 +411,7 @@ proc readUntil*(rstream: AsyncStreamReader, pbytes: pointer, nbytes: int,
doAssert(not(isNil(pbytes)), "pbytes must not be nil") doAssert(not(isNil(pbytes)), "pbytes must not be nil")
doAssert(len(sep) > 0, "separator must not be empty") doAssert(len(sep) > 0, "separator must not be empty")
doAssert(nbytes >= 0, "nbytes must be non-negative value") doAssert(nbytes >= 0, "nbytes must be non-negative value")
checkRunning(rstream) checkStreamClosed(rstream)
if nbytes == 0: if nbytes == 0:
raise newAsyncStreamLimitError() raise newAsyncStreamLimitError()
@ -467,7 +467,7 @@ proc readLine*(rstream: AsyncStreamReader, limit = 0,
## ##
## If ``limit`` more then 0, then result string will be limited to ``limit`` ## If ``limit`` more then 0, then result string will be limited to ``limit``
## bytes. ## bytes.
checkRunning(rstream) checkStreamClosed(rstream)
if isNil(rstream.rsource): if isNil(rstream.rsource):
try: try:
@ -513,7 +513,7 @@ proc read*(rstream: AsyncStreamReader): Future[seq[byte]] {.async.} =
## Read all bytes from read-only stream ``rstream``. ## Read all bytes from read-only stream ``rstream``.
## ##
## This procedure allocates buffer seq[byte] and return it as result. ## This procedure allocates buffer seq[byte] and return it as result.
checkRunning(rstream) checkStreamClosed(rstream)
if isNil(rstream.rsource): if isNil(rstream.rsource):
try: try:
@ -543,7 +543,7 @@ proc read*(rstream: AsyncStreamReader, n: int): Future[seq[byte]] {.async.} =
## ``rstream``. ## ``rstream``.
## ##
## This procedure allocates buffer seq[byte] and return it as result. ## This procedure allocates buffer seq[byte] and return it as result.
checkRunning(rstream) checkStreamClosed(rstream)
if isNil(rstream.rsource): if isNil(rstream.rsource):
try: try:
@ -573,7 +573,7 @@ proc consume*(rstream: AsyncStreamReader): Future[int] {.async.} =
## Consume (discard) all bytes from read-only stream ``rstream``. ## Consume (discard) all bytes from read-only stream ``rstream``.
## ##
## Return number of bytes actually consumed (discarded). ## Return number of bytes actually consumed (discarded).
checkRunning(rstream) checkStreamClosed(rstream)
if isNil(rstream.rsource): if isNil(rstream.rsource):
try: try:
@ -602,7 +602,7 @@ proc consume*(rstream: AsyncStreamReader, n: int): Future[int] {.async.} =
## ``rstream``. ## ``rstream``.
## ##
## Return number of bytes actually consumed (discarded). ## Return number of bytes actually consumed (discarded).
checkRunning(rstream) checkStreamClosed(rstream)
if isNil(rstream.rsource): if isNil(rstream.rsource):
try: try:
@ -645,7 +645,7 @@ proc readMessage*(rstream: AsyncStreamReader, pred: ReadMessagePredicate) {.
## ``predicate`` callback will receive (zero-length) openarray, if stream ## ``predicate`` callback will receive (zero-length) openarray, if stream
## is at EOF. ## is at EOF.
doAssert(not(isNil(pred)), "`predicate` callback should not be `nil`") doAssert(not(isNil(pred)), "`predicate` callback should not be `nil`")
checkRunning(rstream) checkStreamClosed(rstream)
if isNil(rstream.rsource): if isNil(rstream.rsource):
try: try:
@ -675,7 +675,7 @@ proc write*(wstream: AsyncStreamWriter, pbytes: pointer,
## writer stream ``wstream``. ## writer stream ``wstream``.
## ##
## ``nbytes` must be more then zero. ## ``nbytes` must be more then zero.
checkRunning(wstream) checkStreamClosed(wstream)
if nbytes <= 0: if nbytes <= 0:
raise newAsyncStreamIncorrectError("Zero length message") raise newAsyncStreamIncorrectError("Zero length message")
@ -722,7 +722,7 @@ proc write*(wstream: AsyncStreamWriter, sbytes: seq[byte],
## If ``msglen < 0`` whole sequence ``sbytes`` will be writen to stream. ## If ``msglen < 0`` whole sequence ``sbytes`` will be writen to stream.
## If ``msglen > len(sbytes)`` only ``len(sbytes)`` bytes will be written to ## If ``msglen > len(sbytes)`` only ``len(sbytes)`` bytes will be written to
## stream. ## stream.
checkRunning(wstream) checkStreamClosed(wstream)
let length = if msglen <= 0: len(sbytes) else: min(msglen, len(sbytes)) let length = if msglen <= 0: len(sbytes) else: min(msglen, len(sbytes))
if length <= 0: if length <= 0:
raise newAsyncStreamIncorrectError("Zero length message") raise newAsyncStreamIncorrectError("Zero length message")
@ -770,7 +770,7 @@ proc write*(wstream: AsyncStreamWriter, sbytes: string,
## If ``msglen < 0`` whole string ``sbytes`` will be writen to stream. ## If ``msglen < 0`` whole string ``sbytes`` will be writen to stream.
## If ``msglen > len(sbytes)`` only ``len(sbytes)`` bytes will be written to ## If ``msglen > len(sbytes)`` only ``len(sbytes)`` bytes will be written to
## stream. ## stream.
checkRunning(wstream) checkStreamClosed(wstream)
let length = if msglen <= 0: len(sbytes) else: min(msglen, len(sbytes)) let length = if msglen <= 0: len(sbytes) else: min(msglen, len(sbytes))
if length <= 0: if length <= 0:
raise newAsyncStreamIncorrectError("Zero length message") raise newAsyncStreamIncorrectError("Zero length message")
@ -811,7 +811,7 @@ proc write*(wstream: AsyncStreamWriter, sbytes: string,
proc finish*(wstream: AsyncStreamWriter) {.async.} = proc finish*(wstream: AsyncStreamWriter) {.async.} =
## Finish write stream ``wstream``. ## Finish write stream ``wstream``.
checkRunning(wstream) checkStreamClosed(wstream)
if not isNil(wstream.wsource): if not isNil(wstream.wsource):
if isNil(wstream.writerLoop): if isNil(wstream.writerLoop):

View File

@ -48,7 +48,7 @@ proc readUntilBoundary*(rstream: AsyncStreamReader, pbytes: pointer,
doAssert(not(isNil(pbytes)), "pbytes must not be nil") doAssert(not(isNil(pbytes)), "pbytes must not be nil")
doAssert(len(sep) > 0, "separator must not be empty") doAssert(len(sep) > 0, "separator must not be empty")
doAssert(nbytes >= 0, "nbytes must be non-negative value") doAssert(nbytes >= 0, "nbytes must be non-negative value")
checkRunning(rstream) checkStreamClosed(rstream)
var k = 0 var k = 0
var state = 0 var state = 0