use std::net;
use crate::half_connection::HalfConnection;
use crate::SendMode;
use crate::CHANNEL_COUNT;
pub (super) enum DisconnectMode {
Now,
Flush,
}
pub (super) struct PendingState {
pub local_nonce: u32,
pub remote_nonce: u32,
pub remote_max_receive_rate: u32,
pub remote_max_receive_alloc: u32,
pub reply_bytes: Box<[u8]>,
}
pub (super) struct ActiveState {
pub half_connection: HalfConnection,
pub timeout_time_ms: u64,
pub disconnect_signal: Option<DisconnectMode>,
}
pub (super) enum State {
Pending(PendingState),
Active(ActiveState),
Closing,
Closed,
Fin,
}
pub struct RemoteClient {
pub (super) address: net::SocketAddr,
pub (super) state: State,
pub (super) max_packet_size: usize,
}
impl RemoteClient {
pub fn is_active(&self) -> bool {
match self.state {
State::Active(_) => true,
_ => false,
}
}
pub fn send(&mut self, data: Box<[u8]>, channel_id: usize, mode: SendMode) {
assert!(data.len() <= self.max_packet_size,
"send failed: packet of size {} exceeds configured maximum of {}",
data.len(),
self.max_packet_size);
assert!(channel_id < CHANNEL_COUNT,
"send failed: channel ID {} is invalid",
channel_id);
match self.state {
State::Active(ref mut state) => {
state.half_connection.send(data, channel_id as u8, mode);
}
_ => (),
}
}
pub fn disconnect(&mut self) {
match self.state {
State::Active(ref mut state) => {
state.disconnect_signal = Some(DisconnectMode::Flush);
}
_ => (),
}
}
pub fn disconnect_now(&mut self) {
match self.state {
State::Active(ref mut state) => {
state.disconnect_signal = Some(DisconnectMode::Now);
}
_ => (),
}
}
pub fn rtt_s(&self) -> Option<f64> {
match self.state {
State::Active(ref state) => state.half_connection.rtt_s(),
_ => None,
}
}
pub fn send_buffer_size(&self) -> usize {
match self.state {
State::Active(ref state) => state.half_connection.send_buffer_size(),
_ => 0,
}
}
}