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-03 02:21:11 +00:00
|
|
|
import chronos, chronicles, stew/byteutils
|
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,
|
2020-05-20 00:14:15 +00:00
|
|
|
../../stream/bufferstream,
|
2020-03-23 06:03:36 +00:00
|
|
|
../../utility,
|
2020-04-11 04:08:25 +00:00
|
|
|
../../errors,
|
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:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "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-05-20 00:14:15 +00:00
|
|
|
remote: Table[uint64, LPChannel]
|
|
|
|
local: Table[uint64, LPChannel]
|
2020-05-23 16:50:05 +00:00
|
|
|
conns: seq[Connection]
|
2020-05-20 00:14:15 +00:00
|
|
|
handlerFuts: seq[Future[void]]
|
2020-03-23 17:14:06 +00:00
|
|
|
currentId*: uint64
|
|
|
|
maxChannels*: uint64
|
2020-05-20 00:14:15 +00:00
|
|
|
isClosed: bool
|
|
|
|
when chronicles.enabledLogLevel == LogLevel.TRACE:
|
|
|
|
oid*: Oid
|
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:
|
2020-05-20 00:14:15 +00:00
|
|
|
trace "picking local channels", initiator = initiator, oid = m.oid
|
2019-09-03 20:40:51 +00:00
|
|
|
result = m.local
|
2019-12-04 04:44:54 +00:00
|
|
|
else:
|
2020-05-20 00:14:15 +00:00
|
|
|
trace "picking remote channels", initiator = initiator, oid = m.oid
|
2019-12-04 04:44:54 +00:00
|
|
|
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,
|
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
|
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,
|
|
|
|
oid = m.oid
|
|
|
|
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-10 20:50:35 +00:00
|
|
|
method handle*(m: Mplex) {.async, gcsafe.} =
|
2020-05-20 00:14:15 +00:00
|
|
|
trace "starting mplex main loop", oid = m.oid
|
2019-09-07 23:34:40 +00:00
|
|
|
try:
|
2020-05-25 14:33:24 +00:00
|
|
|
try:
|
|
|
|
while not m.connection.closed:
|
|
|
|
trace "waiting for data", oid = m.oid
|
|
|
|
let (id, msgType, data) = await m.connection.readMsg()
|
|
|
|
trace "read message from connection", id = id,
|
|
|
|
msgType = msgType,
|
|
|
|
data = data.shortLog,
|
|
|
|
oid = m.oid
|
|
|
|
let initiator = bool(ord(msgType) and 1)
|
|
|
|
var channel: LPChannel
|
|
|
|
if MessageType(msgType) != MessageType.New:
|
|
|
|
let channels = m.getChannelList(initiator)
|
|
|
|
if id notin channels:
|
|
|
|
trace "Channel not found, skipping", id = id,
|
2020-06-15 18:10:41 +00:00
|
|
|
initiator = initiator,
|
|
|
|
msg = msgType,
|
|
|
|
oid = m.oid
|
2020-05-25 14:33:24 +00:00
|
|
|
continue
|
|
|
|
channel = channels[id]
|
|
|
|
|
2020-06-15 18:10:41 +00:00
|
|
|
logScope:
|
|
|
|
id = id
|
|
|
|
initiator = initiator
|
|
|
|
msgType = msgType
|
|
|
|
size = data.len
|
|
|
|
oid = m.oid
|
|
|
|
|
2020-05-25 14:33:24 +00:00
|
|
|
case msgType:
|
|
|
|
of MessageType.New:
|
2020-06-03 02:21:11 +00:00
|
|
|
let name = string.fromBytes(data)
|
2020-05-25 14:33:24 +00:00
|
|
|
channel = await m.newStreamInternal(false, id, name)
|
2020-06-15 18:10:41 +00:00
|
|
|
logScope:
|
|
|
|
name = channel.name
|
|
|
|
chann_iod = channel.oid
|
|
|
|
|
|
|
|
trace "created channel"
|
|
|
|
|
2020-05-25 14:33:24 +00:00
|
|
|
if not isNil(m.streamHandler):
|
|
|
|
let stream = newConnection(channel)
|
|
|
|
m.conns.add(stream)
|
|
|
|
stream.peerInfo = m.connection.peerInfo
|
2020-06-13 01:56:17 +00:00
|
|
|
stream.observedAddr = m.connection.observedAddr
|
2020-05-25 14:33:24 +00:00
|
|
|
|
|
|
|
var fut = newFuture[void]()
|
|
|
|
proc handler() {.async.} =
|
|
|
|
try:
|
|
|
|
await m.streamHandler(stream)
|
2020-06-15 18:10:41 +00:00
|
|
|
trace "finished handling stream"
|
|
|
|
# doAssert(stream.closed, "connection not closed by handler!")
|
2020-05-25 14:33:24 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception in stream handler", exc = exc.msg
|
|
|
|
finally:
|
|
|
|
m.conns.keepItIf(it != stream)
|
|
|
|
m.handlerFuts.keepItIf(it != fut)
|
|
|
|
|
|
|
|
fut = handler()
|
|
|
|
|
|
|
|
of MessageType.MsgIn, MessageType.MsgOut:
|
2020-06-15 18:10:41 +00:00
|
|
|
logScope:
|
|
|
|
name = channel.name
|
|
|
|
chann_iod = channel.oid
|
|
|
|
|
|
|
|
trace "pushing data to channel"
|
2020-05-25 14:33:24 +00:00
|
|
|
|
|
|
|
if data.len > MaxMsgSize:
|
|
|
|
raise newLPStreamLimitError()
|
|
|
|
await channel.pushTo(data)
|
|
|
|
of MessageType.CloseIn, MessageType.CloseOut:
|
2020-06-15 18:10:41 +00:00
|
|
|
logScope:
|
|
|
|
name = channel.name
|
|
|
|
chann_iod = channel.oid
|
|
|
|
|
|
|
|
trace "closing channel"
|
2020-05-25 14:33:24 +00:00
|
|
|
|
|
|
|
await channel.closeRemote()
|
|
|
|
m.getChannelList(initiator).del(id)
|
2020-06-15 18:10:41 +00:00
|
|
|
trace "deleted channel"
|
2020-05-25 14:33:24 +00:00
|
|
|
of MessageType.ResetIn, MessageType.ResetOut:
|
2020-06-15 18:10:41 +00:00
|
|
|
logScope:
|
|
|
|
name = channel.name
|
|
|
|
chann_iod = channel.oid
|
|
|
|
|
|
|
|
trace "resetting channel"
|
2020-05-25 14:33:24 +00:00
|
|
|
|
|
|
|
await channel.reset()
|
|
|
|
m.getChannelList(initiator).del(id)
|
2020-06-15 18:10:41 +00:00
|
|
|
trace "deleted channel"
|
2020-05-25 14:33:24 +00:00
|
|
|
break
|
|
|
|
finally:
|
2020-06-15 18:10:41 +00:00
|
|
|
trace "stopping mplex main loop", oid = m.oid
|
|
|
|
await m.close()
|
2019-12-04 04:44:54 +00:00
|
|
|
except CatchableError as exc:
|
2020-05-20 00:14:15 +00:00
|
|
|
trace "Exception occurred", exception = exc.msg, oid = m.oid
|
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
|
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-05-20 00:14:15 +00:00
|
|
|
when chronicles.enabledLogLevel == LogLevel.TRACE:
|
|
|
|
result.oid = genOid()
|
|
|
|
|
|
|
|
proc cleanupChann(m: Mplex, chann: LPChannel) {.async, inline.} =
|
|
|
|
## remove the local channel from the internal tables
|
|
|
|
##
|
|
|
|
await chann.closeEvent.wait()
|
|
|
|
if not isNil(chann):
|
|
|
|
m.getChannelList(true).del(chann.id)
|
|
|
|
trace "cleaned up channel", id = chann.id
|
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
|
2020-06-13 01:56:17 +00:00
|
|
|
result.observedAddr = m.connection.observedAddr
|
2019-09-03 20:40:51 +00:00
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
asyncCheck m.cleanupChann(channel)
|
|
|
|
|
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-05-27 20:46:25 +00:00
|
|
|
try:
|
|
|
|
trace "closing mplex muxer", oid = m.oid
|
2020-06-02 17:32:42 +00:00
|
|
|
let channs = toSeq(m.remote.values) &
|
|
|
|
toSeq(m.local.values)
|
|
|
|
|
|
|
|
for chann in channs:
|
|
|
|
try:
|
|
|
|
await chann.reset()
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "error resetting channel", exc = exc.msg
|
|
|
|
|
|
|
|
for conn in m.conns:
|
|
|
|
try:
|
|
|
|
await conn.close()
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "error closing channel's connection"
|
|
|
|
|
|
|
|
checkFutures(
|
|
|
|
await allFinished(m.handlerFuts))
|
2020-05-27 20:46:25 +00:00
|
|
|
|
2020-05-25 14:33:24 +00:00
|
|
|
await m.connection.close()
|
2020-06-02 17:32:42 +00:00
|
|
|
finally:
|
2020-05-27 20:46:25 +00:00
|
|
|
m.remote.clear()
|
|
|
|
m.local.clear()
|
|
|
|
m.conns = @[]
|
|
|
|
m.handlerFuts = @[]
|
|
|
|
m.isClosed = true
|