From 19095a0f85bce77789cd788264113705e3bba30e Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Sun, 8 Mar 2020 04:14:56 +0100 Subject: [PATCH] move `secure` to base as well --- libp2p/protocols/secure/secio.nim | 8 -------- libp2p/protocols/secure/secure.nim | 19 ++++++++++--------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/libp2p/protocols/secure/secio.nim b/libp2p/protocols/secure/secio.nim index db11c7e4a..99d69390f 100644 --- a/libp2p/protocols/secure/secio.nim +++ b/libp2p/protocols/secure/secio.nim @@ -418,14 +418,6 @@ method init(s: Secio) {.gcsafe.} = procCall Secure(s).init() s.codec = SecioCodec -method secure*(s: Secio, conn: Connection): Future[Connection] {.async, gcsafe.} = - try: - result = await s.handleConn(conn) - except CatchableError as exc: - warn "securing connection failed", msg = exc.msg - if not conn.closed(): - await conn.close() - proc newSecio*(localPrivateKey: PrivateKey): Secio = new result result.localPrivateKey = localPrivateKey diff --git a/libp2p/protocols/secure/secure.nim b/libp2p/protocols/secure/secure.nim index eb24eb6c9..5ab1e869c 100644 --- a/libp2p/protocols/secure/secure.nim +++ b/libp2p/protocols/secure/secure.nim @@ -22,7 +22,7 @@ type method handshake(s: Secure, conn: Connection, - initiator: bool = true): Future[SecureConn] {.async, base.} = + initiator: bool = false): Future[SecureConn] {.async, base.} = doAssert(false, "Not implemented!") proc readLoop(sconn: SecureConn, stream: BufferStream) {.async.} = @@ -41,8 +41,8 @@ proc readLoop(sconn: SecureConn, stream: BufferStream) {.async.} = await sconn.close() trace "ending secio readLoop", isclosed = sconn.closed() -proc handleConn*(s: Secure, conn: Connection): Future[Connection] {.async, gcsafe.} = - var sconn = await s.handshake(conn) +proc handleConn*(s: Secure, conn: Connection, initiator: bool = false): Future[Connection] {.async, gcsafe.} = + var sconn = await s.handshake(conn, initiator) proc writeHandler(data: seq[byte]) {.async, gcsafe.} = trace "sending encrypted bytes", bytes = data.toHex() await sconn.writeMessage(data) @@ -71,9 +71,10 @@ method init*(s: Secure) {.gcsafe.} = s.handler = handle -method secure*(p: Secure, conn: Connection): Future[Connection] - {.base, async, gcsafe.} = - ## default implementation matches plaintext - var retFuture = newFuture[Connection]("secure.secure") - retFuture.complete(conn) - return retFuture +method secure*(s: Secure, conn: Connection): Future[Connection] {.async, base, gcsafe.} = + try: + result = await s.handleConn(conn, true) + except CatchableError as exc: + warn "securing connection failed", msg = exc.msg + if not conn.closed(): + await conn.close()