2019-08-22 15:35:47 -06:00
|
|
|
## Nim-LibP2P
|
2019-09-24 11:48:23 -06:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-22 15:35:47 -06: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-11 20:10:38 -06:00
|
|
|
import chronos, chronicles
|
2020-05-07 22:37:46 +02:00
|
|
|
import lpstream, ../utility
|
2019-08-22 15:35:47 -06:00
|
|
|
|
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
|
2019-08-22 15:35:47 -06:00
|
|
|
client: StreamTransport
|
|
|
|
|
2020-05-08 22:10:06 +02:00
|
|
|
proc newChronosStream*(client: StreamTransport): ChronosStream =
|
2019-08-22 15:35:47 -06:00
|
|
|
new result
|
|
|
|
result.client = client
|
2019-12-03 22:44:54 -06:00
|
|
|
result.closeEvent = newAsyncEvent()
|
|
|
|
|
2020-05-19 18:14:15 -06:00
|
|
|
|
2020-03-27 08:25:52 -06:00
|
|
|
template withExceptions(body: untyped) =
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
except TransportIncompleteError:
|
|
|
|
raise newLPStreamIncompleteError()
|
|
|
|
except TransportLimitError:
|
|
|
|
raise newLPStreamLimitError()
|
2020-05-08 22:10:06 +02:00
|
|
|
except TransportUseClosedError:
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
except TransportError:
|
|
|
|
# TODO https://github.com/status-im/nim-chronos/pull/99
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
# raise (ref LPStreamError)(msg: exc.msg, parent: exc)
|
2020-03-27 08:25:52 -06:00
|
|
|
|
2019-12-10 14:50:35 -06:00
|
|
|
method readExactly*(s: ChronosStream,
|
|
|
|
pbytes: pointer,
|
2019-12-03 22:44:54 -06:00
|
|
|
nbytes: int): Future[void] {.async.} =
|
2020-05-19 18:14:15 -06:00
|
|
|
if s.atEof:
|
2019-12-10 14:50:35 -06:00
|
|
|
raise newLPStreamEOFError()
|
2019-12-03 22:44:54 -06:00
|
|
|
|
2020-03-27 08:25:52 -06:00
|
|
|
withExceptions:
|
2020-05-08 22:10:06 +02:00
|
|
|
await s.client.readExactly(pbytes, nbytes)
|
2019-12-03 22:44:54 -06:00
|
|
|
|
|
|
|
method readOnce*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[int] {.async.} =
|
2020-05-19 18:14:15 -06:00
|
|
|
if s.atEof:
|
2019-12-10 14:50:35 -06:00
|
|
|
raise newLPStreamEOFError()
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2020-03-27 08:25:52 -06:00
|
|
|
withExceptions:
|
2020-05-08 22:10:06 +02:00
|
|
|
result = await s.client.readOnce(pbytes, nbytes)
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2020-05-07 22:37:46 +02:00
|
|
|
method write*(s: ChronosStream, msg: seq[byte]) {.async.} =
|
2020-05-19 18:14:15 -06:00
|
|
|
if s.closed:
|
|
|
|
raise newLPStreamClosedError()
|
|
|
|
|
2020-05-08 22:10:06 +02:00
|
|
|
if msg.len == 0:
|
|
|
|
return
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2020-03-27 08:25:52 -06:00
|
|
|
withExceptions:
|
2020-05-11 11:36:40 -06:00
|
|
|
var writen = 0
|
|
|
|
while (writen < msg.len):
|
|
|
|
writen += await s.client.write(msg[writen..<msg.len]) # TODO: does the slice create a copy here?
|
2019-12-03 22:44:54 -06:00
|
|
|
|
2019-12-10 14:50:35 -06:00
|
|
|
method closed*(s: ChronosStream): bool {.inline.} =
|
2020-05-08 22:10:06 +02:00
|
|
|
result = s.client.closed
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2020-05-19 18:14:15 -06:00
|
|
|
method atEof*(s: ChronosStream): bool {.inline.} =
|
|
|
|
s.client.atEof()
|
|
|
|
|
2019-12-03 22:44:54 -06:00
|
|
|
method close*(s: ChronosStream) {.async.} =
|
2020-05-21 11:33:48 -06:00
|
|
|
if not s.isClosed:
|
|
|
|
s.isClosed = true
|
2019-12-03 22:44:54 -06:00
|
|
|
trace "shutting chronos stream", address = $s.client.remoteAddress()
|
|
|
|
if not s.client.closed():
|
2020-05-15 05:56:56 +02:00
|
|
|
try:
|
|
|
|
await s.client.closeWait()
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
# Shouldn't happen, but we can't be sure
|
|
|
|
warn "error while closing connection", msg = exc.msg
|
2019-12-03 22:44:54 -06:00
|
|
|
|
|
|
|
s.closeEvent.fire()
|