reading websocket

This commit is contained in:
maxstrb 2025-11-03 21:23:25 +01:00
parent bb87c23709
commit 46ad5b3bf7

View file

@ -27,7 +27,7 @@ struct DataBlock {
mask_key: Option<u32>,
data: Box<[u8]>,
data: Vec<u8>,
}
struct DataFrame {
@ -42,7 +42,8 @@ enum FrameType {
ConnectionClose,
Ping,
Pong,
Other(u8),
OtherControl(u8),
OtherNonControl(u8),
}
impl WebsocketConnection {
@ -50,15 +51,75 @@ impl WebsocketConnection {
todo!()
}
pub async fn read_next_message(&mut self) -> io::Result<DataBlock> {
pub async fn read_next_message(&mut self) -> io::Result<DataFrame> {
let mut data;
let frame_type;
{
let first_line = self.parse_single_block().await?;
data = first_line.data;
frame_type = first_line.message_type;
}
Ok(DataFrame {
frame_type,
data: data.into_boxed_slice(),
})
}
async fn parse_single_block(&mut self) -> io::Result<DataBlock> {
let mut first_line: [u8; 2] = [0; 2];
self.stream.read_exact(&mut first_line).await?;
todo!()
}
let get_bool = |index: u8, byte: u8| -> bool { byte & (1 << index) != 0 };
async fn parse_single_block(&self) -> io::Result<DataBlock> {
Err(io::Error::new(io::ErrorKind::InvalidData, "uncluky"))
let is_final = get_bool(7, first_line[0]);
let extension_bit_1 = get_bool(6, first_line[0]);
let extension_bit_2 = get_bool(5, first_line[0]);
let extension_bit_3 = get_bool(4, first_line[0]);
let message_type = match first_line[0] & 0b00001111 {
0x0 => FrameType::Continuation,
0x1 => FrameType::TextFrame,
0x2 => FrameType::BinaryFrame,
0x8 => FrameType::ConnectionClose,
0x9 => FrameType::Ping,
0xA => FrameType::Pong,
non_control if (0x3..=7).contains(&non_control) => {
FrameType::OtherNonControl(non_control)
}
control => FrameType::OtherControl(control),
};
let mask = get_bool(7, first_line[1]);
let length = match first_line[1] & 0b01111111 {
126 => self.stream.read_u16().await? as u64,
127 => self.stream.read_u64().await?,
other => other as u64,
};
let masking_key = if mask {
Some(self.stream.read_u32().await?)
} else {
None
};
let mut message_data = Vec::<u8>::with_capacity(length as usize);
self.stream.read_exact(&mut message_data).await?;
Ok(DataBlock {
is_final,
e1: extension_bit_1,
e2: extension_bit_2,
e3: extension_bit_3,
message_type,
is_masked: mask,
length,
mask_key: masking_key,
data: message_data,
})
}
pub async fn initialize_connection(