From 7bd1e04d6f5a51fcaf791f29b2145ae297e443c0 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 1 Sep 2019 15:52:51 -0600 Subject: [PATCH] wip: modeling mplex --- libp2p/mplex/channel.nim | 58 ++++++++++++++++++++++++++++++++++++++++ libp2p/mplex/mplex.nim | 31 +++++++++++++++++++++ libp2p/mplex/types.nim | 19 +++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 libp2p/mplex/channel.nim create mode 100644 libp2p/mplex/mplex.nim create mode 100644 libp2p/mplex/types.nim diff --git a/libp2p/mplex/channel.nim b/libp2p/mplex/channel.nim new file mode 100644 index 0000000..96fa77a --- /dev/null +++ b/libp2p/mplex/channel.nim @@ -0,0 +1,58 @@ +## 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 ../connection, ../stream +import mplex + +type + Channel = ref object of LPStream + id*: int + initiator*: bool + reset*: bool + closedLocal*: bool + closedRemote*: bool + buffer*: seq[byte] + +proc newChannel*(mplex: Mplex, id: int, initiator: bool): Channel = + new result + result.id = id + result.initiator = initiator + +proc closed*(s: Channel): bool = s.closedLocal and s.closedRemote +proc close*(s: Channel) = discard + +method read*(s: Channel, n = -1): Future[seq[byte]] {.async, gcsafe.} = + discard + +method readExactly*(s: Channel, pbytes: pointer, nbytes: int): Future[void] {.async, gcsafe.} = + discard + +method readLine*(s: Channel, limit = 0, sep = "\r\n"): Future[string] {.async, gcsafe.} = + discard + +method readOnce*(s: Channel, pbytes: pointer, nbytes: int): Future[int] {.async, gcsafe.} = + discard + +method readUntil*(s: Channel, + pbytes: pointer, nbytes: int, + sep: seq[byte]): Future[int] {.async, gcsafe.} = + discard + +method write*(s: Channel, pbytes: pointer, nbytes: int) {.async, gcsafe.} = + discard + +method write*(s: Channel, msg: string, msglen = -1) {.async, gcsafe.} = + discard + +method write*(s: Channel, msg: seq[byte], msglen = -1) {.async, gcsafe.} = + discard + +method close*(s: Channel) {.async, gcsafe.} = + discard diff --git a/libp2p/mplex/mplex.nim b/libp2p/mplex/mplex.nim new file mode 100644 index 0000000..6e5c068 --- /dev/null +++ b/libp2p/mplex/mplex.nim @@ -0,0 +1,31 @@ +## 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 tables +import chronos +import ../connection, ../stream +import types +import channel + +type + ChannelHandler* = proc(conn: Connection) {.gcsafe.} + + Mplex* = ref object of RootObj + connections*: seq[Connection] + channels*: TableRef[uint, Connection] + handler*: ChannelHandler + +proc newMplex*(handler: ChannelHandler): Mplex = + new result + result.channels = newTable[uint, Connection]() + result.handler = handler + +proc newStream*(m: Mplex, conn: Connection): Connection {.gcsafe.} = discard +proc handle*(m: Mplex, conn: Connection) {.gcsafe.} = discard +proc send*(m: Mplex, msg: Message) {.async, gcsafe.} = discard diff --git a/libp2p/mplex/types.nim b/libp2p/mplex/types.nim new file mode 100644 index 0000000..7cec08f --- /dev/null +++ b/libp2p/mplex/types.nim @@ -0,0 +1,19 @@ +## 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. + +const MaxMsgSize* = 1 shl 20 # 1mb + +type + MessageType* = enum + New, InMsg, OutMsg, InClose, OutClose, InReset, OutReset + + Message* = ref object of RootObj + id*: int + msgaType*: MessageType + data*: seq[byte]