2019-09-04 03:08:51 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-09-04 03:08: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-02-12 14:38:19 +00:00
|
|
|
import chronos
|
2019-09-09 17:33:32 +00:00
|
|
|
import nimcrypto/utils, chronicles
|
|
|
|
import types,
|
2019-09-12 17:07:34 +00:00
|
|
|
../../connection,
|
2020-05-08 20:58:23 +00:00
|
|
|
../../utility,
|
2019-09-12 17:07:34 +00:00
|
|
|
../../varint,
|
|
|
|
../../vbuffer,
|
2019-09-09 17:33:32 +00:00
|
|
|
../../stream/lpstream
|
2019-09-04 03:08:51 +00:00
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
logScope:
|
2019-09-28 19:54:52 +00:00
|
|
|
topic = "MplexCoder"
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2019-09-06 21:27:55 +00:00
|
|
|
type
|
2019-09-25 22:56:53 +00:00
|
|
|
Msg* = tuple
|
2020-03-23 17:14:06 +00:00
|
|
|
id: uint64
|
2019-09-07 23:34:11 +00:00
|
|
|
msgType: MessageType
|
|
|
|
data: seq[byte]
|
2019-09-06 21:27:55 +00:00
|
|
|
|
2020-03-29 14:28:48 +00:00
|
|
|
InvalidMplexMsgType = object of CatchableError
|
|
|
|
|
|
|
|
proc newInvalidMplexMsgType*(): ref InvalidMplexMsgType =
|
|
|
|
newException(InvalidMplexMsgType, "invalid message type")
|
|
|
|
|
2020-02-12 14:37:22 +00:00
|
|
|
proc readMsg*(conn: Connection): Future[Msg] {.async, gcsafe.} =
|
2020-05-08 20:58:23 +00:00
|
|
|
let header = await conn.readVarint()
|
2020-03-29 14:28:48 +00:00
|
|
|
trace "read header varint", varint = header
|
2019-09-07 23:34:11 +00:00
|
|
|
|
2020-05-08 20:58:23 +00:00
|
|
|
let data = await conn.readLp(MaxMsgSize)
|
|
|
|
trace "read data", dataLen = data.len, data = shortLog(data)
|
2019-09-07 23:34:11 +00:00
|
|
|
|
2020-03-29 14:28:48 +00:00
|
|
|
let msgType = header and 0x7
|
|
|
|
if msgType.int > ord(MessageType.ResetOut):
|
|
|
|
raise newInvalidMplexMsgType()
|
|
|
|
|
2020-05-08 20:58:23 +00:00
|
|
|
result = (header shr 3, MessageType(msgType), data)
|
2019-09-07 06:20:03 +00:00
|
|
|
|
|
|
|
proc writeMsg*(conn: Connection,
|
2020-03-23 17:14:06 +00:00
|
|
|
id: uint64,
|
2019-12-10 20:50:35 +00:00
|
|
|
msgType: MessageType,
|
2019-09-07 23:34:11 +00:00
|
|
|
data: seq[byte] = @[]) {.async, gcsafe.} =
|
2020-04-03 15:26:46 +00:00
|
|
|
trace "sending data over mplex", id,
|
2020-03-11 13:34:35 +00:00
|
|
|
msgType,
|
2020-03-11 23:04:40 +00:00
|
|
|
data = data.len
|
2020-04-03 15:26:46 +00:00
|
|
|
var
|
|
|
|
left = data.len
|
|
|
|
offset = 0
|
|
|
|
while left > 0 or data.len == 0:
|
|
|
|
let
|
|
|
|
chunkSize = if left > MaxMsgSize: MaxMsgSize - 64 else: left
|
|
|
|
chunk = if chunkSize > 0 : data[offset..(offset + chunkSize - 1)] else: data
|
|
|
|
## write lenght prefixed
|
|
|
|
var buf = initVBuffer()
|
|
|
|
buf.writePBVarint(id shl 3 or ord(msgType).uint64)
|
|
|
|
buf.writePBVarint(chunkSize.uint64) # size should be always sent
|
|
|
|
buf.finish()
|
|
|
|
left = left - chunkSize
|
|
|
|
offset = offset + chunkSize
|
|
|
|
try:
|
|
|
|
await conn.write(buf.buffer & chunk)
|
|
|
|
except LPStreamIncompleteError as exc:
|
|
|
|
trace "unable to send message", exc = exc.msg
|
|
|
|
if data.len == 0:
|
|
|
|
return
|
2019-09-07 23:34:11 +00:00
|
|
|
|
|
|
|
proc writeMsg*(conn: Connection,
|
2020-03-23 17:14:06 +00:00
|
|
|
id: uint64,
|
2019-12-10 20:50:35 +00:00
|
|
|
msgType: MessageType,
|
2019-09-07 23:34:11 +00:00
|
|
|
data: string) {.async, gcsafe.} =
|
2019-09-12 02:10:38 +00:00
|
|
|
result = conn.writeMsg(id, msgType, cast[seq[byte]](data))
|