fix: ls and other minor issues

This commit is contained in:
Dmitriy Ryajov 2019-08-25 13:08:25 -06:00
parent 139b8a752b
commit 187645013f
1 changed files with 13 additions and 8 deletions

View File

@ -13,9 +13,9 @@ import connection, varint, vbuffer
const MsgSize* = 64*1024 const MsgSize* = 64*1024
const Codec* = "/multistream/1.0.0" const Codec* = "/multistream/1.0.0"
const MultiCodec* = "\x19" & Codec & "\n" const MultiCodec* = "\x13" & Codec & "\n"
const Na = "\x3na\n" const Na = "\x03na\n"
const Ls = "\x3ls\n" const Ls = "\x03ls\n"
type type
MultisteamSelectException = object of CatchableError MultisteamSelectException = object of CatchableError
@ -65,10 +65,13 @@ proc list*(m: MultisteamSelect, conn: Connection): Future[seq[string]] {.async.}
if not (await m.select(conn)): if not (await m.select(conn)):
return return
await conn.write(m.ls) # send ls
var list = newSeq[string]() var list = newSeq[string]()
let ms = cast[string](await conn.readLp()) let ms = cast[string](await conn.readLp())
for s in ms.split("\n"): for s in ms.split("\n"):
list.add(s) if s.len() > 0:
list.add(s)
result = list result = list
@ -88,16 +91,18 @@ proc handle*(m: MultisteamSelect, conn: Connection) {.async.} =
continue continue
case ms: case ms:
of "ls": of "ls":
var protos = ""
for h in m.handlers: for h in m.handlers:
await conn.writeLp(h.proto & "\n") protos &= (h.proto & "\n")
await conn.writeLp(cast[seq[byte]](toSeq(protos.items)))
else: else:
for h in m.handlers: for h in m.handlers:
if (not isNil(h.match) and h.match(ms)) or ms == h.proto: if (not isNil(h.match) and h.match(ms)) or ms == h.proto:
await conn.writeLp(h.proto & "\n")
await h.handler(conn, ms) await h.handler(conn, ms)
return return
else: await conn.write(m.na)
await conn.write(m.na)
proc addHandler*(m: MultisteamSelect, proc addHandler*(m: MultisteamSelect,
proto: string, proto: string,