await for void features and add closed flag

This commit is contained in:
Dmitriy Ryajov 2019-08-24 13:15:05 -06:00
parent e13f42f9bb
commit 402067ceae
2 changed files with 12 additions and 9 deletions

View File

@ -23,12 +23,13 @@ proc newChronosStream*(server: StreamServer,
result.client = client result.client = client
result.reader = newAsyncStreamReader(client) result.reader = newAsyncStreamReader(client)
result.writer = newAsyncStreamWriter(client) result.writer = newAsyncStreamWriter(client)
result.closed = false
method read*(s: ChronosStream, n = -1): Future[seq[byte]] {.async.} = method read*(s: ChronosStream, n = -1): Future[seq[byte]] {.async.} =
result = await s.reader.read(n) result = await s.reader.read(n)
method readExactly*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[void] {.async.} = 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.} = method readLine*(s: ChronosStream, limit = 0, sep = "\r\n"): Future[string] {.async.} =
result = await s.reader.readLine(limit, sep) 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) result = await s.reader.readUntil(pbytes, nbytes, sep)
method write*(s: ChronosStream, pbytes: pointer, nbytes: int) {.async.} = 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.} = 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.} = 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.} = method close*(s: ChronosStream) {.async.} =
await s.reader.closeWait() await s.reader.closeWait()
@ -57,3 +58,4 @@ method close*(s: ChronosStream) {.async.} =
await s.client.closeWait() await s.client.closeWait()
s.server.stop() s.server.stop()
s.server.close() s.server.close()
s.closed = true

View File

@ -26,7 +26,7 @@ method read*(s: Connection, n = -1): Future[seq[byte]] {.async.} =
result = await s.stream.read(n) result = await s.stream.read(n)
method readExactly*(s: Connection, pbytes: pointer, nbytes: int): Future[void] {.async.} = 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.} = method readLine*(s: Connection, limit = 0, sep = "\r\n"): Future[string] {.async.} =
result = await s.stream.readLine(limit, sep) 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) result = await s.stream.readUntil(pbytes, nbytes, sep)
method write*(s: Connection, pbytes: pointer, nbytes: int) {.async.} = 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.} = 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.} = 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.} = method close*(s: Connection) {.async.} =
result = s.stream.close() await s.stream.close()
s.closed = true
proc readLp*(s: Connection): Future[seq[byte]] {.async.} = proc readLp*(s: Connection): Future[seq[byte]] {.async.} =
## read lenght prefixed msg ## read lenght prefixed msg