Skip to content

Commit

Permalink
Small extensions to the WS protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Jan 20, 2024
1 parent 33885e2 commit 9567096
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
8 changes: 8 additions & 0 deletions edge-http/src/io/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ where
Ok(self.io_mut())
}

pub fn release(mut self) -> (T::Connection<'b>, &'b mut [u8]) {
let mut state = self.unbind();

let io = state.io.take().unwrap();

(io, state.buf)
}

async fn start_request(
&mut self,
http11: bool,
Expand Down
47 changes: 37 additions & 10 deletions edge-ws/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ impl FrameType {
}
}

impl fmt::Display for FrameType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Text(fragmented) => write!(f, "Text{}", if *fragmented { " (fragmented)" } else { "" }),
Self::Binary(fragmented) => write!(f, "Binary{}", if *fragmented { " (fragmented)" } else { "" }),
Self::Ping => write!(f, "Ping"),
Self::Pong => write!(f, "Pong"),
Self::Close => write!(f, "Close"),
Self::Continue(ffinal) => write!(f, "Continue{}", if *ffinal { " (final)" } else { "" }),
}
}
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub enum Error<E> {
Incomplete(usize),
Expand Down Expand Up @@ -159,17 +172,21 @@ impl FrameHeader {
None
};

let frame_type = match opcode {
0 => FrameType::Continue(final_frame),
1 => FrameType::Text(!final_frame),
2 => FrameType::Binary(!final_frame),
8 => FrameType::Close,
9 => FrameType::Ping,
10 => FrameType::Pong,
_ => unreachable!(),
};

let frame_header = FrameHeader {
frame_type: match opcode {
0 => FrameType::Continue(final_frame),
1 => FrameType::Text(!final_frame),
2 => FrameType::Binary(!final_frame),
8 => FrameType::Close,
9 => FrameType::Ping,
10 => FrameType::Pong,
_ => unreachable!(),
},
payload_len,
frame_type,
payload_len: matches!(frame_type, FrameType::Binary(_) | FrameType::Text(_) | FrameType::Continue(_))
.then(|| payload_len)
.unwrap_or(0),
mask_key,
};

Expand Down Expand Up @@ -266,6 +283,16 @@ impl FrameHeader {
}
}

impl fmt::Display for FrameHeader {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Frame {{ {}, payload len {}, mask {:?} }}",
self.frame_type, self.payload_len, self.mask_key
)
}
}

#[cfg(feature = "embedded-svc")]
mod embedded_svc_compat {
use core::convert::TryFrom;
Expand Down

0 comments on commit 9567096

Please sign in to comment.