Struct ws_tool::frame::Header

source ·
pub struct Header(_);
Expand description

owned header buf

Implementations§

get fin bit value

get rsv1 bit value

get rsv2 bit value

get rsv3 bit value

return frame opcode

get mask bit value

return payload len

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
Hide additional 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(())
    }

return (headerlen, the whole frame len)

set fin bit

set rsv1 bit

set rsv2 bit

set rsv3 bit

set opcode

NOTE if change mask bit after setting payload you need to set payload again to adjust data frame

set header payload lens

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
    }

auto generate mask key and apply it

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
    }

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
Hide additional 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(())
    }

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()
    }

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()
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more