From 85f211230a06bbb74120f98f9b7df4e149a839e7 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Fri, 17 Jan 2020 21:23:38 +0100 Subject: [PATCH] websocket: add `can_read()` and `can_write()` Signed-off-by: Alexey Galakhov --- src/protocol/mod.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 7627b46..c30544b 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -110,6 +110,21 @@ impl WebSocket { pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig)) { self.context.set_config(set_func) } + + /// Check if it is possible to read messages. + /// + /// Reading is impossible after receiving `Message::Close`. It is still possible after + /// sending close frame since the peer still may send some data before confirming close. + pub fn can_read(&self) -> bool { + self.context.can_read() + } + + /// Check if it is possible to write messages. + /// + /// Writing gets impossible immediately after sending or receiving `Message::Close`. + pub fn can_write(&self) -> bool { + self.context.can_write() + } } impl WebSocket { @@ -239,6 +254,21 @@ impl WebSocketContext { set_func(&mut self.config) } + /// Check if it is possible to read messages. + /// + /// Reading is impossible after receiving `Message::Close`. It is still possible after + /// sending close frame since the peer still may send some data before confirming close. + pub fn can_read(&self) -> bool { + self.state.can_read() + } + + /// Check if it is possible to write messages. + /// + /// Writing gets impossible immediately after sending or receiving `Message::Close`. + pub fn can_write(&self) -> bool { + self.state.is_active() + } + /// Read a message from the provided stream, if possible. /// /// This function sends pong and close responses automatically.