mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-28 21:50:26 +08:00
node/reactor: Remove unnecessary functions/types
The functions - `Session::run_handshake` - `Session::stream` - `Session::disconnect` are unused. The associated type `Session::Inner` is unused. The function `Session::display` is of limited use with little benefit. `Transport::link_direction` is only used in implementations of `Debug` and `Display`, but is not helpful otherwise.
This commit is contained in:
@@ -2,7 +2,7 @@ use std::error;
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{Shutdown, SocketAddr};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use cyphernet::encrypt::noise::NoiseState;
|
||||
use cyphernet::proxy::socks5;
|
||||
@@ -15,28 +15,13 @@ pub type NoiseSession<E, D, S> = Protocol<NoiseState<E, D>, S>;
|
||||
pub type Socks5Session<S> = Protocol<socks5::Socks5, S>;
|
||||
|
||||
pub trait Session: Send + Read + Write {
|
||||
type Inner: Session;
|
||||
type Artifact: Display;
|
||||
|
||||
fn is_established(&self) -> bool {
|
||||
self.artifact().is_some()
|
||||
}
|
||||
|
||||
fn run_handshake(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn display(&self) -> String {
|
||||
self.artifact()
|
||||
.map(|artifact| artifact.to_string())
|
||||
.unwrap_or_else(|| "<no-id>".to_string())
|
||||
}
|
||||
|
||||
fn artifact(&self) -> Option<Self::Artifact>;
|
||||
|
||||
fn stream(&mut self) -> &mut TcpStream;
|
||||
|
||||
fn disconnect(self) -> io::Result<()>;
|
||||
}
|
||||
|
||||
pub trait StateMachine: Sized + Send {
|
||||
@@ -52,34 +37,6 @@ pub trait StateMachine: Sized + Send {
|
||||
|
||||
fn artifact(&self) -> Option<Self::Artifact>;
|
||||
|
||||
// Blocking
|
||||
fn run_handshake<RW>(&mut self, stream: &mut RW) -> io::Result<()>
|
||||
where
|
||||
RW: Read + Write,
|
||||
{
|
||||
let mut input = vec![];
|
||||
while !self.is_complete() {
|
||||
let act = self.advance(&input).map_err(|err| {
|
||||
log::error!(target: Self::NAME, "Handshake failure: {err}");
|
||||
io::Error::other(err)
|
||||
})?;
|
||||
if !act.is_empty() {
|
||||
log::trace!(target: Self::NAME, "Sending handshake act {act:02x?}");
|
||||
|
||||
stream.write_all(&act)?;
|
||||
}
|
||||
if !self.is_complete() {
|
||||
input = vec![0u8; self.next_read_len()];
|
||||
stream.read_exact(&mut input)?;
|
||||
|
||||
log::trace!(target: Self::NAME, "Receiving handshake act {input:02x?}");
|
||||
}
|
||||
}
|
||||
|
||||
log::debug!(target: Self::NAME, "Handshake protocol {} successfully completed", Self::NAME);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_complete(&self) -> bool {
|
||||
self.artifact().is_some()
|
||||
}
|
||||
@@ -185,33 +142,14 @@ impl<M: StateMachine, S: Session> Write for Protocol<M, S> {
|
||||
}
|
||||
|
||||
impl<M: StateMachine, S: Session> Session for Protocol<M, S> {
|
||||
type Inner = S;
|
||||
type Artifact = ProtocolArtifact<M, S>;
|
||||
|
||||
fn run_handshake(&mut self) -> io::Result<()> {
|
||||
log::debug!(target: M::NAME, "Starting handshake protocol {}", M::NAME);
|
||||
|
||||
if !self.session.is_established() {
|
||||
self.session.run_handshake()?;
|
||||
}
|
||||
|
||||
self.state.run_handshake(self.session.stream())
|
||||
}
|
||||
|
||||
fn artifact(&self) -> Option<Self::Artifact> {
|
||||
Some(ProtocolArtifact {
|
||||
session: self.session.artifact()?,
|
||||
state: self.state.artifact()?,
|
||||
})
|
||||
}
|
||||
|
||||
fn stream(&mut self) -> &mut TcpStream {
|
||||
self.session.stream()
|
||||
}
|
||||
|
||||
fn disconnect(self) -> io::Result<()> {
|
||||
self.session.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<M: StateMachine, S: Session + Source> Source for Protocol<M, S> {
|
||||
@@ -239,20 +177,11 @@ impl<M: StateMachine, S: Session + Source> Source for Protocol<M, S> {
|
||||
}
|
||||
|
||||
impl Session for TcpStream {
|
||||
type Inner = Self;
|
||||
type Artifact = SocketAddr;
|
||||
|
||||
fn artifact(&self) -> Option<Self::Artifact> {
|
||||
self.peer_addr().ok()
|
||||
}
|
||||
|
||||
fn stream(&mut self) -> &mut TcpStream {
|
||||
self
|
||||
}
|
||||
|
||||
fn disconnect(self) -> io::Result<()> {
|
||||
self.shutdown(Shutdown::Both)
|
||||
}
|
||||
}
|
||||
|
||||
mod impl_noise {
|
||||
|
||||
@@ -5,7 +5,6 @@ use std::{fmt, io};
|
||||
|
||||
use mio::event::{Event, Source};
|
||||
use mio::{Interest, Registry, Token};
|
||||
use radicle::node::Link;
|
||||
|
||||
use crate::reactor::session::Session;
|
||||
use crate::reactor::{EventHandler, WriteAtomic};
|
||||
@@ -54,7 +53,6 @@ pub enum TransportState {
|
||||
pub struct Transport<S: Session> {
|
||||
state: TransportState,
|
||||
session: S,
|
||||
link_direction: Link,
|
||||
write_intent: bool,
|
||||
read_buffer: Box<[u8; READ_BUFFER_SIZE]>,
|
||||
write_buffer: VecDeque<u8>,
|
||||
@@ -63,9 +61,7 @@ pub struct Transport<S: Session> {
|
||||
impl<S: Session> std::fmt::Debug for Transport<S> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Transport")
|
||||
.field("session", &self.session.display())
|
||||
.field("state", &self.state)
|
||||
.field("link_direction", &self.link_direction)
|
||||
.field("write_intent", &self.write_intent)
|
||||
.finish()
|
||||
}
|
||||
@@ -101,7 +97,6 @@ impl<S: Session> Display for Transport<S> {
|
||||
None => f
|
||||
.debug_struct("Transport")
|
||||
.field("state", &self.state)
|
||||
.field("link_direction", &self.link_direction)
|
||||
.field("write_intent", &self.write_intent)
|
||||
.finish(),
|
||||
Some(id) => Display::fmt(&id, f),
|
||||
@@ -117,7 +112,7 @@ impl<S: Session> Transport<S> {
|
||||
/// # Errors
|
||||
///
|
||||
/// If a session can be put into a non-blocking mode.
|
||||
pub fn with_session(session: S, link_direction: Link) -> io::Result<Self> {
|
||||
pub fn with_session(session: S) -> io::Result<Self> {
|
||||
let state = if session.is_established() {
|
||||
// If we are disconnected, we will get instantly updated from the
|
||||
// reactor and the state will change automatically
|
||||
@@ -128,7 +123,6 @@ impl<S: Session> Transport<S> {
|
||||
Ok(Self {
|
||||
state,
|
||||
session,
|
||||
link_direction,
|
||||
write_intent: true,
|
||||
read_buffer: Box::new([0u8; READ_BUFFER_SIZE]),
|
||||
write_buffer: VecDeque::new(),
|
||||
@@ -136,7 +130,10 @@ impl<S: Session> Transport<S> {
|
||||
}
|
||||
|
||||
pub fn display(&self) -> impl Display {
|
||||
self.session.display()
|
||||
self.session
|
||||
.artifact()
|
||||
.map(|artifact| artifact.to_string())
|
||||
.unwrap_or_else(|| "<no-artifact>".to_string())
|
||||
}
|
||||
|
||||
fn terminate(&mut self, reason: io::Error) -> SessionEvent<S> {
|
||||
|
||||
@@ -544,7 +544,7 @@ where
|
||||
connection,
|
||||
self.signer.clone().into_inner(),
|
||||
);
|
||||
let transport = match Transport::with_session(session, Link::Inbound) {
|
||||
let transport = match Transport::with_session(session) {
|
||||
Ok(transport) => transport,
|
||||
Err(err) => {
|
||||
log::warn!(target: "wire", "Failed to create transport for accepted connection: {err}");
|
||||
@@ -968,9 +968,8 @@ where
|
||||
self.signer.clone().into_inner(),
|
||||
self.service.config(),
|
||||
)
|
||||
.and_then(|session| {
|
||||
Transport::<WireSession<G>>::with_session(session, Link::Outbound)
|
||||
}) {
|
||||
.and_then(Transport::<WireSession<G>>::with_session)
|
||||
{
|
||||
Ok(transport) => {
|
||||
let token = self.tokens.advance();
|
||||
self.outbound.insert(
|
||||
|
||||
Reference in New Issue
Block a user