diff --git a/server/src/lib.rs b/server/src/lib.rs index ad0531d..3fcdd20 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -4,7 +4,7 @@ use futures_util::FutureExt; use scct_model::{chats, messages, proto::c2s, users}; use sea_orm::{ActiveModelTrait, ConnectionTrait, DatabaseConnection, EntityTrait, IntoActiveModel, ColumnTrait, QueryFilter, QueryOrder}; use tokio::sync::Mutex; -use transport::{Transport, TransportSink, TransportStream}; +use transport::{Transport, TransportSink, TransportStream, client_connection::ClientConnection}; use uuid; pub mod transport; @@ -31,6 +31,7 @@ pub async fn serve(addr: &str, db_connection: DatabaseConnection) -> Result<(), tracing::info!("listening for connections"); while let Some((tx, rx)) = listener.recv().await { + let client = ClientConnection::new((tx, rx)); tracing::info!("new connection, starting handler"); state.lock().await.push(tx); let _state = state.clone(); diff --git a/server/src/transport/client_connection.rs b/server/src/transport/client_connection.rs new file mode 100644 index 0000000..945eaf6 --- /dev/null +++ b/server/src/transport/client_connection.rs @@ -0,0 +1,30 @@ +// Wrapper around a (TransportStream, TransportSink) that represents a single +// client connection. It's self-aware - it gets a uuid so we can distinguish +// different connections and find the same one even if it's in different +// variables. +// Later into the life of a connection, it can be assigned a user uuid, +// when a user logs in using that connection. That can also be unassigned +// when the user logs out. +// Basically lots of helper facilities. + +use uuid::Uuid; + +use super::{TransportSink, TransportStream}; + +pub struct ClientConnection { + uuid: Uuid, + tx: Box, + rx: Box, + user_uuid: Option // gets set if and only if a user is logged in with this connection +} + +impl ClientConnection { + pub fn new((tx, rx): (impl TransportSink + 'static, impl TransportStream + 'static)) -> ClientConnection { + ClientConnection { + uuid: Uuid::new_v4(), + tx: Box::new(tx), + rx: Box::new(rx), + user_uuid: None, + } + } +} \ No newline at end of file diff --git a/server/src/transport/mod.rs b/server/src/transport/mod.rs index 56b5361..58c6920 100644 --- a/server/src/transport/mod.rs +++ b/server/src/transport/mod.rs @@ -10,6 +10,8 @@ //! //! so basically most likely everything down here will get coupled more and merged with the rest +pub mod client_connection; + #[cfg(feature = "websocket")] pub mod websocket;