@@ -22,6 +22,19 @@ impl WebSocket {
22
22
}
23
23
}
24
24
25
+ macro_rules! try_in_poll_io {
26
+ ( $expr: expr) => { {
27
+ match $expr {
28
+ Ok ( o) => o,
29
+ // WebSocket is closed, nothing more to read or write
30
+ Err ( WebSocketError :: ConnectionClose ( event) ) if event. was_clean => {
31
+ return Poll :: Ready ( Ok ( 0 ) ) ;
32
+ }
33
+ Err ( e) => return Poll :: Ready ( Err ( io:: Error :: new( io:: ErrorKind :: Other , e) ) ) ,
34
+ }
35
+ } } ;
36
+ }
37
+
25
38
#[ cfg_attr( docsrs, doc( cfg( feature = "io-util" ) ) ) ]
26
39
impl AsyncRead for WebSocket {
27
40
fn poll_read (
@@ -33,14 +46,10 @@ impl AsyncRead for WebSocket {
33
46
data
34
47
} else {
35
48
match ready ! ( self . as_mut( ) . poll_next( cx) ) {
36
- Some ( Ok ( m ) ) => match m {
49
+ Some ( item ) => match try_in_poll_io ! ( item ) {
37
50
WebSocketMessage :: Text ( s) => s. into_bytes ( ) ,
38
51
WebSocketMessage :: Bytes ( data) => data,
39
52
} ,
40
- Some ( Err ( WebSocketError :: ConnectionClose ( event) ) ) if event. was_clean == true => {
41
- return Poll :: Ready ( Ok ( 0 ) ) ;
42
- }
43
- Some ( Err ( e) ) => return Poll :: Ready ( Err ( io:: Error :: new ( io:: ErrorKind :: Other , e) ) ) ,
44
53
None => return Poll :: Ready ( Ok ( 0 ) ) ,
45
54
}
46
55
} ;
@@ -64,26 +73,14 @@ impl AsyncWrite for WebSocket {
64
73
cx : & mut Context < ' _ > ,
65
74
buf : & [ u8 ] ,
66
75
) -> Poll < io:: Result < usize > > {
67
- macro_rules! try_in_poll {
68
- ( $expr: expr) => { {
69
- match $expr {
70
- Ok ( o) => o,
71
- // When using `AsyncWriteExt::write_all`, `io::ErrorKind::WriteZero` will be raised.
72
- // In this case it means "attempted to write on a closed socket".
73
- Err ( WebSocketError :: ConnectionClose ( _) ) => return Poll :: Ready ( Ok ( 0 ) ) ,
74
- Err ( e) => return Poll :: Ready ( Err ( io:: Error :: new( io:: ErrorKind :: Other , e) ) ) ,
75
- }
76
- } } ;
77
- }
78
-
79
76
// try flushing preemptively
80
77
let _ = AsyncWrite :: poll_flush ( self . as_mut ( ) , cx) ;
81
78
82
79
// make sure sink is ready to send
83
- try_in_poll ! ( ready!( self . as_mut( ) . poll_ready( cx) ) ) ;
80
+ try_in_poll_io ! ( ready!( self . as_mut( ) . poll_ready( cx) ) ) ;
84
81
85
82
// actually submit new item
86
- try_in_poll ! ( self . start_send( WebSocketMessage :: Bytes ( buf. to_vec( ) ) ) ) ;
83
+ try_in_poll_io ! ( self . start_send( WebSocketMessage :: Bytes ( buf. to_vec( ) ) ) ) ;
87
84
// ^ if no error occurred, message is accepted and queued when calling `start_send`
88
85
// (i.e.: `to_vec` is called only once)
89
86
0 commit comments