5 use connect::{ConnectDatagram, ConnectionReader, ConnectionWriter, StreamExt};
8 use tokio::sync::Mutex as AsyncMutex;
10 pub type ClientId = u64;
14 pub conn: AsyncMutex<ConnectionWriter>,
15 pub server: Weak<ServerData>,
20 async fn login(&self, pkt: &Login) {
22 "Client {id}: logged in {name} {pwd}",
29 async fn handle(&self, msg: &ConnectDatagram) {
30 match msg.recipient() {
31 LOGIN if let Some(pkt) = pkt::get::<Login>(msg) => self.login(&pkt).await,
32 _ => warn!("Client {id}: Invalid packet with recipient {rep}",
33 id = self.id, rep = msg.recipient()),
37 pub async fn run(&self, mut reader: ConnectionReader) {
38 let mut quit = self.quit.subscribe();
42 msg = reader.next() => match msg {
43 Some(msg) => self.handle(&msg).await,
45 trace!("Client {id}: Closed connection", id = self.id);
50 trace!("Client {id}: Quit signal received", id = self.id);
53 else => unreachable!("Quit channel broke"),
57 if let Some(server) = self.server.upgrade() {
58 server.clients_by_id.write().unwrap().remove(&self.id);
59 trace!("Client {id}: Removed from clients", id = self.id);
62 info!("Client {id}: Disconnected", id = self.id);