2019-09-03 20:40:51 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-09-03 20:40:51 +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-11 21:06:42 +00:00
|
|
|
import chronos, chronicles
|
2019-09-12 17:07:34 +00:00
|
|
|
import ../protocols/protocol,
|
2020-06-19 17:29:43 +00:00
|
|
|
../stream/connection,
|
2020-04-11 04:08:25 +00:00
|
|
|
../errors
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2019-09-11 21:06:42 +00:00
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "muxer"
|
2019-09-11 21:06:42 +00:00
|
|
|
|
2019-09-03 20:40:51 +00:00
|
|
|
type
|
2019-09-04 21:22:23 +00:00
|
|
|
StreamHandler* = proc(conn: Connection): Future[void] {.gcsafe.}
|
2019-09-11 21:06:42 +00:00
|
|
|
MuxerHandler* = proc(muxer: Muxer): Future[void] {.gcsafe.}
|
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
|
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
|
|
|
|
MuxerConstructor* = proc(conn: Connection): Muxer {.gcsafe, closure.}
|
|
|
|
|
2019-09-03 20:40:51 +00:00
|
|
|
# this wraps a creator proc that knows how to make muxers
|
|
|
|
MuxerProvider* = ref object of LPProtocol
|
2019-11-06 18:09:48 +00: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 06:51:19 +00:00
|
|
|
|
2019-11-06 18:09:48 +00:00
|
|
|
# muxer interface
|
2020-02-12 22:09:41 +00:00
|
|
|
method newStream*(m: Muxer, name: string = "", lazy: bool = false):
|
|
|
|
Future[Connection] {.base, async, gcsafe.} = discard
|
2019-09-06 06:51:19 +00:00
|
|
|
method close*(m: Muxer) {.base, async, gcsafe.} = discard
|
|
|
|
method handle*(m: Muxer): Future[void] {.base, async, gcsafe.} = discard
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc newMuxerProvider*(creator: MuxerConstructor, codec: string): MuxerProvider {.gcsafe.} =
|
2019-09-03 20:40:51 +00:00
|
|
|
new result
|
|
|
|
result.newMuxer = creator
|
|
|
|
result.codec = codec
|
2019-09-06 06:51:19 +00:00
|
|
|
result.init()
|
|
|
|
|
2019-09-03 20:40:51 +00:00
|
|
|
method init(c: MuxerProvider) =
|
|
|
|
proc handler(conn: Connection, proto: string) {.async, gcsafe, closure.} =
|
2020-06-21 09:14:19 +00:00
|
|
|
trace "starting muxer handler", proto=proto, peer = $conn
|
2020-05-23 16:50:05 +00:00
|
|
|
try:
|
|
|
|
let
|
|
|
|
muxer = c.newMuxer(conn)
|
2019-09-11 21:06:42 +00:00
|
|
|
|
2020-05-23 16:50:05 +00:00
|
|
|
if not isNil(c.streamHandler):
|
|
|
|
muxer.streamHandler = c.streamHandler
|
2019-09-11 21:06:42 +00:00
|
|
|
|
2020-05-23 16:50:05 +00:00
|
|
|
var futs = newSeq[Future[void]]()
|
|
|
|
futs &= muxer.handle()
|
2020-04-11 04:08:25 +00:00
|
|
|
|
2020-05-23 16:50:05 +00:00
|
|
|
# finally await both the futures
|
|
|
|
if not isNil(c.muxerHandler):
|
|
|
|
futs &= c.muxerHandler(muxer)
|
2020-04-11 04:08:25 +00:00
|
|
|
|
2020-06-02 17:32:42 +00:00
|
|
|
checkFutures(await allFinished(futs))
|
2020-06-29 15:15:31 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-05-23 16:50:05 +00:00
|
|
|
except CatchableError as exc:
|
2020-06-21 09:14:19 +00:00
|
|
|
trace "exception in muxer handler", exc = exc.msg, peer = $conn, proto=proto
|
2020-06-29 15:15:31 +00:00
|
|
|
finally:
|
|
|
|
await conn.close()
|
2020-04-11 04:08:25 +00:00
|
|
|
|
2019-09-03 20:40:51 +00:00
|
|
|
c.handler = handler
|
2020-06-19 17:29:43 +00:00
|
|
|
|
|
|
|
proc `$`*(m: Muxer): string =
|
2020-06-21 09:14:19 +00:00
|
|
|
$m.connection
|