2021-11-22 16:28:30 +00:00
|
|
|
use crate::intf::*;
|
|
|
|
use crate::xx::*;
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ConnectionTableEntry {
|
|
|
|
pub conn: NetworkConnection,
|
|
|
|
pub established_time: u64,
|
|
|
|
pub last_message_sent_time: Option<u64>,
|
|
|
|
pub last_message_recv_time: Option<u64>,
|
|
|
|
pub stopper: Eventual,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for ConnectionTableEntry {
|
|
|
|
fn eq(&self, other: &ConnectionTableEntry) -> bool {
|
|
|
|
if self.conn != other.conn {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if self.established_time != other.established_time {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if self.last_message_sent_time != other.last_message_sent_time {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if self.last_message_recv_time != other.last_message_recv_time {
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-27 17:44:21 +00:00
|
|
|
true
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ConnectionTable {
|
2022-01-03 04:49:01 +00:00
|
|
|
conn_by_addr: BTreeMap<ConnectionDescriptor, ConnectionTableEntry>,
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConnectionTable {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2022-01-03 04:49:01 +00:00
|
|
|
conn_by_addr: BTreeMap::new(),
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_connection(
|
2022-01-03 04:49:01 +00:00
|
|
|
&mut self,
|
2021-11-22 16:28:30 +00:00
|
|
|
conn: NetworkConnection,
|
2021-12-14 14:48:33 +00:00
|
|
|
) -> Result<ConnectionTableEntry, String> {
|
2022-01-03 04:49:01 +00:00
|
|
|
let descriptor = conn.connection_descriptor();
|
2021-12-14 14:48:33 +00:00
|
|
|
|
2021-11-22 16:28:30 +00:00
|
|
|
assert_ne!(
|
|
|
|
descriptor.protocol_type(),
|
|
|
|
ProtocolType::UDP,
|
|
|
|
"Only connection oriented protocols go in the table!"
|
|
|
|
);
|
|
|
|
|
2022-01-03 04:49:01 +00:00
|
|
|
if self.conn_by_addr.contains_key(&descriptor) {
|
2021-12-14 14:48:33 +00:00
|
|
|
return Err(format!(
|
|
|
|
"Connection already added to table: {:?}",
|
|
|
|
descriptor
|
|
|
|
));
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let timestamp = get_timestamp();
|
|
|
|
|
|
|
|
let entry = ConnectionTableEntry {
|
2021-11-27 17:44:21 +00:00
|
|
|
conn,
|
2021-11-22 16:28:30 +00:00
|
|
|
established_time: timestamp,
|
|
|
|
last_message_sent_time: None,
|
|
|
|
last_message_recv_time: None,
|
|
|
|
stopper: Eventual::new(),
|
|
|
|
};
|
2022-01-03 04:49:01 +00:00
|
|
|
let res = self.conn_by_addr.insert(descriptor, entry.clone());
|
2021-11-22 16:28:30 +00:00
|
|
|
assert!(res.is_none());
|
|
|
|
Ok(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_connection(
|
|
|
|
&self,
|
|
|
|
descriptor: &ConnectionDescriptor,
|
|
|
|
) -> Option<ConnectionTableEntry> {
|
2022-01-03 04:49:01 +00:00
|
|
|
self.conn_by_addr.get(descriptor).cloned()
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn connection_count(&self) -> usize {
|
2022-01-03 04:49:01 +00:00
|
|
|
self.conn_by_addr.len()
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_connection(
|
2022-01-03 04:49:01 +00:00
|
|
|
&mut self,
|
2021-11-22 16:28:30 +00:00
|
|
|
descriptor: &ConnectionDescriptor,
|
2021-12-14 14:48:33 +00:00
|
|
|
) -> Result<ConnectionTableEntry, String> {
|
2022-01-03 04:49:01 +00:00
|
|
|
self.conn_by_addr
|
|
|
|
.remove(descriptor)
|
|
|
|
.ok_or_else(|| format!("Connection not in table: {:?}", descriptor))
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|