aboutsummaryrefslogtreecommitdiff
path: root/server/client-lib/src/network/tokio.rs
blob: 3f68e091f4bc44e16a38adee3ef6ec837226d5ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
use anyhow::{anyhow, Result};
use futures_util::{
    stream::{SplitSink, SplitStream, StreamExt},
    SinkExt, TryStreamExt,
};
use hurrycurry_protocol::{PacketC, PacketS, BINCODE_CONFIG, VERSION};
use log::{debug, info, warn};
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::{net::TcpStream, sync::RwLock};
use tokio_tungstenite::{client_async_tls_with_config, MaybeTlsStream, WebSocketStream};
use tungstenite::{
    client::{uri_mode, IntoClientRequest},
    http::Request,
    stream::Mode,
    Message,
};

pub struct Network {
    sock_recv: RwLock<SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>>,
    sock_send: RwLock<SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>,
    use_bincode: AtomicBool,
}

impl Network {
    pub async fn connect(uri: &str) -> Result<Self> {
        let (parts, _) = uri.into_client_request().unwrap().into_parts();
        let mut builder = Request::builder()
            .uri(parts.uri.clone().clone())
            .method(parts.method.clone())
            .version(parts.version);
        *builder.headers_mut().unwrap() = parts.headers.clone();
        let request = builder.body(()).unwrap();

        let host = request.uri().host().unwrap();
        let host = if host.starts_with('[') {
            &host[1..host.len() - 1]
        } else {
            host
        };
        let port = request
            .uri()
            .port_u16()
            .unwrap_or(match uri_mode(request.uri())? {
                Mode::Plain => 27032,
                Mode::Tls => 443,
            });

        info!("Connecting: host={host:?} port={port}");
        let stream = TcpStream::connect((host, port)).await?;
        Self::connect_raw(stream, uri).await
    }
    pub async fn connect_raw(stream: TcpStream, uri: &str) -> Result<Self> {
        let (parts, _) = uri.into_client_request()?.into_parts();
        let mut builder = Request::builder()
            .uri(parts.uri.clone().clone())
            .method(parts.method.clone())
            .version(parts.version);
        *builder.headers_mut().ok_or(anyhow!("???"))? = parts.headers.clone();
        let request = builder.body(())?;

        stream.set_nodelay(true)?;
        let (sock, _) = client_async_tls_with_config(request, stream, None, None).await?;
        info!("Handshake complete.");
        let (sock_send, sock_recv) = sock.split();
        Ok(Self {
            sock_recv: RwLock::new(sock_recv),
            sock_send: RwLock::new(sock_send),
            use_bincode: false.into(),
        })
    }

    pub async fn receive(&self) -> anyhow::Result<Option<PacketC>> {
        let mut g = self.sock_recv.write().await;
        loop {
            match g.try_next().await? {
                Some(Message::Text(packet)) => match serde_json::from_str(&packet) {
                    Ok(packet) => {
                        debug!("<- {packet:?}");
                        if let PacketC::Version {
                            minor,
                            major,
                            supports_bincode,
                        } = &packet
                        {
                            if *minor == VERSION.0 && *major == VERSION.1 && *supports_bincode {
                                info!("Binary protocol format enabled.");
                                self.use_bincode.store(true, Ordering::Relaxed);
                            }
                        }
                        return Ok(Some(packet));
                    }
                    Err(e) => {
                        warn!("invalid json packet: {e:?}");
                    }
                },
                Some(Message::Binary(packet)) => {
                    match bincode::decode_from_slice(&packet, BINCODE_CONFIG) {
                        Ok((packet, _)) => {
                            debug!("<- {packet:?}");
                            return Ok(Some(packet));
                        }
                        Err(e) => {
                            warn!("invalid bincode packet: {e:?}");
                        }
                    }
                }
                _ => (),
            };
        }
    }
    pub async fn send(&self, packet: PacketS) -> anyhow::Result<()> {
        debug!("-> {packet:?}");
        let mut g = self.sock_send.write().await;
        if self.use_bincode.load(Ordering::Relaxed) {
            g.send(Message::Binary(
                bincode::encode_to_vec(&packet, BINCODE_CONFIG).unwrap(),
            ))
            .await?;
        } else {
            g.send(Message::Text(serde_json::to_string(&packet).unwrap()))
                .await?;
        }
        Ok(())
    }
}