recon_core/session.rs
1//! What a protocol learns about the sessions carrying its messages.
2
3use crate::NodeId;
4
5/// Something happened to the session with a peer.
6///
7/// These are two distinct events and neither substitutes for the other, because a real endpoint
8/// learns them separately and can act on only one.
9///
10/// An **ending** is synchronous and knowable: the operating system closes the handle and the next
11/// read or write fails, so a protocol learns at the moment of failure that its last writes may be
12/// gone. It cannot act on that, the peer being unreachable.
13///
14/// An **establishment** is what can be acted on. It happens when the link manages to reconnect —
15/// a deployed link keeps trying on its own — so it arrives at a moment the layers above neither
16/// choose nor control.
17///
18/// This is a domain concept, not a simulator one: a simulated network and an adapter over TCP or
19/// QUIC report the same thing, because it is what a real endpoint learns.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
21pub enum SessionEvent {
22 /// The session with `peer` has ended. An unknown suffix of what was in flight may be lost.
23 ///
24 /// `epoch` is the one that **ended**, not a prediction of the next: at the moment of failure
25 /// the next epoch is not a fact, and may never become one.
26 Ended { peer: NodeId, epoch: u64 },
27 /// A session with `peer` is in force at `epoch`. The peer can be reached again.
28 ///
29 /// This is the event on which anything must be resent, and the only one on which a resend
30 /// can succeed.
31 Established { peer: NodeId, epoch: u64 },
32}
33
34impl SessionEvent {
35 pub fn peer(&self) -> NodeId {
36 match self {
37 SessionEvent::Ended { peer, .. } | SessionEvent::Established { peer, .. } => *peer,
38 }
39 }
40
41 pub fn epoch(&self) -> u64 {
42 match self {
43 SessionEvent::Ended { epoch, .. } | SessionEvent::Established { epoch, .. } => *epoch,
44 }
45 }
46}