nim-libp2p-experimental/libp2p/muxers/mplex/coder.nim

95 lines
2.5 KiB
Nim
Raw Normal View History

2022-07-01 18:19:57 +00:00
# Nim-LibP2P
# Copyright (c) 2022 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
2019-09-04 03:08:51 +00:00
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
2021-05-24 17:55:33 +00:00
import pkg/[chronos, nimcrypto/utils, chronicles, stew/byteutils]
import ../../stream/connection,
2020-05-08 20:58:23 +00:00
../../utility,
2019-09-12 17:07:34 +00:00
../../varint,
../../vbuffer,
../muxer
2019-09-04 03:08:51 +00:00
2019-09-10 02:15:52 +00:00
logScope:
2020-12-01 17:34:27 +00:00
topics = "libp2p mplexcoder"
2019-09-10 02:15:52 +00:00
2019-09-06 21:27:55 +00:00
type
MessageType* {.pure.} = enum
New,
MsgIn,
MsgOut,
CloseIn,
CloseOut,
ResetIn,
ResetOut
Msg* = tuple
2020-03-23 17:14:06 +00:00
id: uint64
2019-09-07 23:34:11 +00:00
msgType: MessageType
data: seq[byte]
2019-09-06 21:27:55 +00:00
InvalidMplexMsgType* = object of MuxerError
# https://github.com/libp2p/specs/tree/master/mplex#writing-to-a-stream
const MaxMsgSize* = 1 shl 20 # 1mb
2021-05-24 17:55:33 +00:00
proc newInvalidMplexMsgType*(): ref InvalidMplexMsgType =
newException(InvalidMplexMsgType, "invalid message type")
2020-02-12 14:37:22 +00:00
proc readMsg*(conn: Connection): Future[Msg] {.async, gcsafe.} =
2020-05-08 20:58:23 +00:00
let header = await conn.readVarint()
trace "read header varint", varint = header, conn
2019-09-07 23:34:11 +00:00
2020-05-08 20:58:23 +00:00
let data = await conn.readLp(MaxMsgSize)
trace "read data", dataLen = data.len, data = shortLog(data), conn
2019-09-07 23:34:11 +00:00
let msgType = header and 0x7
if msgType.int > ord(MessageType.ResetOut):
raise newInvalidMplexMsgType()
return (header shr 3, MessageType(msgType), data)
proc writeMsg*(conn: Connection,
2020-03-23 17:14:06 +00:00
id: uint64,
msgType: MessageType,
data: seq[byte] = @[]): Future[void] =
2020-05-23 16:50:05 +00:00
var
left = data.len
offset = 0
buf = initVBuffer()
# Split message into length-prefixed chunks
2020-05-23 16:50:05 +00:00
while left > 0 or data.len == 0:
let
chunkSize = if left > MaxMsgSize: MaxMsgSize - 64 else: left
2020-05-23 16:50:05 +00:00
buf.writePBVarint(id shl 3 or ord(msgType).uint64)
buf.writeSeq(data.toOpenArray(offset, offset + chunkSize - 1))
2020-05-23 16:50:05 +00:00
left = left - chunkSize
offset = offset + chunkSize
2020-05-23 16:50:05 +00:00
if data.len == 0:
break
trace "writing mplex message",
conn, id, msgType, data = data.len, encoded = buf.buffer.len
# Write all chunks in a single write to avoid async races where a close
# message gets written before some of the chunks
conn.write(buf.buffer)
2019-09-07 23:34:11 +00:00
proc writeMsg*(conn: Connection,
2020-03-23 17:14:06 +00:00
id: uint64,
msgType: MessageType,
data: string): Future[void] =
conn.writeMsg(id, msgType, data.toBytes())