2025-06-04 22:52:20 -06:00
|
|
|
use std::{
|
|
|
|
|
io::{self, BufRead, BufReader, Write},
|
2025-06-06 19:20:49 -06:00
|
|
|
net::{SocketAddr, TcpListener, TcpStream},
|
2025-06-04 22:52:20 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use crate::networkers::{ClientTrait, Connection, ServerTrait};
|
|
|
|
|
|
|
|
|
|
pub struct TCPConnection {
|
|
|
|
|
stream: TcpStream,
|
|
|
|
|
reader: BufReader<TcpStream>,
|
2025-06-06 19:20:49 -06:00
|
|
|
is_alive: bool,
|
2025-06-04 22:52:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Connection for TCPConnection {
|
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
2025-06-06 19:20:49 -06:00
|
|
|
fn get_info(&self) -> String {
|
|
|
|
|
format!(
|
|
|
|
|
"tcp://{}",
|
|
|
|
|
if let Ok(addr) = &self.stream.peer_addr() {
|
|
|
|
|
addr.to_string()
|
|
|
|
|
} else {
|
|
|
|
|
"ERROR".to_string()
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_alive(&self) -> bool {
|
|
|
|
|
self.is_alive
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-04 22:52:20 -06:00
|
|
|
fn read(&mut self) -> Result<String, Self::Error> {
|
|
|
|
|
let mut line = String::new();
|
2025-06-06 19:20:49 -06:00
|
|
|
let n = self.reader.read_line(&mut line)?;
|
|
|
|
|
|
|
|
|
|
// Stream sends a null buffer if it is disconnected
|
|
|
|
|
if n == 0 {
|
|
|
|
|
self.is_alive = false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-04 22:52:20 -06:00
|
|
|
Ok(line.trim_end().to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write(&mut self, data: &str) -> Result<(), Self::Error> {
|
|
|
|
|
writeln!(self.stream, "{}", data)?;
|
2025-06-06 19:20:49 -06:00
|
|
|
self.stream.flush()?;
|
|
|
|
|
Ok(())
|
2025-06-04 22:52:20 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct TCPServer {
|
|
|
|
|
listener: TcpListener,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ServerTrait<TCPConnection> for TCPServer {
|
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
2025-06-06 19:20:49 -06:00
|
|
|
fn get_info(&self) -> String {
|
|
|
|
|
format!(
|
|
|
|
|
"tcp://{}",
|
|
|
|
|
if let Ok(addr) = &self.listener.local_addr() {
|
|
|
|
|
addr.to_string()
|
|
|
|
|
} else {
|
|
|
|
|
"ERROR".to_string()
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn accept(&self) -> Result<TCPConnection, Self::Error> {
|
2025-06-04 22:52:20 -06:00
|
|
|
let (stream, _) = self.listener.accept()?;
|
|
|
|
|
let reader = BufReader::new(stream.try_clone()?);
|
2025-06-06 19:20:49 -06:00
|
|
|
Ok(TCPConnection {
|
|
|
|
|
stream,
|
|
|
|
|
reader,
|
|
|
|
|
is_alive: true,
|
|
|
|
|
})
|
2025-06-04 22:52:20 -06:00
|
|
|
}
|
|
|
|
|
|
2025-06-06 19:20:49 -06:00
|
|
|
fn bind(address: &SocketAddr) -> Result<Self, Self::Error> {
|
2025-06-04 22:52:20 -06:00
|
|
|
let listener = TcpListener::bind(address)?;
|
|
|
|
|
Ok(Self { listener })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct TCPClient;
|
|
|
|
|
|
|
|
|
|
impl ClientTrait<TCPConnection> for TCPClient {
|
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
2025-06-06 19:20:49 -06:00
|
|
|
fn connect(address: &SocketAddr) -> Result<TCPConnection, Self::Error> {
|
2025-06-04 22:52:20 -06:00
|
|
|
let stream = TcpStream::connect(address)?;
|
|
|
|
|
let reader = BufReader::new(stream.try_clone()?);
|
2025-06-06 19:20:49 -06:00
|
|
|
let conn = TCPConnection {
|
|
|
|
|
stream,
|
|
|
|
|
reader,
|
|
|
|
|
is_alive: true,
|
|
|
|
|
};
|
|
|
|
|
info!("Connected to {}", conn.get_info());
|
|
|
|
|
Ok(conn)
|
2025-06-04 22:52:20 -06:00
|
|
|
}
|
|
|
|
|
}
|