|
|
|
@ -81,6 +81,17 @@ impl Default for WebSocketConfig { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl WebSocketConfig { |
|
|
|
|
/// Panic if values are invalid.
|
|
|
|
|
pub(crate) fn assert_valid(&self) { |
|
|
|
|
assert!( |
|
|
|
|
self.max_write_buffer_size > self.write_buffer_size, |
|
|
|
|
"WebSocketConfig::max_write_buffer_size must be greater than write_buffer_size, \ |
|
|
|
|
see WebSocketConfig docs`" |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// WebSocket input-output stream.
|
|
|
|
|
///
|
|
|
|
|
/// This is THE structure you want to create to be able to speak the WebSocket protocol.
|
|
|
|
@ -101,6 +112,9 @@ impl<Stream> WebSocket<Stream> { |
|
|
|
|
/// Call this function if you're using Tungstenite as a part of a web framework
|
|
|
|
|
/// or together with an existing one. If you need an initial handshake, use
|
|
|
|
|
/// `connect()` or `accept()` functions of the crate to construct a websocket.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
/// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`.
|
|
|
|
|
pub fn from_raw_socket(stream: Stream, role: Role, config: Option<WebSocketConfig>) -> Self { |
|
|
|
|
WebSocket { socket: stream, context: WebSocketContext::new(role, config) } |
|
|
|
|
} |
|
|
|
@ -110,6 +124,9 @@ impl<Stream> WebSocket<Stream> { |
|
|
|
|
/// Call this function if you're using Tungstenite as a part of a web framework
|
|
|
|
|
/// or together with an existing one. If you need an initial handshake, use
|
|
|
|
|
/// `connect()` or `accept()` functions of the crate to construct a websocket.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
/// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`.
|
|
|
|
|
pub fn from_partially_read( |
|
|
|
|
stream: Stream, |
|
|
|
|
part: Vec<u8>, |
|
|
|
@ -132,6 +149,9 @@ impl<Stream> WebSocket<Stream> { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Change the configuration.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
/// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`.
|
|
|
|
|
pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig)) { |
|
|
|
|
self.context.set_config(set_func) |
|
|
|
|
} |
|
|
|
@ -291,16 +311,23 @@ pub struct WebSocketContext { |
|
|
|
|
|
|
|
|
|
impl WebSocketContext { |
|
|
|
|
/// Create a WebSocket context that manages a post-handshake stream.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
/// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`.
|
|
|
|
|
pub fn new(role: Role, config: Option<WebSocketConfig>) -> Self { |
|
|
|
|
Self::_new(role, FrameCodec::new(), config.unwrap_or_default()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Create a WebSocket context that manages an post-handshake stream.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
/// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`.
|
|
|
|
|
pub fn from_partially_read(part: Vec<u8>, role: Role, config: Option<WebSocketConfig>) -> Self { |
|
|
|
|
Self::_new(role, FrameCodec::from_partially_read(part), config.unwrap_or_default()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn _new(role: Role, mut frame: FrameCodec, config: WebSocketConfig) -> Self { |
|
|
|
|
config.assert_valid(); |
|
|
|
|
frame.set_max_out_buffer_len(config.max_write_buffer_size); |
|
|
|
|
frame.set_out_buffer_write_len(config.write_buffer_size); |
|
|
|
|
Self { |
|
|
|
@ -314,8 +341,12 @@ impl WebSocketContext { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Change the configuration.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
/// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`.
|
|
|
|
|
pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig)) { |
|
|
|
|
set_func(&mut self.config); |
|
|
|
|
self.config.assert_valid(); |
|
|
|
|
self.frame.set_max_out_buffer_len(self.config.max_write_buffer_size); |
|
|
|
|
self.frame.set_out_buffer_write_len(self.config.write_buffer_size); |
|
|
|
|
} |
|
|
|
|