mirror of
https://github.com/status-im/nim-libp2p.git
synced 2025-01-11 21:44:24 +00:00
resource cleanup
This commit is contained in:
parent
93e5805c01
commit
74f8b5b5f1
@ -500,7 +500,8 @@ method handshake*(p: Noise, conn: Connection, initiator: bool = false): Future[S
|
|||||||
raise newException(NoiseHandshakeError, "Noise handshake, peer infos don't match! " & $pid & " != " & $conn.peerInfo.peerId)
|
raise newException(NoiseHandshakeError, "Noise handshake, peer infos don't match! " & $pid & " != " & $conn.peerInfo.peerId)
|
||||||
|
|
||||||
var secure = new NoiseConnection
|
var secure = new NoiseConnection
|
||||||
inc getConnectionTracker().opened
|
secure.initStream()
|
||||||
|
|
||||||
secure.stream = conn
|
secure.stream = conn
|
||||||
secure.closeEvent = newAsyncEvent()
|
secure.closeEvent = newAsyncEvent()
|
||||||
secure.peerInfo = PeerInfo.init(remotePubKey)
|
secure.peerInfo = PeerInfo.init(remotePubKey)
|
||||||
|
@ -246,9 +246,8 @@ proc newSecioConn(conn: Connection,
|
|||||||
## cipher algorithm ``cipher``, stretched keys ``secrets`` and order
|
## cipher algorithm ``cipher``, stretched keys ``secrets`` and order
|
||||||
## ``order``.
|
## ``order``.
|
||||||
new result
|
new result
|
||||||
|
result.initStream()
|
||||||
result.stream = conn
|
result.stream = conn
|
||||||
result.closeEvent = newAsyncEvent()
|
|
||||||
|
|
||||||
let i0 = if order < 0: 1 else: 0
|
let i0 = if order < 0: 1 else: 0
|
||||||
let i1 = if order < 0: 0 else: 1
|
let i1 = if order < 0: 0 else: 1
|
||||||
@ -267,10 +266,6 @@ proc newSecioConn(conn: Connection,
|
|||||||
secrets.ivOpenArray(i1))
|
secrets.ivOpenArray(i1))
|
||||||
|
|
||||||
result.peerInfo = PeerInfo.init(remotePubKey)
|
result.peerInfo = PeerInfo.init(remotePubKey)
|
||||||
when chronicles.enabledLogLevel == LogLevel.TRACE:
|
|
||||||
result.oid = genOid()
|
|
||||||
|
|
||||||
inc getConnectionTracker().opened
|
|
||||||
|
|
||||||
proc transactMessage(conn: Connection,
|
proc transactMessage(conn: Connection,
|
||||||
msg: seq[byte]): Future[seq[byte]] {.async.} =
|
msg: seq[byte]): Future[seq[byte]] {.async.} =
|
||||||
|
@ -44,7 +44,6 @@ method init*(s: Secure) {.gcsafe.} =
|
|||||||
discard await s.handleConn(conn, false)
|
discard await s.handleConn(conn, false)
|
||||||
trace "connection secured"
|
trace "connection secured"
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
if not conn.closed():
|
|
||||||
warn "securing connection failed", msg = exc.msg
|
warn "securing connection failed", msg = exc.msg
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
|
||||||
@ -63,6 +62,7 @@ method readExactly*(s: SecureConn,
|
|||||||
pbytes: pointer,
|
pbytes: pointer,
|
||||||
nbytes: int):
|
nbytes: int):
|
||||||
Future[void] {.async, gcsafe.} =
|
Future[void] {.async, gcsafe.} =
|
||||||
|
try:
|
||||||
if nbytes == 0:
|
if nbytes == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -76,11 +76,16 @@ method readExactly*(s: SecureConn,
|
|||||||
var p = cast[ptr UncheckedArray[byte]](pbytes)
|
var p = cast[ptr UncheckedArray[byte]](pbytes)
|
||||||
let consumed = s.buf.consumeTo(toOpenArray(p, 0, nbytes - 1))
|
let consumed = s.buf.consumeTo(toOpenArray(p, 0, nbytes - 1))
|
||||||
doAssert consumed == nbytes, "checked above"
|
doAssert consumed == nbytes, "checked above"
|
||||||
|
except CatchableError as exc:
|
||||||
|
trace "exception reading from secure connection", exc = exc.msg
|
||||||
|
await s.close() # make sure to close the wrapped connection
|
||||||
|
raise exc
|
||||||
|
|
||||||
method readOnce*(s: SecureConn,
|
method readOnce*(s: SecureConn,
|
||||||
pbytes: pointer,
|
pbytes: pointer,
|
||||||
nbytes: int):
|
nbytes: int):
|
||||||
Future[int] {.async, gcsafe.} =
|
Future[int] {.async, gcsafe.} =
|
||||||
|
try:
|
||||||
if nbytes == 0:
|
if nbytes == 0:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@ -92,3 +97,7 @@ method readOnce*(s: SecureConn,
|
|||||||
|
|
||||||
var p = cast[ptr UncheckedArray[byte]](pbytes)
|
var p = cast[ptr UncheckedArray[byte]](pbytes)
|
||||||
return s.buf.consumeTo(toOpenArray(p, 0, nbytes - 1))
|
return s.buf.consumeTo(toOpenArray(p, 0, nbytes - 1))
|
||||||
|
except CatchableError as exc:
|
||||||
|
trace "exception reading from secure connection", exc = exc.msg
|
||||||
|
await s.close() # make sure to close the wrapped connection
|
||||||
|
raise exc
|
||||||
|
Loading…
x
Reference in New Issue
Block a user