use std::fmt::Debug;
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering::Relaxed;
use tokio::net::TcpStream;
static CONNECTION_COUNTER: AtomicU32 = AtomicU32::new(0);
pub type ConnectionId = u32;
#[derive(Debug)]
pub struct SocketConnection<StateType: Debug> {
connection: TcpStream,
closed: bool,
id: ConnectionId,
state: StateType,
}
impl<StateType: Debug> SocketConnection<StateType> {
pub fn new(connection: TcpStream, initial_state: StateType) -> Self {
Self {
connection,
closed: false,
id: CONNECTION_COUNTER.fetch_add(1, Relaxed),
state: initial_state,
}
}
pub fn connection(&self) -> &TcpStream {
&self.connection
}
pub fn connection_mut(&mut self) -> &mut TcpStream {
&mut self.connection
}
pub fn id(&self) -> ConnectionId {
self.id
}
pub fn state(&self) -> &StateType {
&self.state
}
pub fn set_state(&mut self, new_state: StateType) {
self.state = new_state;
}
pub fn closed(&self) -> bool {
self.closed
}
pub fn report_closed(&mut self) {
self.closed = true;
}
}