2019-08-22 21:35:47 +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.
|
|
|
|
|
|
|
|
import chronos
|
2019-08-27 21:45:21 +00:00
|
|
|
import stream
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2019-08-27 21:45:21 +00:00
|
|
|
type ChronosStream* = ref object of LPStream
|
2019-08-22 21:35:47 +00:00
|
|
|
reader: AsyncStreamReader
|
|
|
|
writer: AsyncStreamWriter
|
|
|
|
server: StreamServer
|
|
|
|
client: StreamTransport
|
|
|
|
|
|
|
|
proc newChronosStream*(server: StreamServer,
|
|
|
|
client: StreamTransport): ChronosStream =
|
|
|
|
new result
|
|
|
|
result.server = server
|
|
|
|
result.client = client
|
|
|
|
result.reader = newAsyncStreamReader(client)
|
|
|
|
result.writer = newAsyncStreamWriter(client)
|
2019-08-24 19:15:05 +00:00
|
|
|
result.closed = false
|
2019-08-22 21:35:47 +00:00
|
|
|
|
|
|
|
method read*(s: ChronosStream, n = -1): Future[seq[byte]] {.async.} =
|
|
|
|
result = await s.reader.read(n)
|
|
|
|
|
2019-08-28 19:02:25 +00:00
|
|
|
method readExactly*(s: ChronosStream,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int): Future[void] {.async.} =
|
2019-08-25 19:07:24 +00:00
|
|
|
await s.reader.readExactly(pbytes, nbytes)
|
2019-08-22 21:35:47 +00:00
|
|
|
|
|
|
|
method readLine*(s: ChronosStream, limit = 0, sep = "\r\n"): Future[string] {.async.} =
|
|
|
|
result = await s.reader.readLine(limit, sep)
|
|
|
|
|
|
|
|
method readOnce*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[int] {.async.} =
|
|
|
|
result = await s.reader.readOnce(pbytes, nbytes)
|
|
|
|
|
2019-08-28 19:02:25 +00:00
|
|
|
method readUntil*(s: ChronosStream,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int,
|
|
|
|
sep: seq[byte]): Future[int] {.async.} =
|
2019-08-22 21:35:47 +00:00
|
|
|
result = await s.reader.readUntil(pbytes, nbytes, sep)
|
|
|
|
|
|
|
|
method write*(s: ChronosStream, pbytes: pointer, nbytes: int) {.async.} =
|
2019-08-24 19:15:05 +00:00
|
|
|
await s.writer.write(pbytes, nbytes)
|
2019-08-22 21:35:47 +00:00
|
|
|
|
|
|
|
method write*(s: ChronosStream, msg: string, msglen = -1) {.async.} =
|
2019-08-24 19:15:05 +00:00
|
|
|
await s.writer.write(msg, msglen)
|
2019-08-22 21:35:47 +00:00
|
|
|
|
|
|
|
method write*(s: ChronosStream, msg: seq[byte], msglen = -1) {.async.} =
|
2019-08-24 19:15:05 +00:00
|
|
|
await s.writer.write(msg, msglen)
|
2019-08-22 21:35:47 +00:00
|
|
|
|
|
|
|
method close*(s: ChronosStream) {.async.} =
|
|
|
|
await s.reader.closeWait()
|
|
|
|
|
|
|
|
await s.writer.finish()
|
|
|
|
await s.writer.closeWait()
|
|
|
|
|
|
|
|
await s.client.closeWait()
|
2019-08-24 19:15:05 +00:00
|
|
|
s.closed = true
|