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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// TODO
#![allow(missing_docs)]

use bones_matchmaker_proto::PLAY_ALPN;
use bytes::Bytes;
use iroh_net::NodeAddr;
use tracing::{info, warn};

use crate::networking::get_network_endpoint;

use super::{GameMessage, NetworkSocket, SocketTarget, MAX_PLAYERS, RUNTIME};

/// The [`NetworkSocket`] implementation.
#[derive(Debug, Clone)]
pub struct Socket {
    ///
    pub connections: [Option<iroh_quinn::Connection>; MAX_PLAYERS],
    pub ggrs_receiver: async_channel::Receiver<(usize, GameMessage)>,
    pub reliable_receiver: async_channel::Receiver<(usize, Vec<u8>)>,
    pub player_idx: usize,
    pub player_count: usize,
    /// ID for current match, messages received that do not match ID are dropped.
    pub match_id: u8,
}

impl Socket {
    pub fn new(
        player_idx: usize,
        connections: [Option<iroh_quinn::Connection>; MAX_PLAYERS],
    ) -> Self {
        let (ggrs_sender, ggrs_receiver) = async_channel::unbounded();
        let (reliable_sender, reliable_receiver) = async_channel::unbounded();

        // Spawn tasks to receive network messages from each peer
        #[allow(clippy::needless_range_loop)]
        for i in 0..MAX_PLAYERS {
            if let Some(conn) = connections[i].clone() {
                let ggrs_sender = ggrs_sender.clone();

                // Unreliable message receiver
                let conn_ = conn.clone();
                RUNTIME.spawn(async move {
                    let conn = conn_;

                    #[cfg(feature = "debug-network-slowdown")]
                    use turborand::prelude::*;
                    #[cfg(feature = "debug-network-slowdown")]
                    let rng = AtomicRng::new();

                    loop {
                        tokio::select! {
                            closed = conn.closed() => {
                                warn!("Connection error: {closed}");
                                break;
                            }
                            datagram_result = conn.read_datagram() => match datagram_result {
                                Ok(data) => {
                                    let message: GameMessage = postcard::from_bytes(&data)
                                        .expect("Could not deserialize net message");

                                    // Debugging code to introduce artificial latency
                                    #[cfg(feature = "debug-network-slowdown")]
                                    {
                                        use async_timer::Oneshot;
                                        async_timer::oneshot::Timer::new(
                                            std::time::Duration::from_millis(
                                                (rng.f32_normalized() * 100.0) as u64 + 1,
                                            ),
                                        )
                                        .await;
                                    }
                                    if ggrs_sender.send((i, message)).await.is_err() {
                                        break;
                                    }
                                }
                                Err(e) => {
                                    warn!("Connection error: {e}");
                                }
                            }
                        }
                    }
                });

                // Reliable message receiver
                let reliable_sender = reliable_sender.clone();
                RUNTIME.spawn(async move {
                    #[cfg(feature = "debug-network-slowdown")]
                    use turborand::prelude::*;
                    #[cfg(feature = "debug-network-slowdown")]
                    let rng = AtomicRng::new();

                    loop {
                        tokio::select! {
                            closed = conn.closed() => {
                                warn!("Connection error: {closed}");
                                break;
                            }
                            result = conn.accept_uni() => match result {
                                Ok(mut stream) => {
                                    let data = stream.read_to_end(4096).await.expect("Network read error");

                                    // Debugging code to introduce artificial latency
                                    #[cfg(feature = "debug-network-slowdown")]
                                    {
                                        use async_timer::Oneshot;
                                        async_timer::oneshot::Timer::new(
                                            std::time::Duration::from_millis(
                                                (rng.f32_normalized() * 100.0) as u64 + 1,
                                            ),
                                        )
                                        .await;
                                    }
                                    if reliable_sender.send((i, data)).await.is_err() {
                                        break;
                                    }
                                }
                                Err(e) => {
                                    warn!("Connection error: {e}");
                                }
                            },
                        }
                    }
                });
            }
        }

        Self {
            player_idx,
            player_count: connections.iter().flatten().count() + 1,
            connections,
            ggrs_receiver,
            reliable_receiver,
            match_id: 0,
        }
    }
}

impl NetworkSocket for Socket {
    fn send_reliable(&self, target: SocketTarget, message: &[u8]) {
        let message = Bytes::copy_from_slice(message);

        match target {
            SocketTarget::Player(i) => {
                let conn = self.connections[i].as_ref().unwrap().clone();

                RUNTIME.spawn(async move {
                    let result = async move {
                        let mut stream = conn.open_uni().await?;
                        stream.write_chunk(message).await?;
                        stream.finish().await?;
                        anyhow::Ok(())
                    };
                    if let Err(err) = result.await {
                        warn!("send reliable to {i} failed: {err:?}");
                    }
                });
            }
            SocketTarget::All => {
                for conn in &self.connections {
                    if let Some(conn) = conn.clone() {
                        let message = message.clone();
                        RUNTIME.spawn(async move {
                            let result = async move {
                                let mut stream = conn.open_uni().await?;
                                stream.write_chunk(message).await?;
                                stream.finish().await?;
                                anyhow::Ok(())
                            };
                            if let Err(err) = result.await {
                                warn!("send reliable all failed: {err:?}");
                            }
                        });
                    }
                }
            }
        }
    }

    fn recv_reliable(&self) -> Vec<(usize, Vec<u8>)> {
        let mut messages = Vec::new();
        while let Ok(message) = self.reliable_receiver.try_recv() {
            messages.push(message);
        }
        messages
    }

    fn ggrs_socket(&self) -> Self {
        self.clone()
    }

    fn close(&self) {
        for conn in self.connections.iter().flatten() {
            conn.close(0u8.into(), &[]);
        }
    }

    fn player_idx(&self) -> usize {
        self.player_idx
    }

    fn player_count(&self) -> usize {
        self.player_count
    }

    fn player_is_local(&self) -> [bool; MAX_PLAYERS] {
        std::array::from_fn(|i| self.connections[i].is_none() && i < self.player_count)
    }

    fn increment_match_id(&mut self) {
        self.match_id = self.match_id.wrapping_add(1);
    }
}

pub(super) async fn establish_peer_connections(
    player_idx: usize,
    player_count: usize,
    peer_addrs: [Option<NodeAddr>; MAX_PLAYERS],
    conn: Option<iroh_quinn::Connection>,
) -> anyhow::Result<[Option<iroh_quinn::Connection>; MAX_PLAYERS]> {
    let mut peer_connections = std::array::from_fn(|_| None);

    if let Some(conn) = conn {
        // Set the connection to the matchmaker for player 0
        peer_connections[0] = Some(conn);
    }

    let ep = get_network_endpoint().await;

    // For every peer with a player index that is higher than ours, wait for
    // them to connect to us.
    let range = (player_idx + 1)..player_count;
    info!(players=?range, "Waiting for {} peer connections", range.len());
    for i in range {
        // Wait for connection
        let mut conn = ep
            .accept()
            .await
            .ok_or_else(|| anyhow::anyhow!("no connection for {}", i))?;
        let alpn = conn.alpn().await?;
        anyhow::ensure!(alpn.as_bytes() == PLAY_ALPN, "invalid ALPN: {}", alpn);

        let conn = conn.await?;

        // Receive the player index
        let idx = {
            let mut buf = [0; 1];
            let mut channel = conn.accept_uni().await?;
            channel.read_exact(&mut buf).await?;

            buf[0] as usize
        };
        anyhow::ensure!(idx < MAX_PLAYERS, "Invalid player index");

        peer_connections[idx] = Some(conn);
    }

    // For every peer with a player index lower than ours, connect to them.
    let start_range = if peer_connections[0].is_some() { 1 } else { 0 };
    let range = start_range..player_idx;
    info!(players=?range, "Connecting to {} peers", range.len());

    for i in range {
        let addr = peer_addrs[i].as_ref().unwrap();
        let conn = ep.connect(addr.clone(), PLAY_ALPN).await?;

        // Send player index
        let mut channel = conn.open_uni().await?;
        channel.write(&[player_idx as u8]).await?;
        channel.finish().await?;

        peer_connections[i] = Some(conn);
    }

    Ok(peer_connections)
}

impl ggrs::NonBlockingSocket<usize> for Socket {
    fn send_to(&mut self, msg: &ggrs::Message, addr: &usize) {
        let msg = GameMessage {
            // Consider a way we can send message by reference and avoid clone?
            message: msg.clone(),
            match_id: self.match_id,
        };
        let conn = self.connections[*addr].as_ref().unwrap();

        let msg_bytes = postcard::to_allocvec(&msg).unwrap();
        conn.send_datagram(Bytes::copy_from_slice(&msg_bytes[..]))
            .ok();
    }

    fn receive_all_messages(&mut self) -> Vec<(usize, ggrs::Message)> {
        let mut messages = Vec::new();
        while let Ok(message) = self.ggrs_receiver.try_recv() {
            if message.1.match_id == self.match_id {
                messages.push((message.0, message.1.message));
            }
        }
        messages
    }
}