Skip to content

Commit

Permalink
rename write_mask(this is ambiguous) => write_mask_key
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Apr 12, 2022
1 parent 10d6b73 commit 14cf7a9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lightws"
version = "0.5.0"
version = "0.5.1"
authors = ["zephyr <[email protected]>"]
description = "Lightweight websocket implement for stream transmission."
repository = "https://github.com/zephyrchien/lightws"
Expand Down
12 changes: 6 additions & 6 deletions src/role/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub trait RoleHelper: Clone + Copy {
const LONG_FRAME_HEAD_LEN: u8;

fn new() -> Self;
fn write_mask(&self) -> Mask;
fn write_mask_key(&self) -> Mask;
// by default this is a no-op
fn set_write_mask(&mut self, _: [u8; 4]) {}
fn set_write_mask_key(&mut self, _: [u8; 4]) {}
}

/// Client marker.
Expand Down Expand Up @@ -56,7 +56,7 @@ impl RoleHelper for Client {
fn new() -> Self { Self {} }

#[inline]
fn write_mask(&self) -> Mask { Mask::Skip }
fn write_mask_key(&self) -> Mask { Mask::Skip }
}

impl RoleHelper for Server {
Expand All @@ -69,7 +69,7 @@ impl RoleHelper for Server {

/// Server should not mask the payload.
#[inline]
fn write_mask(&self) -> Mask { Mask::None }
fn write_mask_key(&self) -> Mask { Mask::None }
}

impl RoleHelper for StandardClient {
Expand All @@ -81,10 +81,10 @@ impl RoleHelper for StandardClient {
fn new() -> Self { Self(crate::frame::mask::new_mask_key()) }

#[inline]
fn write_mask(&self) -> Mask { Mask::Key(self.0) }
fn write_mask_key(&self) -> Mask { Mask::Key(self.0) }

#[inline]
fn set_write_mask(&mut self, mask: [u8; 4]) { self.0 = mask; }
fn set_write_mask_key(&mut self, mask: [u8; 4]) { self.0 = mask; }
}

impl ClientRole for Client {}
Expand Down
6 changes: 3 additions & 3 deletions src/stream/ctrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ where
{
/// Get mask for upcoming writes.
#[inline]
pub fn write_mask(&self) -> Mask { self.role.write_mask() }
pub fn write_mask_key(&self) -> Mask { self.role.write_mask_key() }

/// Set mask for upcoming writes.
/// An attempt to set mask during a write will fail with [`CtrlError::SetMaskInWrite`].
#[inline]
pub fn set_write_mask(&mut self, mask: [u8; 4]) -> Result<(), CtrlError> {
pub fn set_write_mask_key(&mut self, mask: [u8; 4]) -> Result<(), CtrlError> {
// make sure this is a new fresh write
if let WriteState::WriteHead(head) = self.write_state {
if head.is_empty() {
self.role.set_write_mask(mask);
self.role.set_write_mask_key(mask);
return Ok(());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/stream/detail/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
let frame_len = buf.len();

if head_store.is_empty() {
let mask = stream.role.write_mask();
let mask = stream.role.write_mask_key();
write_data_frame(&mut head_store, mask, frame_len as u64);
}
// frame head(maybe partial) + payload
Expand Down
16 changes: 8 additions & 8 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@
//!
//! Data written to stream are not automatically masked. A standard client should mask data
//! before sending it. The mask key is prepared by a [`ClientRole`](crate::role::ClientRole),
//! which can be set or fetched via [`Stream::set_write_mask`] and [`Stream::write_mask`].
//! which can be set or fetched via [`Stream::set_write_mask_key`] and [`Stream::write_mask_key`].
//!
//! Example:
//!
//! ```no_run
//! use std::io::{Read, Write};
//! use std::net::TcpStream;
//! use lightws::role::Client;
//! use lightws::role::StandardClient;
//! use lightws::endpoint::Endpoint;
//! use lightws::frame::{new_mask_key, apply_mask4};
//! fn write_data() -> std::io::Result<()> {
//! let mut buf = [0u8; 256];
//! let mut tcp = TcpStream::connect("example.com:80")?;
//! let mut ws = Endpoint::<TcpStream, Client>::connect(tcp, &mut buf, "example.com", "/ws")?;
//!
//! let mut ws = Endpoint::<TcpStream, StandardClient>::connect(tcp, &mut buf, "example.com", "/ws")?;
//!
//! // mask data
//! let key = new_mask_key();
//! apply_mask4(key, &mut buf);
//!
//!
//! // set mask key for next write
//! ws.set_write_mask(key)?;
//!
//! ws.set_write_mask_key(key)?;
//!
//! // write some data
//! ws.write_all(&buf)?;
//! Ok(())
Expand Down Expand Up @@ -203,7 +203,7 @@ mod test {
let head = FrameHead::new(
Fin::Y,
opcode,
R::write_mask(&R::new()),
R::write_mask_key(&R::new()),
PayloadLen::from_num(len as u64),
);

Expand Down

0 comments on commit 14cf7a9

Please sign in to comment.