fixing several issues found while testing

This commit is contained in:
Dmitriy Ryajov 2019-09-06 00:49:01 -06:00
parent dee46c4d37
commit b7f999d316
2 changed files with 37 additions and 61 deletions

View File

@ -7,7 +7,7 @@
## This file may not be copied, modified, or distributed except according to
## those terms.
import sequtils, strutils
import sequtils, strutils, strformat
import chronos
import connection,
varint,
@ -73,17 +73,18 @@ proc select*(m: MultisteamSelect,
proc select*(m: MultisteamSelect,
conn: Connection,
proto: string): Future[string] =
result = if proto.len > 0: m.select(conn, @[proto]) else: m.select(conn, @[])
proto: string): Future[bool] {.async.} =
result = if proto.len > 0:
(await m.select(conn, @[proto])) == proto
else:
(await m.select(conn, @[])) == Codec
proc select*(m: MultisteamSelect,
conn: Connection): Future[string] =
result = m.select(conn, @[])
proc select*(m: MultisteamSelect, conn: Connection): Future[bool] = m.select(conn, "")
proc list*(m: MultisteamSelect,
conn: Connection): Future[seq[string]] {.async.} =
## list remote protos requests on connection
if (await m.select(conn)).len == 0:
if not await m.select(conn):
return
await conn.write(m.ls) # send ls
@ -97,12 +98,10 @@ proc list*(m: MultisteamSelect,
result = list
proc handle*(m: MultisteamSelect, conn: Connection) {.async, gcsafe.} =
## handle requests on connection
if (await m.select(conn)).len == 0:
return
while not conn.closed:
block main:
var ms = cast[string](await conn.readLp())
echo ms
ms.removeSuffix("\n")
if ms.len() <= 0:
await conn.write(m.na)
@ -116,13 +115,19 @@ proc handle*(m: MultisteamSelect, conn: Connection) {.async, gcsafe.} =
var protos = ""
for h in m.handlers:
protos &= (h.proto & "\n")
await conn.writeLp(cast[seq[byte]](toSeq(protos.items)))
await conn.writeLp(protos)
of Codec:
await conn.write(m.codec)
else:
for h in m.handlers:
if (not isNil(h.match) and h.match(ms)) or ms == h.proto:
echo h.proto
await conn.writeLp((h.proto & "\n"))
try:
await h.protocol.handler(conn, ms)
return
break main
except Exception as exc:
echo exc.msg
await conn.write(m.na)
proc addHandler*[T: LPProtocol](m: MultisteamSelect,

View File

@ -1,29 +0,0 @@
## 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.
import chronos
import protocol
import ../connection
const PlainTextCodec* = "/plaintext/1.0.0"
type
PlainText* = ref object of LPProtocol
method init(p: PlainText) {.gcsafe.} =
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
let msg = await conn.readLp()
await conn.writeLp(msg)
p.codec = PlainTextCodec
p.handler = handle
proc newPlainText*(): PlainText =
new result
result.init()