bevy_quinnet 0.19.0

Bevy plugin for Client/Server multiplayer games using QUIC
Documentation
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use std::{
    collections::{
        hash_map::{Iter, IterMut},
        HashMap,
    },
    sync::Mutex,
};

use bevy::prelude::*;

use tokio::{
    runtime::{self},
    sync::oneshot,
};

use crate::{
    client::connection::{create_client_connection_async_channels, ClientConnection},
    shared::{
        channels::{ChannelAsyncMessage, SendChannelsConfiguration},
        error::AsyncChannelError,
        AsyncRuntime, ClientId, InternalConnectionRef, QuinnetSyncPreUpdate,
    },
};

use self::{
    certificate::{
        CertConnectionAbortEvent, CertInteractionEvent, CertTrustUpdateEvent, CertVerificationInfo,
        CertVerificationStatus, CertVerifierAction, CertificateVerificationMode,
    },
    connection::{
        async_connection_task, ClientAddrConfiguration, ClientSideConnection, ConnectionEvent,
        ConnectionFailedEvent, ConnectionLocalId, ConnectionLostEvent, ConnectionState,
        InternalConnectionState,
    },
};

/// Module for the client's certificate features
pub mod certificate;
/// Module for a client's connection to a server
pub mod connection;

mod error;
pub use error::*;

/// Default path for the known hosts file
pub const DEFAULT_KNOWN_HOSTS_FILE: &str = "quinnet/known_hosts";

/// Configuration for a client's connection to a server
#[derive(Debug, Clone)]
pub struct ClientConnectionConfiguration {
    /// See [ClientAddrConfiguration]
    pub addr_config: ClientAddrConfiguration,
    /// How the client should verify the server's certificate
    pub cert_mode: CertificateVerificationMode,
    /// Configuration for a [ClientConnectionConfiguration] that can be defaulted
    pub defaultables: ClientConnectionConfigurationDefaultables,
}

/// Every configuration fields of a client's connection to a server that can be defaulted
#[derive(Debug, Default, Clone)]
pub struct ClientConnectionConfigurationDefaultables {
    /// Configuration of the send channels opened on the connection
    pub send_channels_cfg: SendChannelsConfiguration,
    /// Configuration for the receive channels on the connection
    #[cfg(feature = "recv_channels")]
    pub recv_channels_cfg: crate::shared::peer_connection::RecvChannelsConfiguration,
}

/// Possible errors occuring while a client is connecting to a server
#[derive(thiserror::Error, Debug, Clone)]
pub enum QuinnetConnectionError {
    /// A quic error occurred during the connection
    #[error("Quic connection error")]
    QuicConnectionError(#[from] quinn::ConnectionError),
    /// Client received an invalid client id
    #[error("Client received an invalid client id")]
    InvalidClientId,
    /// Client did not receive its client id
    #[error("Client did not receive its client id")]
    ClientIdNotReceived,
}

#[derive(Debug)]
pub(crate) enum ClientAsyncMessage {
    Connected(InternalConnectionRef, Option<ClientId>),
    ConnectionFailed(QuinnetConnectionError),
    ConnectionClosed, // TODO Might set a ConnectionError
    CertificateInteractionRequest {
        status: CertVerificationStatus,
        info: CertVerificationInfo,
        action_sender: oneshot::Sender<CertVerifierAction>,
    },
    CertificateTrustUpdate(CertVerificationInfo),
    CertificateConnectionAbort {
        status: CertVerificationStatus,
        cert_info: CertVerificationInfo,
    },
}

/// Main quinnet client. Can open multiple [`ClientSideConnection`] with multiple quinnet servers
///
/// Created by the [`QuinnetClientPlugin`] or inserted manually via a call to [`bevy::prelude::World::insert_resource`]. When created, it will look for an existing [`AsyncRuntime`] resource and use it or create one itself.
#[derive(Resource)]
pub struct QuinnetClient {
    runtime: runtime::Handle,
    connections: HashMap<ConnectionLocalId, ClientSideConnection>,
    connection_local_id_gen: ConnectionLocalId,
    default_connection_id: Option<ConnectionLocalId>,
}

impl FromWorld for QuinnetClient {
    fn from_world(world: &mut World) -> Self {
        if world.get_resource::<AsyncRuntime>().is_none() {
            let async_runtime = tokio::runtime::Builder::new_multi_thread()
                .enable_all()
                .build()
                .unwrap();
            world.insert_resource(AsyncRuntime(async_runtime));
        };

        let runtime = world.resource::<AsyncRuntime>();
        QuinnetClient::new(runtime.handle().clone())
    }
}

impl QuinnetClient {
    fn new(runtime_handle: tokio::runtime::Handle) -> Self {
        Self {
            connections: HashMap::new(),
            runtime: runtime_handle,
            connection_local_id_gen: 0,
            default_connection_id: None,
        }
    }

    /// Returns true if the default connection exists and is connecting.
    pub fn is_connecting(&self) -> bool {
        match self.get_connection() {
            Some(connection) => connection.state() == ConnectionState::Connecting,
            None => false,
        }
    }

    /// Returns true if the default connection exists and is connected.
    pub fn is_connected(&self) -> bool {
        match self.get_connection() {
            Some(connection) => connection.state() == ConnectionState::Connected,
            None => false,
        }
    }

    /// Returns true if the default connection does not exists or is disconnected.
    pub fn is_disconnected(&self) -> bool {
        match self.get_connection() {
            Some(connection) => connection.state() == ConnectionState::Disconnected,
            None => true,
        }
    }

    /// Returns the default connection or None.
    pub fn get_connection(&self) -> Option<&ClientSideConnection> {
        match self.default_connection_id {
            Some(id) => self.connections.get(&id),
            None => None,
        }
    }

    /// Returns the default connection as mut or None.
    pub fn get_connection_mut(&mut self) -> Option<&mut ClientSideConnection> {
        match self.default_connection_id {
            Some(id) => self.connections.get_mut(&id),
            None => None,
        }
    }

    /// Returns the default connection. **Warning**, this function panics if there is no default connection.
    pub fn connection(&self) -> &ClientSideConnection {
        self.connections
            .get(&self.default_connection_id.unwrap())
            .unwrap()
    }

    /// Returns the default connection as mut. **Warning**, this function panics if there is no default connection.
    pub fn connection_mut(&mut self) -> &mut ClientSideConnection {
        self.connections
            .get_mut(&self.default_connection_id.unwrap())
            .unwrap()
    }

    /// Returns the requested connection.
    pub fn get_connection_by_id(&self, id: ConnectionLocalId) -> Option<&ClientSideConnection> {
        self.connections.get(&id)
    }

    /// Returns the requested connection as mut.
    pub fn get_connection_mut_by_id(
        &mut self,
        id: ConnectionLocalId,
    ) -> Option<&mut ClientSideConnection> {
        self.connections.get_mut(&id)
    }

    /// Returns an iterator over all connections
    pub fn connections(&'_ self) -> Iter<'_, ConnectionLocalId, ClientSideConnection> {
        self.connections.iter()
    }

    /// Returns an iterator over all connections as muts
    pub fn connections_mut(&'_ mut self) -> IterMut<'_, ConnectionLocalId, ClientSideConnection> {
        self.connections.iter_mut()
    }

    /// Opens a connection to a server.
    ///
    /// The connection will raise an event when fully connected, see [ConnectionEvent]
    ///
    /// Returns the [ConnectionLocalId]
    pub fn open_connection(
        &mut self,
        config: ClientConnectionConfiguration,
    ) -> Result<ConnectionLocalId, AsyncChannelError> {
        // Generate a local connection id
        let local_id = self.connection_local_id_gen;
        self.connection_local_id_gen += 1;

        let (
            bytes_from_server_send,
            bytes_from_server_recv,
            to_sync_client_send,
            to_sync_client_recv,
            from_channels_send,
            from_channels_recv,
            to_channels_send,
            to_channels_recv,
            close_send,
            close_recv,
        ) = create_client_connection_async_channels();

        let mut connection = ClientSideConnection::new(
            ClientConnection::new(
                local_id,
                self.runtime.clone(),
                config.addr_config.clone(),
                config.cert_mode.clone(),
                config.defaultables.send_channels_cfg.clone(),
                to_sync_client_recv,
            ),
            bytes_from_server_recv,
            close_send,
            from_channels_recv,
            to_channels_send,
            #[cfg(feature = "recv_channels")]
            config.defaultables.recv_channels_cfg,
        );

        connection.open_configured_channels(config.defaultables.send_channels_cfg)?;

        self.connections.insert(local_id, connection);
        if self.default_connection_id.is_none() {
            self.default_connection_id = Some(local_id);
        }

        // Async connection
        self.runtime.spawn(async move {
            async_connection_task(
                local_id,
                config.addr_config,
                config.cert_mode,
                to_sync_client_send,
                bytes_from_server_send,
                to_channels_recv,
                from_channels_send,
                close_recv,
            )
            .await
        });

        Ok(local_id)
    }

    /// Set the default connection
    pub fn set_default_connection(&mut self, connection_id: ConnectionLocalId) {
        self.default_connection_id = Some(connection_id);
    }

    /// Get the default Connection Id
    pub fn get_default_connection(&self) -> Option<ConnectionLocalId> {
        self.default_connection_id
    }

    /// Closes a specific connection. Removes it from the client.
    ///
    /// Closing a connection immediately prevents new messages from being sent on the connection and signal it to closes all its background tasks. Before trully closing, the connection will wait for all buffered messages in all its opened channels to be properly sent according to their respective channel type.
    ///
    /// This may fail if no [ClientSideConnection] if found for `connection_id`, or if the connection is already closed.
    pub fn close_connection(
        &mut self,
        connection_id: ConnectionLocalId,
    ) -> Result<(), ClientConnectionCloseError> {
        match self.connections.remove(&connection_id) {
            Some(mut connection) => {
                if Some(connection_id) == self.default_connection_id {
                    self.default_connection_id = None;
                }
                connection.disconnect()
            }
            None => Err(ClientConnectionCloseError::InvalidConnectionId(
                connection_id,
            )),
        }
    }

    /// Calls [Self::close_connection] on all the open connections.
    pub fn close_all_connections(&mut self) {
        for connection_id in self
            .connections
            .keys()
            .cloned()
            .collect::<Vec<ConnectionLocalId>>()
        {
            let _ = self.close_connection(connection_id);
        }
    }
}

/// Receive messages from the async client tasks and update the sync client.
///
/// This system generates client's bevy events
pub fn handle_client_events(
    mut connection_events: MessageWriter<ConnectionEvent>,
    mut connection_failed_events: MessageWriter<ConnectionFailedEvent>,
    mut connection_lost_events: MessageWriter<ConnectionLostEvent>,
    mut certificate_interaction_events: MessageWriter<CertInteractionEvent>,
    mut cert_trust_update_events: MessageWriter<CertTrustUpdateEvent>,
    mut cert_connection_abort_events: MessageWriter<CertConnectionAbortEvent>,
    mut client: ResMut<QuinnetClient>,
) {
    for (connection_id, connection) in &mut client.connections {
        while let Ok(message) = connection.try_recv_from_async() {
            match message {
                ClientAsyncMessage::Connected(internal_connection, client_id) => {
                    connection.set_state(InternalConnectionState::Connected(
                        internal_connection,
                        client_id,
                    ));
                    connection_events.write(ConnectionEvent {
                        id: *connection_id,
                        client_id,
                    });
                }
                ClientAsyncMessage::ConnectionFailed(err) => {
                    connection.set_state(InternalConnectionState::Disconnected);
                    connection_failed_events.write(ConnectionFailedEvent {
                        id: *connection_id,
                        err,
                    });
                }
                ClientAsyncMessage::ConnectionClosed => match connection.internal_state() {
                    InternalConnectionState::Disconnected => (),
                    _ => {
                        connection.try_disconnect_closed_connection();
                        connection_lost_events.write(ConnectionLostEvent { id: *connection_id });
                    }
                },
                ClientAsyncMessage::CertificateInteractionRequest {
                    status,
                    info,
                    action_sender,
                } => {
                    certificate_interaction_events.write(CertInteractionEvent {
                        connection_id: *connection_id,
                        status,
                        info,
                        action_sender: Mutex::new(Some(action_sender)),
                    });
                }
                ClientAsyncMessage::CertificateTrustUpdate(info) => {
                    cert_trust_update_events.write(CertTrustUpdateEvent {
                        connection_id: *connection_id,
                        cert_info: info,
                    });
                }
                ClientAsyncMessage::CertificateConnectionAbort { status, cert_info } => {
                    cert_connection_abort_events.write(CertConnectionAbortEvent {
                        connection_id: *connection_id,
                        status,
                        cert_info,
                    });
                }
            }
        }
        while let Ok(message) = connection.try_recv_from_channels() {
            match message {
                ChannelAsyncMessage::LostConnection => match connection.internal_state() {
                    InternalConnectionState::Disconnected => (),
                    _ => {
                        connection.try_disconnect_closed_connection();
                        connection_lost_events.write(ConnectionLostEvent { id: *connection_id });
                    }
                },
            }
        }
    }
}

#[cfg(feature = "recv_channels")]
/// Type alias for the recv channel error event for the client
pub type ClientRecvChannelError = crate::shared::error::RecvChannelErrorEvent<ConnectionLocalId>;

#[cfg(feature = "recv_channels")]
/// Dispatches received payloads to their respective channel buffers
///
/// This system generates client's bevy events
pub fn dispatch_received_payloads(
    mut recv_error_events: MessageWriter<ClientRecvChannelError>,
    mut client: ResMut<QuinnetClient>,
) {
    for (connection_id, connection) in &mut client.connections {
        match connection.internal_state() {
            InternalConnectionState::Disconnected => (),
            _ => {
                if let Err(recv_errors) = connection.dispatch_received_payloads_to_channel_buffers()
                {
                    for error in recv_errors {
                        error!(
                            "Error while dispatching received payloads to channel buffers: {}",
                            error
                        );
                        recv_error_events.write(ClientRecvChannelError {
                            id: *connection_id,
                            error,
                        });
                    }
                }
            }
        }
    }
}

#[cfg(feature = "recv_channels")]
/// Clears stale payloads on all receive channels
pub fn clear_stale_received_payloads(mut client: ResMut<QuinnetClient>) {
    for connection in client.connections.values_mut() {
        connection.clear_stale_received_payloads();
    }
}

/// Quinnet Client's plugin
///
/// It is possbile to add both this plugin and the [`crate::server::QuinnetServerPlugin`]
#[derive(Default)]
pub struct QuinnetClientPlugin {
    /// In order to have more control and only do the strict necessary, which is registering systems and events in the Bevy schedule, `initialize_later` can be set to `true`. This will prevent the plugin from initializing the `Client` Resource.
    /// Client systems are scheduled to only run if the `Client` resource exists.
    /// A Bevy command to create the resource `commands.init_resource::<Client>();` can be done later on, when needed.
    pub initialize_later: bool,
}

impl Plugin for QuinnetClientPlugin {
    fn build(&self, app: &mut App) {
        app.add_message::<ConnectionEvent>()
            .add_message::<ConnectionFailedEvent>()
            .add_message::<ConnectionLostEvent>()
            .add_message::<CertInteractionEvent>()
            .add_message::<CertTrustUpdateEvent>()
            .add_message::<CertConnectionAbortEvent>();

        if !self.initialize_later {
            app.init_resource::<QuinnetClient>();
        }

        app.add_systems(
            PreUpdate,
            handle_client_events
                .in_set(QuinnetSyncPreUpdate)
                .run_if(resource_exists::<QuinnetClient>),
        );
        #[cfg(feature = "recv_channels")]
        {
            app.add_message::<ClientRecvChannelError>();
            app.add_systems(
                PreUpdate,
                dispatch_received_payloads
                    .in_set(QuinnetSyncPreUpdate)
                    .run_if(resource_exists::<QuinnetClient>),
            );
            app.add_systems(
                Last,
                clear_stale_received_payloads
                    .in_set(crate::shared::QuinnetSyncLast)
                    .run_if(resource_exists::<QuinnetClient>),
            );
        }
    }
}

/// Returns true if the following conditions are all true:
/// - the client Resource exists
/// - its default connection is connecting.
pub fn client_connecting(client: Option<Res<QuinnetClient>>) -> bool {
    match client {
        Some(client) => client.is_connecting(),
        None => false,
    }
}

/// Returns true if the following conditions are all true:
/// - the client Resource exists
/// - its default connection is connected.
pub fn client_connected(client: Option<Res<QuinnetClient>>) -> bool {
    match client {
        Some(client) => client.is_connected(),
        None => false,
    }
}

/// Returns true if the following conditions are all true:
/// - the client Resource exists and its default connection is connected
/// - the previous condition was false during the previous update
pub fn client_just_connected(
    mut last_connected: Local<bool>,
    client: Option<Res<QuinnetClient>>,
) -> bool {
    let connected = client.map(|client| client.is_connected()).unwrap_or(false);

    let just_connected = !*last_connected && connected;
    *last_connected = connected;
    just_connected
}

/// Returns true if the following conditions are all true:
/// - the client Resource does not exists or its default connection is disconnected
/// - the previous condition was false during the previous update
pub fn client_just_disconnected(
    mut last_connected: Local<bool>,
    client: Option<Res<QuinnetClient>>,
) -> bool {
    let disconnected = client
        .map(|client| client.is_disconnected())
        .unwrap_or(true);

    let just_disconnected = *last_connected && disconnected;
    *last_connected = !disconnected;
    just_disconnected
}