#![cfg_attr(not(feature="explicitly_aligned_masking"),forbid(unsafe_code))]
#![warn(missing_docs)]
#![doc=include_str!("../README.md")]
#![no_std]
mod masking;
pub use masking::apply_mask;
#[cfg(feature="large_frames")]
pub type PayloadLength = u64;
#[cfg(not(feature="large_frames"))]
pub type PayloadLength = u16;
mod frame_encoding;
pub use frame_encoding::WebsocketFrameEncoder;
mod frame_decoding;
pub use frame_decoding::{FrameDecoderError,WebsocketFrameDecoder, WebsocketFrameEvent,WebsocketFrameDecoderAddDataResult};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum Opcode {
Continuation = 0,
Text = 1,
Binary = 2,
#[doc(hidden)]
ReservedData3 = 3,
#[doc(hidden)]
ReservedData4 = 4,
#[doc(hidden)]
ReservedData5 = 5,
#[doc(hidden)]
ReservedData6 = 6,
#[doc(hidden)]
ReservedData7 = 7,
ConnectionClose = 8,
Ping = 9,
Pong = 0xA,
#[doc(hidden)]
ReservedControlB = 0xB,
#[doc(hidden)]
ReservedControlC = 0xC,
#[doc(hidden)]
ReservedControlD = 0xD,
#[doc(hidden)]
ReservedControlE = 0xE,
#[doc(hidden)]
#[default]
ReservedControlF = 0xF,
}
impl Opcode {
pub fn is_data(&self) -> bool {
match self {
Opcode::Continuation => true,
Opcode::Text => true,
Opcode::Binary => true,
Opcode::ReservedData3 => true,
Opcode::ReservedData4 => true,
Opcode::ReservedData5 => true,
Opcode::ReservedData6 => true,
Opcode::ReservedData7 => true,
_ => false,
}
}
pub fn is_control(&self) -> bool {
! self.is_data()
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct FrameInfo {
pub opcode: Opcode,
pub payload_length: PayloadLength,
pub mask: Option<[u8; 4]>,
pub fin: bool,
pub reserved: u8,
}
impl FrameInfo {
pub const fn is_reasonable(&self) -> bool {
if self.reserved != 0 { return false; }
match self.opcode {
Opcode::Continuation => true,
Opcode::Text => true,
Opcode::Binary => true,
Opcode::ConnectionClose => self.fin,
Opcode::Ping => self.fin,
Opcode::Pong => self.fin,
_ => false,
}
}
}
#[cfg(feature = "large_frames")]
pub const MAX_HEADER_LENGTH: usize = 2 + 8 + 4;
#[cfg(not(feature = "large_frames"))]
pub const MAX_HEADER_LENGTH: usize = 2 + 2 + 4;
#[cfg(test)]
mod decoding_test;
#[cfg(test)]
mod frame_roundtrip_test;