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-09-07 23:34:40 +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,
|
2019-09-24 16:16:39 +00:00
|
|
|
../../varint,
|
|
|
|
../../connection,
|
|
|
|
../../vbuffer,
|
2019-09-06 06:51:19 +00:00
|
|
|
../../protocols/protocol,
|
2019-09-24 16:16:39 +00:00
|
|
|
../../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]
|
2019-09-07 06:20:03 +00:00
|
|
|
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,
|
2019-09-07 06:20:03 +00:00
|
|
|
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
|
|
|
|
2019-09-07 23:34:40 +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-09-07 23:34:40 +00:00
|
|
|
let msgRes = await m.connection.readMsg()
|
|
|
|
if msgRes.isNone:
|
|
|
|
continue
|
2019-09-06 21:27:55 +00:00
|
|
|
|
2019-09-07 23:34:40 +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
|
2019-09-07 23:34:40 +00:00
|
|
|
if MessageType(msgType) != MessageType.New:
|
|
|
|
let channels = m.getChannelList(initiator)
|
|
|
|
if not channels.contains(id):
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "handle: Channel with id and msg type ", id = id, 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-09-23 21:51:28 +00:00
|
|
|
trace "handle: created channel ", id = id, name = name
|
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-09-11 21:06:42 +00:00
|
|
|
let handlerFut = m.streamHandler(stream)
|
2019-09-08 06:34:08 +00:00
|
|
|
|
|
|
|
# channel cleanup routine
|
2019-09-07 23:34:40 +00:00
|
|
|
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) =
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "handle: cleaned up channel ", id = id))
|
2019-09-09 17:33:32 +00:00
|
|
|
handlerFut.addCallback(cleanUpChan)
|
2019-09-07 23:34:40 +00:00
|
|
|
continue
|
|
|
|
of MessageType.MsgIn, MessageType.MsgOut:
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "handle: pushing data to channel ", id = id, msgType = msgType
|
2019-09-07 23:34:40 +00:00
|
|
|
await channel.pushTo(data)
|
|
|
|
of MessageType.CloseIn, MessageType.CloseOut:
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "handle: closing channel ", id = id, msgType = msgType
|
2019-09-07 23:34:40 +00:00
|
|
|
await channel.closedByRemote()
|
|
|
|
m.getChannelList(initiator).del(id)
|
|
|
|
of MessageType.ResetIn, MessageType.ResetOut:
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "handle: resetting channel ", id = id
|
2019-09-07 23:34:40 +00:00
|
|
|
await channel.resetByRemote()
|
|
|
|
break
|
|
|
|
else: raise newMplexUnknownMsgError()
|
2019-09-12 02:10:38 +00:00
|
|
|
except:
|
2019-09-23 21:51:28 +00:00
|
|
|
error "exception occurred", exception = getCurrentExceptionMsg()
|
2019-09-07 23:34:40 +00:00
|
|
|
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()
|
2019-09-07 23:34:40 +00:00
|
|
|
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()
|