use std::{mem::MaybeUninit, sync::Arc};
#[cfg(feature = "tokio")]
use tokio::sync::Mutex;
#[cfg(not(feature = "tokio"))]
use std::sync::Mutex;
use crate::{
net::client_server::ClientServer,
types::{
request::{Authentication, CredentialValue, Password, Username},
traits::ReadWrite,
},
};
#[derive(Debug)]
pub struct ClientServerBuilder<T: ReadWrite> {
stream: T,
auth: Option<Authentication>,
max_messagesize: i32,
}
impl<T: ReadWrite> ClientServerBuilder<T> {
pub fn new(stream: T) -> Self {
Self {
stream,
auth: None,
max_messagesize: i32::MAX,
}
}
pub fn with_credentials(mut self, username: String, password: Option<String>) -> Self {
self.auth = Some(Authentication::build(CredentialValue::UsernameAndPassword(
Username(username),
password.map(Password),
)));
self
}
pub fn with_max_message_size(mut self, max: i32) -> Self {
self.max_messagesize = max;
self
}
pub fn build(self) -> ClientServer<T> {
let auth = self.auth;
let stream = Arc::new(Mutex::new(self.stream));
let max_message_size = self.max_messagesize;
let read_buf = vec![0u8; 8192];
let write_buf = vec![MaybeUninit::uninit(); 8192];
let connection_error_count = Default::default();
ClientServer {
auth,
max_message_size,
stream,
read_buf,
write_buf,
connection_error_count,
}
}
}