wip: modeling mplex

This commit is contained in:
Dmitriy Ryajov 2019-09-01 15:52:51 -06:00
parent 7f0627378c
commit 7bd1e04d6f
3 changed files with 108 additions and 0 deletions

58
libp2p/mplex/channel.nim Normal file
View File

@ -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

31
libp2p/mplex/mplex.nim Normal file
View File

@ -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

19
libp2p/mplex/types.nim Normal file
View File

@ -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]