Skip to content

Commit

Permalink
Remove send_queue, use out_buffer instead
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic committed May 24, 2023
1 parent 483d229 commit 0203a18
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 164 deletions.
4 changes: 1 addition & 3 deletions examples/srv_accept_unmasked_frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ fn main() {
};

let config = Some(WebSocketConfig {
max_send_queue: None,
max_message_size: None,
max_frame_size: None,
// This setting allows to accept client frames which are not masked
// This is not in compliance with RFC 6455 but might be handy in some
// rare cases where it is necessary to integrate with existing/legacy
// clients which are sending unmasked frames
accept_unmasked_frames: true,
..<_>::default()
});

let mut websocket = accept_hdr_with_config(stream.unwrap(), callback, config).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ pub enum Error {
/// Protocol violation.
#[error("WebSocket protocol error: {0}")]
Protocol(#[from] ProtocolError),
/// Message send queue full.
#[error("Send queue is full")]
SendQueueFull(Message),
/// Message write buffer is full.
#[error("Write buffer is full")]
WriteBufferFull(Message),
/// UTF coding error.
#[error("UTF-8 encoding error")]
Utf8,
Expand Down
55 changes: 40 additions & 15 deletions src/protocol/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ pub mod coding;
mod frame;
mod mask;

use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read, Write};

use log::*;

pub use self::frame::{CloseFrame, Frame, FrameHeader};
use crate::{
error::{CapacityError, Error, Result},
ReadBuffer,
Message, ReadBuffer,
};
use log::*;
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read, Write};

pub use self::frame::{CloseFrame, Frame, FrameHeader};

/// A reader and writer for WebSocket frames.
#[derive(Debug)]
Expand Down Expand Up @@ -89,25 +88,44 @@ pub(super) struct FrameCodec {
in_buffer: ReadBuffer,
/// Buffer to send packets to the network.
out_buffer: Vec<u8>,
/// Capacity limit for `out_buffer`.
max_out_buffer_len: usize,
/// Header and remaining size of the incoming packet being processed.
header: Option<(FrameHeader, u64)>,
}

impl FrameCodec {
/// Create a new frame codec.
pub(super) fn new() -> Self {
Self { in_buffer: ReadBuffer::new(), out_buffer: Vec::new(), header: None }
Self {
in_buffer: ReadBuffer::new(),
out_buffer: Vec::new(),
max_out_buffer_len: usize::MAX,
header: None,
}
}

/// Create a new frame codec from partially read data.
pub(super) fn from_partially_read(part: Vec<u8>) -> Self {
Self {
in_buffer: ReadBuffer::from_partially_read(part),
out_buffer: Vec::new(),
max_out_buffer_len: usize::MAX,
header: None,
}
}

/// Sets a maximum size for the out buffer.
pub(super) fn with_max_out_buffer_len(mut self, max: usize) -> Self {
self.max_out_buffer_len = max;
self
}

/// Sets a maximum size for the out buffer.
pub(super) fn set_max_out_buffer_len(&mut self, max: usize) {
self.max_out_buffer_len = max;
}

/// Read a frame from the provided stream.
pub(super) fn read_frame<Stream>(
&mut self,
Expand Down Expand Up @@ -173,10 +191,25 @@ impl FrameCodec {
where
Stream: Write,
{
if frame.len() + self.out_buffer.len() > self.max_out_buffer_len {
return Err(Error::WriteBufferFull(Message::Frame(frame)));
}

trace!("writing frame {}", frame);

self.out_buffer.reserve(frame.len());
frame.format(&mut self.out_buffer).expect("Bug: can't write to vector");

self.write_out_buffer(stream)
}

/// Write any buffered frames to the provided stream.
///
/// Does **not** flush.
pub(super) fn write_out_buffer<Stream>(&mut self, stream: &mut Stream) -> Result<()>
where
Stream: Write,
{
while !self.out_buffer.is_empty() {
let len = stream.write(&self.out_buffer)?;
if len == 0 {
Expand All @@ -194,14 +227,6 @@ impl FrameCodec {
}
}

#[cfg(test)]
impl FrameCodec {
/// Returns the size of the output buffer.
pub(super) fn output_buffer_len(&self) -> usize {
self.out_buffer.len()
}
}

#[cfg(test)]
mod tests {

Expand Down
Loading

0 comments on commit 0203a18

Please sign in to comment.