2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2024-03-05 07:06:27 +00:00
|
|
|
# Copyright (c) 2023-2024 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: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2019-09-11 21:06:42 +00:00
|
|
|
import chronos, chronicles
|
2024-06-11 15:18:06 +00:00
|
|
|
import ../stream/connection, ../errors
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2019-09-11 21:06:42 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p muxer"
|
2019-09-11 21:06:42 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
const DefaultChanTimeout* = 5.minutes
|
2020-07-17 18:44:41 +00:00
|
|
|
|
2019-09-03 20:40:51 +00:00
|
|
|
type
|
2021-05-21 16:27:01 +00:00
|
|
|
MuxerError* = object of LPError
|
2022-08-01 12:52:42 +00:00
|
|
|
TooManyChannels* = object of MuxerError
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
StreamHandler* = proc(conn: Connection): Future[void] {.async: (raises: []).}
|
|
|
|
MuxerHandler* = proc(muxer: Muxer): Future[void] {.async: (raises: []).}
|
2019-09-04 21:22:23 +00:00
|
|
|
|
2019-09-03 20:40:51 +00:00
|
|
|
Muxer* = ref object of RootObj
|
2019-09-04 21:22:23 +00:00
|
|
|
streamHandler*: StreamHandler
|
2024-03-05 07:06:27 +00:00
|
|
|
handler*: Future[void].Raising([])
|
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
|
2024-06-11 15:18:06 +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 =
|
2024-06-11 15:18:06 +00:00
|
|
|
if m == nil:
|
|
|
|
"nil"
|
|
|
|
else:
|
|
|
|
shortLog(m.connection)
|
2024-03-05 07:06:27 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
chronicles.formatIt(Muxer):
|
|
|
|
shortLog(it)
|
2020-09-06 08:31:47 +00:00
|
|
|
|
2019-11-06 18:09:48 +00:00
|
|
|
# muxer interface
|
2024-03-05 07:06:27 +00:00
|
|
|
method newStream*(
|
2024-06-11 15:18:06 +00:00
|
|
|
m: Muxer, name: string = "", lazy: bool = false
|
|
|
|
): Future[Connection] {.
|
|
|
|
base, async: (raises: [CancelledError, LPStreamError, MuxerError], raw: true)
|
|
|
|
.} =
|
2024-03-05 07:06:27 +00:00
|
|
|
raiseAssert("Not implemented!")
|
|
|
|
|
|
|
|
method close*(m: Muxer) {.base, async: (raises: []).} =
|
|
|
|
if m.connection != nil:
|
2023-03-08 11:30:19 +00:00
|
|
|
await m.connection.close()
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
method handle*(m: Muxer): Future[void] {.base, async: (raises: []).} =
|
|
|
|
discard
|
2021-06-07 07:32:08 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
proc new*(
|
2024-06-11 15:18:06 +00:00
|
|
|
T: typedesc[MuxerProvider], creator: MuxerConstructor, codec: string
|
|
|
|
): T {.gcsafe.} =
|
2023-03-08 11:30:19 +00:00
|
|
|
let muxerProvider = T(newMuxer: creator, codec: codec)
|
2021-06-07 07:32:08 +00:00
|
|
|
muxerProvider
|
2023-03-30 22:16:39 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
method getStreams*(m: Muxer): seq[Connection] {.base.} =
|
|
|
|
raiseAssert("Not implemented!")
|