reading websocket
This commit is contained in:
parent
bb87c23709
commit
46ad5b3bf7
1 changed files with 68 additions and 7 deletions
|
|
@ -27,7 +27,7 @@ struct DataBlock {
|
||||||
|
|
||||||
mask_key: Option<u32>,
|
mask_key: Option<u32>,
|
||||||
|
|
||||||
data: Box<[u8]>,
|
data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DataFrame {
|
struct DataFrame {
|
||||||
|
|
@ -42,7 +42,8 @@ enum FrameType {
|
||||||
ConnectionClose,
|
ConnectionClose,
|
||||||
Ping,
|
Ping,
|
||||||
Pong,
|
Pong,
|
||||||
Other(u8),
|
OtherControl(u8),
|
||||||
|
OtherNonControl(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebsocketConnection {
|
impl WebsocketConnection {
|
||||||
|
|
@ -50,15 +51,75 @@ impl WebsocketConnection {
|
||||||
todo!()
|
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];
|
let mut first_line: [u8; 2] = [0; 2];
|
||||||
self.stream.read_exact(&mut first_line).await?;
|
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> {
|
let is_final = get_bool(7, first_line[0]);
|
||||||
Err(io::Error::new(io::ErrorKind::InvalidData, "uncluky"))
|
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(
|
pub async fn initialize_connection(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue