mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-29 05:50:31 +08:00
93 lines
2.4 KiB
Rust
93 lines
2.4 KiB
Rust
use crate::service::message::*;
|
|
use crate::service::*;
|
|
|
|
#[derive(Debug, Default)]
|
|
#[allow(clippy::large_enum_variant)]
|
|
pub enum SessionState {
|
|
/// Initial peer state. For outgoing peers this
|
|
/// means we've attempted a connection. For incoming
|
|
/// peers, this means they've successfully connected
|
|
/// to us.
|
|
#[default]
|
|
Initial,
|
|
/// State after successful handshake.
|
|
Negotiated {
|
|
/// The peer's unique identifier.
|
|
id: NodeId,
|
|
since: LocalTime,
|
|
/// Addresses this peer is reachable on.
|
|
addrs: Vec<Address>,
|
|
git: Url,
|
|
},
|
|
/// When a peer is disconnected.
|
|
Disconnected { since: LocalTime },
|
|
}
|
|
|
|
#[derive(thiserror::Error, Debug, Clone)]
|
|
pub enum SessionError {
|
|
#[error("wrong network constant in message: {0}")]
|
|
WrongMagic(u32),
|
|
#[error("wrong protocol version in message: {0}")]
|
|
WrongVersion(u32),
|
|
#[error("invalid announcement timestamp: {0}")]
|
|
InvalidTimestamp(u64),
|
|
#[error("session not found for address `{0}`")]
|
|
NotFound(net::IpAddr),
|
|
#[error("peer misbehaved")]
|
|
Misbehavior,
|
|
}
|
|
|
|
/// A peer session. Each connected peer will have one session.
|
|
#[derive(Debug)]
|
|
pub struct Session {
|
|
/// Peer address.
|
|
pub addr: net::SocketAddr,
|
|
/// Connection direction.
|
|
pub link: Link,
|
|
/// Whether we should attempt to re-connect
|
|
/// to this peer upon disconnection.
|
|
pub persistent: bool,
|
|
/// Peer connection state.
|
|
pub state: SessionState,
|
|
/// Peer subscription.
|
|
pub subscribe: Option<Subscribe>,
|
|
|
|
/// Connection attempts. For persistent peers, Tracks
|
|
/// how many times we've attempted to connect. We reset this to zero
|
|
/// upon successful connection.
|
|
attempts: usize,
|
|
}
|
|
|
|
impl Session {
|
|
pub fn new(addr: net::SocketAddr, link: Link, persistent: bool) -> Self {
|
|
Self {
|
|
addr,
|
|
state: SessionState::default(),
|
|
link,
|
|
subscribe: None,
|
|
persistent,
|
|
attempts: 0,
|
|
}
|
|
}
|
|
|
|
pub fn ip(&self) -> IpAddr {
|
|
self.addr.ip()
|
|
}
|
|
|
|
pub fn is_negotiated(&self) -> bool {
|
|
matches!(self.state, SessionState::Negotiated { .. })
|
|
}
|
|
|
|
pub fn attempts(&self) -> usize {
|
|
self.attempts
|
|
}
|
|
|
|
pub fn attempted(&mut self) {
|
|
self.attempts += 1;
|
|
}
|
|
|
|
pub fn connected(&mut self, _link: Link) {
|
|
self.attempts = 0;
|
|
}
|
|
}
|