]> git.lizzy.rs Git - dragonblocks-rs.git/blob - src/client.rs
Initial commit
[dragonblocks-rs.git] / src / client.rs
1 use super::pkt;
2 use super::quit::Quit;
3 use super::server::pkts as server_bound;
4 use connect::{Connection, ConnectionReader, ConnectionWriter, StreamExt};
5 use tokio::sync::Mutex as AsyncMutex;
6
7 pub struct Client {
8     pub conn: AsyncMutex<ConnectionWriter>,
9     quit: Quit,
10 }
11
12 impl Client {
13     pub async fn run(addr: &str, quit: Quit) {
14         let (reader, writer) = Connection::tcp_client(addr).await.unwrap().split();
15
16         println!("client connect {addr}");
17         Self {
18             conn: AsyncMutex::new(writer),
19             quit,
20         }
21         .run_loop(reader)
22         .await;
23     }
24
25     async fn run_loop(self, mut reader: ConnectionReader) {
26         let mut quit = self.quit.subscribe();
27
28         pkt::put(
29             &self.conn,
30             server_bound::LOGIN,
31             &server_bound::Login {
32                 name: "player".into(),
33                 pwd: "password123".into(),
34             },
35         )
36         .await;
37
38         loop {
39             tokio::select! {
40                 Some(msg) = reader.next() => {
41                     println!("{}", msg.recipient());
42                 },
43                 _ = quit.recv() => break,
44                 else => break,
45             }
46         }
47
48         println!("client disconnect");
49     }
50 }