Crate nakamoto_net_mio
source ·Expand description
I/O reactor that drives the protocol state machine.
The reactor translates network events into protocol events. This has the added benefit that it’s trivial to swap nakamoto’s networking code with a different implementation, as the code is fully self-contained.
To illustrate the above, lets trace the behavior of the system when a ping
message is received via a peer connection to the client:
- The
Reactor
reads from the socket and decodes aNetworkMessage::Ping
message. - The
Reactor
wraps this message into a protocol inputInput::Received(addr, NetworkMessage::Ping)
, whereaddr
is the remote address of the socket on which it received this message. - The
Reactor
callsProtocol::step(input, time)
, whereinput
is the above input, andtime
is the current local time. - The
Protocol
forwards this message to thePingManager
, which constructs a new outputOut::Message(addr, NetworkMessage::Pong)
, and forwards it upstream, to the reactor. - The
Reactor
processes the output, encodes the raw message and writes it to the socket corresponding to theaddr
address, effectively sending apong
message back to the original sender.
Though simplified, the above steps provide a good mental model of how the reactor and protocol interplay to handle network events.
Re-exports
Modules
Poll-based reactor. This is a single-threaded reactor using a
poll
loop.Peer-to-peer socket abstraction.
Time-related functionality useful for reactors.
Macros
Makes a function randomly fail with the given error.