2019-09-04 03:08:51 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
## 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-07 23:34:11 +00:00
|
|
|
import chronos, options, sequtils
|
2019-09-06 06:51:19 +00:00
|
|
|
import types,
|
|
|
|
../../connection,
|
|
|
|
../../varint,
|
|
|
|
../../vbuffer,
|
|
|
|
../../stream/lpstream,
|
|
|
|
nimcrypto/utils
|
2019-09-04 03:08:51 +00:00
|
|
|
|
2019-09-06 21:27:55 +00:00
|
|
|
type
|
2019-09-07 23:34:11 +00:00
|
|
|
Msg* = tuple
|
|
|
|
id: uint
|
|
|
|
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
|
|
|
|
var 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):
|
|
|
|
await conn.readExactly(addr buffer[i], 1)
|
2019-09-07 06:20:03 +00:00
|
|
|
res = LP.getUVarint(buffer.toOpenArray(0, i), length, varint)
|
2019-09-04 03:08:51 +00:00
|
|
|
if res == VarintStatus.Success:
|
2019-09-07 23:34:11 +00:00
|
|
|
return some(varint)
|
2019-09-04 03:08:51 +00:00
|
|
|
if res != VarintStatus.Success:
|
|
|
|
buffer.setLen(0)
|
|
|
|
return
|
2019-09-07 06:20:03 +00:00
|
|
|
except TransportIncompleteError, AsyncStreamIncompleteError:
|
2019-09-04 03:08:51 +00:00
|
|
|
buffer.setLen(0)
|
|
|
|
|
2019-09-07 23:34:11 +00:00
|
|
|
proc readMsg*(conn: Connection): Future[Option[Msg]] {.async, gcsafe.} =
|
|
|
|
let headerVarint = await conn.readMplexVarint()
|
|
|
|
if headerVarint.isNone:
|
|
|
|
return
|
|
|
|
|
|
|
|
let dataLenVarint = await conn.readMplexVarint()
|
2019-09-07 06:20:03 +00:00
|
|
|
var data: seq[byte]
|
2019-09-07 23:34:11 +00:00
|
|
|
if dataLenVarint.isSome and dataLenVarint.get() > 0.uint:
|
|
|
|
data = await conn.read(dataLenVarint.get().int)
|
|
|
|
|
|
|
|
let header = headerVarint.get()
|
|
|
|
result = some((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,
|
|
|
|
msgType: MessageType,
|
|
|
|
data: seq[byte] = @[]) {.async, gcsafe.} =
|
2019-09-04 03:08:51 +00:00
|
|
|
## write lenght prefixed
|
|
|
|
var buf = initVBuffer()
|
2019-09-07 23:34:11 +00:00
|
|
|
let header = (id shl 3 or ord(msgType).uint)
|
|
|
|
buf.writeVarint(id shl 3 or ord(msgType).uint)
|
2019-09-07 06:20:03 +00:00
|
|
|
buf.writeVarint(data.len().uint) # size should be always sent
|
2019-09-04 03:08:51 +00:00
|
|
|
buf.finish()
|
2019-09-07 06:20:03 +00:00
|
|
|
await conn.write(buf.buffer & data)
|
2019-09-07 23:34:11 +00:00
|
|
|
|
|
|
|
proc writeMsg*(conn: Connection,
|
|
|
|
id: uint,
|
|
|
|
msgType: MessageType,
|
|
|
|
data: string) {.async, gcsafe.} =
|
|
|
|
result = conn.writeMsg(id, msgType, cast[seq[byte]](toSeq(data.items)))
|