From 843f9c3916045ba8693fffdab74f3915c57a7eae Mon Sep 17 00:00:00 2001 From: kdeme Date: Tue, 20 Aug 2019 17:33:43 +0200 Subject: [PATCH] Update p2p.md Add explanation on async proc --- doc/p2p.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/doc/p2p.md b/doc/p2p.md index f3888b0..adb3312 100644 --- a/doc/p2p.md +++ b/doc/p2p.md @@ -177,10 +177,10 @@ The `nextMsg` helper proc can be used to pause the execution of an async proc until a particular incoming message from a peer arrives: ``` nim -proc helloExample(peer: Peer) {.async.} = +proc helloExample(peer: Peer) = ... # send a hello message - peer.hello(...) + await peer.hello(...) # wait for a matching hello response, might want to add a timeout here let response = await peer.nextMsg(p2p.hello) @@ -205,6 +205,10 @@ place). If there are multiple outstanding calls to `nextMsg`, they will complete together. Any other messages received in the meantime will still be dispatched to their respective handlers. +Please also note that the `p2pProtocol` macro will make this `helloExample` proc +`async`. Practically see it as `proc helloExample(peer: Peer) {.async.}`, and +thus never use `waitFor`, but rather `await` inside this proc. + For implementing protocol handshakes with `nextMsg` there are specific helpers which are explained [below](https://github.com/status-im/nim-eth/blob/master/doc/p2p.md#implementing-handshakes-and-reacting-to-other-events). @@ -254,7 +258,6 @@ peers or misbehaving or disconnecting peers: ``` nim p2pProtocol foo(version = fooVersion): - onPeerConnected do (peer: Peer): let m = await peer.status(fooVersion, timeout = chronos.milliseconds(5000)) @@ -263,10 +266,10 @@ p2pProtocol foo(version = fooVersion): debug "Foo peer", peer, fooVersion else: raise newException(UselessPeerError, "Incompatible Foo version") - + onPeerDisconnected do (peer: Peer, reason: DisconnectionReason): debug "peer disconnected", peer - + handshake: proc status(peer: Peer, protocolVersion: uint)