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.
|
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
import tables, sequtils, oids
|
2020-06-29 15:15:31 +00:00
|
|
|
import chronos, chronicles, stew/byteutils, metrics
|
2019-12-04 04:44:54 +00:00
|
|
|
import ../muxer,
|
2020-06-19 17:29:43 +00:00
|
|
|
../../stream/connection,
|
2020-05-20 00:14:15 +00:00
|
|
|
../../stream/bufferstream,
|
2020-03-23 06:03:36 +00:00
|
|
|
../../utility,
|
2020-06-29 15:15:31 +00:00
|
|
|
../../peerinfo,
|
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
|
|
|
|
2020-07-17 18:44:41 +00:00
|
|
|
export muxer
|
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "mplex"
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2020-08-05 05:16:04 +00:00
|
|
|
const
|
|
|
|
MaxChannelCount = 200
|
|
|
|
|
2020-08-04 23:27:59 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
2020-08-05 05:16:04 +00:00
|
|
|
declareGauge(libp2p_mplex_channels,
|
|
|
|
"mplex channels", labels = ["initiator", "peer"])
|
2020-06-29 15:15:31 +00:00
|
|
|
|
2019-09-04 03:08:51 +00:00
|
|
|
type
|
2020-08-05 05:16:04 +00:00
|
|
|
TooManyChannels* = object of CatchableError
|
|
|
|
|
2019-09-03 20:40:51 +00:00
|
|
|
Mplex* = ref object of Muxer
|
2020-08-15 05:58:30 +00:00
|
|
|
channels: array[bool, Table[uint64, LPChannel]]
|
|
|
|
currentId: uint64
|
2020-07-17 18:44:41 +00:00
|
|
|
inChannTimeout: Duration
|
|
|
|
outChannTimeout: Duration
|
2020-05-20 00:14:15 +00:00
|
|
|
isClosed: bool
|
2020-07-17 18:44:41 +00:00
|
|
|
oid*: Oid
|
2020-08-05 05:16:04 +00:00
|
|
|
maxChannCount: int
|
|
|
|
|
|
|
|
proc newTooManyChannels(): ref TooManyChannels =
|
|
|
|
newException(TooManyChannels, "max allowed channel count exceeded")
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2020-08-15 05:58:30 +00:00
|
|
|
proc cleanupChann(m: Mplex, chann: LPChannel) {.async, inline.} =
|
|
|
|
## remove the local channel from the internal tables
|
|
|
|
##
|
2020-09-04 16:30:45 +00:00
|
|
|
try:
|
|
|
|
await chann.join()
|
|
|
|
m.channels[chann.initiator].del(chann.id)
|
|
|
|
trace "cleaned up channel", id = chann.id, oid = $chann.oid
|
|
|
|
|
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_mplex_channels.set(
|
|
|
|
m.channels[chann.initiator].len.int64,
|
|
|
|
labelValues = [$chann.initiator, $m.connection.peerInfo])
|
|
|
|
except CancelledError:
|
|
|
|
# This is top-level procedure which will work as separate task, so it
|
|
|
|
# do not need to propogate CancelledError.
|
|
|
|
trace "Unexpected cancellation in mplex channel cleanup"
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "error cleaning up mplex channel", exc = exc.msg
|
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,
|
2020-02-11 17:30:36 +00:00
|
|
|
name: string = "",
|
2020-07-17 18:44:41 +00:00
|
|
|
lazy: bool = false,
|
|
|
|
timeout: Duration):
|
2020-08-15 05:58:30 +00:00
|
|
|
LPChannel {.gcsafe.} =
|
2019-09-03 20:40:51 +00:00
|
|
|
## create new channel/stream
|
2020-06-29 15:15:31 +00:00
|
|
|
##
|
2020-05-23 19:25:53 +00:00
|
|
|
let id = if initiator:
|
|
|
|
m.currentId.inc(); m.currentId
|
|
|
|
else: chanId
|
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
trace "creating new channel", channelId = id,
|
|
|
|
initiator = initiator,
|
|
|
|
name = name,
|
2020-06-29 15:15:31 +00:00
|
|
|
oid = $m.oid
|
2020-07-17 18:44:41 +00:00
|
|
|
result = LPChannel.init(
|
|
|
|
id,
|
|
|
|
m.connection,
|
|
|
|
initiator,
|
|
|
|
name,
|
|
|
|
lazy = lazy,
|
|
|
|
timeout = timeout)
|
2020-06-19 17:29:43 +00:00
|
|
|
|
|
|
|
result.peerInfo = m.connection.peerInfo
|
|
|
|
result.observedAddr = m.connection.observedAddr
|
|
|
|
|
2020-08-15 05:58:30 +00:00
|
|
|
doAssert(id notin m.channels[initiator],
|
2020-06-29 15:15:31 +00:00
|
|
|
"channel slot already taken!")
|
|
|
|
|
2020-08-15 05:58:30 +00:00
|
|
|
m.channels[initiator][id] = result
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2020-09-04 16:30:45 +00:00
|
|
|
# All the errors are handled inside `cleanupChann()` procedure.
|
|
|
|
asyncSpawn m.cleanupChann(result)
|
2020-06-29 15:15:31 +00:00
|
|
|
|
2020-08-04 23:27:59 +00:00
|
|
|
when defined(libp2p_expensive_metrics):
|
|
|
|
libp2p_mplex_channels.set(
|
2020-08-15 05:58:30 +00:00
|
|
|
m.channels[initiator].len.int64,
|
|
|
|
labelValues = [$initiator, $m.connection.peerInfo])
|
2020-06-29 15:15:31 +00:00
|
|
|
|
|
|
|
proc handleStream(m: Mplex, chann: LPChannel) {.async.} =
|
|
|
|
## call the muxer stream handler for this channel
|
|
|
|
##
|
2020-06-24 15:08:44 +00:00
|
|
|
try:
|
|
|
|
await m.streamHandler(chann)
|
|
|
|
trace "finished handling stream"
|
|
|
|
doAssert(chann.closed, "connection not closed by handler!")
|
2020-09-04 16:30:45 +00:00
|
|
|
except CancelledError:
|
|
|
|
trace "Unexpected cancellation in stream handler"
|
2020-06-29 15:15:31 +00:00
|
|
|
await chann.reset()
|
2020-09-04 16:30:45 +00:00
|
|
|
# This is top-level procedure which will work as separate task, so it
|
|
|
|
# do not need to propogate CancelledError.
|
2020-06-24 15:08:44 +00:00
|
|
|
except CatchableError as exc:
|
2020-09-04 16:30:45 +00:00
|
|
|
trace "Exception in mplex stream handler", exc = exc.msg
|
2020-06-24 15:08:44 +00:00
|
|
|
await chann.reset()
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method handle*(m: Mplex) {.async, gcsafe.} =
|
2020-08-15 05:58:30 +00:00
|
|
|
logScope: moid = $m.oid
|
|
|
|
|
|
|
|
trace "starting mplex main loop"
|
2019-09-07 23:34:40 +00:00
|
|
|
try:
|
2020-06-29 15:15:31 +00:00
|
|
|
defer:
|
2020-08-15 05:58:30 +00:00
|
|
|
trace "stopping mplex main loop"
|
2020-06-15 18:10:41 +00:00
|
|
|
await m.close()
|
2020-06-29 15:15:31 +00:00
|
|
|
|
2020-08-03 05:20:11 +00:00
|
|
|
while not m.connection.atEof:
|
2020-08-15 05:58:30 +00:00
|
|
|
trace "waiting for data"
|
|
|
|
let
|
|
|
|
(id, msgType, data) = await m.connection.readMsg()
|
|
|
|
initiator = bool(ord(msgType) and 1)
|
2020-06-29 15:15:31 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
id = id
|
|
|
|
initiator = initiator
|
|
|
|
msgType = msgType
|
|
|
|
size = data.len
|
|
|
|
|
2020-08-15 05:58:30 +00:00
|
|
|
trace "read message from connection", data = data.shortLog
|
|
|
|
|
|
|
|
var channel =
|
|
|
|
if MessageType(msgType) != MessageType.New:
|
|
|
|
let tmp = m.channels[initiator].getOrDefault(id, nil)
|
|
|
|
if tmp == nil:
|
|
|
|
trace "Channel not found, skipping"
|
|
|
|
continue
|
|
|
|
|
|
|
|
tmp
|
|
|
|
else:
|
|
|
|
if m.channels[false].len > m.maxChannCount - 1:
|
2020-09-04 16:30:45 +00:00
|
|
|
warn "too many channels created by remote peer",
|
|
|
|
allowedMax = MaxChannelCount
|
2020-08-05 05:16:04 +00:00
|
|
|
raise newTooManyChannels()
|
|
|
|
|
2020-08-15 05:58:30 +00:00
|
|
|
let name = string.fromBytes(data)
|
|
|
|
m.newStreamInternal(false, id, name, timeout = m.outChannTimeout)
|
2020-06-29 15:15:31 +00:00
|
|
|
|
2020-08-15 05:58:30 +00:00
|
|
|
logScope:
|
|
|
|
name = channel.name
|
|
|
|
oid = $channel.oid
|
|
|
|
|
|
|
|
case msgType:
|
|
|
|
of MessageType.New:
|
|
|
|
trace "created channel"
|
2020-06-29 15:15:31 +00:00
|
|
|
|
|
|
|
if not isNil(m.streamHandler):
|
2020-09-04 16:30:45 +00:00
|
|
|
# Launch handler task
|
|
|
|
# All the errors are handled inside `handleStream()` procedure.
|
|
|
|
asyncSpawn m.handleStream(channel)
|
2020-06-29 15:15:31 +00:00
|
|
|
|
|
|
|
of MessageType.MsgIn, MessageType.MsgOut:
|
|
|
|
if data.len > MaxMsgSize:
|
2020-09-04 16:30:45 +00:00
|
|
|
warn "attempting to send a packet larger than allowed",
|
|
|
|
allowed = MaxMsgSize
|
2020-06-29 15:15:31 +00:00
|
|
|
raise newLPStreamLimitError()
|
2020-08-03 05:20:11 +00:00
|
|
|
|
2020-08-15 05:58:30 +00:00
|
|
|
trace "pushing data to channel"
|
2020-06-29 15:15:31 +00:00
|
|
|
await channel.pushTo(data)
|
2020-08-15 05:58:30 +00:00
|
|
|
trace "pushed data to channel"
|
2020-06-29 15:15:31 +00:00
|
|
|
|
|
|
|
of MessageType.CloseIn, MessageType.CloseOut:
|
|
|
|
trace "closing channel"
|
|
|
|
await channel.closeRemote()
|
2020-08-15 05:58:30 +00:00
|
|
|
trace "closed channel"
|
2020-06-29 15:15:31 +00:00
|
|
|
of MessageType.ResetIn, MessageType.ResetOut:
|
|
|
|
trace "resetting channel"
|
|
|
|
await channel.reset()
|
2020-08-15 05:58:30 +00:00
|
|
|
trace "reset channel"
|
2020-09-04 16:30:45 +00:00
|
|
|
except CancelledError:
|
|
|
|
# This procedure is spawned as task and it is not part of public API, so
|
|
|
|
# there no way for this procedure to be cancelled implicitely.
|
|
|
|
trace "Unexpected cancellation in mplex handler"
|
2019-12-04 04:44:54 +00:00
|
|
|
except CatchableError as exc:
|
2020-06-29 15:15:31 +00:00
|
|
|
trace "Exception occurred", exception = exc.msg, oid = $m.oid
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2020-07-17 18:44:41 +00:00
|
|
|
proc init*(M: type Mplex,
|
|
|
|
conn: Connection,
|
2020-08-05 05:16:04 +00:00
|
|
|
inTimeout, outTimeout: Duration = DefaultChanTimeout,
|
|
|
|
maxChannCount: int = MaxChannelCount): Mplex =
|
2020-07-17 18:44:41 +00:00
|
|
|
M(connection: conn,
|
|
|
|
inChannTimeout: inTimeout,
|
|
|
|
outChannTimeout: outTimeout,
|
2020-08-05 05:16:04 +00:00
|
|
|
oid: genOid(),
|
|
|
|
maxChannCount: maxChannCount)
|
2020-05-20 00:14:15 +00:00
|
|
|
|
2020-02-12 14:37:22 +00:00
|
|
|
method newStream*(m: Mplex,
|
|
|
|
name: string = "",
|
|
|
|
lazy: bool = false): Future[Connection] {.async, gcsafe.} =
|
2020-08-15 05:58:30 +00:00
|
|
|
let channel = m.newStreamInternal(
|
2020-07-17 18:44:41 +00:00
|
|
|
lazy = lazy, timeout = m.inChannTimeout)
|
|
|
|
|
2020-02-11 17:30:36 +00:00
|
|
|
if not lazy:
|
|
|
|
await channel.open()
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
return Connection(channel)
|
2020-05-20 00:14:15 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method close*(m: Mplex) {.async, gcsafe.} =
|
2020-05-20 00:14:15 +00:00
|
|
|
if m.isClosed:
|
|
|
|
return
|
2020-04-11 04:08:25 +00:00
|
|
|
|
2020-08-15 05:58:30 +00:00
|
|
|
trace "closing mplex muxer", moid = $m.oid
|
2020-06-29 15:15:31 +00:00
|
|
|
|
2020-08-15 05:58:30 +00:00
|
|
|
m.isClosed = true
|
|
|
|
|
|
|
|
let channs = toSeq(m.channels[false].values) & toSeq(m.channels[true].values)
|
2020-06-29 15:15:31 +00:00
|
|
|
|
|
|
|
for chann in channs:
|
|
|
|
await chann.reset()
|
|
|
|
|
|
|
|
await m.connection.close()
|
2020-08-15 05:58:30 +00:00
|
|
|
|
|
|
|
# TODO while we're resetting, new channels may be created that will not be
|
|
|
|
# closed properly
|
|
|
|
m.channels[false].clear()
|
|
|
|
m.channels[true].clear()
|