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