diff options
author | gilles henaux <gill.henaux@gmail.com> | 2022-12-24 10:33:07 +0100 |
---|---|---|
committer | gilles henaux <gill.henaux@gmail.com> | 2022-12-24 10:33:07 +0100 |
commit | cc3a4e934bf9ecf52d7e675ded9781bec5d43289 (patch) | |
tree | 2ce17e33ea00e7102fb367dc09f34dca34de2f47 | |
parent | 028dcba74117a0400e38992a3ff200a133e520e3 (diff) | |
parent | 3e886b46ddbcbe079d1ac2537e58dc3e8ea2f813 (diff) |
Merge branch 'main' of https://github.com/Henauxg/bevy_quinnet
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/client.rs | 32 |
3 files changed, 25 insertions, 9 deletions
@@ -522,6 +522,7 @@ dependencies = [ "futures", "futures-util", "quinn", + "quinn-proto", "rand", "rcgen", "ring", @@ -20,6 +20,7 @@ tokio = { version = "1.21.2", features = ["sync", "rt-multi-thread", "macros"] } tokio-util = { version = "0.7.4", features = ["codec"] } rcgen = "0.10.0" quinn = "0.9.1" +quinn-proto = "0.9.1" futures-util = "0.3.24" futures = "0.3.24" bincode = "1.3.3" diff --git a/src/client.rs b/src/client.rs index 48b868e..3ad2174 100644 --- a/src/client.rs +++ b/src/client.rs @@ -12,7 +12,8 @@ use bevy::prelude::*; use bytes::Bytes; use futures::sink::SinkExt; use futures_util::StreamExt; -use quinn::{ClientConfig, Endpoint}; +use quinn::{ClientConfig, Connection as QuinnConnection, Endpoint}; +use quinn_proto::ConnectionStats; use serde::Deserialize; use tokio::{ runtime::{self}, @@ -95,16 +96,18 @@ impl ConnectionConfiguration { } } -/// Current state of the client driver -#[derive(Debug, PartialEq, Eq)] +type InternalConnectionRef = QuinnConnection; + +/// Current state of a client connection +#[derive(Debug)] enum ConnectionState { Disconnected, - Connected, + Connected(InternalConnectionRef), } #[derive(Debug)] pub(crate) enum InternalAsyncMessage { - Connected, + Connected(InternalConnectionRef), LostConnection, CertificateInteractionRequest { status: CertVerificationStatus, @@ -229,7 +232,18 @@ impl Connection { } pub fn is_connected(&self) -> bool { - return self.state == ConnectionState::Connected; + match self.state { + ConnectionState::Disconnected => false, + ConnectionState::Connected(_) => true, + } + } + + /// Returns statistics about the current connection if connected. + pub fn stats(&self) -> Option<ConnectionStats> { + match &self.state { + ConnectionState::Disconnected => None, + ConnectionState::Connected(connection) => Some(connection.stats()), + } } } @@ -446,7 +460,7 @@ async fn connection_task(mut spawn_config: ConnectionSpawnConfig) { spawn_config .to_sync_client - .send(InternalAsyncMessage::Connected) + .send(InternalAsyncMessage::Connected(connection.clone())) .await .expect("Failed to signal connection to sync client"); @@ -523,8 +537,8 @@ fn update_sync_client( for (connection_id, mut connection) in &mut client.connections { while let Ok(message) = connection.internal_receiver.try_recv() { match message { - InternalAsyncMessage::Connected => { - connection.state = ConnectionState::Connected; + InternalAsyncMessage::Connected(internal_connection) => { + connection.state = ConnectionState::Connected(internal_connection); connection_events.send(ConnectionEvent(*connection_id)); } InternalAsyncMessage::LostConnection => { |