2019-08-22 21:35:47 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-22 21:35:47 +00:00
|
|
|
## 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-12 02:10:38 +00:00
|
|
|
import chronos, chronicles
|
2019-09-01 17:31:24 +00:00
|
|
|
import lpstream
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
logScope:
|
|
|
|
topic = "ChronosStream"
|
|
|
|
|
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
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc newChronosStream*(server: StreamServer,
|
2019-08-22 21:35:47 +00:00
|
|
|
client: StreamTransport): ChronosStream =
|
|
|
|
new result
|
|
|
|
result.server = server
|
|
|
|
result.client = client
|
|
|
|
result.reader = newAsyncStreamReader(client)
|
|
|
|
result.writer = newAsyncStreamWriter(client)
|
2019-12-04 04:44:54 +00:00
|
|
|
result.closeEvent = newAsyncEvent()
|
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
template withExceptions(body: untyped) =
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
except TransportIncompleteError:
|
|
|
|
raise newLPStreamIncompleteError()
|
|
|
|
except TransportLimitError:
|
|
|
|
raise newLPStreamLimitError()
|
|
|
|
except TransportError as exc:
|
2020-05-06 16:31:47 +00:00
|
|
|
raise newLPStreamIncorrectDefect(exc.msg)
|
2020-03-27 14:25:52 +00:00
|
|
|
except AsyncStreamIncompleteError:
|
|
|
|
raise newLPStreamIncompleteError()
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method readExactly*(s: ChronosStream,
|
|
|
|
pbytes: pointer,
|
2019-12-04 04:44:54 +00:00
|
|
|
nbytes: int): Future[void] {.async.} =
|
|
|
|
if s.reader.atEof:
|
2019-12-10 20:50:35 +00:00
|
|
|
raise newLPStreamEOFError()
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
withExceptions:
|
2019-09-08 17:58:19 +00:00
|
|
|
await s.reader.readExactly(pbytes, nbytes)
|
2019-12-04 04:44:54 +00:00
|
|
|
|
|
|
|
method readOnce*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[int] {.async.} =
|
|
|
|
if s.reader.atEof:
|
2019-12-10 20:50:35 +00:00
|
|
|
raise newLPStreamEOFError()
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
withExceptions:
|
2019-09-25 18:42:52 +00:00
|
|
|
result = await s.reader.readOnce(pbytes, nbytes)
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
method write*(s: ChronosStream, msg: seq[byte], msglen = -1) {.async.} =
|
|
|
|
if s.writer.atEof:
|
2019-12-10 20:50:35 +00:00
|
|
|
raise newLPStreamEOFError()
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
withExceptions:
|
2019-09-08 17:58:19 +00:00
|
|
|
await s.writer.write(msg, msglen)
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method closed*(s: ChronosStream): bool {.inline.} =
|
2019-12-04 04:44:54 +00:00
|
|
|
# TODO: we might only need to check for reader's EOF
|
|
|
|
result = s.reader.atEof()
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
method close*(s: ChronosStream) {.async.} =
|
2019-09-04 01:41:28 +00:00
|
|
|
if not s.closed:
|
2019-12-04 04:44:54 +00:00
|
|
|
trace "shutting chronos stream", address = $s.client.remoteAddress()
|
|
|
|
if not s.writer.closed():
|
|
|
|
await s.writer.closeWait()
|
|
|
|
|
|
|
|
if not s.reader.closed():
|
|
|
|
await s.reader.closeWait()
|
|
|
|
|
|
|
|
if not s.client.closed():
|
|
|
|
await s.client.closeWait()
|
|
|
|
|
|
|
|
s.closeEvent.fire()
|