@@ -112,19 +112,6 @@ impl Handle {
112
112
& mut self . codec
113
113
}
114
114
115
- /// Send a frame
116
- pub fn send ( & mut self , item : SendFrame ) -> Result < ( ) , SendError > {
117
- block_on ( async {
118
- // Queue the frame
119
- self . codec . send ( item) . await ?;
120
-
121
- // Flush the frame
122
- self . codec . flush ( ) . await ?;
123
-
124
- Ok ( ( ) )
125
- } )
126
- }
127
-
128
115
/// Writes the client preface
129
116
pub fn write_preface ( & mut self ) {
130
117
// Write the connnection preface
@@ -156,8 +143,7 @@ impl Handle {
156
143
T : Into < frame:: Settings > ,
157
144
{
158
145
let settings = settings. into ( ) ;
159
- self . send ( frame:: Settings :: from ( settings) . into ( ) ) . unwrap ( ) ;
160
- self . expected_settings_acks += 1 ;
146
+ self . send_frame ( frame:: Settings :: from ( settings) ) . await ;
161
147
self . read_preface ( ) . await
162
148
}
163
149
@@ -176,18 +162,24 @@ impl Handle {
176
162
T : Into < frame:: Settings > ,
177
163
{
178
164
self . write_preface ( ) ;
179
- self . send ( settings. into ( ) . into ( ) ) . unwrap ( ) ;
180
- self . expected_settings_acks += 1 ;
165
+ self . send_frame ( settings. into ( ) ) . await ;
181
166
self
182
167
}
183
168
184
169
pub async fn close ( mut self ) {
185
170
futures:: future:: poll_fn ( move |cx| {
186
- let _ = self . poll_next_unpin ( cx) ;
187
- if self . expected_settings_acks == 0 {
188
- Poll :: Ready ( ( ) )
189
- } else {
190
- Poll :: Pending
171
+ loop {
172
+ match self . poll_next_unpin ( cx) {
173
+ Poll :: Ready ( Some ( _) ) => { } ,
174
+ Poll :: Ready ( None ) => return Poll :: Ready ( ( ) ) ,
175
+ Poll :: Pending => {
176
+ if self . expected_settings_acks == 0 {
177
+ return Poll :: Ready ( ( ) )
178
+ } else {
179
+ return Poll :: Pending
180
+ }
181
+ }
182
+ }
191
183
}
192
184
} ) . await
193
185
}
@@ -211,7 +203,13 @@ impl Handle {
211
203
}
212
204
213
205
pub async fn send_frame < T : Into < SendFrame > > ( & mut self , item : T ) {
214
- self . codec . send ( item. into ( ) ) . await . unwrap ( ) ;
206
+ let frame = item. into ( ) ;
207
+ if let Frame :: Settings ( ref settings) = frame {
208
+ if !settings. is_ack ( ) {
209
+ self . expected_settings_acks += 1 ;
210
+ }
211
+ }
212
+ self . codec . send ( frame) . await . unwrap ( ) ;
215
213
self . codec . flush ( ) . await . unwrap ( ) ;
216
214
}
217
215
}
@@ -230,7 +228,7 @@ impl Stream for Handle {
230
228
}
231
229
self . poll_next ( cx)
232
230
} else {
233
- self . send ( frame:: Settings :: ack ( ) . into ( ) ) . unwrap ( ) ;
231
+ block_on ( self . send_frame ( frame:: Settings :: ack ( ) ) ) ;
234
232
Poll :: Ready ( Some ( Ok ( settings. into ( ) ) ) )
235
233
}
236
234
}
@@ -285,7 +283,7 @@ impl Drop for Handle {
285
283
fn drop ( & mut self ) {
286
284
block_on ( self . codec . close ( ) ) . unwrap ( ) ;
287
285
288
- let mut me = self . codec . get_ref ( ) . get_ref ( ) . get_ref ( ) . inner . try_lock ( ) . unwrap ( ) ;
286
+ let mut me = block_on ( self . codec . get_ref ( ) . get_ref ( ) . get_ref ( ) . inner . lock ( ) ) ;
289
287
me. closed = true ;
290
288
291
289
if let Some ( task) = me. rx_task . take ( ) {
@@ -311,7 +309,7 @@ impl AsyncRead for Mock {
311
309
"attempted read with zero length buffer... wut?"
312
310
) ;
313
311
314
- let mut me = self . pipe . inner . try_lock ( ) . unwrap ( ) ;
312
+ let mut me = ready ! ( self . pipe. inner. lock ( ) . poll_unpin ( cx ) ) ;
315
313
316
314
if me. rx . is_empty ( ) {
317
315
if me. closed {
@@ -336,7 +334,7 @@ impl AsyncWrite for Mock {
336
334
cx : & mut Context ,
337
335
mut src : & [ u8 ] ,
338
336
) -> Poll < io:: Result < usize > > {
339
- let mut me = self . pipe . inner . try_lock ( ) . unwrap ( ) ;
337
+ let mut me = ready ! ( self . pipe. inner. lock ( ) . poll_unpin ( cx ) ) ;
340
338
341
339
if me. closed {
342
340
return Poll :: Ready ( Ok ( src. len ( ) ) ) ;
@@ -378,7 +376,7 @@ impl AsyncWrite for Mock {
378
376
379
377
impl Drop for Mock {
380
378
fn drop ( & mut self ) {
381
- let mut me = self . pipe . inner . try_lock ( ) . unwrap ( ) ;
379
+ let mut me = block_on ( self . pipe . inner . lock ( ) ) ;
382
380
me. closed = true ;
383
381
384
382
if let Some ( task) = me. tx_task . take ( ) {
@@ -400,7 +398,7 @@ impl AsyncRead for Pipe {
400
398
"attempted read with zero length buffer... wut?"
401
399
) ;
402
400
403
- let mut me = self . inner . try_lock ( ) . unwrap ( ) ;
401
+ let mut me = ready ! ( self . inner. lock ( ) . poll_unpin ( cx ) ) ;
404
402
405
403
if me. tx . is_empty ( ) {
406
404
if me. closed {
@@ -421,10 +419,10 @@ impl AsyncRead for Pipe {
421
419
impl AsyncWrite for Pipe {
422
420
fn poll_write (
423
421
self : Pin < & mut Self > ,
424
- _ : & mut Context ,
422
+ cx : & mut Context ,
425
423
src : & [ u8 ] ,
426
424
) -> Poll < io:: Result < usize > > {
427
- let mut me = self . inner . try_lock ( ) . unwrap ( ) ;
425
+ let mut me = ready ! ( self . inner. lock ( ) . poll_unpin ( cx ) ) ;
428
426
me. rx . extend ( src) ;
429
427
430
428
if let Some ( task) = me. rx_task . take ( ) {
@@ -456,6 +454,19 @@ pub trait HandleFutureExt: Future<Output = Handle> + Send + Sized + 'static {
456
454
self . recv_frame ( settings. into ( ) )
457
455
}
458
456
457
+ fn ignore_settings ( self ) -> Pin < Box < dyn Future < Output = Handle > + Send > > {
458
+ async {
459
+ let mut handle = self . await ;
460
+ let frame = handle. next ( ) . await ;
461
+ match frame {
462
+ Some ( Ok ( frame:: Frame :: Settings ( _) ) ) => { } ,
463
+ Some ( frame) => panic ! ( "Expected settings, got {:?}" , frame) ,
464
+ None => panic ! ( "Expected settings, got EOF" ) ,
465
+ } ;
466
+ handle
467
+ } . boxed ( )
468
+ }
469
+
459
470
fn recv_frame < T > ( self , frame : T ) -> Pin < Box < dyn Future < Output = Handle > + Send > >
460
471
where
461
472
T : Into < Frame > ,
@@ -506,23 +517,11 @@ pub trait HandleFutureExt: Future<Output = Handle> + Send + Sized + 'static {
506
517
}
507
518
508
519
fn idle_ms ( self , ms : usize ) -> Pin < Box < dyn Future < Output = Handle > + Send > > {
509
- use std:: thread;
510
- use std:: time:: Duration ;
511
-
512
- self . then ( move |handle| {
513
- // This is terrible... but oh well
514
- let ( tx, rx) = oneshot:: channel ( ) ;
515
-
516
- thread:: spawn ( move || {
517
- thread:: sleep ( Duration :: from_millis ( ms as u64 ) ) ;
518
- tx. send ( ( ) ) . unwrap ( ) ;
519
- } ) ;
520
-
521
- Idle {
522
- handle : Some ( handle) ,
523
- timeout : rx,
524
- }
525
- } ) . boxed ( )
520
+ async move {
521
+ let res = self . await ;
522
+ crate :: util:: idle_ms ( ms) . await ;
523
+ res
524
+ } . boxed ( )
526
525
}
527
526
528
527
fn buffer_bytes ( self , num : usize ) -> Pin < Box < dyn Future < Output = Handle > + Send > > {
@@ -586,25 +585,6 @@ pub trait HandleFutureExt: Future<Output = Handle> + Send + Sized + 'static {
586
585
}
587
586
}
588
587
589
- pub struct Idle {
590
- handle : Option < Handle > ,
591
- timeout : oneshot:: Receiver < ( ) > ,
592
- }
593
-
594
- impl Future for Idle {
595
- type Output = Handle ;
596
-
597
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < Self :: Output > {
598
- if self . timeout . poll_unpin ( cx) . is_ready ( ) {
599
- return Poll :: Ready ( self . handle . take ( ) . unwrap ( ) ) ;
600
- }
601
-
602
- self . handle . as_mut ( ) . unwrap ( ) . poll_next_unpin ( cx) . map ( |res| {
603
- panic ! ( "Idle received unexpected frame on handle; frame={:?}" , res) ;
604
- } )
605
- }
606
- }
607
-
608
588
impl < T > HandleFutureExt for T
609
589
where
610
590
T : Future < Output = Handle > + Send + ' static ,
0 commit comments