1919// DEALINGS IN THE SOFTWARE.
2020
2121use crate :: error:: PlainTextError ;
22- use crate :: handshake:: Remote ;
2322
24- use bytes:: BytesMut ;
2523use futures:: future:: { self , Ready } ;
2624use futures:: prelude:: * ;
27- use futures:: { future:: BoxFuture , Sink , Stream } ;
28- use futures_codec:: Framed ;
25+ use futures:: future:: BoxFuture ;
2926use libp2p_core:: {
3027 identity,
3128 InboundUpgrade ,
@@ -35,9 +32,7 @@ use libp2p_core::{
3532 PublicKey ,
3633} ;
3734use log:: debug;
38- use rw_stream_sink:: RwStreamSink ;
3935use std:: { io, iter, pin:: Pin , task:: { Context , Poll } } ;
40- use unsigned_varint:: codec:: UviBytes ;
4136use void:: Void ;
4237
4338mod error;
@@ -153,76 +148,26 @@ impl PlainText2Config {
153148 T : AsyncRead + AsyncWrite + Send + Unpin + ' static
154149 {
155150 debug ! ( "Starting plaintext handshake." ) ;
156- let ( stream_sink , remote) = PlainTextMiddleware :: handshake ( socket, self ) . await ?;
151+ let ( socket , remote) = handshake :: handshake ( socket, self ) . await ?;
157152 debug ! ( "Finished plaintext handshake." ) ;
158153
159154 Ok ( (
160155 remote. peer_id ,
161156 PlainTextOutput {
162- stream : RwStreamSink :: new ( stream_sink ) ,
157+ socket ,
163158 remote_key : remote. public_key ,
164159 }
165160 ) )
166161 }
167162}
168163
169- pub struct PlainTextMiddleware < S > {
170- inner : Framed < S , UviBytes < BytesMut > > ,
171- }
172-
173- impl < S > PlainTextMiddleware < S >
174- where
175- S : AsyncRead + AsyncWrite + Send + Unpin ,
176- {
177- async fn handshake ( socket : S , config : PlainText2Config )
178- -> Result < ( PlainTextMiddleware < S > , Remote ) , PlainTextError >
179- {
180- let ( inner, remote) = handshake:: handshake ( socket, config) . await ?;
181- Ok ( ( PlainTextMiddleware { inner } , remote) )
182- }
183- }
184-
185- impl < S > Sink < BytesMut > for PlainTextMiddleware < S >
186- where
187- S : AsyncRead + AsyncWrite + Unpin ,
188- {
189- type Error = io:: Error ;
190-
191- fn poll_ready ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
192- Sink :: poll_ready ( Pin :: new ( & mut self . inner ) , cx)
193- }
194-
195- fn start_send ( mut self : Pin < & mut Self > , item : BytesMut ) -> Result < ( ) , Self :: Error > {
196- Sink :: start_send ( Pin :: new ( & mut self . inner ) , item)
197- }
198-
199- fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
200- Sink :: poll_flush ( Pin :: new ( & mut self . inner ) , cx)
201- }
202-
203- fn poll_close ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
204- Sink :: poll_close ( Pin :: new ( & mut self . inner ) , cx)
205- }
206- }
207-
208- impl < S > Stream for PlainTextMiddleware < S >
209- where
210- S : AsyncRead + AsyncWrite + Unpin ,
211- {
212- type Item = Result < BytesMut , io:: Error > ;
213-
214- fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
215- Stream :: poll_next ( Pin :: new ( & mut self . inner ) , cx)
216- }
217- }
218-
219164/// Output of the plaintext protocol.
220165pub struct PlainTextOutput < S >
221166where
222167 S : AsyncRead + AsyncWrite + Unpin ,
223168{
224169 /// The plaintext stream.
225- pub stream : RwStreamSink < PlainTextMiddleware < S > > ,
170+ pub socket : S ,
226171 /// The public key of the remote.
227172 pub remote_key : PublicKey ,
228173}
@@ -231,26 +176,26 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for PlainTextOutput<S> {
231176 fn poll_read ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > , buf : & mut [ u8 ] )
232177 -> Poll < Result < usize , io:: Error > >
233178 {
234- AsyncRead :: poll_read ( Pin :: new ( & mut self . stream ) , cx, buf)
179+ AsyncRead :: poll_read ( Pin :: new ( & mut self . socket ) , cx, buf)
235180 }
236181}
237182
238183impl < S : AsyncRead + AsyncWrite + Unpin > AsyncWrite for PlainTextOutput < S > {
239184 fn poll_write ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > , buf : & [ u8 ] )
240185 -> Poll < Result < usize , io:: Error > >
241186 {
242- AsyncWrite :: poll_write ( Pin :: new ( & mut self . stream ) , cx, buf)
187+ AsyncWrite :: poll_write ( Pin :: new ( & mut self . socket ) , cx, buf)
243188 }
244189
245190 fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > )
246191 -> Poll < Result < ( ) , io:: Error > >
247192 {
248- AsyncWrite :: poll_flush ( Pin :: new ( & mut self . stream ) , cx)
193+ AsyncWrite :: poll_flush ( Pin :: new ( & mut self . socket ) , cx)
249194 }
250195
251196 fn poll_close ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > )
252197 -> Poll < Result < ( ) , io:: Error > >
253198 {
254- AsyncWrite :: poll_close ( Pin :: new ( & mut self . stream ) , cx)
199+ AsyncWrite :: poll_close ( Pin :: new ( & mut self . socket ) , cx)
255200 }
256201}
0 commit comments