Skip to main content

lightws/role/
client.rs

1use super::{RoleHelper, ClientRole, AutoMaskClientRole};
2use crate::frame::Mask;
3
4macro_rules! client_consts {
5    () => {
6        const SHORT_FRAME_HEAD_LEN: u8 = 2;
7        const COMMON_FRAME_HEAD_LEN: u8 = 2 + 2;
8        const LONG_FRAME_HEAD_LEN: u8 = 2 + 8;
9    };
10}
11
12/// Simple client using an empty(fake) mask key.
13///
14/// It simply skips masking before writing data.
15#[derive(Clone, Copy)]
16pub struct Client;
17
18impl RoleHelper for Client {
19    client_consts!();
20
21    #[inline]
22    fn new() -> Self { Self {} }
23
24    #[inline]
25    fn mask_key(&self) -> Mask { Mask::Skip }
26}
27
28impl ClientRole for Client {}
29
30/// Standard client using random mask key.
31///
32/// With `unsafe_auto_mask_write` feature enabled, it will automatically
33/// update its inner mask key and mask payload data before a write.
34#[derive(Clone, Copy)]
35pub struct StandardClient([u8; 4]);
36
37impl RoleHelper for StandardClient {
38    client_consts!();
39
40    #[inline]
41    fn new() -> Self { Self([0u8; 4]) }
42
43    #[inline]
44    fn mask_key(&self) -> Mask { Mask::Key(self.0) }
45
46    #[inline]
47    fn set_mask_key(&mut self, mask: [u8; 4]) { self.0 = mask; }
48}
49
50impl ClientRole for StandardClient {}
51
52impl AutoMaskClientRole for StandardClient {
53    const UPDATE_MASK_KEY: bool = true;
54}
55
56/// Client using a fixed mask key.
57///
58/// With `unsafe_auto_mask_write` feature enabled, it will automatically
59/// mask payload data before a write, where its inner mask key is not updated.
60#[derive(Clone, Copy)]
61pub struct FixedMaskClient([u8; 4]);
62
63impl RoleHelper for FixedMaskClient {
64    client_consts!();
65
66    #[inline]
67    fn new() -> Self { Self(crate::frame::new_mask_key()) }
68
69    #[inline]
70    fn mask_key(&self) -> Mask { Mask::Key(self.0) }
71
72    #[inline]
73    fn set_mask_key(&mut self, mask: [u8; 4]) { self.0 = mask; }
74}
75
76impl ClientRole for FixedMaskClient {}
77
78impl AutoMaskClientRole for FixedMaskClient {
79    const UPDATE_MASK_KEY: bool = false;
80}