164 lines
5.7 KiB
Nim
Raw Normal View History

2019-09-03 14:40:51 -06:00
## Nim-LibP2P
2019-09-24 11:48:23 -06:00
## Copyright (c) 2019 Status Research & Development GmbH
2019-09-03 14:40:51 -06: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 10:40:05 -06:00
import tables, sequtils
2019-09-09 11:33:32 -06:00
import chronos, chronicles
import ../muxer,
../../connection,
../../stream/lpstream,
../../utility,
coder,
types,
lpchannel
2019-09-03 14:40:51 -06:00
2019-09-09 20:15:52 -06:00
logScope:
2019-09-11 20:10:38 -06:00
topic = "Mplex"
2019-09-09 20:15:52 -06:00
2020-02-12 09:37:22 -05:00
const DefaultRWTimeout = InfiniteDuration
2019-09-03 21:08:51 -06:00
type
2019-09-03 14:40:51 -06:00
Mplex* = ref object of Muxer
2020-03-23 11:14:06 -06:00
remote*: Table[uint64, LPChannel]
local*: Table[uint64, LPChannel]
currentId*: uint64
maxChannels*: uint64
2019-09-03 14:40:51 -06:00
2020-03-23 11:14:06 -06:00
proc getChannelList(m: Mplex, initiator: bool): var Table[uint64, LPChannel] =
2019-09-03 19:42:00 -06:00
if initiator:
trace "picking local channels", initiator = initiator
2019-09-03 14:40:51 -06:00
result = m.local
else:
trace "picking remote channels", initiator = initiator
result = m.remote
2019-09-03 14:40:51 -06:00
2019-09-03 19:42:00 -06:00
proc newStreamInternal*(m: Mplex,
initiator: bool = true,
2020-03-23 11:14:06 -06:00
chanId: uint64 = 0,
name: string = "",
lazy: bool = false):
Future[LPChannel] {.async, gcsafe.} =
2019-09-03 14:40:51 -06:00
## create new channel/stream
2019-09-03 19:42:00 -06: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-03 19:42:00 -06:00
m.getChannelList(initiator)[id] = result
2019-09-03 14:40:51 -06:00
proc cleanupChann(m: Mplex, chann: LPChannel, initiator: bool) {.async, inline.} =
## call the channel's `close` to signal the
## remote that the channel is closing
if not isNil(chann) and not chann.closed:
await chann.close()
await chann.cleanUp()
m.getChannelList(initiator).del(chann.id)
trace "cleaned up channel", id = chann.id
method handle*(m: Mplex) {.async, gcsafe.} =
trace "starting mplex main loop"
try:
2019-09-03 19:42:00 -06:00
while not m.connection.closed:
trace "waiting for data"
2020-02-12 09:37:22 -05: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 11:07:34 -06: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 00:34:08 -06:00
continue
channel = channels[id]
case msgType:
of MessageType.New:
2019-09-08 00:34:08 -06:00
let name = cast[string](data)
channel = await m.newStreamInternal(false, id, name)
trace "created channel", id = id, name = name, inititator = true
if not isNil(m.streamHandler):
2019-09-11 13:04:27 -06:00
let stream = newConnection(channel)
stream.peerInfo = m.connection.peerInfo
# cleanup channel once handler is finished
# stream.closeEvent.wait().addCallback(
# proc(udata: pointer) =
# asyncCheck cleanupChann(m, channel, initiator))
asyncCheck m.streamHandler(stream)
continue
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 16:23:39 -06: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 11:40:46 -06: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-04 20:42:08 -06:00
await m.close()
2019-09-03 14:40:51 -06:00
2020-04-07 09:49:43 -06: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 14:40:51 -06:00
maxChanns: uint = MaxChannels): Mplex =
new result
result.connection = conn
result.maxChannels = maxChanns
2020-03-23 11:14:06 -06:00
result.remote = initTable[uint64, LPChannel]()
result.local = initTable[uint64, LPChannel]()
2019-09-03 14:40:51 -06:00
2020-04-07 09:49:43 -06:00
asyncCheck result.internalCleanup(conn)
2020-02-12 09:37:22 -05: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-03 19:42:00 -06:00
result = newConnection(channel)
2019-09-11 13:04:27 -06:00
result.peerInfo = m.connection.peerInfo
2019-09-03 14:40:51 -06:00
method close*(m: Mplex) {.async, gcsafe.} =
trace "closing mplex muxer"
2020-04-04 20:42:08 -06:00
if not m.connection.closed():
await m.connection.close()
await allFutures(@[allFutures(toSeq(m.remote.values).mapIt(it.reset())),
allFutures(toSeq(m.local.values).mapIt(it.reset()))])
2020-04-04 20:42:08 -06:00
m.remote.clear()
m.local.clear()