nim-libp2p-experimental/libp2p/muxers/muxer.nim

42 lines
1.4 KiB
Nim
Raw Normal View History

2019-09-03 20:40:51 +00:00
## Nim-LibP2P
## Copyright (c) 2018 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.
import chronos
import ../protocol, ../connection
type
StreamHandler* = proc(conn: Connection): Future[void] {.gcsafe.}
2019-09-03 20:40:51 +00:00
Muxer* = ref object of RootObj
streamHandler*: StreamHandler
2019-09-03 20:40:51 +00:00
connection*: Connection
MuxerCreator* = proc(conn: Connection): Muxer {.gcsafe, closure.}
# this wraps a creator proc that knows how to make muxers
MuxerProvider* = ref object of LPProtocol
newMuxer*: MuxerCreator
proc newMuxerProvider*(creator: MuxerCreator, codec: string): MuxerProvider {.gcsafe.} =
new result
result.newMuxer = creator
result.codec = codec
method init(c: MuxerProvider) =
proc handler(conn: Connection, proto: string) {.async, gcsafe, closure.} =
let muxer = c.newMuxer(conn)
c.handler = handler
method newStream*(m: Muxer): Future[Connection] {.base, async, gcsafe.} = discard
method close*(m: Muxer) {.base, async, gcsafe.} = discard
2019-09-04 16:40:05 +00:00
method handle*(m: Muxer): Future[void] {.base, async, gcsafe.} = discard
method `=streamHandler`*(m: Muxer, handler: StreamHandler) {.base, gcsafe.} =
m.streamHandler = handler