mirror of
https://github.com/status-im/nim-libp2p.git
synced 2025-02-28 04:20:39 +00:00
implement streaming length prefixed parser
This commit is contained in:
parent
a644a19a2d
commit
b92f8368c1
@ -72,7 +72,6 @@ proc read(lp: LenPrefixed,
|
|||||||
lp.mode = Mode.Reading
|
lp.mode = Mode.Reading
|
||||||
else:
|
else:
|
||||||
result = lp.readBuff.read(lp.size)
|
result = lp.readBuff.read(lp.size)
|
||||||
echo result
|
|
||||||
lp.size -= result.len
|
lp.size -= result.len
|
||||||
if lp.size == 0:
|
if lp.size == 0:
|
||||||
lp.mode = Mode.Decoding
|
lp.mode = Mode.Decoding
|
||||||
|
@ -110,7 +110,7 @@ proc read*[T](b: var RingBuffer[T], size: int = -1): seq[T] =
|
|||||||
## assert(buff.read() == @[...])
|
## assert(buff.read() == @[...])
|
||||||
##
|
##
|
||||||
var isize = size
|
var isize = size
|
||||||
if size < 0:
|
if size < 0 or size > b.len:
|
||||||
isize = b.len
|
isize = b.len
|
||||||
|
|
||||||
result = newSeq[T](isize)
|
result = newSeq[T](isize)
|
||||||
@ -131,7 +131,9 @@ proc clear*[T](b: var RingBuffer[T]) =
|
|||||||
b.buff.setLen(0)
|
b.buff.setLen(0)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
block Basic:
|
|
||||||
|
block:
|
||||||
|
## Basic tests
|
||||||
var buff = RingBuffer[char].init(10)
|
var buff = RingBuffer[char].init(10)
|
||||||
var data = newSeq[char](10)
|
var data = newSeq[char](10)
|
||||||
|
|
||||||
@ -194,7 +196,23 @@ when isMainModule:
|
|||||||
assert(buff.head == 4, "head should be 9")
|
assert(buff.head == 4, "head should be 9")
|
||||||
assert(buff.tail == 4, "tail should be 9")
|
assert(buff.tail == 4, "tail should be 9")
|
||||||
|
|
||||||
block Errors:
|
block:
|
||||||
|
## Complex reading scenarios
|
||||||
|
var buff = RingBuffer[char].init(10)
|
||||||
|
var data = newSeq[char](10)
|
||||||
|
|
||||||
|
buff.append(@['a'])
|
||||||
|
assert(buff.len == 1, "len should be 1")
|
||||||
|
assert(buff.head == 0, "head should be 0")
|
||||||
|
assert(buff.tail == 1, "tail should be 1")
|
||||||
|
|
||||||
|
assert(buff.read(5) == @['a'])
|
||||||
|
assert(buff.len == 0, "len should be 0")
|
||||||
|
assert(buff.head == 1, "head should be 9")
|
||||||
|
assert(buff.tail == 1, "tail should be 9")
|
||||||
|
|
||||||
|
block:
|
||||||
|
## Error checking
|
||||||
var buff = RingBuffer[char].init(5)
|
var buff = RingBuffer[char].init(5)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -219,7 +237,8 @@ when isMainModule:
|
|||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
assert(true)
|
assert(true)
|
||||||
|
|
||||||
block Cleanup:
|
block:
|
||||||
|
## Cleanup
|
||||||
var buff = RingBuffer[char].init(5)
|
var buff = RingBuffer[char].init(5)
|
||||||
|
|
||||||
buff.reset()
|
buff.reset()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user