mirror of
https://github.com/logos-storage/nim-libp2p.git
synced 2026-01-06 23:53:12 +00:00
* call write until all is written out * add comments to lpchannel fields * add an eof flag to signal which end closed * wip: rework with proper half-closed * add eof and closed handling * propagate closes to piped * call parent close * moving bufferstream trackers out * move writeLock to bufferstream * move writeLock out * remove unused call * wip * rebasing master * fix mplex tests * wip * fix bufferstream after backport * wip * rename to differentiate from chronos tracker * close connection on chronos close * make reset request asyncCheck * fix channel cleanup * misc * don't use read * fix backports * make noise work again * proper exception handling * don't reraise just yet * add convenience templates * dont double wrap * use async pragma * fixes after backporting * muxer owns connection * remove on transport close cleanup * revert back allread * adding some todos * read from stream * inc count before closing * rebasing master * rebase master * use correct exception type * use try/finally insted of defer * fix compile in trace mode * reset channels on mplex close
86 lines
2.4 KiB
Nim
86 lines
2.4 KiB
Nim
## Nim-LibP2P
|
|
## 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.
|
|
|
|
import chronos, chronicles
|
|
import lpstream, ../utility
|
|
|
|
logScope:
|
|
topic = "ChronosStream"
|
|
|
|
type ChronosStream* = ref object of LPStream
|
|
client: StreamTransport
|
|
|
|
proc newChronosStream*(client: StreamTransport): ChronosStream =
|
|
new result
|
|
result.client = client
|
|
result.closeEvent = newAsyncEvent()
|
|
|
|
|
|
template withExceptions(body: untyped) =
|
|
try:
|
|
body
|
|
except TransportIncompleteError:
|
|
raise newLPStreamIncompleteError()
|
|
except TransportLimitError:
|
|
raise newLPStreamLimitError()
|
|
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)
|
|
|
|
method readExactly*(s: ChronosStream,
|
|
pbytes: pointer,
|
|
nbytes: int): Future[void] {.async.} =
|
|
if s.atEof:
|
|
raise newLPStreamEOFError()
|
|
|
|
withExceptions:
|
|
await s.client.readExactly(pbytes, nbytes)
|
|
|
|
method readOnce*(s: ChronosStream, pbytes: pointer, nbytes: int): Future[int] {.async.} =
|
|
if s.atEof:
|
|
raise newLPStreamEOFError()
|
|
|
|
withExceptions:
|
|
result = await s.client.readOnce(pbytes, nbytes)
|
|
|
|
method write*(s: ChronosStream, msg: seq[byte]) {.async.} =
|
|
if s.closed:
|
|
raise newLPStreamClosedError()
|
|
|
|
if msg.len == 0:
|
|
return
|
|
|
|
withExceptions:
|
|
# Returns 0 sometimes when write fails - but there's not much we can do here?
|
|
if (await s.client.write(msg)) != msg.len:
|
|
raise (ref LPStreamError)(msg: "Write couldn't finish writing")
|
|
|
|
method closed*(s: ChronosStream): bool {.inline.} =
|
|
result = s.client.closed
|
|
|
|
method atEof*(s: ChronosStream): bool {.inline.} =
|
|
s.client.atEof()
|
|
|
|
method close*(s: ChronosStream) {.async.} =
|
|
if not s.closed:
|
|
trace "shutting chronos stream", address = $s.client.remoteAddress()
|
|
if not s.client.closed():
|
|
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
|
|
|
|
s.closeEvent.fire()
|