66 lines
1.8 KiB
Nim
66 lines
1.8 KiB
Nim
# Nim-LibP2P
|
|
# Copyright (c) 2023 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.
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
{.push raises: [Defect].}
|
|
else:
|
|
{.push raises: [].}
|
|
|
|
import chronos, chronicles
|
|
import ../protocols/protocol,
|
|
../stream/connection,
|
|
../errors
|
|
|
|
logScope:
|
|
topics = "libp2p muxer"
|
|
|
|
const
|
|
DefaultChanTimeout* = 5.minutes
|
|
|
|
type
|
|
MuxerError* = object of LPError
|
|
TooManyChannels* = object of MuxerError
|
|
|
|
StreamHandler* = proc(conn: Connection): Future[void] {.gcsafe, raises: [Defect].}
|
|
MuxerHandler* = proc(muxer: Muxer): Future[void] {.gcsafe, raises: [Defect].}
|
|
|
|
Muxer* = ref object of RootObj
|
|
streamHandler*: StreamHandler
|
|
handler*: Future[void]
|
|
connection*: Connection
|
|
|
|
# user provider proc that returns a constructed Muxer
|
|
MuxerConstructor* = proc(conn: Connection): Muxer {.gcsafe, closure, raises: [Defect].}
|
|
|
|
# this wraps a creator proc that knows how to make muxers
|
|
MuxerProvider* = object
|
|
newMuxer*: MuxerConstructor
|
|
codec*: string
|
|
|
|
func shortLog*(m: Muxer): auto =
|
|
if isNil(m): "nil"
|
|
else: shortLog(m.connection)
|
|
chronicles.formatIt(Muxer): shortLog(it)
|
|
|
|
# muxer interface
|
|
method newStream*(m: Muxer, name: string = "", lazy: bool = false):
|
|
Future[Connection] {.base, async, gcsafe.} = discard
|
|
method close*(m: Muxer) {.base, async, gcsafe.} =
|
|
if not isNil(m.connection):
|
|
await m.connection.close()
|
|
method handle*(m: Muxer): Future[void] {.base, async, gcsafe.} = discard
|
|
|
|
proc new*(
|
|
T: typedesc[MuxerProvider],
|
|
creator: MuxerConstructor,
|
|
codec: string): T {.gcsafe.} =
|
|
|
|
let muxerProvider = T(newMuxer: creator, codec: codec)
|
|
muxerProvider
|