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.
|
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
import oids
|
2019-09-12 02:10:38 +00:00
|
|
|
import chronos, chronicles
|
2020-06-19 17:29:43 +00:00
|
|
|
import connection, ../utility
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "chronosstream"
|
2019-09-12 02:10:38 +00:00
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
type ChronosStream* = ref object of Connection
|
2019-08-22 21:35:47 +00:00
|
|
|
client: StreamTransport
|
|
|
|
|
2020-06-19 17:29:43 +00:00
|
|
|
method initStream*(s: ChronosStream) =
|
|
|
|
if s.objName.len == 0:
|
|
|
|
s.objName = "ChronosStream"
|
|
|
|
|
|
|
|
procCall Connection(s).initStream()
|
|
|
|
|
2020-05-08 20:10:06 +00:00
|
|
|
proc newChronosStream*(client: StreamTransport): ChronosStream =
|
2019-08-22 21:35:47 +00:00
|
|
|
new result
|
|
|
|
result.client = client
|
2020-06-19 17:29:43 +00:00
|
|
|
result.initStream()
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
template withExceptions(body: untyped) =
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
except TransportIncompleteError:
|
2020-05-23 16:52:33 +00:00
|
|
|
# for all intents and purposes this is an EOF
|
|
|
|
raise newLPStreamEOFError()
|
2020-03-27 14:25:52 +00:00
|
|
|
except TransportLimitError:
|
|
|
|
raise newLPStreamLimitError()
|
2020-05-08 20:10:06 +00:00
|
|
|
except TransportUseClosedError:
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
except TransportError:
|
|
|
|
# TODO https://github.com/status-im/nim-chronos/pull/99
|
|
|
|
raise newLPStreamEOFError()
|
2020-03-27 14:25:52 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
method readOnce*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[int] {.async.} =
|
2020-05-20 00:14:15 +00:00
|
|
|
if s.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:
|
2020-05-08 20:10:06 +00:00
|
|
|
result = await s.client.readOnce(pbytes, nbytes)
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2020-05-07 20:37:46 +00:00
|
|
|
method write*(s: ChronosStream, msg: seq[byte]) {.async.} =
|
2020-05-20 00:14:15 +00:00
|
|
|
if s.closed:
|
|
|
|
raise newLPStreamClosedError()
|
|
|
|
|
2020-05-08 20:10:06 +00:00
|
|
|
if msg.len == 0:
|
|
|
|
return
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
withExceptions:
|
2020-05-29 16:46:27 +00:00
|
|
|
var written = 0
|
|
|
|
while not s.client.closed and written < msg.len:
|
|
|
|
written += await s.client.write(msg[written..<msg.len])
|
2020-05-23 16:52:33 +00:00
|
|
|
|
2020-05-29 16:46:27 +00:00
|
|
|
if written < msg.len:
|
2020-05-23 16:52:33 +00:00
|
|
|
raise (ref LPStreamClosedError)(msg: "Write couldn't finish writing")
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method closed*(s: ChronosStream): bool {.inline.} =
|
2020-05-08 20:10:06 +00:00
|
|
|
result = s.client.closed
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2020-05-20 00:14:15 +00:00
|
|
|
method atEof*(s: ChronosStream): bool {.inline.} =
|
|
|
|
s.client.atEof()
|
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
method close*(s: ChronosStream) {.async.} =
|
2020-05-23 16:52:33 +00:00
|
|
|
try:
|
|
|
|
if not s.isClosed:
|
2020-07-08 00:33:05 +00:00
|
|
|
trace "shutting down chronos stream", address = $s.client.remoteAddress(),
|
|
|
|
oid = s.oid
|
2020-05-23 16:52:33 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
# TODO: the sequence here matters
|
|
|
|
# don't move it after the connections
|
|
|
|
# close bellow
|
2020-05-23 16:52:33 +00:00
|
|
|
if not s.client.closed():
|
2020-05-15 03:56:56 +00:00
|
|
|
await s.client.closeWait()
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-07-08 00:33:05 +00:00
|
|
|
await procCall Connection(s).close()
|
|
|
|
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-05-23 16:52:33 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
trace "error closing chronosstream", exc = exc.msg
|