80 lines
2.4 KiB
Nim
Raw Normal View History

2019-09-03 21:08:51 -06:00
## Nim-LibP2P
2019-09-24 11:48:23 -06:00
## Copyright (c) 2019 Status Research & Development GmbH
2019-09-03 21:08:51 -06: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.
import chronos
2020-06-02 20:21:11 -06:00
import nimcrypto/utils, chronicles, stew/byteutils
2019-09-09 11:33:32 -06:00
import types,
../../stream/connection,
2020-05-08 22:58:23 +02:00
../../utility,
2019-09-12 11:07:34 -06:00
../../varint,
../../vbuffer
2019-09-03 21:08:51 -06:00
2019-09-09 20:15:52 -06:00
logScope:
topics = "mplexcoder"
2019-09-09 20:15:52 -06:00
2019-09-06 15:27:55 -06:00
type
Msg* = tuple
2020-03-23 11:14:06 -06:00
id: uint64
2019-09-07 17:34:11 -06:00
msgType: MessageType
data: seq[byte]
2019-09-06 15:27:55 -06:00
InvalidMplexMsgType = object of CatchableError
proc newInvalidMplexMsgType*(): ref InvalidMplexMsgType =
newException(InvalidMplexMsgType, "invalid message type")
2020-02-12 09:37:22 -05:00
proc readMsg*(conn: Connection): Future[Msg] {.async, gcsafe.} =
2020-05-08 22:58:23 +02:00
let header = await conn.readVarint()
trace "read header varint", varint = header
2019-09-07 17:34:11 -06:00
2020-05-08 22:58:23 +02:00
let data = await conn.readLp(MaxMsgSize)
trace "read data", dataLen = data.len, data = shortLog(data)
2019-09-07 17:34:11 -06:00
let msgType = header and 0x7
if msgType.int > ord(MessageType.ResetOut):
raise newInvalidMplexMsgType()
2020-05-08 22:58:23 +02:00
result = (header shr 3, MessageType(msgType), data)
proc writeMsg*(conn: Connection,
2020-03-23 11:14:06 -06:00
id: uint64,
msgType: MessageType,
2019-09-07 17:34:11 -06:00
data: seq[byte] = @[]) {.async, gcsafe.} =
trace "sending data over mplex", id,
msgType,
2020-03-11 17:04:40 -06:00
data = data.len
2020-05-23 10:50:05 -06: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
2020-05-29 10:25:25 -06:00
## write length prefixed
2020-05-23 10:50:05 -06:00
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
await conn.write(buf.buffer & chunk)
2020-05-23 10:50:05 -06:00
if data.len == 0:
return
2019-09-07 17:34:11 -06:00
proc writeMsg*(conn: Connection,
2020-03-23 11:14:06 -06:00
id: uint64,
msgType: MessageType,
2019-09-07 17:34:11 -06:00
data: string) {.async, gcsafe.} =
# TODO: changing this to
2020-06-02 20:21:11 -06:00
#`await conn.writeMsg(id, msgType, data.toBytes())`
# causes all sorts of race conditions and hangs.
# DON'T DO IT!
2020-06-02 20:21:11 -06:00
result = conn.writeMsg(id, msgType, data.toBytes())