159 lines
4.7 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.
import chronos, chronicles
import peerinfo,
multiaddress,
stream/lpstream,
peerinfo,
varint,
vbuffer
const DefaultReadSize* = 1 shl 20
type
2019-08-27 15:45:21 -06:00
Connection* = ref object of LPStream
peerInfo*: PeerInfo
2019-08-29 23:15:43 -06:00
stream*: LPStream
2019-09-12 11:07:34 -06:00
observedAddrs*: Multiaddress
InvalidVarintException = object of LPStreamError
InvalidVarintSizeException = object of LPStreamError
proc newInvalidVarintException*(): ref InvalidVarintException =
2020-02-06 15:24:02 +09:00
newException(InvalidVarintException, "unable to parse varint")
proc newInvalidVarintSizeException*(): ref InvalidVarintSizeException =
newException(InvalidVarintSizeException, "wrong varint size")
proc init*[T: Connection](self: var T, stream: LPStream) =
## create a new Connection for the specified async reader/writer
new self
self.stream = stream
self.closeEvent = newAsyncEvent()
# bind stream's close event to connection's close
# to ensure correct close propagation
let this = self
if not isNil(self.stream.closeEvent):
self.stream.closeEvent.wait().
addCallback do (udata: pointer):
if not this.closed:
trace "closing this connection because wrapped stream closed"
asyncCheck this.close()
2019-09-04 15:21:12 -06:00
proc newConnection*(stream: LPStream): Connection =
## create a new Connection for the specified async reader/writer
result.init(stream)
2019-08-30 15:55:07 -06:00
method read*(s: Connection, n = -1): Future[seq[byte]] {.gcsafe.} =
2020-02-05 18:54:03 +01:00
s.stream.read(n)
2019-08-26 09:37:15 -06:00
method readExactly*(s: Connection,
pbytes: pointer,
nbytes: int):
2019-09-02 14:45:00 -06:00
Future[void] {.gcsafe.} =
2020-02-05 18:54:03 +01:00
s.stream.readExactly(pbytes, nbytes)
2019-08-26 09:37:15 -06:00
method readLine*(s: Connection,
limit = 0,
sep = "\r\n"):
2019-09-02 14:45:00 -06:00
Future[string] {.gcsafe.} =
2020-02-05 18:54:03 +01:00
s.stream.readLine(limit, sep)
2019-08-21 13:15:51 -06:00
2019-08-26 09:37:15 -06:00
method readOnce*(s: Connection,
pbytes: pointer,
nbytes: int):
2019-09-02 14:45:00 -06:00
Future[int] {.gcsafe.} =
2020-02-05 18:54:03 +01:00
s.stream.readOnce(pbytes, nbytes)
2019-08-26 09:37:15 -06:00
method readUntil*(s: Connection,
pbytes: pointer,
nbytes: int,
sep: seq[byte]):
2019-09-02 14:45:00 -06:00
Future[int] {.gcsafe.} =
2020-02-05 18:54:03 +01:00
s.stream.readUntil(pbytes, nbytes, sep)
method write*(s: Connection,
pbytes: pointer,
nbytes: int):
2019-09-02 14:45:00 -06:00
Future[void] {.gcsafe.} =
2020-02-05 18:54:03 +01:00
s.stream.write(pbytes, nbytes)
method write*(s: Connection,
msg: string,
msglen = -1):
2019-09-02 14:45:00 -06:00
Future[void] {.gcsafe.} =
2020-02-05 18:54:03 +01:00
s.stream.write(msg, msglen)
2019-08-21 16:53:16 -06:00
method write*(s: Connection,
msg: seq[byte],
msglen = -1):
2019-09-02 14:45:00 -06:00
Future[void] {.gcsafe.} =
2020-02-05 18:54:03 +01:00
s.stream.write(msg, msglen)
2019-08-21 16:53:16 -06:00
method closed*(s: Connection): bool =
if isNil(s.stream):
return false
result = s.stream.closed
2019-08-30 09:28:07 -06:00
method close*(s: Connection) {.async, gcsafe.} =
trace "closing connection"
if not s.closed:
if not isNil(s.stream) and not s.stream.closed:
await s.stream.close()
s.closeEvent.fire()
s.isClosed = true
trace "connection closed", closed = s.closed
2019-08-30 09:28:07 -06:00
proc readLp*(s: Connection): Future[seq[byte]] {.async, gcsafe.} =
## read lenght prefixed msg
var
size: uint
length: int
res: VarintStatus
2020-01-29 14:59:04 -06:00
buff = newSeq[byte](10)
try:
2020-01-29 14:59:04 -06:00
for i in 0..<len(buff):
await s.readExactly(addr buff[i], 1)
res = LP.getUVarint(buff.toOpenArray(0, i), length, size)
if res == VarintStatus.Success:
break
if res != VarintStatus.Success:
raise newInvalidVarintException()
if size.int > DefaultReadSize:
raise newInvalidVarintSizeException()
2020-01-29 14:59:04 -06:00
buff.setLen(size)
if size > 0.uint:
trace "reading exact bytes from stream", size = size
2020-01-29 14:59:04 -06:00
await s.readExactly(addr buff[0], int(size))
return buff
except LPStreamIncompleteError as exc:
trace "remote connection ended unexpectedly", exc = exc.msg
2020-01-29 12:05:05 -06:00
raise exc
except LPStreamReadError as exc:
trace "couldn't read from stream", exc = exc.msg
2020-01-29 12:05:05 -06:00
raise exc
2019-09-08 01:43:33 -06:00
proc writeLp*(s: Connection, msg: string | seq[byte]): Future[void] {.gcsafe.} =
## write lenght prefixed
var buf = initVBuffer()
buf.writeSeq(msg)
buf.finish()
s.write(buf.buffer)
2019-08-30 09:28:07 -06:00
method getObservedAddrs*(c: Connection): Future[MultiAddress] {.base, async, gcsafe.} =
## get resolved multiaddresses for the connection
2019-09-12 11:07:34 -06:00
result = c.observedAddrs
2019-09-09 17:08:30 -06:00
proc `$`*(conn: Connection): string =
if not isNil(conn.peerInfo):
result = $(conn.peerInfo)