mirror of https://github.com/vacp2p/nim-libp2p.git
use sane defaults
This commit is contained in:
parent
e5b782f094
commit
c7fd08e002
|
@ -34,6 +34,8 @@ import deques, tables, sequtils, math
|
||||||
import chronos
|
import chronos
|
||||||
import ../stream/lpstream
|
import ../stream/lpstream
|
||||||
|
|
||||||
|
const DefaultBufferSize* = 1024
|
||||||
|
|
||||||
type
|
type
|
||||||
# TODO: figure out how to make this generic to avoid casts
|
# TODO: figure out how to make this generic to avoid casts
|
||||||
WriteHandler* = proc (data: seq[byte]): Future[void] {.gcsafe.}
|
WriteHandler* = proc (data: seq[byte]): Future[void] {.gcsafe.}
|
||||||
|
@ -51,14 +53,14 @@ proc requestReadBytes(s: BufferStream): Future[void] =
|
||||||
result = newFuture[void]()
|
result = newFuture[void]()
|
||||||
s.readReqs.addLast(result)
|
s.readReqs.addLast(result)
|
||||||
|
|
||||||
proc initBufferStream*(s: BufferStream, handler: WriteHandler, size: int = 1024) =
|
proc initBufferStream*(s: BufferStream, handler: WriteHandler, size: int = DefaultBufferSize) =
|
||||||
s.maxSize = if isPowerOfTwo(size): size else: nextPowerOfTwo(size)
|
s.maxSize = if isPowerOfTwo(size): size else: nextPowerOfTwo(size)
|
||||||
s.readBuf = initDeque[byte](s.maxSize)
|
s.readBuf = initDeque[byte](s.maxSize)
|
||||||
s.readReqs = initDeque[Future[void]]()
|
s.readReqs = initDeque[Future[void]]()
|
||||||
s.dataReadEvent = newAsyncEvent()
|
s.dataReadEvent = newAsyncEvent()
|
||||||
s.writeHandler = handler
|
s.writeHandler = handler
|
||||||
|
|
||||||
proc newBufferStream*(handler: WriteHandler, size: int = 1024): BufferStream =
|
proc newBufferStream*(handler: WriteHandler, size: int = DefaultBufferSize): BufferStream =
|
||||||
new result
|
new result
|
||||||
result.initBufferStream(handler, size)
|
result.initBufferStream(handler, size)
|
||||||
|
|
||||||
|
@ -124,7 +126,6 @@ method readExactly*(s: BufferStream,
|
||||||
## If EOF is received and ``nbytes`` is not yet read, the procedure
|
## If EOF is received and ``nbytes`` is not yet read, the procedure
|
||||||
## will raise ``LPStreamIncompleteError``.
|
## will raise ``LPStreamIncompleteError``.
|
||||||
let buff = await s.read(nbytes)
|
let buff = await s.read(nbytes)
|
||||||
|
|
||||||
if nbytes > buff.len():
|
if nbytes > buff.len():
|
||||||
raise newLPStreamIncompleteError()
|
raise newLPStreamIncompleteError()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue