2021-01-20 13:40:15 +00:00
|
|
|
#
|
|
|
|
# Chronos Asynchronous Bound Stream
|
|
|
|
# (c) Copyright 2021-Present
|
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
|
|
|
|
## This module implements bounded stream reading and writing.
|
|
|
|
##
|
|
|
|
## For stream reading it means that you should read exactly bounded size of
|
2021-01-24 21:40:52 +00:00
|
|
|
## bytes or you should read all bytes until specific boundary.
|
2021-01-20 13:40:15 +00:00
|
|
|
##
|
|
|
|
## For stream writing it means that you should write exactly bounded size
|
2021-01-24 21:40:52 +00:00
|
|
|
## of bytes.
|
2021-04-26 11:05:37 +00:00
|
|
|
import std/options
|
2021-01-20 13:40:15 +00:00
|
|
|
import ../asyncloop, ../timer
|
|
|
|
import asyncstream, ../transports/stream, ../transports/common
|
|
|
|
export asyncstream, stream, timer, common
|
|
|
|
|
|
|
|
type
|
2021-02-09 11:56:33 +00:00
|
|
|
BoundCmp* {.pure.} = enum
|
|
|
|
Equal, LessOrEqual
|
|
|
|
|
2021-01-20 13:40:15 +00:00
|
|
|
BoundedStreamReader* = ref object of AsyncStreamReader
|
2021-04-26 11:05:37 +00:00
|
|
|
boundSize: Option[uint64]
|
2021-01-24 21:40:52 +00:00
|
|
|
boundary: seq[byte]
|
2021-04-26 11:05:37 +00:00
|
|
|
offset: uint64
|
2021-02-09 11:56:33 +00:00
|
|
|
cmpop: BoundCmp
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
BoundedStreamWriter* = ref object of AsyncStreamWriter
|
2021-04-26 11:05:37 +00:00
|
|
|
boundSize: uint64
|
|
|
|
offset: uint64
|
2021-02-09 11:56:33 +00:00
|
|
|
cmpop: BoundCmp
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
BoundedStreamError* = object of AsyncStreamError
|
|
|
|
BoundedStreamIncompleteError* = object of BoundedStreamError
|
|
|
|
BoundedStreamOverflowError* = object of BoundedStreamError
|
|
|
|
|
|
|
|
BoundedStreamRW* = BoundedStreamReader | BoundedStreamWriter
|
|
|
|
|
|
|
|
const
|
|
|
|
BoundedBufferSize* = 4096
|
2021-04-26 11:05:37 +00:00
|
|
|
BoundarySizeDefectMessage = "Boundary must not be empty array"
|
2021-01-20 13:40:15 +00:00
|
|
|
|
2021-04-26 11:05:37 +00:00
|
|
|
template newBoundedStreamIncompleteError(): ref BoundedStreamError =
|
2021-01-20 13:40:15 +00:00
|
|
|
newException(BoundedStreamIncompleteError,
|
|
|
|
"Stream boundary is not reached yet")
|
|
|
|
|
2021-04-26 11:05:37 +00:00
|
|
|
template newBoundedStreamOverflowError(): ref BoundedStreamOverflowError =
|
|
|
|
newException(BoundedStreamOverflowError, "Stream boundary exceeded")
|
|
|
|
|
|
|
|
proc readUntilBoundary(rstream: AsyncStreamReader, pbytes: pointer,
|
|
|
|
nbytes: int, sep: seq[byte]): Future[int] {.async.} =
|
2021-01-24 21:40:52 +00:00
|
|
|
doAssert(not(isNil(pbytes)), "pbytes must not be nil")
|
|
|
|
doAssert(nbytes >= 0, "nbytes must be non-negative value")
|
2021-01-30 18:15:34 +00:00
|
|
|
checkStreamClosed(rstream)
|
2021-01-24 21:40:52 +00:00
|
|
|
|
2021-02-09 11:56:33 +00:00
|
|
|
if nbytes == 0:
|
|
|
|
return 0
|
|
|
|
|
2021-01-24 21:40:52 +00:00
|
|
|
var k = 0
|
|
|
|
var state = 0
|
|
|
|
var pbuffer = cast[ptr UncheckedArray[byte]](pbytes)
|
|
|
|
|
|
|
|
proc predicate(data: openarray[byte]): tuple[consumed: int, done: bool] =
|
|
|
|
if len(data) == 0:
|
|
|
|
(0, true)
|
|
|
|
else:
|
|
|
|
var index = 0
|
|
|
|
while index < len(data):
|
|
|
|
if k >= nbytes:
|
|
|
|
return (index, true)
|
|
|
|
let ch = data[index]
|
|
|
|
inc(index)
|
|
|
|
pbuffer[k] = ch
|
|
|
|
inc(k)
|
2021-02-09 11:56:33 +00:00
|
|
|
if len(sep) > 0:
|
|
|
|
if sep[state] == ch:
|
|
|
|
inc(state)
|
|
|
|
if state == len(sep):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
state = 0
|
|
|
|
(index, (state == len(sep)) or (k == nbytes))
|
2021-01-24 21:40:52 +00:00
|
|
|
|
|
|
|
await rstream.readMessage(predicate)
|
2021-02-09 11:56:33 +00:00
|
|
|
return k
|
2021-01-24 21:40:52 +00:00
|
|
|
|
|
|
|
func endsWith(s, suffix: openarray[byte]): bool =
|
|
|
|
var i = 0
|
|
|
|
var j = len(s) - len(suffix)
|
|
|
|
while i + j >= 0 and i + j < len(s):
|
|
|
|
if s[i + j] != suffix[i]: return false
|
|
|
|
inc(i)
|
|
|
|
if i >= len(suffix): return true
|
|
|
|
|
2021-01-20 13:40:15 +00:00
|
|
|
proc boundedReadLoop(stream: AsyncStreamReader) {.async.} =
|
2021-02-17 00:03:12 +00:00
|
|
|
var rstream = BoundedStreamReader(stream)
|
2021-01-20 13:40:15 +00:00
|
|
|
rstream.state = AsyncStreamState.Running
|
2021-01-24 21:40:52 +00:00
|
|
|
var buffer = newSeq[byte](rstream.buffer.bufferLen())
|
2021-01-20 13:40:15 +00:00
|
|
|
while true:
|
2021-04-26 11:05:37 +00:00
|
|
|
let toRead =
|
|
|
|
if rstream.boundSize.isNone():
|
|
|
|
len(buffer)
|
|
|
|
else:
|
|
|
|
int(min(rstream.boundSize.get() - rstream.offset, uint64(len(buffer))))
|
|
|
|
try:
|
2021-05-07 15:52:44 +00:00
|
|
|
if toRead == 0:
|
|
|
|
# When ``rstream.boundSize`` is set and we already readed
|
|
|
|
# ``rstream.boundSize`` bytes.
|
|
|
|
rstream.state = AsyncStreamState.Finished
|
|
|
|
else:
|
|
|
|
let res = await readUntilBoundary(rstream.rsource, addr buffer[0],
|
|
|
|
toRead, rstream.boundary)
|
|
|
|
if res > 0:
|
|
|
|
if len(rstream.boundary) > 0:
|
|
|
|
if endsWith(buffer.toOpenArray(0, res - 1), rstream.boundary):
|
|
|
|
let length = res - len(rstream.boundary)
|
|
|
|
rstream.offset = rstream.offset + uint64(length)
|
|
|
|
# There should be one step between transferring last bytes to the
|
|
|
|
# consumer and declaring stream EOF. Otherwise could not be
|
|
|
|
# consumed.
|
|
|
|
await upload(addr rstream.buffer, addr buffer[0], length)
|
|
|
|
rstream.state = AsyncStreamState.Finished
|
|
|
|
else:
|
|
|
|
rstream.offset = rstream.offset + uint64(res)
|
|
|
|
# There should be one step between transferring last bytes to the
|
|
|
|
# consumer and declaring stream EOF. Otherwise could not be
|
|
|
|
# consumed.
|
|
|
|
await upload(addr rstream.buffer, addr buffer[0], res)
|
|
|
|
|
|
|
|
if (res < toRead) and rstream.rsource.atEof():
|
|
|
|
case rstream.cmpop
|
|
|
|
of BoundCmp.Equal:
|
|
|
|
rstream.state = AsyncStreamState.Error
|
|
|
|
rstream.error = newBoundedStreamIncompleteError()
|
|
|
|
of BoundCmp.LessOrEqual:
|
|
|
|
rstream.state = AsyncStreamState.Finished
|
2021-01-24 21:40:52 +00:00
|
|
|
else:
|
2021-04-26 21:07:49 +00:00
|
|
|
rstream.offset = rstream.offset + uint64(res)
|
|
|
|
# There should be one step between transferring last bytes to the
|
|
|
|
# consumer and declaring stream EOF. Otherwise could not be
|
|
|
|
# consumed.
|
|
|
|
await upload(addr rstream.buffer, addr buffer[0], res)
|
|
|
|
|
2021-02-18 12:08:21 +00:00
|
|
|
if (res < toRead) and rstream.rsource.atEof():
|
|
|
|
case rstream.cmpop
|
|
|
|
of BoundCmp.Equal:
|
|
|
|
rstream.state = AsyncStreamState.Error
|
|
|
|
rstream.error = newBoundedStreamIncompleteError()
|
|
|
|
of BoundCmp.LessOrEqual:
|
|
|
|
rstream.state = AsyncStreamState.Finished
|
2021-02-09 11:56:33 +00:00
|
|
|
else:
|
2021-05-07 15:52:44 +00:00
|
|
|
case rstream.cmpop
|
|
|
|
of BoundCmp.Equal:
|
|
|
|
rstream.state = AsyncStreamState.Error
|
|
|
|
rstream.error = newBoundedStreamIncompleteError()
|
|
|
|
of BoundCmp.LessOrEqual:
|
2021-02-09 11:56:33 +00:00
|
|
|
rstream.state = AsyncStreamState.Finished
|
2021-01-24 21:40:52 +00:00
|
|
|
|
2021-04-26 11:05:37 +00:00
|
|
|
except AsyncStreamError as exc:
|
|
|
|
rstream.state = AsyncStreamState.Error
|
|
|
|
rstream.error = exc
|
|
|
|
except CancelledError:
|
|
|
|
rstream.state = AsyncStreamState.Error
|
|
|
|
rstream.error = newAsyncStreamUseClosedError()
|
2021-02-09 11:56:33 +00:00
|
|
|
|
2021-04-26 11:05:37 +00:00
|
|
|
case rstream.state
|
|
|
|
of AsyncStreamState.Running:
|
|
|
|
discard
|
|
|
|
of AsyncStreamState.Error, AsyncStreamState.Stopped:
|
2021-04-26 21:07:49 +00:00
|
|
|
# Send `Error` or `Stopped` state to the consumer without waiting.
|
2021-04-26 11:05:37 +00:00
|
|
|
rstream.buffer.forget()
|
|
|
|
break
|
2021-04-26 21:07:49 +00:00
|
|
|
of AsyncStreamState.Finished:
|
|
|
|
# Send `EOF` state to the consumer and wait until it will be received.
|
|
|
|
await rstream.buffer.transfer()
|
|
|
|
break
|
|
|
|
of AsyncStreamState.Closed:
|
2021-02-09 11:56:33 +00:00
|
|
|
break
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
proc boundedWriteLoop(stream: AsyncStreamWriter) {.async.} =
|
2021-04-26 11:05:37 +00:00
|
|
|
var error: ref AsyncStreamError
|
2021-02-17 00:03:12 +00:00
|
|
|
var wstream = BoundedStreamWriter(stream)
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
wstream.state = AsyncStreamState.Running
|
|
|
|
while true:
|
2021-04-26 11:05:37 +00:00
|
|
|
var item: WriteItem
|
2021-01-20 13:40:15 +00:00
|
|
|
try:
|
|
|
|
item = await wstream.queue.get()
|
|
|
|
if item.size > 0:
|
2021-04-26 11:05:37 +00:00
|
|
|
if uint64(item.size) <= (wstream.boundSize - wstream.offset):
|
2021-01-20 13:40:15 +00:00
|
|
|
# Writing chunk data.
|
|
|
|
case item.kind
|
|
|
|
of WriteType.Pointer:
|
2021-02-18 12:08:21 +00:00
|
|
|
await wstream.wsource.write(item.dataPtr, item.size)
|
2021-01-20 13:40:15 +00:00
|
|
|
of WriteType.Sequence:
|
2021-02-18 12:08:21 +00:00
|
|
|
await wstream.wsource.write(addr item.dataSeq[0], item.size)
|
2021-01-20 13:40:15 +00:00
|
|
|
of WriteType.String:
|
2021-02-18 12:08:21 +00:00
|
|
|
await wstream.wsource.write(addr item.dataStr[0], item.size)
|
2021-04-26 11:05:37 +00:00
|
|
|
wstream.offset = wstream.offset + uint64(item.size)
|
2021-01-20 13:40:15 +00:00
|
|
|
item.future.complete()
|
|
|
|
else:
|
|
|
|
wstream.state = AsyncStreamState.Error
|
2021-04-26 11:05:37 +00:00
|
|
|
error = newBoundedStreamOverflowError()
|
2021-01-20 13:40:15 +00:00
|
|
|
else:
|
2021-04-26 11:05:37 +00:00
|
|
|
if wstream.offset == wstream.boundSize:
|
|
|
|
wstream.state = AsyncStreamState.Finished
|
|
|
|
item.future.complete()
|
|
|
|
else:
|
2021-02-09 11:56:33 +00:00
|
|
|
case wstream.cmpop
|
|
|
|
of BoundCmp.Equal:
|
|
|
|
wstream.state = AsyncStreamState.Error
|
|
|
|
error = newBoundedStreamIncompleteError()
|
|
|
|
of BoundCmp.LessOrEqual:
|
|
|
|
wstream.state = AsyncStreamState.Finished
|
|
|
|
item.future.complete()
|
2021-01-20 13:40:15 +00:00
|
|
|
except CancelledError:
|
|
|
|
wstream.state = AsyncStreamState.Stopped
|
|
|
|
error = newAsyncStreamUseClosedError()
|
2021-04-26 11:05:37 +00:00
|
|
|
except AsyncStreamError as exc:
|
2021-01-20 13:40:15 +00:00
|
|
|
wstream.state = AsyncStreamState.Error
|
|
|
|
error = exc
|
|
|
|
|
2021-04-26 11:05:37 +00:00
|
|
|
case wstream.state
|
|
|
|
of AsyncStreamState.Running:
|
|
|
|
discard
|
|
|
|
of AsyncStreamState.Error, AsyncStreamState.Stopped:
|
|
|
|
if not(isNil(item.future)):
|
|
|
|
if not(item.future.finished()):
|
|
|
|
item.future.fail(error)
|
2021-01-20 13:40:15 +00:00
|
|
|
break
|
2021-04-26 11:05:37 +00:00
|
|
|
of AsyncStreamState.Finished, AsyncStreamState.Closed:
|
|
|
|
error = newAsyncStreamUseClosedError()
|
|
|
|
break
|
|
|
|
|
|
|
|
doAssert(not(isNil(error)))
|
|
|
|
while not(wstream.queue.empty()):
|
|
|
|
let item = wstream.queue.popFirstNoWait()
|
|
|
|
if not(item.future.finished()):
|
|
|
|
item.future.fail(error)
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
proc bytesLeft*(stream: BoundedStreamRW): uint64 =
|
|
|
|
## Returns number of bytes left in stream.
|
2021-04-26 11:05:37 +00:00
|
|
|
if stream.boundSize.isSome():
|
|
|
|
stream.boundSize.get() - stream.bytesCount
|
|
|
|
else:
|
|
|
|
0'u64
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
proc init*[T](child: BoundedStreamReader, rsource: AsyncStreamReader,
|
2021-04-26 11:05:37 +00:00
|
|
|
boundSize: uint64, comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
bufferSize = BoundedBufferSize, udata: ref T) =
|
2021-04-26 11:05:37 +00:00
|
|
|
child.boundSize = some(boundSize)
|
|
|
|
child.cmpop = comparison
|
2021-02-17 00:03:12 +00:00
|
|
|
init(AsyncStreamReader(child), rsource, boundedReadLoop, bufferSize,
|
2021-01-20 13:40:15 +00:00
|
|
|
udata)
|
|
|
|
|
2021-04-26 11:05:37 +00:00
|
|
|
proc init*[T](child: BoundedStreamReader, rsource: AsyncStreamReader,
|
|
|
|
boundary: openarray[byte], comparison = BoundCmp.Equal,
|
|
|
|
bufferSize = BoundedBufferSize, udata: ref T) =
|
|
|
|
doAssert(len(boundary) > 0, BoundarySizeDefectMessage)
|
|
|
|
child.boundary = @boundary
|
|
|
|
child.cmpop = comparison
|
|
|
|
init(AsyncStreamReader(child), rsource, boundedReadLoop, bufferSize,
|
|
|
|
udata)
|
|
|
|
|
|
|
|
proc init*[T](child: BoundedStreamReader, rsource: AsyncStreamReader,
|
|
|
|
boundSize: uint64, boundary: openarray[byte],
|
|
|
|
comparison = BoundCmp.Equal,
|
|
|
|
bufferSize = BoundedBufferSize, udata: ref T) =
|
|
|
|
doAssert(len(boundary) > 0, BoundarySizeDefectMessage)
|
|
|
|
child.boundSize = some(boundSize)
|
|
|
|
child.boundary = @boundary
|
|
|
|
child.cmpop = comparison
|
|
|
|
init(AsyncStreamReader(child), rsource, boundedReadLoop, bufferSize,
|
|
|
|
udata)
|
|
|
|
|
|
|
|
proc init*(child: BoundedStreamReader, rsource: AsyncStreamReader,
|
|
|
|
boundSize: uint64, comparison = BoundCmp.Equal,
|
|
|
|
bufferSize = BoundedBufferSize) =
|
|
|
|
child.boundSize = some(boundSize)
|
|
|
|
child.cmpop = comparison
|
|
|
|
init(AsyncStreamReader(child), rsource, boundedReadLoop, bufferSize)
|
|
|
|
|
2021-01-20 13:40:15 +00:00
|
|
|
proc init*(child: BoundedStreamReader, rsource: AsyncStreamReader,
|
2021-04-26 11:05:37 +00:00
|
|
|
boundary: openarray[byte], comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
bufferSize = BoundedBufferSize) =
|
2021-04-26 11:05:37 +00:00
|
|
|
doAssert(len(boundary) > 0, BoundarySizeDefectMessage)
|
|
|
|
child.boundary = @boundary
|
|
|
|
child.cmpop = comparison
|
|
|
|
init(AsyncStreamReader(child), rsource, boundedReadLoop, bufferSize)
|
|
|
|
|
|
|
|
proc init*(child: BoundedStreamReader, rsource: AsyncStreamReader,
|
|
|
|
boundSize: uint64, boundary: openarray[byte],
|
|
|
|
comparison = BoundCmp.Equal, bufferSize = BoundedBufferSize) =
|
|
|
|
doAssert(len(boundary) > 0, BoundarySizeDefectMessage)
|
|
|
|
child.boundSize = some(boundSize)
|
|
|
|
child.boundary = @boundary
|
|
|
|
child.cmpop = comparison
|
2021-02-17 00:03:12 +00:00
|
|
|
init(AsyncStreamReader(child), rsource, boundedReadLoop, bufferSize)
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
proc newBoundedStreamReader*[T](rsource: AsyncStreamReader,
|
2021-04-26 11:05:37 +00:00
|
|
|
boundSize: uint64,
|
2021-02-09 11:56:33 +00:00
|
|
|
comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
bufferSize = BoundedBufferSize,
|
|
|
|
udata: ref T): BoundedStreamReader =
|
2021-04-26 11:05:37 +00:00
|
|
|
## Create new stream reader which will be limited by size ``boundSize``. When
|
|
|
|
## number of bytes readed by consumer reaches ``boundSize``,
|
|
|
|
## BoundedStreamReader will enter EOF state (no more bytes will be returned
|
|
|
|
## to the consumer).
|
|
|
|
##
|
|
|
|
## If ``comparison`` operator is ``BoundCmp.Equal`` and number of bytes readed
|
|
|
|
## from source stream reader ``rsource`` is less than ``boundSize`` -
|
|
|
|
## ``BoundedStreamIncompleteError`` will be raised. But comparison operator
|
|
|
|
## ``BoundCmp.LessOrEqual`` allows to consume less bytes without
|
|
|
|
## ``BoundedStreamIncompleteError`` exception.
|
|
|
|
var res = BoundedStreamReader()
|
|
|
|
res.init(rsource, boundSize, comparison, bufferSize, udata)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc newBoundedStreamReader*[T](rsource: AsyncStreamReader,
|
|
|
|
boundary: openarray[byte],
|
|
|
|
comparison = BoundCmp.Equal,
|
|
|
|
bufferSize = BoundedBufferSize,
|
|
|
|
udata: ref T): BoundedStreamReader =
|
|
|
|
## Create new stream reader which will be limited by binary boundary
|
|
|
|
## ``boundary``. As soon as reader reaches ``boundary`` BoundedStreamReader
|
|
|
|
## will enter EOF state (no more bytes will be returned to the consumer).
|
|
|
|
##
|
|
|
|
## If ``comparison`` operator is ``BoundCmp.Equal`` and number of bytes readed
|
|
|
|
## from source stream reader ``rsource`` is less than ``boundSize`` -
|
|
|
|
## ``BoundedStreamIncompleteError`` will be raised. But comparison operator
|
|
|
|
## ``BoundCmp.LessOrEqual`` allows to consume less bytes without
|
|
|
|
## ``BoundedStreamIncompleteError`` exception.
|
|
|
|
var res = BoundedStreamReader()
|
|
|
|
res.init(rsource, boundary, comparison, bufferSize, udata)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc newBoundedStreamReader*[T](rsource: AsyncStreamReader,
|
|
|
|
boundSize: uint64,
|
|
|
|
boundary: openarray[byte],
|
|
|
|
comparison = BoundCmp.Equal,
|
|
|
|
bufferSize = BoundedBufferSize,
|
|
|
|
udata: ref T): BoundedStreamReader =
|
|
|
|
## Create new stream reader which will be limited by size ``boundSize`` or
|
|
|
|
## boundary ``boundary``. As soon as reader reaches ``boundary`` ``OR`` number
|
|
|
|
## of bytes readed from source stream reader ``rsource`` reaches ``boundSize``
|
|
|
|
## BoundStreamReader will enter EOF state (no more bytes will be returned to
|
|
|
|
## the consumer).
|
|
|
|
##
|
|
|
|
## If ``comparison`` operator is ``BoundCmp.Equal`` and number of bytes readed
|
|
|
|
## from source stream reader ``rsource`` is less than ``boundSize`` -
|
|
|
|
## ``BoundedStreamIncompleteError`` will be raised. But comparison operator
|
|
|
|
## ``BoundCmp.LessOrEqual`` allows to consume less bytes without
|
|
|
|
## ``BoundedStreamIncompleteError`` exception.
|
|
|
|
var res = BoundedStreamReader()
|
|
|
|
res.init(rsource, boundSize, boundary, comparison, bufferSize, udata)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc newBoundedStreamReader*(rsource: AsyncStreamReader,
|
|
|
|
boundSize: uint64,
|
|
|
|
comparison = BoundCmp.Equal,
|
|
|
|
bufferSize = BoundedBufferSize,
|
|
|
|
): BoundedStreamReader =
|
|
|
|
## Create new stream reader which will be limited by size ``boundSize``. When
|
|
|
|
## number of bytes readed by consumer reaches ``boundSize``,
|
|
|
|
## BoundedStreamReader will enter EOF state (no more bytes will be returned
|
|
|
|
## to the consumer).
|
|
|
|
##
|
|
|
|
## If ``comparison`` operator is ``BoundCmp.Equal`` and number of bytes readed
|
|
|
|
## from source stream reader ``rsource`` is less than ``boundSize`` -
|
|
|
|
## ``BoundedStreamIncompleteError`` will be raised. But comparison operator
|
|
|
|
## ``BoundCmp.LessOrEqual`` allows to consume less bytes without
|
|
|
|
## ``BoundedStreamIncompleteError`` exception.
|
|
|
|
var res = BoundedStreamReader()
|
|
|
|
res.init(rsource, boundSize, comparison, bufferSize)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc newBoundedStreamReader*(rsource: AsyncStreamReader,
|
|
|
|
boundary: openarray[byte],
|
|
|
|
comparison = BoundCmp.Equal,
|
|
|
|
bufferSize = BoundedBufferSize,
|
|
|
|
): BoundedStreamReader =
|
|
|
|
## Create new stream reader which will be limited by binary boundary
|
|
|
|
## ``boundary``. As soon as reader reaches ``boundary`` BoundedStreamReader
|
|
|
|
## will enter EOF state (no more bytes will be returned to the consumer).
|
|
|
|
##
|
|
|
|
## If ``comparison`` operator is ``BoundCmp.Equal`` and number of bytes readed
|
|
|
|
## from source stream reader ``rsource`` is less than ``boundSize`` -
|
|
|
|
## ``BoundedStreamIncompleteError`` will be raised. But comparison operator
|
|
|
|
## ``BoundCmp.LessOrEqual`` allows to consume less bytes without
|
|
|
|
## ``BoundedStreamIncompleteError`` exception.
|
|
|
|
var res = BoundedStreamReader()
|
|
|
|
res.init(rsource, boundary, comparison, bufferSize)
|
2021-01-20 13:40:15 +00:00
|
|
|
res
|
|
|
|
|
|
|
|
proc newBoundedStreamReader*(rsource: AsyncStreamReader,
|
2021-04-26 11:05:37 +00:00
|
|
|
boundSize: uint64,
|
|
|
|
boundary: openarray[byte],
|
2021-02-09 11:56:33 +00:00
|
|
|
comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
bufferSize = BoundedBufferSize,
|
2021-04-26 11:05:37 +00:00
|
|
|
): BoundedStreamReader =
|
|
|
|
## Create new stream reader which will be limited by size ``boundSize`` or
|
|
|
|
## boundary ``boundary``. As soon as reader reaches ``boundary`` ``OR`` number
|
|
|
|
## of bytes readed from source stream reader ``rsource`` reaches ``boundSize``
|
|
|
|
## BoundStreamReader will enter EOF state (no more bytes will be returned to
|
|
|
|
## the consumer).
|
|
|
|
##
|
|
|
|
## If ``comparison`` operator is ``BoundCmp.Equal`` and number of bytes readed
|
|
|
|
## from source stream reader ``rsource`` is less than ``boundSize`` -
|
|
|
|
## ``BoundedStreamIncompleteError`` will be raised. But comparison operator
|
|
|
|
## ``BoundCmp.LessOrEqual`` allows to consume less bytes without
|
|
|
|
## ``BoundedStreamIncompleteError`` exception.
|
|
|
|
var res = BoundedStreamReader()
|
|
|
|
res.init(rsource, boundSize, boundary, comparison, bufferSize)
|
2021-01-20 13:40:15 +00:00
|
|
|
res
|
|
|
|
|
|
|
|
proc init*[T](child: BoundedStreamWriter, wsource: AsyncStreamWriter,
|
2021-04-26 11:05:37 +00:00
|
|
|
boundSize: uint64, comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
queueSize = AsyncStreamDefaultQueueSize, udata: ref T) =
|
2021-04-26 11:05:37 +00:00
|
|
|
child.boundSize = boundSize
|
|
|
|
child.cmpop = comparison
|
2021-02-17 00:03:12 +00:00
|
|
|
init(AsyncStreamWriter(child), wsource, boundedWriteLoop, queueSize,
|
2021-01-20 13:40:15 +00:00
|
|
|
udata)
|
|
|
|
|
|
|
|
proc init*(child: BoundedStreamWriter, wsource: AsyncStreamWriter,
|
2021-04-26 11:05:37 +00:00
|
|
|
boundSize: uint64, comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
queueSize = AsyncStreamDefaultQueueSize) =
|
2021-04-26 11:05:37 +00:00
|
|
|
child.boundSize = boundSize
|
|
|
|
child.cmpop = comparison
|
2021-02-17 00:03:12 +00:00
|
|
|
init(AsyncStreamWriter(child), wsource, boundedWriteLoop, queueSize)
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
proc newBoundedStreamWriter*[T](wsource: AsyncStreamWriter,
|
2021-04-26 11:05:37 +00:00
|
|
|
boundSize: uint64,
|
2021-02-09 11:56:33 +00:00
|
|
|
comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
queueSize = AsyncStreamDefaultQueueSize,
|
|
|
|
udata: ref T): BoundedStreamWriter =
|
2021-04-26 11:05:37 +00:00
|
|
|
## Create new stream writer which will be limited by size ``boundSize``. As
|
|
|
|
## soon as number of bytes written to the destination stream ``wsource``
|
|
|
|
## reaches ``boundSize`` stream will enter EOF state (no more bytes will be
|
|
|
|
## sent to remote destination stream ``wsource``).
|
|
|
|
##
|
|
|
|
## If ``comparison`` operator is ``BoundCmp.Equal`` and number of bytes
|
|
|
|
## written to destination stream ``wsource`` is less than ``boundSize`` -
|
|
|
|
## ``BoundedStreamIncompleteError`` will be raised on stream finishing. But
|
|
|
|
## comparison operator ``BoundCmp.LessOrEqual`` allows to send less bytes
|
|
|
|
## without ``BoundedStreamIncompleteError`` exception.
|
|
|
|
##
|
|
|
|
## For both comparison operators any attempt to write more bytes than
|
|
|
|
## ``boundSize`` will be interrupted with ``BoundedStreamOverflowError``
|
|
|
|
## exception.
|
|
|
|
var res = BoundedStreamWriter()
|
|
|
|
res.init(wsource, boundSize, comparison, queueSize, udata)
|
2021-01-20 13:40:15 +00:00
|
|
|
res
|
|
|
|
|
|
|
|
proc newBoundedStreamWriter*(wsource: AsyncStreamWriter,
|
2021-04-26 11:05:37 +00:00
|
|
|
boundSize: uint64,
|
2021-02-09 11:56:33 +00:00
|
|
|
comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
queueSize = AsyncStreamDefaultQueueSize,
|
|
|
|
): BoundedStreamWriter =
|
2021-04-26 11:05:37 +00:00
|
|
|
## Create new stream writer which will be limited by size ``boundSize``. As
|
|
|
|
## soon as number of bytes written to the destination stream ``wsource``
|
|
|
|
## reaches ``boundSize`` stream will enter EOF state (no more bytes will be
|
|
|
|
## sent to remote destination stream ``wsource``).
|
|
|
|
##
|
|
|
|
## If ``comparison`` operator is ``BoundCmp.Equal`` and number of bytes
|
|
|
|
## written to destination stream ``wsource`` is less than ``boundSize`` -
|
|
|
|
## ``BoundedStreamIncompleteError`` will be raised on stream finishing. But
|
|
|
|
## comparison operator ``BoundCmp.LessOrEqual`` allows to send less bytes
|
|
|
|
## without ``BoundedStreamIncompleteError`` exception.
|
|
|
|
##
|
|
|
|
## For both comparison operators any attempt to write more bytes than
|
|
|
|
## ``boundSize`` will be interrupted with ``BoundedStreamOverflowError``
|
|
|
|
## exception.
|
|
|
|
var res = BoundedStreamWriter()
|
|
|
|
res.init(wsource, boundSize, comparison, queueSize)
|
2021-01-20 13:40:15 +00:00
|
|
|
res
|