From 07695b2260897b2d565530179aa81090dfe0b0dd Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 11 Mar 2020 07:34:35 -0600 Subject: [PATCH] use `readExactly` not `read` to read all bytes --- libp2p/muxers/mplex/coder.nim | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libp2p/muxers/mplex/coder.nim b/libp2p/muxers/mplex/coder.nim index 2be46bd..83c079b 100644 --- a/libp2p/muxers/mplex/coder.nim +++ b/libp2p/muxers/mplex/coder.nim @@ -54,10 +54,11 @@ proc readMsg*(conn: Connection): Future[Msg] {.async, gcsafe.} = let dataLenVarint = await conn.readMplexVarint() trace "read data len varint", varint = dataLenVarint - var data: seq[byte] + + var data: seq[byte] = newSeq[byte](dataLenVarint.int) if dataLenVarint.int > 0: - data = await conn.read(dataLenVarint.int) - trace "read data", data = data + await conn.readExactly(addr data[0], dataLenVarint.int) + trace "read data", len = data.len let header = headerVarint result = (header shr 3, MessageType(header and 0x7), data) @@ -66,6 +67,9 @@ proc writeMsg*(conn: Connection, id: uint, msgType: MessageType, data: seq[byte] = @[]) {.async, gcsafe.} = + trace "seding data over mplex", id, + msgType, + data ## write lenght prefixed var buf = initVBuffer() buf.writePBVarint(id shl 3 or ord(msgType).uint)