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-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-01-24 21:40:52 +00:00
|
|
|
boundSize: int
|
|
|
|
boundary: seq[byte]
|
|
|
|
offset: int
|
2021-02-09 11:56:33 +00:00
|
|
|
cmpop: BoundCmp
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
BoundedStreamWriter* = ref object of AsyncStreamWriter
|
2021-01-24 21:40:52 +00:00
|
|
|
boundSize: int
|
|
|
|
offset: int
|
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-02-17 00:03:12 +00:00
|
|
|
proc newBoundedStreamIncompleteError*(): ref BoundedStreamError {.noinline.} =
|
2021-01-20 13:40:15 +00:00
|
|
|
newException(BoundedStreamIncompleteError,
|
|
|
|
"Stream boundary is not reached yet")
|
|
|
|
|
2021-01-24 21:40:52 +00:00
|
|
|
proc readUntilBoundary*(rstream: AsyncStreamReader, pbytes: pointer,
|
|
|
|
nbytes: int, sep: seq[byte]): Future[int] {.async.} =
|
|
|
|
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-02-09 11:56:33 +00:00
|
|
|
# r1 is `true` if `boundSize` was not set.
|
|
|
|
let r1 = rstream.boundSize < 0
|
|
|
|
# r2 is `true` if number of bytes read is less then `boundSize`.
|
|
|
|
let r2 = (rstream.boundSize > 0) and (rstream.offset < rstream.boundSize)
|
|
|
|
if r1 or r2:
|
|
|
|
let toRead =
|
|
|
|
if rstream.boundSize < 0:
|
|
|
|
len(buffer)
|
|
|
|
else:
|
|
|
|
min(rstream.boundSize - rstream.offset, len(buffer))
|
|
|
|
try:
|
|
|
|
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 + length
|
|
|
|
await upload(addr rstream.buffer, addr buffer[0], length)
|
|
|
|
rstream.state = AsyncStreamState.Finished
|
|
|
|
else:
|
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
|
|
|
rstream.offset = rstream.offset + res
|
|
|
|
await upload(addr rstream.buffer, addr buffer[0], res)
|
2021-01-24 21:40:52 +00:00
|
|
|
else:
|
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-01-24 21:40:52 +00:00
|
|
|
rstream.offset = rstream.offset + res
|
|
|
|
await upload(addr rstream.buffer, addr buffer[0], res)
|
2021-02-09 11:56:33 +00:00
|
|
|
else:
|
|
|
|
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
|
|
|
|
2021-02-09 11:56:33 +00:00
|
|
|
except AsyncStreamReadError as exc:
|
|
|
|
rstream.state = AsyncStreamState.Error
|
|
|
|
rstream.error = exc
|
|
|
|
except CancelledError:
|
|
|
|
rstream.state = AsyncStreamState.Stopped
|
|
|
|
|
|
|
|
if rstream.state != AsyncStreamState.Running:
|
|
|
|
if rstream.state == AsyncStreamState.Finished:
|
|
|
|
# This is state when BoundCmp.LessOrEqual and readExactly returned
|
|
|
|
# `AsyncStreamIncompleteError`.
|
|
|
|
await rstream.buffer.transfer()
|
2021-01-24 21:40:52 +00:00
|
|
|
break
|
2021-02-09 11:56:33 +00:00
|
|
|
else:
|
|
|
|
rstream.state = AsyncStreamState.Finished
|
|
|
|
break
|
2021-01-20 13:40:15 +00:00
|
|
|
|
2021-01-24 21:40:52 +00:00
|
|
|
# We need to notify consumer about error/close, but we do not care about
|
|
|
|
# incoming data anymore.
|
|
|
|
rstream.buffer.forget()
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
proc boundedWriteLoop(stream: AsyncStreamWriter) {.async.} =
|
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:
|
|
|
|
var
|
|
|
|
item: WriteItem
|
|
|
|
error: ref AsyncStreamError
|
|
|
|
|
|
|
|
try:
|
|
|
|
item = await wstream.queue.get()
|
|
|
|
if item.size > 0:
|
2021-01-24 21:40:52 +00:00
|
|
|
if 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-01-24 21:40:52 +00:00
|
|
|
wstream.offset = wstream.offset + item.size
|
2021-01-20 13:40:15 +00:00
|
|
|
item.future.complete()
|
|
|
|
else:
|
|
|
|
wstream.state = AsyncStreamState.Error
|
2021-02-17 00:03:12 +00:00
|
|
|
error = newException(BoundedStreamOverflowError,
|
|
|
|
"Stream boundary exceeded")
|
2021-01-20 13:40:15 +00:00
|
|
|
else:
|
|
|
|
if wstream.offset != wstream.boundSize:
|
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
|
|
|
else:
|
|
|
|
wstream.state = AsyncStreamState.Finished
|
|
|
|
item.future.complete()
|
|
|
|
except CancelledError:
|
|
|
|
wstream.state = AsyncStreamState.Stopped
|
|
|
|
error = newAsyncStreamUseClosedError()
|
|
|
|
except AsyncStreamWriteError as exc:
|
|
|
|
wstream.state = AsyncStreamState.Error
|
|
|
|
error = exc
|
|
|
|
except AsyncStreamIncompleteError as exc:
|
|
|
|
wstream.state = AsyncStreamState.Error
|
|
|
|
error = exc
|
|
|
|
|
|
|
|
if wstream.state != AsyncStreamState.Running:
|
|
|
|
if wstream.state == AsyncStreamState.Finished:
|
|
|
|
error = newAsyncStreamUseClosedError()
|
|
|
|
else:
|
|
|
|
if not(isNil(item.future)):
|
|
|
|
if not(item.future.finished()):
|
|
|
|
item.future.fail(error)
|
|
|
|
while not(wstream.queue.empty()):
|
|
|
|
let pitem = wstream.queue.popFirstNoWait()
|
|
|
|
if not(pitem.future.finished()):
|
|
|
|
pitem.future.fail(error)
|
|
|
|
break
|
|
|
|
|
|
|
|
proc bytesLeft*(stream: BoundedStreamRW): uint64 =
|
|
|
|
## Returns number of bytes left in stream.
|
2021-02-10 08:43:05 +00:00
|
|
|
uint64(stream.boundSize) - stream.bytesCount
|
2021-01-20 13:40:15 +00:00
|
|
|
|
|
|
|
proc init*[T](child: BoundedStreamReader, rsource: AsyncStreamReader,
|
|
|
|
bufferSize = BoundedBufferSize, udata: ref T) =
|
2021-02-17 00:03:12 +00:00
|
|
|
init(AsyncStreamReader(child), rsource, boundedReadLoop, bufferSize,
|
2021-01-20 13:40:15 +00:00
|
|
|
udata)
|
|
|
|
|
|
|
|
proc init*(child: BoundedStreamReader, rsource: AsyncStreamReader,
|
|
|
|
bufferSize = BoundedBufferSize) =
|
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-01-24 21:40:52 +00:00
|
|
|
boundSize: int,
|
|
|
|
boundary: openarray[byte] = [],
|
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-02-09 11:56:33 +00:00
|
|
|
doAssert(not(boundSize <= 0 and (len(boundary) == 0)),
|
2021-01-24 21:40:52 +00:00
|
|
|
"At least one type of boundary should be set")
|
2021-02-09 11:56:33 +00:00
|
|
|
var res = BoundedStreamReader(boundSize: boundSize, boundary: @boundary,
|
|
|
|
cmpop: comparison)
|
2021-01-20 13:40:15 +00:00
|
|
|
res.init(rsource, bufferSize, udata)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc newBoundedStreamReader*(rsource: AsyncStreamReader,
|
2021-01-24 21:40:52 +00:00
|
|
|
boundSize: int,
|
|
|
|
boundary: openarray[byte] = [],
|
2021-02-09 11:56:33 +00:00
|
|
|
comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
bufferSize = BoundedBufferSize,
|
|
|
|
): BoundedStreamReader =
|
2021-02-09 11:56:33 +00:00
|
|
|
doAssert(not(boundSize <= 0 and (len(boundary) == 0)),
|
2021-01-24 21:40:52 +00:00
|
|
|
"At least one type of boundary should be set")
|
2021-02-09 11:56:33 +00:00
|
|
|
var res = BoundedStreamReader(boundSize: boundSize, boundary: @boundary,
|
|
|
|
cmpop: comparison)
|
2021-01-20 13:40:15 +00:00
|
|
|
res.init(rsource, bufferSize)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc init*[T](child: BoundedStreamWriter, wsource: AsyncStreamWriter,
|
|
|
|
queueSize = AsyncStreamDefaultQueueSize, udata: ref T) =
|
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,
|
|
|
|
queueSize = AsyncStreamDefaultQueueSize) =
|
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-01-24 21:40:52 +00:00
|
|
|
boundSize: int,
|
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-02-09 11:56:33 +00:00
|
|
|
doAssert(boundSize > 0, "Bound size must be bigger then zero")
|
|
|
|
var res = BoundedStreamWriter(boundSize: boundSize, cmpop: comparison)
|
2021-01-20 13:40:15 +00:00
|
|
|
res.init(wsource, queueSize, udata)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc newBoundedStreamWriter*(wsource: AsyncStreamWriter,
|
2021-01-24 21:40:52 +00:00
|
|
|
boundSize: int,
|
2021-02-09 11:56:33 +00:00
|
|
|
comparison = BoundCmp.Equal,
|
2021-01-20 13:40:15 +00:00
|
|
|
queueSize = AsyncStreamDefaultQueueSize,
|
|
|
|
): BoundedStreamWriter =
|
2021-02-09 11:56:33 +00:00
|
|
|
doAssert(boundSize > 0, "Bound size must be bigger then zero")
|
|
|
|
var res = BoundedStreamWriter(boundSize: boundSize, cmpop: comparison)
|
2021-01-20 13:40:15 +00:00
|
|
|
res.init(wsource, queueSize)
|
|
|
|
res
|