add init proc to bufferedstream
This commit is contained in:
parent
f4a9b9cba9
commit
8549ce76bb
|
@ -50,13 +50,16 @@ proc requestReadBytes(s: BufferStream): Future[int] =
|
||||||
result = newFuture[int]()
|
result = newFuture[int]()
|
||||||
s.readReqs.addLast(result)
|
s.readReqs.addLast(result)
|
||||||
|
|
||||||
|
proc initBufferStream*(s: BufferStream, handler: WriteHandler, size: int = 1024) =
|
||||||
|
s.maxSize = if isPowerOfTwo(size): size else: nextPowerOfTwo(size)
|
||||||
|
s.readBuf = initDeque[byte](s.maxSize)
|
||||||
|
s.readReqs = initDeque[Future[int]]()
|
||||||
|
s.dataReadEvent = newAsyncEvent()
|
||||||
|
s.writeHandler = handler
|
||||||
|
|
||||||
proc newBufferStream*(handler: WriteHandler, size: int = 1024): BufferStream =
|
proc newBufferStream*(handler: WriteHandler, size: int = 1024): BufferStream =
|
||||||
new result
|
new result
|
||||||
result.maxSize = if isPowerOfTwo(size): size else: nextPowerOfTwo(size)
|
result.initBufferStream(handler, size)
|
||||||
result.readBuf = initDeque[byte](result.maxSize)
|
|
||||||
result.readReqs = initDeque[Future[int]]()
|
|
||||||
result.dataReadEvent = newAsyncEvent()
|
|
||||||
result.writeHandler = handler
|
|
||||||
|
|
||||||
proc popFirst*(s: BufferStream): byte =
|
proc popFirst*(s: BufferStream): byte =
|
||||||
result = s.readBuf.popFirst()
|
result = s.readBuf.popFirst()
|
||||||
|
@ -117,12 +120,13 @@ method readExactly*(s: BufferStream,
|
||||||
## Read exactly ``nbytes`` bytes from read-only stream ``rstream`` and store
|
## Read exactly ``nbytes`` bytes from read-only stream ``rstream`` and store
|
||||||
## it to ``pbytes``.
|
## it to ``pbytes``.
|
||||||
##
|
##
|
||||||
## If EOF is received and ``nbytes`` is not yet readed, the procedure
|
## If EOF is received and ``nbytes`` is not yet read, the procedure
|
||||||
## will raise ``LPStreamIncompleteError``.
|
## will raise ``LPStreamIncompleteError``.
|
||||||
if nbytes > s.readBuf.len():
|
let buff = await s.read(nbytes)
|
||||||
|
|
||||||
|
if nbytes > buff.len():
|
||||||
raise newLPStreamIncompleteError()
|
raise newLPStreamIncompleteError()
|
||||||
|
|
||||||
let buff = await s.read(nbytes)
|
|
||||||
copyMem(pbytes, unsafeAddr buff[0], nbytes)
|
copyMem(pbytes, unsafeAddr buff[0], nbytes)
|
||||||
|
|
||||||
method readLine*(s: BufferStream,
|
method readLine*(s: BufferStream,
|
||||||
|
|
Loading…
Reference in New Issue