@@ -4,7 +4,9 @@ extern crate url;
44use std:: net:: TcpStream ;
55use std:: net:: SocketAddr ;
66use std:: io:: Result as IoResult ;
7+ use std:: io:: { Read , Write } ;
78use hyper:: header:: Headers ;
9+ use hyper:: buffer:: BufReader ;
810
911use ws;
1012use ws:: sender:: Sender as SenderTrait ;
@@ -56,7 +58,7 @@ pub use self::builder::{ClientBuilder, Url, ParseError};
5658pub struct Client < S >
5759 where S : Stream
5860{
59- pub stream : S ,
61+ stream : BufReader < S > ,
6062 headers : Headers ,
6163 sender : Sender ,
6264 receiver : Receiver ,
@@ -66,13 +68,13 @@ impl Client<TcpStream> {
6668 /// Shuts down the sending half of the client connection, will cause all pending
6769 /// and future IO to return immediately with an appropriate value.
6870 pub fn shutdown_sender ( & self ) -> IoResult < ( ) > {
69- self . stream . as_tcp ( ) . shutdown ( Shutdown :: Write )
71+ self . stream . get_ref ( ) . as_tcp ( ) . shutdown ( Shutdown :: Write )
7072 }
7173
7274 /// Shuts down the receiving half of the client connection, will cause all pending
7375 /// and future IO to return immediately with an appropriate value.
7476 pub fn shutdown_receiver ( & self ) -> IoResult < ( ) > {
75- self . stream . as_tcp ( ) . shutdown ( Shutdown :: Read )
77+ self . stream . get_ref ( ) . as_tcp ( ) . shutdown ( Shutdown :: Read )
7678 }
7779}
7880
@@ -82,27 +84,27 @@ impl<S> Client<S>
8284 /// Shuts down the client connection, will cause all pending and future IO to
8385 /// return immediately with an appropriate value.
8486 pub fn shutdown ( & self ) -> IoResult < ( ) > {
85- self . stream . as_tcp ( ) . shutdown ( Shutdown :: Both )
87+ self . stream . get_ref ( ) . as_tcp ( ) . shutdown ( Shutdown :: Both )
8688 }
8789
8890 /// See `TcpStream.peer_addr()`.
8991 pub fn peer_addr ( & self ) -> IoResult < SocketAddr > {
90- self . stream . as_tcp ( ) . peer_addr ( )
92+ self . stream . get_ref ( ) . as_tcp ( ) . peer_addr ( )
9193 }
9294
9395 /// See `TcpStream.local_addr()`.
9496 pub fn local_addr ( & self ) -> IoResult < SocketAddr > {
95- self . stream . as_tcp ( ) . local_addr ( )
97+ self . stream . get_ref ( ) . as_tcp ( ) . local_addr ( )
9698 }
9799
98100 /// See `TcpStream.set_nodelay()`.
99101 pub fn set_nodelay ( & mut self , nodelay : bool ) -> IoResult < ( ) > {
100- self . stream . as_tcp ( ) . set_nodelay ( nodelay)
102+ self . stream . get_ref ( ) . as_tcp ( ) . set_nodelay ( nodelay)
101103 }
102104
103105 /// Changes whether the stream is in nonblocking mode.
104106 pub fn set_nonblocking ( & self , nonblocking : bool ) -> IoResult < ( ) > {
105- self . stream . as_tcp ( ) . set_nonblocking ( nonblocking)
107+ self . stream . get_ref ( ) . as_tcp ( ) . set_nonblocking ( nonblocking)
106108 }
107109}
108110
@@ -113,7 +115,7 @@ impl<S> Client<S>
113115 /// **without sending any handshake** this is meant to only be used with
114116 /// a stream that has a websocket connection already set up.
115117 /// If in doubt, don't use this!
116- pub fn unchecked ( stream : S , headers : Headers ) -> Self {
118+ pub fn unchecked ( stream : BufReader < S > , headers : Headers ) -> Self {
117119 Client {
118120 headers : headers,
119121 stream : stream,
@@ -128,15 +130,15 @@ impl<S> Client<S>
128130 pub fn send_dataframe < D > ( & mut self , dataframe : & D ) -> WebSocketResult < ( ) >
129131 where D : DataFrameable
130132 {
131- self . sender . send_dataframe ( & mut self . stream , dataframe)
133+ self . sender . send_dataframe ( self . stream . get_mut ( ) , dataframe)
132134 }
133135
134136 /// Sends a single message to the remote endpoint.
135137 pub fn send_message < ' m , M , D > ( & mut self , message : & ' m M ) -> WebSocketResult < ( ) >
136138 where M : ws:: Message < ' m , D > ,
137139 D : DataFrameable
138140 {
139- self . sender . send_message ( & mut self . stream , message)
141+ self . sender . send_message ( self . stream . get_mut ( ) , message)
140142 }
141143
142144 /// Reads a single data frame from the remote endpoint.
@@ -145,7 +147,7 @@ impl<S> Client<S>
145147 }
146148
147149 /// Returns an iterator over incoming data frames.
148- pub fn incoming_dataframes < ' a > ( & ' a mut self ) -> DataFrameIterator < ' a , Receiver , S > {
150+ pub fn incoming_dataframes < ' a > ( & ' a mut self ) -> DataFrameIterator < ' a , Receiver , BufReader < S > > {
149151 self . receiver . incoming_dataframes ( & mut self . stream )
150152 }
151153
@@ -180,15 +182,20 @@ impl<S> Client<S>
180182 }
181183
182184 pub fn stream_ref ( & self ) -> & S {
183- & self . stream
185+ self . stream . get_ref ( )
184186 }
185187
186- pub fn stream_ref_mut ( & mut self ) -> & mut S {
188+ pub fn writer_mut ( & mut self ) -> & mut Write {
189+ self . stream . get_mut ( )
190+ }
191+
192+ pub fn reader_mut ( & mut self ) -> & mut Read {
187193 & mut self . stream
188194 }
189195
190- pub fn into_stream ( self ) -> S {
191- self . stream
196+ pub fn into_stream ( self ) -> ( S , Option < ( Vec < u8 > , usize , usize ) > ) {
197+ let ( stream, buf, pos, cap) = self . stream . into_parts ( ) ;
198+ ( stream, Some ( ( buf, pos, cap) ) )
192199 }
193200
194201 /// Returns an iterator over incoming messages.
@@ -229,7 +236,8 @@ impl<S> Client<S>
229236 ///}
230237 ///# }
231238 ///```
232- pub fn incoming_messages < ' a , M , D > ( & ' a mut self ) -> MessageIterator < ' a , Receiver , D , M , S >
239+ pub fn incoming_messages < ' a , M , D > ( & ' a mut self , )
240+ -> MessageIterator < ' a , Receiver , D , M , BufReader < S > >
233241 where M : ws:: Message < ' a , D > ,
234242 D : DataFrameable
235243 {
@@ -269,9 +277,10 @@ impl<S> Client<S>
269277 pub fn split
270278 ( self , )
271279 -> IoResult < ( Reader < <S as Splittable >:: Reader > , Writer < <S as Splittable >:: Writer > ) > {
272- let ( read, write) = try!( self . stream . split ( ) ) ;
280+ let ( stream, buf, pos, cap) = self . stream . into_parts ( ) ;
281+ let ( read, write) = try!( stream. split ( ) ) ;
273282 Ok ( ( Reader {
274- stream : read,
283+ stream : BufReader :: from_parts ( read, buf , pos , cap ) ,
275284 receiver : self . receiver ,
276285 } ,
277286 Writer {
0 commit comments