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

187 lines
6.2 KiB
Nim
Raw Normal View History

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.
## TODO:
## Timeouts and message limits are still missing
## they need to be added ASAP
2019-09-04 16:40:05 +00:00
import tables, sequtils
2019-09-09 17:33:32 +00:00
import chronos, chronicles
import ../muxer,
../../connection,
../../stream/lpstream,
../../utility,
../../errors,
coder,
types,
lpchannel
2019-09-03 20:40:51 +00:00
2019-09-10 02:15:52 +00:00
logScope:
2019-09-12 02:10:38 +00:00
topic = "Mplex"
2019-09-10 02:15:52 +00:00
2019-09-04 03:08:51 +00:00
type
2019-09-03 20:40:51 +00:00
Mplex* = ref object of Muxer
2020-03-23 17:14:06 +00:00
remote*: Table[uint64, LPChannel]
local*: Table[uint64, LPChannel]
handlers*: array[2, Table[uint64, Future[void]]]
2020-03-23 17:14:06 +00:00
currentId*: uint64
maxChannels*: uint64
2019-09-03 20:40:51 +00:00
2020-03-23 17:14:06 +00:00
proc getChannelList(m: Mplex, initiator: bool): var Table[uint64, LPChannel] =
2019-09-04 01:42:00 +00:00
if initiator:
trace "picking local channels", initiator = initiator
2019-09-03 20:40:51 +00:00
result = m.local
else:
trace "picking remote channels", initiator = initiator
result = m.remote
2019-09-03 20:40:51 +00:00
2019-09-04 01:42:00 +00:00
proc newStreamInternal*(m: Mplex,
initiator: bool = true,
2020-03-23 17:14:06 +00:00
chanId: uint64 = 0,
name: string = "",
lazy: bool = false):
Future[LPChannel] {.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
trace "creating new channel", channelId = id, initiator = initiator
result = newChannel(id, m.connection, initiator, name, lazy = lazy)
2019-09-04 01:42:00 +00:00
m.getChannelList(initiator)[id] = result
2019-09-03 20:40:51 +00:00
proc cleanupChann(m: Mplex, chann: LPChannel, initiator: bool) {.async.} =
## call the channel's `close` to signal the
## remote that the channel is closing
if not isNil(chann) and not chann.closed:
trace "cleaning up channel", id = chann.id
await chann.close()
await chann.cleanUp()
m.getChannelList(initiator).del(chann.id)
trace "cleaned up channel", id = chann.id
proc cleanupChann(chann: LPChannel) {.async.} =
trace "cleaning up channel", id = chann.id
await chann.reset()
await chann.close()
await chann.cleanUp()
trace "cleaned up channel", id = chann.id
method handle*(m: Mplex) {.async, gcsafe.} =
trace "starting mplex main loop"
try:
2019-09-04 01:42:00 +00:00
while not m.connection.closed:
trace "waiting for data"
2020-02-12 14:37:22 +00:00
let (id, msgType, data) = await m.connection.readMsg()
trace "read message from connection", id = id,
msgType = msgType,
data = data.shortLog
let initiator = bool(ord(msgType) and 1)
2019-09-12 17:07:34 +00:00
var channel: LPChannel
if MessageType(msgType) != MessageType.New:
let channels = m.getChannelList(initiator)
if id notin channels:
trace "Channel not found, skipping", id = id,
initiator = initiator,
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)
trace "created channel", id = id, name = name, inititator = initiator
if not isNil(m.streamHandler):
2019-09-11 19:04:27 +00:00
let stream = newConnection(channel)
stream.peerInfo = m.connection.peerInfo
proc handler() {.async.} =
tryAndWarn "mplex channel handler":
await m.streamHandler(stream)
if not initiator:
await m.cleanupChann(channel, false)
if not initiator:
m.handlers[0][id] = handler()
else:
m.handlers[1][id] = handler()
of MessageType.MsgIn, MessageType.MsgOut:
trace "pushing data to channel", id = id,
initiator = initiator,
msgType = msgType,
size = data.len
if data.len > MaxMsgSize:
2020-03-11 22:23:39 +00:00
raise newLPStreamLimitError()
await channel.pushTo(data)
of MessageType.CloseIn, MessageType.CloseOut:
trace "closing channel", id = id,
initiator = initiator,
msgType = msgType
await channel.closedByRemote()
2020-02-16 17:40:46 +00:00
m.getChannelList(initiator).del(id)
of MessageType.ResetIn, MessageType.ResetOut:
trace "resetting channel", id = id,
initiator = initiator,
msgType = msgType
await channel.resetByRemote()
m.getChannelList(initiator).del(id)
break
except CatchableError as exc:
trace "Exception occurred", exception = exc.msg
finally:
trace "stopping mplex main loop"
2020-04-05 02:42:08 +00:00
await m.close()
2019-09-03 20:40:51 +00:00
2020-04-07 15:49:43 +00:00
proc internalCleanup(m: Mplex, conn: Connection) {.async.} =
await conn.closeEvent.wait()
trace "connection closed, cleaning up mplex"
await m.close()
proc newMplex*(conn: Connection,
2019-09-03 20:40:51 +00:00
maxChanns: uint = MaxChannels): Mplex =
new result
result.connection = conn
result.maxChannels = maxChanns
2020-03-23 17:14:06 +00:00
result.remote = initTable[uint64, LPChannel]()
result.local = initTable[uint64, LPChannel]()
2019-09-03 20:40:51 +00:00
2020-04-07 15:49:43 +00:00
asyncCheck result.internalCleanup(conn)
2020-02-12 14:37:22 +00:00
method newStream*(m: Mplex,
name: string = "",
lazy: bool = false): Future[Connection] {.async, gcsafe.} =
let channel = await m.newStreamInternal(lazy = lazy)
if not lazy:
await channel.open()
2019-09-04 01:42:00 +00:00
result = newConnection(channel)
2019-09-11 19:04:27 +00:00
result.peerInfo = m.connection.peerInfo
2019-09-03 20:40:51 +00:00
method close*(m: Mplex) {.async, gcsafe.} =
trace "closing mplex muxer"
2020-04-05 02:42:08 +00:00
if not m.connection.closed():
await m.connection.close()
let
futs = await allFinished(
toSeq(m.remote.values).mapIt(it.cleanupChann()) &
toSeq(m.local.values).mapIt(it.cleanupChann()) &
toSeq(m.handlers[0].values).mapIt(it) &
toSeq(m.handlers[1].values).mapIt(it))
checkFutures(futs)
m.handlers[0].clear()
m.handlers[1].clear()
2020-04-05 02:42:08 +00:00
m.remote.clear()
m.local.clear()
trace "mplex muxer closed"