lightws/role/mod.rs
1//! Markers.
2//!
3//! Markers are used to apply different strategies to clients or servers.
4//!
5//! For example, `Endpoint<IO, Client>::connect` is used to to open a connection,
6//! and returns `Stream<IO, Client>`; `Endpoint<IO, Server>` is used to accept
7//! a connection and returns `Stream<IO, Server>`.
8//!
9//! Both client and server meet [`RoleHelper`], which indicates frame head length
10//! (currently unused), and how to mask payload data. Only client meets [`ClientRole`],
11//! and only server meets [`ServerRole`].
12//!
13//! Any type implements these traits will be treated as a `client` or `server`.
14
15use crate::frame::Mask;
16
17/// Client or Server marker.
18pub trait RoleHelper: Clone + Copy {
19 const SHORT_FRAME_HEAD_LEN: u8;
20 const COMMON_FRAME_HEAD_LEN: u8;
21 const LONG_FRAME_HEAD_LEN: u8;
22
23 fn new() -> Self;
24 fn mask_key(&self) -> Mask;
25 // by default this is a no-op
26 fn set_mask_key(&mut self, _: [u8; 4]) {}
27}
28
29/// Client marker.
30pub trait ClientRole: RoleHelper {
31 const SHORT_FRAME_HEAD_LEN: u8 = 2;
32 const COMMON_FRAME_HEAD_LEN: u8 = 2 + 2;
33 const LONG_FRAME_HEAD_LEN: u8 = 2 + 8;
34}
35
36/// Server marker.
37pub trait ServerRole: RoleHelper {}
38
39/// Client marker.
40pub trait AutoMaskClientRole: ClientRole {
41 const UPDATE_MASK_KEY: bool;
42}
43
44mod server;
45mod client;
46
47pub use server::Server;
48pub use client::{Client, StandardClient, FixedMaskClient};