trying to wrap client connections in a wrapper struct

This commit is contained in:
cqql 2024-08-28 10:29:07 +02:00
parent d2985c3be8
commit 0f5dfb2c5c
3 changed files with 34 additions and 1 deletions

View file

@ -4,7 +4,7 @@ use futures_util::FutureExt;
use scct_model::{chats, messages, proto::c2s, users}; use scct_model::{chats, messages, proto::c2s, users};
use sea_orm::{ActiveModelTrait, ConnectionTrait, DatabaseConnection, EntityTrait, IntoActiveModel, ColumnTrait, QueryFilter, QueryOrder}; use sea_orm::{ActiveModelTrait, ConnectionTrait, DatabaseConnection, EntityTrait, IntoActiveModel, ColumnTrait, QueryFilter, QueryOrder};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use transport::{Transport, TransportSink, TransportStream}; use transport::{Transport, TransportSink, TransportStream, client_connection::ClientConnection};
use uuid; use uuid;
pub mod transport; pub mod transport;
@ -31,6 +31,7 @@ pub async fn serve(addr: &str, db_connection: DatabaseConnection) -> Result<(),
tracing::info!("listening for connections"); tracing::info!("listening for connections");
while let Some((tx, rx)) = listener.recv().await { while let Some((tx, rx)) = listener.recv().await {
let client = ClientConnection::new((tx, rx));
tracing::info!("new connection, starting handler"); tracing::info!("new connection, starting handler");
state.lock().await.push(tx); state.lock().await.push(tx);
let _state = state.clone(); let _state = state.clone();

View file

@ -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<dyn TransportSink>,
rx: Box<dyn TransportStream>,
user_uuid: Option<Uuid> // 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,
}
}
}

View file

@ -10,6 +10,8 @@
//! //!
//! so basically most likely everything down here will get coupled more and merged with the rest //! so basically most likely everything down here will get coupled more and merged with the rest
pub mod client_connection;
#[cfg(feature = "websocket")] #[cfg(feature = "websocket")]
pub mod websocket; pub mod websocket;