aboutsummaryrefslogtreecommitdiff
path: root/src/shared.rs
diff options
context:
space:
mode:
authorHenauxg <19689618+Henauxg@users.noreply.github.com>2022-11-17 15:17:02 +0100
committerHenauxg <19689618+Henauxg@users.noreply.github.com>2022-11-17 15:17:02 +0100
commitd76ef7bc0feb2b14762412b6bc83a3b8ebe64afd (patch)
tree42787112d4c39b0334d570512d6ad87b9854458c /src/shared.rs
parentf0ed1eb2e2768c68d803d6981d80bf9ca58c0bb8 (diff)
[client & server] Move shared code in a module
Diffstat (limited to 'src/shared.rs')
-rw-r--r--src/shared.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/shared.rs b/src/shared.rs
new file mode 100644
index 0000000..40b4514
--- /dev/null
+++ b/src/shared.rs
@@ -0,0 +1,68 @@
+use std::{fmt, sync::PoisonError};
+
+use crate::client::ConnectionId;
+use bevy::prelude::{Deref, DerefMut, Resource};
+use tokio::runtime::Runtime;
+
+pub const DEFAULT_MESSAGE_QUEUE_SIZE: usize = 150;
+pub const DEFAULT_KILL_MESSAGE_QUEUE_SIZE: usize = 10;
+pub const DEFAULT_KEEP_ALIVE_INTERVAL_S: u64 = 4;
+
+pub type ClientId = u64;
+
+#[derive(Resource, Deref, DerefMut)]
+pub(crate) struct AsyncRuntime(pub(crate) Runtime);
+
+/// Enum with possibles errors that can occur in Bevy Quinnet
+#[derive(thiserror::Error, Debug)]
+pub enum QuinnetError {
+ #[error("Client with id `{0}` is unknown")]
+ UnknownClient(ClientId),
+ #[error("Connection with id `{0}` is unknown")]
+ UnknownConnection(ConnectionId),
+ #[error("Failed serialization")]
+ Serialization,
+ #[error("Failed deserialization")]
+ Deserialization,
+ #[error("The data could not be sent on the channel because the channel is currently full and sending would require blocking")]
+ FullQueue,
+ #[error("The receiving half of the channel was explicitly closed or has been dropped")]
+ ChannelClosed,
+ #[error("The hosts file is invalid")]
+ InvalidHostFile,
+ #[error("Lock acquisition failure")]
+ LockAcquisitionFailure,
+ #[error("A Certificate action was already sent for a CertificateInteractionEvent")]
+ CertificateActionAlreadyApplied,
+}
+
+impl<T> From<PoisonError<T>> for QuinnetError {
+ fn from(_: PoisonError<T>) -> Self {
+ Self::LockAcquisitionFailure
+ }
+}
+
+/// SHA-256 hash of the certificate data in DER form
+#[derive(Debug, Clone, Eq, PartialEq)]
+pub struct CertificateFingerprint([u8; 32]);
+
+impl CertificateFingerprint {
+ pub fn new(buf: [u8; 32]) -> Self {
+ CertificateFingerprint(buf)
+ }
+}
+
+impl From<&rustls::Certificate> for CertificateFingerprint {
+ fn from(cert: &rustls::Certificate) -> CertificateFingerprint {
+ let hash = ring::digest::digest(&ring::digest::SHA256, &cert.0);
+ let fingerprint_bytes = hash.as_ref().try_into().unwrap();
+ CertificateFingerprint(fingerprint_bytes)
+ }
+}
+
+impl fmt::Display for CertificateFingerprint {
+ #[inline]
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt::Display::fmt(&base64::encode(&self.0), f)
+ }
+}