pub trait StateMachine<Id: PeerId = SocketAddr>: Iterator<Item = Io<<Self::Message as ToOwned>::Owned, Self::Event, Self::DisconnectReason, Id>> {
    type Message: Debug + ToOwned + ?Sized;
    type Event: Debug;
    type DisconnectReason: Debug + Display + Into<Disconnect<Self::DisconnectReason>>;

    fn message_received(&mut self, addr: &Id, message: Cow<'_, Self::Message>);
    fn attempted(&mut self, addr: &Id);
    fn connected(&mut self, addr: Id, local_addr: &SocketAddr, link: Link);
    fn disconnected(
        &mut self,
        addr: &Id,
        reason: Disconnect<Self::DisconnectReason>
    ); fn tick(&mut self, local_time: LocalTime); fn timer_expired(&mut self); fn initialize(&mut self, _time: LocalTime) { ... } }
Expand description

A service state-machine to implement a network protocol’s logic.

This trait defines an API for connecting specific protocol domain logic to a Reactor. It is parametrized by a peer id, which is shared between the reactor and state machine.

The state machine emits Io instructions to the reactor via its Iterator trait.

Required Associated Types

Message type sent between peers.

Events emitted by the state machine. These are forwarded by the reactor to the user thread.

Reason a peer was disconnected, in case the peer was disconnected by the internal state-machine logic.

Required Methods

Called by the reactor upon receiving a message from a remote peer.

Connection attempt underway.

This is only encountered when an outgoing connection attempt is made, and is always called before StateMachine::connected.

For incoming connections, StateMachine::connected is called directly.

New connection with a peer.

Called whenever a remote peer was disconnected, either because of a network-related event or due to a local instruction from this state machine, using Io::Disconnect.

Called by the reactor every time the event loop gets data from the network, or times out. Used to update the state machine’s internal clock.

“a regular short, sharp sound, especially that made by a clock or watch, typically every second.”

A timer set with Io::SetTimer has expired.

Provided Methods

Initialize the state machine. Called once before any event is sent to the state machine.

Implementors