From 4edd2c9f8aa16ff5a2b67fc42b7052841ac007b9 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 2 Sep 2019 14:46:51 -0600 Subject: [PATCH] mplex move everything to one file --- libp2p/mplex.nim | 145 +++++++++++++++++++++++++++++++++++++++ libp2p/mplex/channel.nim | 58 ---------------- libp2p/mplex/mplex.nim | 31 --------- libp2p/mplex/types.nim | 19 ----- 4 files changed, 145 insertions(+), 108 deletions(-) create mode 100644 libp2p/mplex.nim delete mode 100644 libp2p/mplex/channel.nim delete mode 100644 libp2p/mplex/mplex.nim delete mode 100644 libp2p/mplex/types.nim diff --git a/libp2p/mplex.nim b/libp2p/mplex.nim new file mode 100644 index 000000000..1ecb9cd21 --- /dev/null +++ b/libp2p/mplex.nim @@ -0,0 +1,145 @@ +## Nim-LibP2P +## Copyright (c) 2018 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. + +import chronos +import ../varint, ../connection, + ../vbuffer, ../protocol + +const DefaultReadSize: uint = 1024 + +const MaxMsgSize* = 1 shl 20 # 1mb +const MaxChannels* = 1000 + +type + MplexUnknownMsgError* = object of CatchableError + + MessageType* {.pure.} = enum + New, + MsgIn, + MsgOut, + CloseIn, + CloseOut, + ResetIn, + ResetOut + + ChannelHandler* = proc(conn: Connection) {.gcsafe.} + + Mplex* = ref object of LPProtocol + remote*: seq[Connection] + local*: seq[Connection] + channelHandler*: ChannelHandler + currentId*: uint + + Channel* = ref object of BufferStream + id*: int + initiator*: bool + reset*: bool + closedLocal*: bool + closedRemote*: bool + mplex*: Mplex + +proc newMplexUnknownMsgError*(): ref MplexUnknownMsgError = + result = newException(MplexUnknownMsgError, "Unknown mplex message type") + +proc readLp*(conn: Connection): Future[tuple[id: uint, msgType: MessageType]] {.gcsafe.} = + var + header: uint + length: int + res: VarintStatus + var buffer = newSeq[byte](10) + try: + for i in 0..