add muxer handler and properly set stream peerinfo

This commit is contained in:
Dmitriy Ryajov 2019-09-11 15:06:42 -06:00
parent 32b74c5d1c
commit 5f2d944545
2 changed files with 28 additions and 6 deletions

View File

@ -70,7 +70,7 @@ method handle*(m: Mplex) {.async, gcsafe.} =
if MessageType(msgType) != MessageType.New: if MessageType(msgType) != MessageType.New:
let channels = m.getChannelList(initiator) let channels = m.getChannelList(initiator)
if not channels.contains(id): if not channels.contains(id):
# debug "handle: Channel with id and msg type ", id = id, msg = msgType debug "handle: Channel with id and msg type ", id = id, msg = msgType
continue continue
channel = channels[id] channel = channels[id]
@ -82,15 +82,13 @@ method handle*(m: Mplex) {.async, gcsafe.} =
if not isNil(m.streamHandler): if not isNil(m.streamHandler):
let stream = newConnection(channel) let stream = newConnection(channel)
stream.peerInfo = m.connection.peerInfo stream.peerInfo = m.connection.peerInfo
let handlerFut = m.streamHandler(newConnection(stream)) let handlerFut = m.streamHandler(stream)
# TODO: don't use a closure?
# channel cleanup routine # channel cleanup routine
proc cleanUpChan(udata: pointer) {.gcsafe.} = proc cleanUpChan(udata: pointer) {.gcsafe.} =
if handlerFut.finished: if handlerFut.finished:
channel.close().addCallback( channel.close().addCallback(
proc(udata: pointer) = proc(udata: pointer) =
# TODO: is waitFor() OK here?
channel.cleanUp() channel.cleanUp()
.addCallback(proc(udata: pointer) = .addCallback(proc(udata: pointer) =
debug "handle: cleaned up channel ", id = id)) debug "handle: cleaned up channel ", id = id))

View File

@ -7,12 +7,16 @@
## This file may not be copied, modified, or distributed except according to ## This file may not be copied, modified, or distributed except according to
## those terms. ## those terms.
import chronos import chronos, chronicles
import ../protocols/protocol, import ../protocols/protocol,
../connection ../connection
logScope:
topic = "Muxer"
type type
StreamHandler* = proc(conn: Connection): Future[void] {.gcsafe.} StreamHandler* = proc(conn: Connection): Future[void] {.gcsafe.}
MuxerHandler* = proc(muxer: Muxer): Future[void] {.gcsafe.}
Muxer* = ref object of RootObj Muxer* = ref object of RootObj
streamHandler*: StreamHandler streamHandler*: StreamHandler
@ -23,6 +27,7 @@ type
MuxerProvider* = ref object of LPProtocol MuxerProvider* = ref object of LPProtocol
newMuxer*: MuxerCreator newMuxer*: MuxerCreator
streamHandler*: StreamHandler streamHandler*: StreamHandler
muxerHandler*: MuxerHandler
method newStream*(m: Muxer, name: string = ""): Future[Connection] {.base, async, gcsafe.} = discard method newStream*(m: Muxer, name: string = ""): Future[Connection] {.base, async, gcsafe.} = discard
method close*(m: Muxer) {.base, async, gcsafe.} = discard method close*(m: Muxer) {.base, async, gcsafe.} = discard
@ -37,13 +42,32 @@ proc newMuxerProvider*(creator: MuxerCreator, codec: string): MuxerProvider {.gc
result.init() result.init()
method `=streamHandler`*(m: MuxerProvider, handler: StreamHandler) {.base, gcsafe.} = method `=streamHandler`*(m: MuxerProvider, handler: StreamHandler) {.base, gcsafe.} =
## new stream (channels) handler
##
## triggered every time there is a new
## stream (channel) oppened over a muxed
## connection
##
m.streamHandler = handler m.streamHandler = handler
method `=muxerHandler`*(m: MuxerProvider, handler: MuxerHandler) {.base, gcsafe.} =
## new muxer (muxed connections) handler
##
## triggered every time there is a new muxed
## connection created
##
m.muxerHandler = handler
method init(c: MuxerProvider) = method init(c: MuxerProvider) =
proc handler(conn: Connection, proto: string) {.async, gcsafe, closure.} = proc handler(conn: Connection, proto: string) {.async, gcsafe, closure.} =
let muxer = c.newMuxer(conn) let muxer = c.newMuxer(conn)
if not isNil(c.muxerHandler):
debug "CALLING MUXER HANDLER"
await c.muxerHandler(muxer)
if not isNil(c.streamHandler): if not isNil(c.streamHandler):
muxer.streamHandler = c.streamHandler muxer.streamHandler = c.streamHandler
await muxer.handle()
await muxer.handle()
c.handler = handler c.handler = handler