Appearance
presentation: width: 1024 height: 800
需求
- listen & accept
- new client
- 其他暂不考虑
数据结构定义
rust
#[derive(Debug, Default)]
pub struct Server<T: SubListTrait> {
state: Arc<Mutex<ServerState<T>>>,
}
#[derive(Debug, Default)]
pub struct ServerState<T: SubListTrait> {
clients: HashMap<u64, Arc<Mutex<ClientMessageSender>>>,
pub sublist: T,
pub gen_cid: u64,
}
泛型 & async
- 使用async
- 在task之间传送
rust
impl<T: SubListTrait + Send + 'static> Server<T> {
}
实现
- start 主要任务就是listen & accept
- new_client则是调用Client的process_connection 创建Client实例, 并保存.
rust
impl<T: SubListTrait + Send + 'static> Server<T> {
pub async fn start(&self) -> Result<(), Box<dyn Error>> {
}
async fn new_client(&self, conn: TcpStream) {
}
}
使用
rust
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
println!("server start..");
let s: Server<SimpleSubList> = Server::default();
s.start().await
}