nim-libp2p/libp2p/stream/chronosstream.nim

110 lines
3.5 KiB
Nim
Raw Normal View History

## Nim-LibP2P
2019-09-24 11:48:23 -06:00
## Copyright (c) 2019 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.
2019-09-11 20:10:38 -06:00
import chronos, chronicles
2019-09-01 11:31:24 -06:00
import lpstream
2019-09-11 20:10:38 -06:00
logScope:
topic = "ChronosStream"
2019-08-27 15:45:21 -06:00
type ChronosStream* = ref object of LPStream
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)
result.closed = false
2019-08-30 09:28:07 -06:00
method read*(s: ChronosStream, n = -1): Future[seq[byte]] {.async, gcsafe.} =
2019-09-08 11:58:19 -06:00
try:
result = await s.reader.read(n)
except AsyncStreamReadError as exc:
raise newLPStreamReadError(exc.par)
2019-08-28 13:02:25 -06:00
method readExactly*(s: ChronosStream,
pbytes: pointer,
2019-08-30 09:28:07 -06:00
nbytes: int): Future[void] {.async, gcsafe.} =
2019-09-08 11:58:19 -06:00
try:
await s.reader.readExactly(pbytes, nbytes)
2019-09-09 17:08:30 -06:00
except AsyncStreamIncompleteError:
2019-09-08 11:58:19 -06:00
raise newLPStreamIncompleteError()
except AsyncStreamReadError as exc:
raise newLPStreamReadError(exc.par)
2019-08-30 09:28:07 -06:00
method readLine*(s: ChronosStream, limit = 0, sep = "\r\n"): Future[string] {.async, gcsafe.} =
2019-09-25 12:42:52 -06:00
try:
result = await s.reader.readLine(limit, sep)
except AsyncStreamReadError as exc:
raise newLPStreamReadError(exc.par)
2019-08-30 09:28:07 -06:00
method readOnce*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[int] {.async, gcsafe.} =
2019-09-25 12:42:52 -06:00
try:
result = await s.reader.readOnce(pbytes, nbytes)
except AsyncStreamReadError as exc:
raise newLPStreamReadError(exc.par)
2019-08-28 13:02:25 -06:00
method readUntil*(s: ChronosStream,
pbytes: pointer,
nbytes: int,
2019-08-30 09:28:07 -06:00
sep: seq[byte]): Future[int] {.async, gcsafe.} =
2019-09-08 11:58:19 -06:00
try:
result = await s.reader.readUntil(pbytes, nbytes, sep)
2019-09-25 12:42:52 -06:00
except AsyncStreamIncompleteError:
2019-09-08 11:58:19 -06:00
raise newLPStreamIncompleteError()
2019-09-25 12:42:52 -06:00
except AsyncStreamLimitError:
2019-09-08 11:58:19 -06:00
raise newLPStreamLimitError()
except LPStreamReadError as exc:
raise newLPStreamReadError(exc.par)
2019-08-30 09:28:07 -06:00
method write*(s: ChronosStream, pbytes: pointer, nbytes: int) {.async, gcsafe.} =
2019-09-08 11:58:19 -06:00
try:
await s.writer.write(pbytes, nbytes)
except AsyncStreamWriteError as exc:
raise newLPStreamWriteError(exc.par)
except AsyncStreamIncompleteError:
raise newLPStreamIncompleteError()
2019-08-30 09:28:07 -06:00
method write*(s: ChronosStream, msg: string, msglen = -1) {.async, gcsafe.} =
2019-09-08 11:58:19 -06:00
try:
await s.writer.write(msg, msglen)
except AsyncStreamWriteError as exc:
raise newLPStreamWriteError(exc.par)
except AsyncStreamIncompleteError:
raise newLPStreamIncompleteError()
2019-08-30 09:28:07 -06:00
method write*(s: ChronosStream, msg: seq[byte], msglen = -1) {.async, gcsafe.} =
2019-09-08 11:58:19 -06:00
try:
await s.writer.write(msg, msglen)
except AsyncStreamWriteError as exc:
raise newLPStreamWriteError(exc.par)
except AsyncStreamIncompleteError:
raise newLPStreamIncompleteError()
2019-08-30 09:28:07 -06:00
method close*(s: ChronosStream) {.async, gcsafe.} =
2019-09-03 19:41:28 -06:00
if not s.closed:
trace "closing connection for", address = $s.client.remoteAddress()
2019-09-03 19:41:28 -06:00
if not s.reader.closed:
await s.reader.closeWait()
2019-09-03 19:41:28 -06:00
await s.writer.finish()
if not s.writer.closed:
await s.writer.closeWait()
2019-09-04 21:02:22 -06:00
if not s.client.closed:
await s.client.closeWait()
2019-09-25 12:42:52 -06:00
s.closed = true