nim-libp2p/libp2p/muxers/mplex/mplex.nim

129 lines
4.6 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.
2019-09-05 15:20:21 +00:00
## TODO: I have to be carefull to clean channels up correctly,
## both by removing from the internal tables as well as
## releasing resource when the channel is completelly
## finished. This is complicated because half-closed
## streams makes closing channels non-deterministic.
##
## This still needs to be implemented properly - I'm leaving it
## here to not forget that this needs to be fixed ASAP.
2019-09-04 16:40:05 +00:00
import tables, sequtils, options, strformat
2019-09-09 17:33:32 +00:00
import chronos, chronicles
2019-09-06 06:51:19 +00:00
import coder, types, channel,
2019-09-09 17:33:32 +00:00
../muxer,
2019-09-06 06:51:19 +00:00
../../varint,
../../connection,
../../vbuffer,
../../protocols/protocol,
2019-09-04 03:08:51 +00:00
../../stream/bufferstream,
2019-09-09 17:33:32 +00:00
../../stream/lpstream
2019-09-03 20:40:51 +00:00
2019-09-10 02:15:52 +00:00
logScope:
topic = "mplex"
2019-09-04 03:08:51 +00:00
type
2019-09-03 20:40:51 +00:00
Mplex* = ref object of Muxer
remote*: Table[uint, Channel]
local*: Table[uint, Channel]
currentId*: uint
2019-09-03 20:40:51 +00:00
maxChannels*: uint
2019-09-04 03:08:51 +00:00
proc newMplexUnknownMsgError(): ref MplexUnknownMsgError =
2019-09-03 20:40:51 +00:00
result = newException(MplexUnknownMsgError, "Unknown mplex message type")
proc getChannelList(m: Mplex, initiator: bool): var Table[uint, Channel] =
2019-09-04 01:42:00 +00:00
if initiator:
2019-09-03 20:40:51 +00:00
result = m.remote
else:
result = m.local
2019-09-04 01:42:00 +00:00
proc newStreamInternal*(m: Mplex,
initiator: bool = true,
chanId: uint = 0,
2019-09-06 21:27:55 +00:00
name: string = ""):
2019-09-04 01:42:00 +00:00
Future[Channel] {.async, gcsafe.} =
2019-09-03 20:40:51 +00:00
## create new channel/stream
2019-09-04 01:42:00 +00:00
let id = if initiator: m.currentId.inc(); m.currentId else: chanId
2019-09-06 21:27:55 +00:00
result = newChannel(id, m.connection, initiator, name)
2019-09-04 01:42:00 +00:00
m.getChannelList(initiator)[id] = result
2019-09-03 20:40:51 +00:00
method handle*(m: Mplex) {.async, gcsafe.} =
try:
2019-09-04 01:42:00 +00:00
while not m.connection.closed:
let msgRes = await m.connection.readMsg()
if msgRes.isNone:
await sleepAsync(100.millis)
continue
2019-09-06 21:27:55 +00:00
let (id, msgType, data) = msgRes.get()
let initiator = bool(ord(msgType) and 1)
var channel: Channel
if MessageType(msgType) != MessageType.New:
let channels = m.getChannelList(initiator)
if not channels.contains(id):
2019-09-09 17:33:32 +00:00
# debug "handle: Channel with id and msg type ", id = id, msg = msgType
2019-09-08 06:34:08 +00:00
continue
channel = channels[id]
case msgType:
of MessageType.New:
2019-09-08 06:34:08 +00:00
let name = cast[string](data)
channel = await m.newStreamInternal(false, id, name)
2019-09-09 17:45:16 +00:00
debug "handle: created channel ", id = id, name = name
if not isNil(m.streamHandler):
let handlerFut = m.streamHandler(newConnection(channel))
2019-09-08 06:34:08 +00:00
# TODO: don't use a closure?
# channel cleanup routine
proc cleanUpChan(udata: pointer) {.gcsafe.} =
if handlerFut.finished:
channel.close().addCallback(
proc(udata: pointer) =
# TODO: is waitFor() OK here?
channel.cleanUp()
2019-09-09 17:33:32 +00:00
.addCallback(proc(udata: pointer) =
debug "handle: cleaned up channel ", id = id))
handlerFut.addCallback(cleanUpChan)
continue
of MessageType.MsgIn, MessageType.MsgOut:
2019-09-09 17:33:32 +00:00
debug "handle: pushing data to channel ", id = id, msgType = msgType
await channel.pushTo(data)
of MessageType.CloseIn, MessageType.CloseOut:
2019-09-09 17:33:32 +00:00
debug "handle: closing channel ", id = id, msgType = msgType
await channel.closedByRemote()
m.getChannelList(initiator).del(id)
of MessageType.ResetIn, MessageType.ResetOut:
2019-09-09 17:33:32 +00:00
debug "handle: resetting channel ", id = id
await channel.resetByRemote()
break
else: raise newMplexUnknownMsgError()
finally:
await m.connection.close()
2019-09-03 20:40:51 +00:00
proc newMplex*(conn: Connection,
maxChanns: uint = MaxChannels): Mplex =
new result
result.connection = conn
result.maxChannels = maxChanns
result.remote = initTable[uint, Channel]()
result.local = initTable[uint, Channel]()
2019-09-03 20:40:51 +00:00
2019-09-06 21:27:55 +00:00
method newStream*(m: Mplex, name: string = ""): Future[Connection] {.async, gcsafe.} =
2019-09-04 01:42:00 +00:00
let channel = await m.newStreamInternal()
await m.connection.writeMsg(channel.id, MessageType.New, name)
2019-09-04 01:42:00 +00:00
result = newConnection(channel)
2019-09-03 20:40:51 +00:00
2019-09-04 01:42:00 +00:00
method close*(m: Mplex) {.async, gcsafe.} =
await allFutures(@[allFutures(toSeq(m.remote.values).mapIt(it.close())),
allFutures(toSeq(m.local.values).mapIt(it.close()))])
2019-09-04 16:40:05 +00:00
m.connection.reset()