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.
|
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
import chronos, options
|
2019-09-09 17:33:32 +00:00
|
|
|
import nimcrypto/utils, chronicles
|
|
|
|
import types,
|
2019-09-12 17:07:34 +00:00
|
|
|
../../connection,
|
|
|
|
../../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
|
|
|
|
id: uint
|
2019-09-07 23:34:11 +00:00
|
|
|
msgType: MessageType
|
|
|
|
data: seq[byte]
|
2019-09-06 21:27:55 +00:00
|
|
|
|
2019-09-07 23:34:11 +00:00
|
|
|
proc readMplexVarint(conn: Connection): Future[Option[uint]] {.async, gcsafe.} =
|
2019-09-04 03:08:51 +00:00
|
|
|
var
|
2019-09-07 06:20:03 +00:00
|
|
|
varint: uint
|
2019-09-04 03:08:51 +00:00
|
|
|
length: int
|
|
|
|
res: VarintStatus
|
2019-12-04 04:44:54 +00:00
|
|
|
buffer = newSeq[byte](10)
|
|
|
|
|
2019-09-07 23:34:11 +00:00
|
|
|
result = none(uint)
|
2019-09-04 03:08:51 +00:00
|
|
|
try:
|
|
|
|
for i in 0..<len(buffer):
|
2019-12-06 02:16:18 +00:00
|
|
|
await conn.readExactly(addr buffer[i], 1)
|
|
|
|
res = PB.getUVarint(buffer.toOpenArray(0, i), length, varint)
|
|
|
|
if res == VarintStatus.Success:
|
|
|
|
return some(varint)
|
2019-09-04 03:08:51 +00:00
|
|
|
if res != VarintStatus.Success:
|
2019-09-25 22:56:53 +00:00
|
|
|
raise newInvalidVarintException()
|
2019-12-04 04:44:54 +00:00
|
|
|
except LPStreamIncompleteError as exc:
|
|
|
|
trace "unable to read varint", exc = exc.msg
|
2019-09-04 03:08:51 +00:00
|
|
|
|
2020-02-12 14:37:22 +00:00
|
|
|
proc readMsg*(conn: Connection): Future[Msg] {.async, gcsafe.} =
|
2019-09-07 23:34:11 +00:00
|
|
|
let headerVarint = await conn.readMplexVarint()
|
2020-02-12 14:37:22 +00:00
|
|
|
trace "read header varint", varint = headerVarint
|
2019-09-07 23:34:11 +00:00
|
|
|
|
|
|
|
let dataLenVarint = await conn.readMplexVarint()
|
2020-02-12 14:37:22 +00:00
|
|
|
trace "read data len varing", varint = dataLenVarint
|
2019-09-07 06:20:03 +00:00
|
|
|
var data: seq[byte]
|
2020-02-12 14:37:22 +00:00
|
|
|
if dataLenVarint.int > 0:
|
|
|
|
data = await conn.read(dataLenVarint.int)
|
|
|
|
trace "read data", data = data
|
2019-09-07 23:34:11 +00:00
|
|
|
|
2020-02-12 14:37:22 +00:00
|
|
|
let header = headerVarint
|
|
|
|
result = (header shr 3, MessageType(header and 0x7), data)
|
2019-09-07 06:20:03 +00:00
|
|
|
|
|
|
|
proc writeMsg*(conn: Connection,
|
2019-09-07 23:34:11 +00:00
|
|
|
id: uint,
|
2019-12-10 20:50:35 +00:00
|
|
|
msgType: MessageType,
|
2019-09-07 23:34:11 +00:00
|
|
|
data: seq[byte] = @[]) {.async, gcsafe.} =
|
2019-09-04 03:08:51 +00:00
|
|
|
## write lenght prefixed
|
|
|
|
var buf = initVBuffer()
|
2019-12-04 04:44:54 +00:00
|
|
|
buf.writePBVarint(id shl 3 or ord(msgType).uint)
|
|
|
|
buf.writePBVarint(data.len().uint) # size should be always sent
|
2019-09-04 03:08:51 +00:00
|
|
|
buf.finish()
|
2019-12-04 04:44:54 +00:00
|
|
|
try:
|
|
|
|
await conn.write(buf.buffer & data)
|
|
|
|
except LPStreamIncompleteError as exc:
|
|
|
|
trace "unable to send message", exc = exc.msg
|
2019-09-07 23:34:11 +00:00
|
|
|
|
|
|
|
proc writeMsg*(conn: Connection,
|
|
|
|
id: uint,
|
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))
|