bitfold_host/
session.rs

1use std::{fmt::Debug, net::SocketAddr, time::Instant};
2
3use bitfold_core::config::Config;
4
5use super::event_types::Action;
6
7/// Returns an address of an event.
8pub trait SessionEventAddress {
9    /// Returns event address
10    fn address(&self) -> SocketAddr;
11}
12
13/// Manages the lifecycle and state of a peer session.
14/// Defines a type of `Send` and `Receive` events used by a session.
15pub trait Session: Debug {
16    /// Defines a user event type.
17    type SendEvent: Debug + SessionEventAddress;
18    /// Defines a session event type.
19    type ReceiveEvent: Debug + SessionEventAddress;
20
21    /// Creates new session and initialize it.
22    fn create_session(config: &Config, address: SocketAddr, time: Instant) -> Self;
23
24    /// Sessions are considered established once they have both had a send and a receive.
25    fn is_established(&self) -> bool;
26
27    /// Determines if the session should be dropped due to its state.
28    fn should_drop(&mut self, time: Instant) -> (bool, Vec<Action<Self::ReceiveEvent>>);
29
30    /// Processes a received packet: parse it and emit an event.
31    fn process_packet(&mut self, payload: &[u8], time: Instant) -> Vec<Action<Self::ReceiveEvent>>;
32
33    /// Processes a received event and send a packet.
34    fn process_event(
35        &mut self,
36        event: Self::SendEvent,
37        time: Instant,
38    ) -> Vec<Action<Self::ReceiveEvent>>;
39
40    /// Processes session-related tasks: resend dropped packets, send heartbeat, etc.
41    fn update(&mut self, time: Instant) -> Vec<Action<Self::ReceiveEvent>>;
42}