pub struct Header(_);
Expand description
owned header buf
Implementations§
source§impl Header
impl Header
sourcepub fn payload_len(&self) -> u64
pub fn payload_len(&self) -> u64
return payload len
sourcepub fn masking_key(&self) -> Option<[u8; 4]>
pub fn masking_key(&self) -> Option<[u8; 4]>
get frame mask key
Examples found in repository?
src/frame.rs (line 568)
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
pub fn new<M: Into<Option<[u8; 4]>>>(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
mut payload: BytesMut,
mask_payload: bool,
) -> Self {
let header = Header::new(
fin,
rsv1,
rsv2,
rsv3,
mask_key,
opcode,
payload.len() as u64,
);
if mask_payload {
if let Some(key) = header.masking_key() {
apply_mask_fast32(&mut payload, key);
}
};
Self { header, payload }
}
/// immutable header view
pub fn header(&self) -> HeaderView {
self.header.view()
}
/// mutable header view
pub fn header_mut(&mut self) -> HeaderViewMut {
self.header.view_mut()
}
/// immutable payload
pub fn payload(&self) -> &[u8] {
&self.payload
}
/// mutable payload
pub fn payload_mut(&mut self) -> &mut BytesMut {
&mut self.payload
}
/// replace old payload
pub fn replace(&mut self, new: BytesMut, mask_payload: bool) -> BytesMut {
self.header_mut().set_payload_len(new.len() as u64);
let old = self.payload.split_to(self.payload.len());
self.payload = new;
if mask_payload {
if let Some(key) = self.header.masking_key() {
apply_mask_fast32(&mut self.payload, key);
}
}
old
}
More examples
src/codec/frame/non_blocking.rs (line 96)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
async fn async_send_one_mut<'a, S: AsyncWrite + Unpin, M: Into<Option<[u8; 4]>>>(
&mut self,
stream: &mut S,
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
mut payload: PayloadMut<'a>,
mask_payload: bool,
) -> IOResult<()> {
let header = Header::new(
fin,
rsv1,
rsv2,
rsv3,
mask_key,
opcode,
payload.len() as u64,
);
if mask_payload {
if let Some(key) = header.masking_key() {
payload.apply_mask(key)
}
}
stream.write_all(&header.0).await?;
for part in payload.0 {
stream.write_all(part).await?;
}
Ok(())
}
sourcepub fn payload_idx(&self) -> (usize, usize)
pub fn payload_idx(&self) -> (usize, usize)
return (headerlen, the whole frame len)
sourcepub fn set_opcode(&mut self, code: OpCode)
pub fn set_opcode(&mut self, code: OpCode)
set opcode
sourcepub fn set_mask(&mut self, mask: bool)
pub fn set_mask(&mut self, mask: bool)
NOTE if change mask bit after setting payload you need to set payload again to adjust data frame
sourcepub fn set_payload_len(&mut self, len: u64) -> usize
pub fn set_payload_len(&mut self, len: u64) -> usize
set header payload lens
sourcepub fn set_masking_key(&mut self, key: [u8; 4])
pub fn set_masking_key(&mut self, key: [u8; 4])
key header mask key
Examples found in repository?
src/frame.rs (line 422)
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
pub fn new<M: Into<Option<[u8; 4]>>>(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
payload_len: u64,
) -> Self {
let mut buf = BytesMut::new();
let mask_key = mask_key.into();
Header::config(
fin,
rsv1,
rsv2,
rsv3,
opcode,
mask_key.is_some(),
payload_len,
&mut buf,
);
let mut header = Header(buf);
if let Some(key) = mask_key {
header.set_masking_key(key);
}
header
}
sourcepub fn config(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
opcode: OpCode,
mask: bool,
payload_len: u64,
buf: &mut BytesMut
)
pub fn config(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
opcode: OpCode,
mask: bool,
payload_len: u64,
buf: &mut BytesMut
)
config mut buf as a valid header
NOTE this operation will override buf content, and try to extend if there is no enough len
Examples found in repository?
src/frame.rs (lines 410-419)
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
pub fn new<M: Into<Option<[u8; 4]>>>(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
payload_len: u64,
) -> Self {
let mut buf = BytesMut::new();
let mask_key = mask_key.into();
Header::config(
fin,
rsv1,
rsv2,
rsv3,
opcode,
mask_key.is_some(),
payload_len,
&mut buf,
);
let mut header = Header(buf);
if let Some(key) = mask_key {
header.set_masking_key(key);
}
header
}
sourcepub fn new<M: Into<Option<[u8; 4]>>>(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
payload_len: u64
) -> Self
pub fn new<M: Into<Option<[u8; 4]>>>(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
payload_len: u64
) -> Self
construct new header
Examples found in repository?
src/frame.rs (lines 558-566)
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
pub fn new<M: Into<Option<[u8; 4]>>>(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
mut payload: BytesMut,
mask_payload: bool,
) -> Self {
let header = Header::new(
fin,
rsv1,
rsv2,
rsv3,
mask_key,
opcode,
payload.len() as u64,
);
if mask_payload {
if let Some(key) = header.masking_key() {
apply_mask_fast32(&mut payload, key);
}
};
Self { header, payload }
}
More examples
src/codec/frame/non_blocking.rs (lines 86-94)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
async fn async_send_one_mut<'a, S: AsyncWrite + Unpin, M: Into<Option<[u8; 4]>>>(
&mut self,
stream: &mut S,
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
mut payload: PayloadMut<'a>,
mask_payload: bool,
) -> IOResult<()> {
let header = Header::new(
fin,
rsv1,
rsv2,
rsv3,
mask_key,
opcode,
payload.len() as u64,
);
if mask_payload {
if let Some(key) = header.masking_key() {
payload.apply_mask(key)
}
}
stream.write_all(&header.0).await?;
for part in payload.0 {
stream.write_all(part).await?;
}
Ok(())
}
/// send mutable payload
///
/// if mask_payload is set, mask payload first
///
/// will auto fragment if auto_fragment_size > 0
pub async fn async_send_mut<'a, S: AsyncWrite + Unpin>(
&mut self,
stream: &mut S,
opcode: OpCode,
payload: PayloadMut<'a>,
mask_payload: bool,
) -> IOResult<()> {
let split_size = self.config.auto_fragment_size;
let mask_send = self.config.mask_send_frame;
let mask_fn = || {
if mask_send {
Some(rand::random())
} else {
None
}
};
if split_size > 0 {
let parts = payload.split_with(split_size);
let total = parts.len();
for (idx, part) in parts.into_iter().enumerate() {
let fin = idx + 1 == total;
let key = mask_fn();
self.async_send_one_mut(
stream,
fin,
false,
false,
false,
key,
opcode.clone(),
part,
mask_payload,
)
.await?;
}
} else {
let key = mask_fn();
self.async_send_one_mut(
stream,
true,
false,
false,
false,
key,
opcode,
payload,
mask_payload,
)
.await?;
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
async fn async_send_one<'a, S: AsyncWrite + Unpin, M: Into<Option<[u8; 4]>>>(
&mut self,
stream: &mut S,
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
payload: Payload<'a>,
) -> IOResult<()> {
let header = Header::new(
fin,
rsv1,
rsv2,
rsv3,
mask_key,
opcode,
payload.len() as u64,
);
stream.write_all(&header.0).await?;
for part in payload.0 {
stream.write_all(part).await?;
}
Ok(())
}
sourcepub fn view(&self) -> HeaderView<'_>
pub fn view(&self) -> HeaderView<'_>
return read only header view
Examples found in repository?
src/frame.rs (line 524)
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
pub fn header(&self) -> HeaderView {
self.header.view()
}
/// mutable header view
pub fn header_mut(&mut self) -> HeaderViewMut {
self.header.view_mut()
}
/// payload
pub fn payload(&self) -> Payload {
self.payload.clone()
}
}
/// header for user p
#[derive(Debug, Clone)]
pub struct OwnedFrame {
header: Header,
payload: BytesMut,
}
impl OwnedFrame {
/// construct new owned frame
#[allow(clippy::too_many_arguments)]
pub fn new<M: Into<Option<[u8; 4]>>>(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
mut payload: BytesMut,
mask_payload: bool,
) -> Self {
let header = Header::new(
fin,
rsv1,
rsv2,
rsv3,
mask_key,
opcode,
payload.len() as u64,
);
if mask_payload {
if let Some(key) = header.masking_key() {
apply_mask_fast32(&mut payload, key);
}
};
Self { header, payload }
}
/// immutable header view
pub fn header(&self) -> HeaderView {
self.header.view()
}
sourcepub fn view_mut(&mut self) -> HeaderViewMut<'_>
pub fn view_mut(&mut self) -> HeaderViewMut<'_>
return mutable header view
Examples found in repository?
src/frame.rs (line 529)
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
pub fn header_mut(&mut self) -> HeaderViewMut {
self.header.view_mut()
}
/// payload
pub fn payload(&self) -> Payload {
self.payload.clone()
}
}
/// header for user p
#[derive(Debug, Clone)]
pub struct OwnedFrame {
header: Header,
payload: BytesMut,
}
impl OwnedFrame {
/// construct new owned frame
#[allow(clippy::too_many_arguments)]
pub fn new<M: Into<Option<[u8; 4]>>>(
fin: bool,
rsv1: bool,
rsv2: bool,
rsv3: bool,
mask_key: M,
opcode: OpCode,
mut payload: BytesMut,
mask_payload: bool,
) -> Self {
let header = Header::new(
fin,
rsv1,
rsv2,
rsv3,
mask_key,
opcode,
payload.len() as u64,
);
if mask_payload {
if let Some(key) = header.masking_key() {
apply_mask_fast32(&mut payload, key);
}
};
Self { header, payload }
}
/// immutable header view
pub fn header(&self) -> HeaderView {
self.header.view()
}
/// mutable header view
pub fn header_mut(&mut self) -> HeaderViewMut {
self.header.view_mut()
}