use `result` instead of buffer

This commit is contained in:
Dmitriy Ryajov 2019-09-25 14:13:22 -06:00
parent f5508be544
commit 15bae7bec5
1 changed files with 6 additions and 9 deletions

View File

@ -84,23 +84,20 @@ proc readLp*(s: Connection): Future[seq[byte]] {.async, gcsafe.} =
size: uint size: uint
length: int length: int
res: VarintStatus res: VarintStatus
var buffer = newSeq[byte](10) result = newSeq[byte](10)
try: try:
for i in 0..<len(buffer): for i in 0..<len(result):
await s.readExactly(addr buffer[i], 1) await s.readExactly(addr result[i], 1)
res = LP.getUVarint(buffer.toOpenArray(0, i), length, size) res = LP.getUVarint(result.toOpenArray(0, i), length, size)
if res == VarintStatus.Success: if res == VarintStatus.Success:
break break
if res != VarintStatus.Success or size > DefaultReadSize: if res != VarintStatus.Success or size > DefaultReadSize:
result = buffer
return return
buffer.setLen(size) result.setLen(size)
if size > 0.uint: if size > 0.uint:
await s.readExactly(addr buffer[0], int(size)) await s.readExactly(addr result[0], int(size))
except LPStreamIncompleteError, LPStreamReadError: except LPStreamIncompleteError, LPStreamReadError:
error "readLp: could not read from remote", exception = getCurrentExceptionMsg() error "readLp: could not read from remote", exception = getCurrentExceptionMsg()
result = buffer
proc writeLp*(s: Connection, msg: string | seq[byte]): Future[void] {.gcsafe.} = proc writeLp*(s: Connection, msg: string | seq[byte]): Future[void] {.gcsafe.} =
## write lenght prefixed ## write lenght prefixed