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.
|
|
|
|
|
2019-09-23 23:14:41 +00:00
|
|
|
## TODO:
|
|
|
|
## Timeouts and message limits are still missing
|
|
|
|
## they need to be added ASAP
|
2019-09-04 16:40:05 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
import tables, sequtils, options
|
2019-09-09 17:33:32 +00:00
|
|
|
import chronos, chronicles
|
2019-12-04 04:44:54 +00:00
|
|
|
import ../muxer,
|
2019-09-24 16:16:39 +00:00
|
|
|
../../connection,
|
2019-12-04 04:44:54 +00:00
|
|
|
../../stream/lpstream,
|
2019-12-10 20:50:35 +00:00
|
|
|
coder,
|
|
|
|
types,
|
2019-12-04 04:44:54 +00:00
|
|
|
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
|
|
|
|
2020-02-12 14:37:22 +00:00
|
|
|
const DefaultRWTimeout = InfiniteDuration
|
|
|
|
|
2019-09-04 03:08:51 +00:00
|
|
|
type
|
2019-09-03 20:40:51 +00:00
|
|
|
Mplex* = ref object of Muxer
|
2019-09-12 17:07:34 +00:00
|
|
|
remote*: Table[uint, LPChannel]
|
|
|
|
local*: Table[uint, LPChannel]
|
2019-09-07 06:20:03 +00:00
|
|
|
currentId*: uint
|
2019-09-03 20:40:51 +00:00
|
|
|
maxChannels*: uint
|
|
|
|
|
2019-09-12 17:07:34 +00:00
|
|
|
proc getChannelList(m: Mplex, initiator: bool): var Table[uint, LPChannel] =
|
2019-09-04 01:42:00 +00:00
|
|
|
if initiator:
|
2019-12-04 04:44:54 +00:00
|
|
|
trace "picking local channels", initiator = initiator
|
2019-09-03 20:40:51 +00:00
|
|
|
result = m.local
|
2019-12-04 04:44:54 +00:00
|
|
|
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,
|
2019-09-07 06:20:03 +00:00
|
|
|
chanId: uint = 0,
|
2020-02-11 17:30:36 +00:00
|
|
|
name: string = "",
|
|
|
|
lazy: bool = false):
|
2019-12-10 20:50:35 +00:00
|
|
|
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
|
2019-12-04 04:44:54 +00:00
|
|
|
trace "creating new channel", channelId = id, initiator = initiator
|
2020-02-11 17:30:36 +00:00
|
|
|
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
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
proc cleanupChann(m: Mplex, chann: LPChannel, initiator: bool) {.async, inline.} =
|
2019-12-10 20:50:35 +00:00
|
|
|
## call the channel's `close` to signal the
|
2019-12-04 04:44:54 +00:00
|
|
|
## 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
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method handle*(m: Mplex) {.async, gcsafe.} =
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "starting mplex main loop"
|
2019-09-07 23:34:40 +00:00
|
|
|
try:
|
2019-09-04 01:42:00 +00:00
|
|
|
while not m.connection.closed:
|
2019-12-04 04:44:54 +00:00
|
|
|
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
|
2019-09-07 23:34:40 +00:00
|
|
|
let initiator = bool(ord(msgType) and 1)
|
2019-09-12 17:07:34 +00:00
|
|
|
var channel: LPChannel
|
2019-09-07 23:34:40 +00:00
|
|
|
if MessageType(msgType) != MessageType.New:
|
|
|
|
let channels = m.getChannelList(initiator)
|
2019-12-04 04:44:54 +00:00
|
|
|
if id notin channels:
|
|
|
|
trace "Channel not found, skipping", id = id,
|
|
|
|
initiator = initiator,
|
|
|
|
msg = msgType
|
2019-09-08 06:34:08 +00:00
|
|
|
continue
|
2019-09-07 23:34:40 +00:00
|
|
|
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-12-04 04:44:54 +00:00
|
|
|
trace "created channel", id = id, name = name, inititator = true
|
2019-09-07 23:34:40 +00:00
|
|
|
if not isNil(m.streamHandler):
|
2019-09-11 19:04:27 +00:00
|
|
|
let stream = newConnection(channel)
|
|
|
|
stream.peerInfo = m.connection.peerInfo
|
2019-12-04 04:44:54 +00:00
|
|
|
|
|
|
|
# cleanup channel once handler is finished
|
|
|
|
# stream.closeEvent.wait().addCallback(
|
|
|
|
# proc(udata: pointer) =
|
|
|
|
# asyncCheck cleanupChann(m, channel, initiator))
|
|
|
|
|
|
|
|
asyncCheck m.streamHandler(stream)
|
2019-09-07 23:34:40 +00:00
|
|
|
continue
|
|
|
|
of MessageType.MsgIn, MessageType.MsgOut:
|
2019-12-10 20:50:35 +00:00
|
|
|
trace "pushing data to channel", id = id,
|
|
|
|
initiator = initiator,
|
2020-02-06 06:24:11 +00:00
|
|
|
msgType = msgType,
|
|
|
|
size = data.len
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-02-06 06:24:11 +00:00
|
|
|
if data.len > MaxMsgSize:
|
2020-02-11 05:48:52 +00:00
|
|
|
raise newLPStreamLimitError();
|
2019-12-10 20:50:35 +00:00
|
|
|
await channel.pushTo(data)
|
2019-09-07 23:34:40 +00:00
|
|
|
of MessageType.CloseIn, MessageType.CloseOut:
|
2019-12-10 20:50:35 +00:00
|
|
|
trace "closing channel", id = id,
|
|
|
|
initiator = initiator,
|
2019-12-04 04:44:54 +00:00
|
|
|
msgType = msgType
|
|
|
|
|
2019-09-07 23:34:40 +00:00
|
|
|
await channel.closedByRemote()
|
2020-02-16 17:40:46 +00:00
|
|
|
m.getChannelList(initiator).del(id)
|
2019-09-07 23:34:40 +00:00
|
|
|
of MessageType.ResetIn, MessageType.ResetOut:
|
2019-12-10 20:50:35 +00:00
|
|
|
trace "resetting channel", id = id,
|
|
|
|
initiator = initiator,
|
2019-12-04 04:44:54 +00:00
|
|
|
msgType = msgType
|
|
|
|
|
2019-09-07 23:34:40 +00:00
|
|
|
await channel.resetByRemote()
|
2019-12-04 04:44:54 +00:00
|
|
|
m.getChannelList(initiator).del(id)
|
2019-09-07 23:34:40 +00:00
|
|
|
break
|
2019-12-04 04:44:54 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception occurred", exception = exc.msg
|
2019-09-07 23:34:40 +00:00
|
|
|
finally:
|
2019-12-04 04:44:54 +00:00
|
|
|
trace "stopping mplex main loop"
|
2020-01-07 08:02:37 +00:00
|
|
|
if not m.connection.closed():
|
|
|
|
await m.connection.close()
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc newMplex*(conn: Connection,
|
2019-09-03 20:40:51 +00:00
|
|
|
maxChanns: uint = MaxChannels): Mplex =
|
|
|
|
new result
|
|
|
|
result.connection = conn
|
|
|
|
result.maxChannels = maxChanns
|
2019-09-12 17:07:34 +00:00
|
|
|
result.remote = initTable[uint, LPChannel]()
|
|
|
|
result.local = initTable[uint, LPChannel]()
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
let m = result
|
2020-01-07 08:02:37 +00:00
|
|
|
conn.closeEvent.wait()
|
|
|
|
.addCallback do (udata: pointer):
|
2019-12-10 20:50:35 +00:00
|
|
|
trace "connection closed, cleaning up mplex"
|
|
|
|
asyncCheck m.close()
|
2020-02-12 14:37:22 +00:00
|
|
|
|
|
|
|
method newStream*(m: Mplex,
|
|
|
|
name: string = "",
|
|
|
|
lazy: bool = false): Future[Connection] {.async, gcsafe.} =
|
2020-02-11 17:30:36 +00:00
|
|
|
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
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method close*(m: Mplex) {.async, gcsafe.} =
|
2019-12-04 04:44:54 +00:00
|
|
|
trace "closing mplex muxer"
|
|
|
|
await allFutures(@[allFutures(toSeq(m.remote.values).mapIt(it.reset())),
|
|
|
|
allFutures(toSeq(m.local.values).mapIt(it.reset()))])
|