use crate::{
misc::{
ConnectionState, LeaseMut, Lock, StreamReader, StreamWriter, Vector, Xorshift64,
partitioned_filled_buffer::PartitionedFilledBuffer,
},
web_socket::{
Frame, FrameMut,
compression::NegotiatedCompression,
web_socket_parts::web_socket_part::{
WebSocketCommonPart, WebSocketReaderPart, WebSocketWriterPart,
},
},
};
use core::marker::PhantomData;
#[derive(Debug)]
pub struct WebSocketPartsOwned<C, NC, SR, SW, const IS_CLIENT: bool> {
pub reader: WebSocketReaderPartOwned<C, NC, SR, IS_CLIENT>,
pub writer: WebSocketWriterPartOwned<C, NC, SW, IS_CLIENT>,
}
#[derive(Debug)]
pub struct WebSocketCommonPartOwned<NC, SW, const IS_CLIENT: bool> {
pub(crate) wsc: WebSocketCommonPart<ConnectionState, NC, Xorshift64, SW, IS_CLIENT>,
}
#[derive(Debug)]
pub struct WebSocketReaderPartOwned<C, NC, SR, const IS_CLIENT: bool> {
pub(crate) common: C,
pub(crate) phantom: PhantomData<(NC, SR)>,
pub(crate) stream_reader: SR,
pub(crate) wsrp: WebSocketReaderPart<PartitionedFilledBuffer, Vector<u8>, IS_CLIENT>,
}
impl<C, NC, SR, SW, const IS_CLIENT: bool> WebSocketReaderPartOwned<C, NC, SR, IS_CLIENT>
where
C: Lock<Resource = WebSocketCommonPartOwned<NC, SW, IS_CLIENT>>,
NC: NegotiatedCompression,
SR: StreamReader,
SW: StreamWriter,
{
#[inline]
pub async fn read_frame(&mut self) -> crate::Result<FrameMut<'_, IS_CLIENT>> {
self.wsrp.read_frame_from_parts(&mut self.common, &mut self.stream_reader).await
}
}
#[derive(Debug)]
pub struct WebSocketWriterPartOwned<C, NC, SW, const IS_CLIENT: bool> {
pub(crate) common: C,
pub(crate) phantom: PhantomData<(NC, SW)>,
pub(crate) wswp: WebSocketWriterPart<Vector<u8>, IS_CLIENT>,
}
impl<C, NC, SW, const IS_CLIENT: bool> WebSocketWriterPartOwned<C, NC, SW, IS_CLIENT>
where
C: Lock<Resource = WebSocketCommonPartOwned<NC, SW, IS_CLIENT>>,
NC: NegotiatedCompression,
SW: StreamWriter,
{
#[inline]
pub async fn write_frame<P>(&mut self, frame: &mut Frame<P, IS_CLIENT>) -> crate::Result<()>
where
P: LeaseMut<[u8]>,
{
self.wswp.write_frame(&mut self.common.lock().await.wsc, frame).await
}
}