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.
|
|
|
|
|
2020-04-14 13:27:07 +00:00
|
|
|
import oids
|
|
|
|
import chronos, chronicles, metrics
|
2019-09-06 21:25:28 +00:00
|
|
|
import peerinfo,
|
2020-04-21 01:24:42 +00:00
|
|
|
errors,
|
2019-09-06 21:25:28 +00:00
|
|
|
multiaddress,
|
|
|
|
stream/lpstream,
|
|
|
|
peerinfo,
|
|
|
|
varint,
|
|
|
|
vbuffer
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2020-05-06 16:31:47 +00:00
|
|
|
export lpstream
|
|
|
|
|
2020-03-27 14:25:52 +00:00
|
|
|
logScope:
|
|
|
|
topic = "Connection"
|
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
const
|
|
|
|
ConnectionTrackerName* = "libp2p.connection"
|
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
|
2020-04-21 01:24:42 +00:00
|
|
|
# notice this is a ugly circular reference collection
|
|
|
|
# (we got many actually :-))
|
|
|
|
readLoops*: seq[Future[void]]
|
2019-08-22 21:35:47 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
ConnectionTracker* = ref object of TrackerBase
|
|
|
|
opened*: uint64
|
|
|
|
closed*: uint64
|
|
|
|
|
|
|
|
proc setupConnectionTracker(): ConnectionTracker {.gcsafe.}
|
|
|
|
|
|
|
|
proc getConnectionTracker*(): ConnectionTracker {.gcsafe.} =
|
|
|
|
result = cast[ConnectionTracker](getTracker(ConnectionTrackerName))
|
|
|
|
if isNil(result):
|
|
|
|
result = setupConnectionTracker()
|
|
|
|
|
|
|
|
proc dumpTracking(): string {.gcsafe.} =
|
|
|
|
var tracker = getConnectionTracker()
|
|
|
|
result = "Opened conns: " & $tracker.opened & "\n" &
|
|
|
|
"Closed conns: " & $tracker.closed
|
|
|
|
|
|
|
|
proc leakTransport(): bool {.gcsafe.} =
|
|
|
|
var tracker = getConnectionTracker()
|
|
|
|
result = (tracker.opened != tracker.closed)
|
|
|
|
|
|
|
|
proc setupConnectionTracker(): ConnectionTracker =
|
|
|
|
result = new ConnectionTracker
|
|
|
|
result.opened = 0
|
|
|
|
result.closed = 0
|
|
|
|
result.dump = dumpTracking
|
|
|
|
result.isLeaked = leakTransport
|
|
|
|
addTracker(ConnectionTrackerName, result)
|
|
|
|
|
2020-04-14 13:27:07 +00:00
|
|
|
declareGauge libp2p_open_connection, "open Connection instances"
|
|
|
|
|
2020-04-07 15:49:43 +00:00
|
|
|
proc bindStreamClose(conn: Connection) {.async.} =
|
|
|
|
# bind stream's close event to connection's close
|
|
|
|
# to ensure correct close propagation
|
|
|
|
if not isNil(conn.stream.closeEvent):
|
|
|
|
await conn.stream.closeEvent.wait()
|
2020-04-07 20:55:05 +00:00
|
|
|
trace "wrapped stream closed, about to close conn", closed = conn.isClosed,
|
|
|
|
peer = if not isNil(conn.peerInfo):
|
|
|
|
conn.peerInfo.id else: ""
|
2020-04-07 15:49:43 +00:00
|
|
|
if not conn.isClosed:
|
2020-04-07 20:55:05 +00:00
|
|
|
trace "wrapped stream closed, closing conn", closed = conn.isClosed,
|
|
|
|
peer = if not isNil(conn.peerInfo):
|
|
|
|
conn.peerInfo.id else: ""
|
2020-04-21 01:24:42 +00:00
|
|
|
await conn.close()
|
2020-04-07 15:49:43 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
proc init[T: Connection](self: var T, stream: LPStream): T =
|
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()
|
2020-04-14 13:21:16 +00:00
|
|
|
when chronicles.enabledLogLevel == LogLevel.TRACE:
|
|
|
|
self.oid = genOid()
|
2020-04-07 15:49:43 +00:00
|
|
|
asyncCheck self.bindStreamClose()
|
2020-04-21 01:24:42 +00:00
|
|
|
inc getConnectionTracker().opened
|
2020-04-14 13:27:07 +00:00
|
|
|
libp2p_open_connection.inc()
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2020-04-07 15:49:43 +00:00
|
|
|
return self
|
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-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-05-07 20:37:46 +00:00
|
|
|
s.stream.readExactly(pbytes, nbytes)
|
2019-08-22 21:35:47 +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-12-10 20:50:35 +00:00
|
|
|
method write*(s: Connection,
|
2020-05-07 20:37:46 +00:00
|
|
|
msg: seq[byte]):
|
2019-09-02 20:45:00 +00:00
|
|
|
Future[void] {.gcsafe.} =
|
2020-05-07 20:37:46 +00:00
|
|
|
s.stream.write(msg)
|
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):
|
2020-04-21 01:24:42 +00:00
|
|
|
return true
|
2019-12-04 04:44:54 +00:00
|
|
|
|
|
|
|
result = s.stream.closed
|
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
method close*(s: Connection) {.async, gcsafe.} =
|
2020-04-21 01:24:42 +00:00
|
|
|
trace "about to close connection", closed = s.closed,
|
|
|
|
peer = if not isNil(s.peerInfo):
|
|
|
|
s.peerInfo.id else: ""
|
|
|
|
|
|
|
|
if not s.isClosed:
|
|
|
|
s.isClosed = true
|
|
|
|
inc getConnectionTracker().closed
|
2020-04-14 13:27:07 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
if not isNil(s.stream) and not s.stream.closed:
|
2020-04-07 15:49:43 +00:00
|
|
|
trace "closing child stream", closed = s.closed,
|
|
|
|
peer = if not isNil(s.peerInfo):
|
|
|
|
s.peerInfo.id else: ""
|
2019-12-04 04:44:54 +00:00
|
|
|
await s.stream.close()
|
2020-04-03 20:36:51 +00:00
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
s.closeEvent.fire()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
trace "waiting readloops", count=s.readLoops.len
|
|
|
|
let loopFuts = await allFinished(s.readLoops)
|
|
|
|
checkFutures(loopFuts)
|
|
|
|
s.readLoops = @[]
|
2020-04-06 21:33:44 +00:00
|
|
|
|
2020-04-14 13:27:07 +00:00
|
|
|
trace "connection closed", closed = s.closed,
|
|
|
|
peer = if not isNil(s.peerInfo):
|
|
|
|
s.peerInfo.id else: ""
|
|
|
|
libp2p_open_connection.dec()
|
2019-08-20 16:18:15 +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)
|