2022-07-01 20:19:57 +02:00
|
|
|
# Nim-LibP2P
|
2024-03-05 08:06:27 +01:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2022-07-01 20:19:57 +02:00
|
|
|
# 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-03 21:08:51 -06:00
|
|
|
|
2023-06-07 13:12:49 +02:00
|
|
|
{.push raises: [].}
|
2021-05-21 10:27:01 -06:00
|
|
|
|
2023-05-18 10:24:17 +02:00
|
|
|
import pkg/[chronos, chronicles, stew/byteutils]
|
2020-09-14 10:19:54 +02:00
|
|
|
import ../../stream/connection, ../../utility, ../../varint, ../../vbuffer, ../muxer
|
2019-09-03 21:08:51 -06:00
|
|
|
|
2019-09-09 20:15:52 -06:00
|
|
|
logScope:
|
2020-12-01 11:34:27 -06:00
|
|
|
topics = "libp2p mplexcoder"
|
2019-09-09 20:15:52 -06:00
|
|
|
|
2019-09-06 15:27:55 -06:00
|
|
|
type
|
2020-09-14 10:19:54 +02:00
|
|
|
MessageType* {.pure.} = enum
|
|
|
|
New
|
|
|
|
MsgIn
|
|
|
|
MsgOut
|
|
|
|
CloseIn
|
|
|
|
CloseOut
|
|
|
|
ResetIn
|
|
|
|
ResetOut
|
|
|
|
|
2019-09-07 17:34:11 -06:00
|
|
|
Msg* = tuple[id: uint64, msgType: MessageType, data: seq[byte]]
|
2019-09-06 15:27:55 -06:00
|
|
|
|
2021-05-21 10:27:01 -06:00
|
|
|
InvalidMplexMsgType* = object of MuxerError
|
2020-03-29 08:28:48 -06:00
|
|
|
|
2020-09-14 10:19:54 +02:00
|
|
|
# https://github.com/libp2p/specs/tree/master/mplex#writing-to-a-stream
|
|
|
|
const MaxMsgSize* = 1 shl 20 # 1mb
|
|
|
|
|
2021-05-24 11:55:33 -06:00
|
|
|
proc newInvalidMplexMsgType*(): ref InvalidMplexMsgType =
|
2020-03-29 08:28:48 -06:00
|
|
|
newException(InvalidMplexMsgType, "invalid message type")
|
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
proc readMsg*(
|
|
|
|
conn: Connection
|
|
|
|
): Future[Msg] {.async: (raises: [CancelledError, LPStreamError, MuxerError]).} =
|
2020-05-08 22:58:23 +02:00
|
|
|
let header = await conn.readVarint()
|
2020-09-06 10:31:47 +02:00
|
|
|
trace "read header varint", varint = header, conn
|
2019-09-07 17:34:11 -06:00
|
|
|
|
2020-05-08 22:58:23 +02:00
|
|
|
let data = await conn.readLp(MaxMsgSize)
|
2020-09-06 10:31:47 +02:00
|
|
|
trace "read data", dataLen = data.len, data = shortLog(data), conn
|
2019-09-07 17:34:11 -06:00
|
|
|
|
2020-03-29 08:28:48 -06:00
|
|
|
let msgType = header and 0x7
|
|
|
|
if msgType.int > ord(MessageType.ResetOut):
|
|
|
|
raise newInvalidMplexMsgType()
|
|
|
|
|
2020-09-21 19:48:19 +02:00
|
|
|
return (header shr 3, MessageType(msgType), data)
|
2019-09-07 00:20:03 -06:00
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
proc writeMsg*(
|
|
|
|
conn: Connection, id: uint64, msgType: MessageType, data: seq[byte] = @[]
|
|
|
|
): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} =
|
2020-05-23 10:50:05 -06:00
|
|
|
var
|
|
|
|
left = data.len
|
|
|
|
offset = 0
|
2020-09-21 19:48:19 +02:00
|
|
|
buf = initVBuffer()
|
|
|
|
|
|
|
|
# Split message into length-prefixed chunks
|
2020-05-23 10:50:05 -06:00
|
|
|
while left > 0 or data.len == 0:
|
|
|
|
let chunkSize =
|
|
|
|
if left > MaxMsgSize:
|
|
|
|
MaxMsgSize - 64
|
|
|
|
else:
|
|
|
|
left
|
2020-09-21 19:48:19 +02:00
|
|
|
|
2020-05-23 10:50:05 -06:00
|
|
|
buf.writePBVarint(id shl 3 or ord(msgType).uint64)
|
2020-08-15 07:58:30 +02:00
|
|
|
buf.writeSeq(data.toOpenArray(offset, offset + chunkSize - 1))
|
2020-05-23 10:50:05 -06:00
|
|
|
left = left - chunkSize
|
|
|
|
offset = offset + chunkSize
|
2020-05-15 05:56:56 +02:00
|
|
|
|
2020-05-23 10:50:05 -06:00
|
|
|
if data.len == 0:
|
2020-09-21 19:48:19 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
trace "writing mplex message",
|
|
|
|
conn, id, msgType, data = data.len, encoded = buf.buffer.len
|
|
|
|
|
2020-12-09 15:56:40 +01:00
|
|
|
# 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 17:34:11 -06:00
|
|
|
|
2024-03-05 08:06:27 +01:00
|
|
|
proc writeMsg*(
|
|
|
|
conn: Connection, id: uint64, msgType: MessageType, data: string
|
|
|
|
): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} =
|
2020-08-06 10:30:57 +09:00
|
|
|
conn.writeMsg(id, msgType, data.toBytes())
|