nim-libp2p/libp2p/muxers/muxer.nim

64 lines
1.8 KiB
Nim
Raw Normal View History

2022-07-01 18:19:57 +00:00
# Nim-LibP2P
2023-01-20 14:47:40 +00:00
# Copyright (c) 2023 Status Research & Development GmbH
2022-07-01 18:19:57 +00: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 20:40:51 +00:00
2023-06-07 11:12:49 +00:00
{.push raises: [].}
import chronos, chronicles
import ../stream/connection,
../errors
2019-09-03 20:40:51 +00:00
logScope:
2020-12-01 17:34:27 +00:00
topics = "libp2p muxer"
const
DefaultChanTimeout* = 5.minutes
2019-09-03 20:40:51 +00:00
type
MuxerError* = object of LPError
TooManyChannels* = object of MuxerError
2023-06-07 11:12:49 +00:00
StreamHandler* = proc(conn: Connection): Future[void] {.gcsafe, raises: [].}
MuxerHandler* = proc(muxer: Muxer): Future[void] {.gcsafe, raises: [].}
2019-09-03 20:40:51 +00:00
Muxer* = ref object of RootObj
streamHandler*: StreamHandler
2023-03-08 11:30:19 +00:00
handler*: Future[void]
2019-09-03 20:40:51 +00:00
connection*: Connection
2019-11-06 18:09:48 +00:00
# user provider proc that returns a constructed Muxer
2023-06-07 11:12:49 +00:00
MuxerConstructor* = proc(conn: Connection): Muxer {.gcsafe, closure, raises: [].}
2019-11-06 18:09:48 +00:00
2019-09-03 20:40:51 +00:00
# this wraps a creator proc that knows how to make muxers
2023-03-08 11:30:19 +00:00
MuxerProvider* = object
2019-11-06 18:09:48 +00:00
newMuxer*: MuxerConstructor
2023-03-08 11:30:19 +00:00
codec*: string
2019-09-06 06:51:19 +00:00
2023-03-08 11:30:19 +00:00
func shortLog*(m: Muxer): auto =
if isNil(m): "nil"
else: shortLog(m.connection)
chronicles.formatIt(Muxer): shortLog(it)
2019-11-06 18:09:48 +00:00
# muxer interface
method newStream*(m: Muxer, name: string = "", lazy: bool = false):
Future[Connection] {.base, async, gcsafe.} = discard
2023-03-08 11:30:19 +00:00
method close*(m: Muxer) {.base, async, gcsafe.} =
if not isNil(m.connection):
await m.connection.close()
2019-09-06 06:51:19 +00:00
method handle*(m: Muxer): Future[void] {.base, async, gcsafe.} = discard
2019-09-03 20:40:51 +00:00
proc new*(
T: typedesc[MuxerProvider],
creator: MuxerConstructor,
codec: string): T {.gcsafe.} =
2023-03-08 11:30:19 +00:00
let muxerProvider = T(newMuxer: creator, codec: codec)
muxerProvider
method getStreams*(m: Muxer): seq[Connection] {.base.} = doAssert false, "not implemented"