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

127 lines
4.4 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, options, strformat
2019-09-09 17:33:32 +00:00
import chronos, chronicles
2019-09-12 17:07:34 +00:00
import coder, types, lpchannel,
2019-09-09 17:33:32 +00:00
../muxer,
../../varint,
../../connection,
../../vbuffer,
2019-09-06 06:51:19 +00:00
../../protocols/protocol,
../../stream/bufferstream,
2019-09-09 17:33:32 +00:00
../../stream/lpstream
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
2019-09-12 17:07:34 +00:00
remote*: Table[uint, LPChannel]
local*: Table[uint, LPChannel]
currentId*: uint
2019-09-03 20:40:51 +00:00
maxChannels*: uint
2019-09-04 03:08:51 +00:00
proc newMplexUnknownMsgError(): ref MplexUnknownMsgError =
2019-09-03 20:40:51 +00:00
result = newException(MplexUnknownMsgError, "Unknown mplex message type")
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-09-03 20:40:51 +00:00
result = m.remote
else:
result = m.local
2019-09-04 01:42:00 +00:00
proc newStreamInternal*(m: Mplex,
initiator: bool = true,
chanId: uint = 0,
2019-09-06 21:27:55 +00:00
name: string = ""):
2019-09-12 17:07:34 +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-09-06 21:27:55 +00:00
result = newChannel(id, m.connection, initiator, name)
2019-09-04 01:42:00 +00:00
m.getChannelList(initiator)[id] = result
2019-09-03 20:40:51 +00:00
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:
let msgRes = await m.connection.readMsg()
if msgRes.isNone:
continue
2019-09-06 21:27:55 +00:00
let (id, msgType, data) = msgRes.get()
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 not channels.contains(id):
trace "handle: Channel with id and msg type ", id = id, 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 "handle: created channel ", id = id, name = name
if not isNil(m.streamHandler):
2019-09-11 19:04:27 +00:00
let stream = newConnection(channel)
stream.peerInfo = m.connection.peerInfo
let handlerFut = m.streamHandler(stream)
2019-09-08 06:34:08 +00:00
# channel cleanup routine
proc cleanUpChan(udata: pointer) {.gcsafe.} =
if handlerFut.finished:
channel.close().addCallback(
proc(udata: pointer) =
channel.cleanUp()
2019-09-09 17:33:32 +00:00
.addCallback(proc(udata: pointer) =
trace "handle: cleaned up channel ", id = id))
2019-09-09 17:33:32 +00:00
handlerFut.addCallback(cleanUpChan)
continue
of MessageType.MsgIn, MessageType.MsgOut:
trace "handle: pushing data to channel ", id = id, msgType = msgType
await channel.pushTo(data)
of MessageType.CloseIn, MessageType.CloseOut:
trace "handle: closing channel ", id = id, msgType = msgType
await channel.closedByRemote()
m.getChannelList(initiator).del(id)
of MessageType.ResetIn, MessageType.ResetOut:
trace "handle: resetting channel ", id = id
await channel.resetByRemote()
break
else: raise newMplexUnknownMsgError()
2019-09-12 02:10:38 +00:00
except:
error "exception occurred", exception = getCurrentExceptionMsg()
finally:
await m.connection.close()
2019-09-03 20:40:51 +00:00
proc newMplex*(conn: Connection,
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-09-06 21:27:55 +00:00
method newStream*(m: Mplex, name: string = ""): Future[Connection] {.async, gcsafe.} =
2019-09-04 01:42:00 +00:00
let channel = await m.newStreamInternal()
await m.connection.writeMsg(channel.id, MessageType.New, name)
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-09-04 01:42:00 +00:00
method close*(m: Mplex) {.async, gcsafe.} =
await allFutures(@[allFutures(toSeq(m.remote.values).mapIt(it.close())),
allFutures(toSeq(m.local.values).mapIt(it.close()))])
2019-09-04 16:40:05 +00:00
m.connection.reset()