use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::TcpStream;
use crate::control::state::SharedState;
use crate::types::DatabaseId;
const MAX_FRAME_SIZE: u32 = 16 * 1024 * 1024;
mod auth;
mod dispatch;
mod lifecycle;
#[cfg(test)]
mod tests;
use super::conn_stream::ConnStream;
pub struct Session {
stream: ConnStream,
peer_addr: SocketAddr,
state: Arc<SharedState>,
auth_mode: crate::config::auth::AuthMode,
identity: Option<crate::control::security::identity::AuthenticatedIdentity>,
connected_at: std::time::Instant,
session_id: String,
identity_version: u64,
kill_rx: Option<tokio::sync::watch::Receiver<crate::control::security::sessions::KillReason>>,
current_database: Option<DatabaseId>,
}
impl Session {
fn with_stream(
stream: ConnStream,
peer_addr: SocketAddr,
state: Arc<SharedState>,
auth_mode: crate::config::auth::AuthMode,
) -> Self {
Self {
stream,
peer_addr,
state,
auth_mode,
identity: None,
connected_at: std::time::Instant::now(),
session_id: uuid::Uuid::new_v4().to_string(),
identity_version: 0,
kill_rx: None,
current_database: None,
}
}
pub fn new(
stream: TcpStream,
peer_addr: SocketAddr,
state: Arc<SharedState>,
auth_mode: crate::config::auth::AuthMode,
) -> Self {
Self::with_stream(ConnStream::plain(stream), peer_addr, state, auth_mode)
}
pub fn new_tls(
stream: tokio_rustls::server::TlsStream<TcpStream>,
peer_addr: SocketAddr,
state: Arc<SharedState>,
auth_mode: crate::config::auth::AuthMode,
) -> Self {
Self::with_stream(ConnStream::tls(stream), peer_addr, state, auth_mode)
}
}