@@ -4,6 +4,11 @@ use std::path::PathBuf;
44
55use futures:: { Async , AsyncSink , Poll , Sink , StartSend , Stream } ;
66
7+ #[ cfg( feature = "unstable-futures" ) ]
8+ use futures2:: { self , task} ;
9+ #[ cfg( feature = "unstable-futures" ) ]
10+ use futures_sink;
11+
712use UnixDatagram ;
813
914/// Encoding of frames via buffers.
@@ -80,6 +85,20 @@ impl<C: UnixDatagramCodec> Stream for UnixDatagramFramed<C> {
8085 }
8186}
8287
88+ #[ cfg( feature = "unstable-futures" ) ]
89+ impl < C : UnixDatagramCodec > futures2:: Stream for UnixDatagramFramed < C > {
90+ type Item = C :: In ;
91+ type Error = io:: Error ;
92+
93+ fn poll_next ( & mut self , cx : & mut task:: Context ) -> futures2:: Poll < Option < C :: In > , io:: Error > {
94+ let ( n, addr) = try_ready2 ! ( self . socket. recv_from2( cx, & mut self . rd) ) ;
95+ trace ! ( "received {} bytes, decoding" , n) ;
96+ let frame = try!( self . codec . decode ( & addr, & self . rd [ ..n] ) ) ;
97+ trace ! ( "frame decoded from buffer" ) ;
98+ Ok ( futures2:: Async :: Ready ( Some ( frame) ) )
99+ }
100+ }
101+
83102impl < C : UnixDatagramCodec > Sink for UnixDatagramFramed < C > {
84103 type SinkItem = C :: Out ;
85104 type SinkError = io:: Error ;
@@ -124,6 +143,53 @@ impl<C: UnixDatagramCodec> Sink for UnixDatagramFramed<C> {
124143 }
125144}
126145
146+ #[ cfg( feature = "unstable-futures" ) ]
147+ impl < C : UnixDatagramCodec > futures_sink:: Sink for UnixDatagramFramed < C > {
148+ type SinkItem = C :: Out ;
149+ type SinkError = io:: Error ;
150+
151+ fn poll_ready ( & mut self , cx : & mut task:: Context ) -> futures2:: Poll < ( ) , io:: Error > {
152+ if self . wr . len ( ) > 0 {
153+ try!( self . poll_flush ( cx) ) ;
154+ if self . wr . len ( ) > 0 {
155+ return Ok ( futures2:: Async :: Pending ) ;
156+ }
157+ }
158+ Ok ( ( ) . into ( ) )
159+ }
160+
161+ fn start_send ( & mut self , item : C :: Out ) -> Result < ( ) , io:: Error > {
162+ self . out_addr = try!( self . codec . encode ( item, & mut self . wr ) ) ;
163+ Ok ( ( ) )
164+ }
165+
166+ fn poll_flush ( & mut self , cx : & mut task:: Context ) -> futures2:: Poll < ( ) , io:: Error > {
167+ trace ! ( "flushing framed transport" ) ;
168+
169+ if self . wr . is_empty ( ) {
170+ return Ok ( futures2:: Async :: Ready ( ( ) ) ) ;
171+ }
172+
173+ trace ! ( "writing; remaining={}" , self . wr. len( ) ) ;
174+ let n = try_ready2 ! ( self . socket. send_to2( cx, & self . wr, & self . out_addr) ) ;
175+ trace ! ( "written {}" , n) ;
176+ let wrote_all = n == self . wr . len ( ) ;
177+ self . wr . clear ( ) ;
178+ if wrote_all {
179+ Ok ( futures2:: Async :: Ready ( ( ) ) )
180+ } else {
181+ Err ( io:: Error :: new (
182+ io:: ErrorKind :: Other ,
183+ "failed to write entire datagram to socket" ,
184+ ) )
185+ }
186+ }
187+
188+ fn poll_close ( & mut self , cx : & mut task:: Context ) -> futures2:: Poll < ( ) , io:: Error > {
189+ self . poll_flush ( cx)
190+ }
191+ }
192+
127193pub fn new < C : UnixDatagramCodec > ( socket : UnixDatagram , codec : C ) -> UnixDatagramFramed < C > {
128194 UnixDatagramFramed {
129195 socket : socket,
0 commit comments