mirror of https://github.com/vacp2p/nim-libp2p.git
use `result` instead of buffer
This commit is contained in:
parent
f5508be544
commit
15bae7bec5
|
@ -84,24 +84,21 @@ 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
|
||||||
var buf = initVBuffer()
|
var buf = initVBuffer()
|
||||||
|
|
Loading…
Reference in New Issue