完善服务端功能
This commit is contained in:
@@ -6,9 +6,11 @@
|
||||
|
||||
pub mod Client;
|
||||
pub mod accept;
|
||||
pub mod event;
|
||||
|
||||
use crate::close_sender::CloseSender;
|
||||
use crate::server::accept::AcceptSocket;
|
||||
use crate::server::event::ServerEvent;
|
||||
use crate::ssl::ServerCert;
|
||||
use async_trait::async_trait;
|
||||
use log::{debug, error};
|
||||
@@ -32,30 +34,40 @@ pub struct Server {
|
||||
client_list: ClientList,
|
||||
next_id: AtomicU64,
|
||||
cert: Arc<ServerCert>,
|
||||
event: Arc<dyn ServerEvent>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
fn new(listener: Box<dyn AcceptSocket>, cert: ServerCert) -> Self {
|
||||
fn new(listener: Box<dyn AcceptSocket>, cert: ServerCert, event: Arc<dyn ServerEvent>) -> Self {
|
||||
Server {
|
||||
listener,
|
||||
close_sender: CloseSender::new(),
|
||||
client_list: Arc::new(Mutex::new(HashMap::new())),
|
||||
next_id: AtomicU64::new(0),
|
||||
cert: Arc::new(cert),
|
||||
event,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn new_tcp<A: ToSocketAddrs>(addr: A, cert: ServerCert) -> io::Result<Self> {
|
||||
pub async fn new_tcp<A: ToSocketAddrs>(
|
||||
addr: A,
|
||||
cert: ServerCert,
|
||||
event: Arc<dyn ServerEvent>,
|
||||
) -> io::Result<Self> {
|
||||
let listener = TcpListener::bind(addr).await?;
|
||||
Ok(Server::new(Box::new(listener), cert))
|
||||
Ok(Server::new(Box::new(listener), cert, event))
|
||||
}
|
||||
|
||||
pub async fn new_unix<P>(path: P, cert: ServerCert) -> io::Result<Self>
|
||||
pub async fn new_unix<P>(
|
||||
path: P,
|
||||
cert: ServerCert,
|
||||
event: Arc<dyn ServerEvent>,
|
||||
) -> io::Result<Self>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let unix = UnixListener::bind(path)?;
|
||||
Ok(Server::new(Box::new(unix), cert))
|
||||
Ok(Server::new(Box::new(unix), cert, event))
|
||||
}
|
||||
|
||||
/// 广播关闭消息
|
||||
@@ -69,15 +81,18 @@ impl Server {
|
||||
}
|
||||
|
||||
/// 挂断一个客户端
|
||||
pub async fn close_client(&self, id: ClientID) {
|
||||
Self::close_client_form_arc(&self.client_list, id).await;
|
||||
pub async fn close_client(&self, client: &Arc<Client::Client>) {
|
||||
Self::close_client_form_arc(&self.client_list, client).await;
|
||||
}
|
||||
|
||||
pub async fn close_client_form_arc(list: &ClientList, id: ClientID) {
|
||||
pub async fn close_client_form_arc(list: &ClientList, client: &Arc<Client::Client>) {
|
||||
let client_id = client.id;
|
||||
let mut lock = list.lock().await;
|
||||
if let Some(client) = lock.get(&id) {
|
||||
if let Some(client) = lock.get(&client_id) {
|
||||
//向使用者报告客户端关闭
|
||||
client.event.client_close_listener(client.clone()).await;
|
||||
client.close().await;
|
||||
lock.remove(&id);
|
||||
lock.remove(&client_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,17 +124,21 @@ impl Server {
|
||||
id,
|
||||
addr,
|
||||
self.cert.clone(),
|
||||
self.event.clone(),
|
||||
));
|
||||
|
||||
let mut lock = self.client_list.lock().await;
|
||||
lock.insert(id, client.clone());
|
||||
drop(lock);
|
||||
|
||||
//向使用者报告新的客户端连入
|
||||
self.event.client_linker_listener(client.clone()).await;
|
||||
|
||||
let list = self.get_client_list();
|
||||
tokio::spawn(async move {
|
||||
client.start().await;
|
||||
client.clone().start().await;
|
||||
//当连接的事件轮退出,则自动挂断
|
||||
Self::close_client_form_arc(&list, id).await;
|
||||
Self::close_client_form_arc(&list, &client).await;
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user