2019-08-20 10:18:15 -06:00
|
|
|
## Nim-LibP2P
|
2019-09-24 11:48:23 -06:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-20 10:18:15 -06: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 15:27:07 +02:00
|
|
|
import chronos, chronicles, metrics
|
2019-09-06 15:25:28 -06:00
|
|
|
import peerinfo,
|
2020-04-21 10:24:42 +09:00
|
|
|
errors,
|
2019-09-06 15:25:28 -06:00
|
|
|
multiaddress,
|
|
|
|
stream/lpstream,
|
2020-05-11 21:05:24 +02:00
|
|
|
peerinfo
|
2019-08-20 10:18:15 -06:00
|
|
|
|
2020-05-23 11:09:52 -06:00
|
|
|
when chronicles.enabledLogLevel == LogLevel.TRACE:
|
|
|
|
import oids
|
|
|
|
|
2020-05-06 18:31:47 +02:00
|
|
|
export lpstream
|
|
|
|
|
2020-03-27 08:25:52 -06:00
|
|
|
logScope:
|
|
|
|
topic = "Connection"
|
|
|
|
|
2020-04-21 10:24:42 +09:00
|
|
|
const
|
|
|
|
ConnectionTrackerName* = "libp2p.connection"
|
2019-08-20 10:18:15 -06:00
|
|
|
|
|
|
|
type
|
2019-08-27 15:45:21 -06:00
|
|
|
Connection* = ref object of LPStream
|
2019-12-10 14:50:35 -06:00
|
|
|
peerInfo*: PeerInfo
|
2019-08-29 23:15:43 -06:00
|
|
|
stream*: LPStream
|
2019-09-12 11:07:34 -06:00
|
|
|
observedAddrs*: Multiaddress
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2020-04-21 10:24:42 +09: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 15:27:07 +02:00
|
|
|
declareGauge libp2p_open_connection, "open Connection instances"
|
|
|
|
|
2020-05-11 21:05:24 +02:00
|
|
|
proc `$`*(conn: Connection): string =
|
|
|
|
if not isNil(conn.peerInfo):
|
|
|
|
result = $(conn.peerInfo)
|
|
|
|
|
2020-04-21 10:24:42 +09:00
|
|
|
proc init[T: Connection](self: var T, stream: LPStream): T =
|
2019-08-22 15:35:47 -06:00
|
|
|
## create a new Connection for the specified async reader/writer
|
2020-02-12 02:30:36 +09:00
|
|
|
new self
|
|
|
|
self.stream = stream
|
2020-05-23 11:09:52 -06:00
|
|
|
self.initStream()
|
2020-04-07 09:49:43 -06:00
|
|
|
return self
|
2019-09-04 15:21:12 -06:00
|
|
|
|
2020-02-12 02:30:36 +09:00
|
|
|
proc newConnection*(stream: LPStream): Connection =
|
|
|
|
## create a new Connection for the specified async reader/writer
|
|
|
|
result.init(stream)
|
|
|
|
|
2020-05-23 11:09:52 -06:00
|
|
|
method initStream*(s: Connection) =
|
|
|
|
procCall LPStream(s).initStream()
|
|
|
|
trace "created connection", oid = s.oid
|
|
|
|
inc getConnectionTracker().opened
|
|
|
|
libp2p_open_connection.inc()
|
|
|
|
|
2019-08-26 09:37:15 -06:00
|
|
|
method readExactly*(s: Connection,
|
|
|
|
pbytes: pointer,
|
2019-12-10 14:50:35 -06:00
|
|
|
nbytes: int):
|
2020-05-23 11:09:52 -06:00
|
|
|
Future[void] {.async, gcsafe.} =
|
|
|
|
try:
|
|
|
|
await s.stream.readExactly(pbytes, nbytes)
|
|
|
|
except CatchableError as exc:
|
|
|
|
await s.close()
|
|
|
|
raise exc
|
2019-08-22 15:35:47 -06:00
|
|
|
|
2019-08-26 09:37:15 -06:00
|
|
|
method readOnce*(s: Connection,
|
|
|
|
pbytes: pointer,
|
2019-12-03 22:44:54 -06:00
|
|
|
nbytes: int):
|
2020-05-23 11:09:52 -06:00
|
|
|
Future[int] {.async, gcsafe.} =
|
|
|
|
try:
|
|
|
|
result = await s.stream.readOnce(pbytes, nbytes)
|
|
|
|
except CatchableError as exc:
|
|
|
|
await s.close()
|
|
|
|
raise exc
|
2019-08-20 10:18:15 -06:00
|
|
|
|
2019-12-10 14:50:35 -06:00
|
|
|
method write*(s: Connection,
|
2020-05-07 22:37:46 +02:00
|
|
|
msg: seq[byte]):
|
2020-05-23 11:09:52 -06:00
|
|
|
Future[void] {.async, gcsafe.} =
|
|
|
|
try:
|
|
|
|
await s.stream.write(msg)
|
|
|
|
except CatchableError as exc:
|
|
|
|
await s.close()
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
method atEof*(s: Connection): bool {.inline.} =
|
|
|
|
if isNil(s.stream):
|
|
|
|
return true
|
|
|
|
|
|
|
|
s.stream.atEof
|
2019-08-21 16:53:16 -06:00
|
|
|
|
2019-12-10 14:50:35 -06:00
|
|
|
method closed*(s: Connection): bool =
|
2019-12-03 22:44:54 -06:00
|
|
|
if isNil(s.stream):
|
2020-04-21 10:24:42 +09:00
|
|
|
return true
|
2019-12-03 22:44:54 -06:00
|
|
|
|
|
|
|
result = s.stream.closed
|
|
|
|
|
2019-08-30 09:28:07 -06:00
|
|
|
method close*(s: Connection) {.async, gcsafe.} =
|
2020-05-23 11:09:52 -06:00
|
|
|
try:
|
|
|
|
if not s.isClosed:
|
|
|
|
s.isClosed = true
|
2020-04-21 10:24:42 +09:00
|
|
|
|
2020-05-23 11:09:52 -06:00
|
|
|
trace "about to close connection", closed = s.closed,
|
|
|
|
conn = $s,
|
|
|
|
oid = s.oid
|
2020-04-14 15:27:07 +02:00
|
|
|
|
2020-04-03 14:36:51 -06:00
|
|
|
|
2020-05-23 11:09:52 -06:00
|
|
|
if not isNil(s.stream) and not s.stream.closed:
|
|
|
|
trace "closing child stream", closed = s.closed,
|
|
|
|
conn = $s,
|
|
|
|
oid = s.stream.oid
|
|
|
|
await s.stream.close()
|
|
|
|
# s.stream = nil
|
|
|
|
|
|
|
|
s.closeEvent.fire()
|
|
|
|
trace "connection closed", closed = s.closed,
|
|
|
|
conn = $s,
|
|
|
|
oid = s.oid
|
|
|
|
|
|
|
|
inc getConnectionTracker().closed
|
|
|
|
libp2p_open_connection.dec()
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception closing connections", exc = exc.msg
|
2019-08-20 10:18:15 -06:00
|
|
|
|
2019-08-30 09:28:07 -06:00
|
|
|
method getObservedAddrs*(c: Connection): Future[MultiAddress] {.base, async, gcsafe.} =
|
2019-08-20 10:18:15 -06:00
|
|
|
## get resolved multiaddresses for the connection
|
2019-09-12 11:07:34 -06:00
|
|
|
result = c.observedAddrs
|