2022-07-01 20:19:57 +02:00
|
|
|
# Nim-LibP2P
|
|
|
|
# Copyright (c) 2022 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.
|
2019-09-03 14:40:51 -06:00
|
|
|
|
2022-08-03 13:33:19 +02:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-05-21 10:27:01 -06:00
|
|
|
|
2019-09-11 15:06:42 -06:00
|
|
|
import chronos, chronicles
|
2019-09-12 11:07:34 -06:00
|
|
|
import ../protocols/protocol,
|
2020-06-19 11:29:43 -06:00
|
|
|
../stream/connection,
|
2020-04-11 13:08:25 +09:00
|
|
|
../errors
|
2019-09-03 14:40:51 -06:00
|
|
|
|
2019-09-11 15:06:42 -06:00
|
|
|
logScope:
|
2020-12-01 11:34:27 -06:00
|
|
|
topics = "libp2p muxer"
|
2019-09-11 15:06:42 -06:00
|
|
|
|
2020-07-17 12:44:41 -06:00
|
|
|
const
|
2020-08-04 07:22:05 -06:00
|
|
|
DefaultChanTimeout* = 5.minutes
|
2020-07-17 12:44:41 -06:00
|
|
|
|
2019-09-03 14:40:51 -06:00
|
|
|
type
|
2021-05-21 10:27:01 -06:00
|
|
|
MuxerError* = object of LPError
|
2022-08-01 14:52:42 +02:00
|
|
|
TooManyChannels* = object of MuxerError
|
2021-05-21 10:27:01 -06:00
|
|
|
|
|
|
|
StreamHandler* = proc(conn: Connection): Future[void] {.gcsafe, raises: [Defect].}
|
|
|
|
MuxerHandler* = proc(muxer: Muxer): Future[void] {.gcsafe, raises: [Defect].}
|
2019-09-04 15:22:23 -06:00
|
|
|
|
2019-09-03 14:40:51 -06:00
|
|
|
Muxer* = ref object of RootObj
|
2019-09-04 15:22:23 -06:00
|
|
|
streamHandler*: StreamHandler
|
2019-09-03 14:40:51 -06:00
|
|
|
connection*: Connection
|
|
|
|
|
2019-11-06 12:09:48 -06:00
|
|
|
# user provider proc that returns a constructed Muxer
|
2021-05-21 10:27:01 -06:00
|
|
|
MuxerConstructor* = proc(conn: Connection): Muxer {.gcsafe, closure, raises: [Defect].}
|
2019-11-06 12:09:48 -06:00
|
|
|
|
2019-09-03 14:40:51 -06:00
|
|
|
# this wraps a creator proc that knows how to make muxers
|
|
|
|
MuxerProvider* = ref object of LPProtocol
|
2019-11-06 12:09:48 -06:00
|
|
|
newMuxer*: MuxerConstructor
|
|
|
|
streamHandler*: StreamHandler # triggered every time there is a new stream, called for any muxer instance
|
|
|
|
muxerHandler*: MuxerHandler # triggered every time there is a new muxed connection created
|
2019-09-06 00:51:19 -06:00
|
|
|
|
2020-09-06 10:31:47 +02:00
|
|
|
func shortLog*(m: Muxer): auto = shortLog(m.connection)
|
|
|
|
chronicles.formatIt(Muxer): shortLog(it)
|
|
|
|
|
2019-11-06 12:09:48 -06:00
|
|
|
# muxer interface
|
2020-02-12 17:09:41 -05:00
|
|
|
method newStream*(m: Muxer, name: string = "", lazy: bool = false):
|
|
|
|
Future[Connection] {.base, async, gcsafe.} = discard
|
2019-09-06 00:51:19 -06:00
|
|
|
method close*(m: Muxer) {.base, async, gcsafe.} = discard
|
|
|
|
method handle*(m: Muxer): Future[void] {.base, async, gcsafe.} = discard
|
2019-09-03 14:40:51 -06:00
|
|
|
|
2021-06-07 09:32:08 +02:00
|
|
|
proc new*(
|
|
|
|
T: typedesc[MuxerProvider],
|
|
|
|
creator: MuxerConstructor,
|
|
|
|
codec: string): T {.gcsafe.} =
|
|
|
|
|
|
|
|
let muxerProvider = T(newMuxer: creator)
|
|
|
|
muxerProvider.codec = codec
|
|
|
|
muxerProvider.init()
|
|
|
|
muxerProvider
|
|
|
|
|
2019-09-03 14:40:51 -06:00
|
|
|
method init(c: MuxerProvider) =
|
|
|
|
proc handler(conn: Connection, proto: string) {.async, gcsafe, closure.} =
|
2020-09-06 10:31:47 +02:00
|
|
|
trace "starting muxer handler", proto=proto, conn
|
2020-05-23 10:50:05 -06:00
|
|
|
try:
|
|
|
|
let
|
|
|
|
muxer = c.newMuxer(conn)
|
2019-09-11 15:06:42 -06:00
|
|
|
|
2020-05-23 10:50:05 -06:00
|
|
|
if not isNil(c.streamHandler):
|
|
|
|
muxer.streamHandler = c.streamHandler
|
2019-09-11 15:06:42 -06:00
|
|
|
|
2020-05-23 10:50:05 -06:00
|
|
|
var futs = newSeq[Future[void]]()
|
|
|
|
futs &= muxer.handle()
|
2020-04-11 13:08:25 +09:00
|
|
|
|
2020-05-23 10:50:05 -06:00
|
|
|
# finally await both the futures
|
|
|
|
if not isNil(c.muxerHandler):
|
2021-09-08 11:07:46 +02:00
|
|
|
await c.muxerHandler(muxer)
|
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
conn.shortAgent = muxer.connection.shortAgent
|
2020-04-11 13:08:25 +09:00
|
|
|
|
2020-06-02 11:32:42 -06:00
|
|
|
checkFutures(await allFinished(futs))
|
2020-06-29 09:15:31 -06:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-05-23 10:50:05 -06:00
|
|
|
except CatchableError as exc:
|
2020-09-06 10:31:47 +02:00
|
|
|
trace "exception in muxer handler", exc = exc.msg, conn, proto
|
2020-06-29 09:15:31 -06:00
|
|
|
finally:
|
|
|
|
await conn.close()
|
2020-04-11 13:08:25 +09:00
|
|
|
|
2019-09-03 14:40:51 -06:00
|
|
|
c.handler = handler
|