add channel open support

This commit is contained in:
nora 2022-02-19 23:25:06 +01:00
parent dc8efd4e4e
commit 46cccab748
10 changed files with 1950 additions and 1462 deletions

View file

@ -4,6 +4,8 @@ use std::net::SocketAddr;
use std::sync::Arc;
use uuid::Uuid;
type Handle<T> = Arc<Mutex<T>>;
#[derive(Debug, Clone)]
pub struct GlobalData {
inner: Arc<Mutex<GlobalDataInner>>,
@ -14,6 +16,7 @@ impl Default for GlobalData {
Self {
inner: Arc::new(Mutex::new(GlobalDataInner {
connections: HashMap::new(),
channels: HashMap::new(),
})),
}
}
@ -28,15 +31,17 @@ impl GlobalData {
#[derive(Debug)]
pub struct GlobalDataInner {
pub connections: HashMap<Uuid, ConnectionHandle>,
pub channels: HashMap<Uuid, ChannelHandle>,
}
pub type ConnectionHandle = Arc<Mutex<Connection>>;
pub type ConnectionHandle = Handle<Connection>;
#[derive(Debug)]
pub struct Connection {
pub id: Uuid,
pub peer_addr: SocketAddr,
pub global_data: GlobalData,
pub channels: HashMap<u16, ChannelHandle>,
}
impl Connection {
@ -49,6 +54,7 @@ impl Connection {
id,
peer_addr,
global_data,
channels: HashMap::new(),
}))
}
@ -57,3 +63,34 @@ impl Connection {
global_data.connections.remove(&self.id);
}
}
pub type ChannelHandle = Handle<Channel>;
#[derive(Debug)]
pub struct Channel {
pub id: Uuid,
pub num: u16,
pub connection: ConnectionHandle,
pub global_data: GlobalData,
}
impl Channel {
pub fn new_handle(
id: Uuid,
num: u16,
connection: ConnectionHandle,
global_data: GlobalData,
) -> ChannelHandle {
Arc::new(Mutex::new(Self {
id,
num,
connection,
global_data,
}))
}
pub fn close(&self) {
let mut global_data = self.global_data.lock();
global_data.channels.remove(&self.id);
}
}