From 5cc3bba5cc5c8794fc46cb40f9590f4b94ec1039 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 29 Jan 2018 15:45:37 -0800 Subject: [PATCH] change ws client names --- examples/websocket/src/client.rs | 6 ++-- src/client/parser.rs | 55 ++++++++++++++++---------------- src/ws/client.rs | 26 ++++++++------- src/ws/mod.rs | 2 +- 4 files changed, 46 insertions(+), 43 deletions(-) diff --git a/examples/websocket/src/client.rs b/examples/websocket/src/client.rs index d2c11617e..2b114263f 100644 --- a/examples/websocket/src/client.rs +++ b/examples/websocket/src/client.rs @@ -13,7 +13,7 @@ use std::time::Duration; use actix::*; use futures::Future; use tokio_core::net::TcpStream; -use actix_web::ws::{client, Message, WsClientError}; +use actix_web::ws::{Message, WsClientError, WsClient, WsClientWriter}; fn main() { @@ -22,7 +22,7 @@ fn main() { let sys = actix::System::new("ws-example"); Arbiter::handle().spawn( - client::WsClient::new("http://127.0.0.1:8080/ws/") + WsClient::new("http://127.0.0.1:8080/ws/") .connect().unwrap() .map_err(|e| { println!("Error: {}", e); @@ -54,7 +54,7 @@ fn main() { } -struct ChatClient(client::WsWriter); +struct ChatClient(WsClientWriter); #[derive(Message)] struct ClientCommand(String); diff --git a/src/client/parser.rs b/src/client/parser.rs index 9e81692ee..32995ecaa 100644 --- a/src/client/parser.rs +++ b/src/client/parser.rs @@ -43,32 +43,6 @@ pub enum HttpResponseParserError { impl HttpResponseParser { - fn decode(&mut self, buf: &mut BytesMut) -> Result { - if let Some(ref mut payload) = self.payload { - if payload.tx.capacity() > DEFAULT_BUFFER_SIZE { - return Ok(Decoding::Paused) - } - loop { - match payload.decoder.decode(buf) { - Ok(Async::Ready(Some(bytes))) => { - payload.tx.feed_data(bytes) - }, - Ok(Async::Ready(None)) => { - payload.tx.feed_eof(); - return Ok(Decoding::Ready) - }, - Ok(Async::NotReady) => return Ok(Decoding::NotReady), - Err(err) => { - payload.tx.set_error(err.into()); - return Err(HttpResponseParserError::Payload) - } - } - } - } else { - return Ok(Decoding::Ready) - } - } - pub fn parse(&mut self, io: &mut T, buf: &mut BytesMut) -> Poll where T: IoStream @@ -154,7 +128,34 @@ impl HttpResponseParser { } } - fn parse_message(buf: &mut BytesMut) -> Poll<(ClientResponse, Option), ParseError> + fn decode(&mut self, buf: &mut BytesMut) -> Result { + if let Some(ref mut payload) = self.payload { + if payload.tx.capacity() > DEFAULT_BUFFER_SIZE { + return Ok(Decoding::Paused) + } + loop { + match payload.decoder.decode(buf) { + Ok(Async::Ready(Some(bytes))) => { + payload.tx.feed_data(bytes) + }, + Ok(Async::Ready(None)) => { + payload.tx.feed_eof(); + return Ok(Decoding::Ready) + }, + Ok(Async::NotReady) => return Ok(Decoding::NotReady), + Err(err) => { + payload.tx.set_error(err.into()); + return Err(HttpResponseParserError::Payload) + } + } + } + } else { + return Ok(Decoding::Ready) + } + } + + fn parse_message(buf: &mut BytesMut) + -> Poll<(ClientResponse, Option), ParseError> { // Parse http message let bytes_ptr = buf.as_ref().as_ptr() as usize; diff --git a/src/ws/client.rs b/src/ws/client.rs index f9536cea3..b1a75fa89 100644 --- a/src/ws/client.rs +++ b/src/ws/client.rs @@ -28,6 +28,10 @@ use super::proto::{CloseCode, OpCode}; use super::frame::Frame; use super::connect::{TcpConnector, TcpConnectorError}; +pub type WsClientFuture = + Future, WsClientWriter), Error=WsClientError>; + + /// Websockt client error #[derive(Fail, Debug)] pub enum WsClientError { @@ -85,8 +89,6 @@ impl From for WsClientError { } } -pub type WsFuture = Future, WsWriter), Error=WsClientError>; - /// Websockt client pub struct WsClient { request: ClientRequestBuilder, @@ -143,7 +145,7 @@ impl WsClient { self } - pub fn connect(&mut self) -> Result>, WsClientError> { + pub fn connect(&mut self) -> Result>, WsClientError> { if let Some(e) = self.err.take() { return Err(e) } @@ -234,7 +236,7 @@ impl WsHandshake { } impl Future for WsHandshake { - type Item = (WsReader, WsWriter); + type Item = (WsClientReader, WsClientWriter); type Error = WsClientError; fn poll(&mut self) -> Poll { @@ -296,8 +298,8 @@ impl Future for WsHandshake { let inner = Rc::new(UnsafeCell::new(Inner{inner: inner})); Ok(Async::Ready( - (WsReader{inner: Rc::clone(&inner)}, - WsWriter{inner: inner}))) + (WsClientReader{inner: Rc::clone(&inner)}, + WsClientWriter{inner: inner}))) }, Ok(Async::NotReady) => { self.inner = Some(inner); @@ -313,18 +315,18 @@ struct Inner { inner: WsInner, } -pub struct WsReader { +pub struct WsClientReader { inner: Rc>> } -impl WsReader { +impl WsClientReader { #[inline] fn as_mut(&mut self) -> &mut Inner { unsafe{ &mut *self.inner.get() } } } -impl Stream for WsReader { +impl Stream for WsClientReader { type Item = Message; type Error = WsClientError; @@ -404,18 +406,18 @@ impl Stream for WsReader { } } -pub struct WsWriter { +pub struct WsClientWriter { inner: Rc>> } -impl WsWriter { +impl WsClientWriter { #[inline] fn as_mut(&mut self) -> &mut Inner { unsafe{ &mut *self.inner.get() } } } -impl WsWriter { +impl WsClientWriter { /// Write payload #[inline] diff --git a/src/ws/mod.rs b/src/ws/mod.rs index 1cb59a04b..0effcd9ac 100644 --- a/src/ws/mod.rs +++ b/src/ws/mod.rs @@ -67,7 +67,7 @@ use self::frame::Frame; use self::proto::{hash_key, OpCode}; pub use self::proto::CloseCode; pub use self::context::WebsocketContext; -pub use self::client::{WsClient, WsClientError, WsReader, WsWriter, WsFuture}; +pub use self::client::{WsClient, WsClientError, WsClientReader, WsClientWriter, WsClientFuture}; const SEC_WEBSOCKET_ACCEPT: &str = "SEC-WEBSOCKET-ACCEPT"; const SEC_WEBSOCKET_KEY: &str = "SEC-WEBSOCKET-KEY";