From 402067ceae4725007e70c39ad48a6220c13dffbc Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sat, 24 Aug 2019 13:15:05 -0600 Subject: [PATCH] await for void features and add closed flag --- libp2p/chronosstream.nim | 10 ++++++---- libp2p/connection.nim | 11 ++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/libp2p/chronosstream.nim b/libp2p/chronosstream.nim index bbfd9ad..2498ae2 100644 --- a/libp2p/chronosstream.nim +++ b/libp2p/chronosstream.nim @@ -23,12 +23,13 @@ proc newChronosStream*(server: StreamServer, result.client = client result.reader = newAsyncStreamReader(client) result.writer = newAsyncStreamWriter(client) + result.closed = false method read*(s: ChronosStream, n = -1): Future[seq[byte]] {.async.} = result = await s.reader.read(n) method readExactly*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[void] {.async.} = - result = s.readExactly(pbytes, nbytes) + await s.readExactly(pbytes, nbytes) method readLine*(s: ChronosStream, limit = 0, sep = "\r\n"): Future[string] {.async.} = result = await s.reader.readLine(limit, sep) @@ -40,13 +41,13 @@ method readUntil*(s: ChronosStream, pbytes: pointer, nbytes: int, sep: seq[byte] result = await s.reader.readUntil(pbytes, nbytes, sep) method write*(s: ChronosStream, pbytes: pointer, nbytes: int) {.async.} = - result = s.writer.write(pbytes, nbytes) + await s.writer.write(pbytes, nbytes) method write*(s: ChronosStream, msg: string, msglen = -1) {.async.} = - result = s.writer.write(msg, msglen) + await s.writer.write(msg, msglen) method write*(s: ChronosStream, msg: seq[byte], msglen = -1) {.async.} = - result = s.writer.write(msg, msglen) + await s.writer.write(msg, msglen) method close*(s: ChronosStream) {.async.} = await s.reader.closeWait() @@ -57,3 +58,4 @@ method close*(s: ChronosStream) {.async.} = await s.client.closeWait() s.server.stop() s.server.close() + s.closed = true \ No newline at end of file diff --git a/libp2p/connection.nim b/libp2p/connection.nim index 7d765b1..f8b164e 100644 --- a/libp2p/connection.nim +++ b/libp2p/connection.nim @@ -26,7 +26,7 @@ method read*(s: Connection, n = -1): Future[seq[byte]] {.async.} = result = await s.stream.read(n) method readExactly*(s: Connection, pbytes: pointer, nbytes: int): Future[void] {.async.} = - result = s.stream.readExactly(pbytes, nbytes) + await s.stream.readExactly(pbytes, nbytes) method readLine*(s: Connection, limit = 0, sep = "\r\n"): Future[string] {.async.} = result = await s.stream.readLine(limit, sep) @@ -38,16 +38,17 @@ method readUntil*(s: Connection, pbytes: pointer, nbytes: int, sep: seq[byte]): result = await s.stream.readUntil(pbytes, nbytes, sep) method write*(s: Connection, pbytes: pointer, nbytes: int) {.async.} = - result = s.stream.write(pbytes, nbytes) + await s.stream.write(pbytes, nbytes) method write*(s: Connection, msg: string, msglen = -1) {.async.} = - result = s.stream.write(msg, msglen) + await s.stream.write(msg, msglen) method write*(s: Connection, msg: seq[byte], msglen = -1) {.async.} = - result = s.stream.write(msg, msglen) + await s.stream.write(msg, msglen) method close*(s: Connection) {.async.} = - result = s.stream.close() + await s.stream.close() + s.closed = true proc readLp*(s: Connection): Future[seq[byte]] {.async.} = ## read lenght prefixed msg