@@ -22,6 +22,7 @@ use crate::Result;
22
22
pub struct Client {
23
23
closed : Arc < Notify > ,
24
24
socket : DuplexSocket ,
25
+ closing : mpsc:: Sender < ( ) > ,
25
26
}
26
27
27
28
pub struct ClientBuilder < T , C > {
@@ -171,28 +172,41 @@ where
171
172
172
173
// begin read loop
173
174
let closer = self . closer . take ( ) ;
174
- let closed = Arc :: new ( Notify :: new ( ) ) ;
175
- let closed_clone = closed. clone ( ) ;
175
+ let close_notify = Arc :: new ( Notify :: new ( ) ) ;
176
+ let close_notify_clone = close_notify. clone ( ) ;
177
+ let ( closing, mut closing_rx) = mpsc:: channel :: < ( ) > ( 1 ) ;
176
178
177
179
let ( read_tx, mut read_rx) = mpsc:: unbounded_channel :: < Frame > ( ) ;
178
180
181
+ // read frames from stream, then writes into channel
179
182
runtime:: spawn ( async move {
180
- while let Some ( next) = stream. next ( ) . await {
181
- match next {
182
- Ok ( frame) => {
183
- if let Err ( e) = read_tx. send ( frame) {
184
- error ! ( "read next frame failed: {}" , e) ;
185
- break ;
183
+ loop {
184
+ tokio:: select! {
185
+ res = stream. next( ) => {
186
+ match res {
187
+ Some ( next) => match next {
188
+ Ok ( frame) => {
189
+ if let Err ( e) = read_tx. send( frame) {
190
+ error!( "forward frame failed: {}" , e) ;
191
+ break ;
192
+ }
193
+ }
194
+ Err ( e) => {
195
+ error!( "read frame failed: {}" , e) ;
196
+ break ;
197
+ }
198
+ }
199
+ None => break ,
186
200
}
187
201
}
188
- Err ( e) => {
189
- error ! ( "read next frame failed: {}" , e) ;
190
- break ;
202
+ _ = closing_rx. recv( ) => {
203
+ break
191
204
}
192
205
}
193
206
}
194
207
} ) ;
195
208
209
+ // process frames
196
210
runtime:: spawn ( async move {
197
211
while let Some ( next) = read_rx. recv ( ) . await {
198
212
if let Err ( e) = cloned_socket. dispatch ( next, None ) . await {
@@ -205,12 +219,12 @@ where
205
219
let close_frame = frame:: Error :: builder ( 0 , 0 )
206
220
. set_code ( ERR_CONN_CLOSED )
207
221
. build ( ) ;
208
- if let Err ( _ ) = cloned_snd_tx. send ( close_frame) {
209
- debug ! ( "send close notify frame failed!" ) ;
222
+ if let Err ( e ) = cloned_snd_tx. send ( close_frame) {
223
+ debug ! ( "send close notify frame failed: {}" , e ) ;
210
224
}
211
225
212
226
// notify client closed
213
- closed_clone . notify_one ( ) ;
227
+ close_notify_clone . notify_one ( ) ;
214
228
215
229
// invoke on_close handler
216
230
if let Some ( mut invoke) = closer {
@@ -219,13 +233,18 @@ where
219
233
} ) ;
220
234
221
235
socket. setup ( setup) . await ;
222
- Ok ( Client :: new ( socket, closed) )
236
+
237
+ Ok ( Client :: new ( socket, close_notify, closing) )
223
238
}
224
239
}
225
240
226
241
impl Client {
227
- fn new ( socket : DuplexSocket , closed : Arc < Notify > ) -> Client {
228
- Client { socket, closed }
242
+ fn new ( socket : DuplexSocket , closed : Arc < Notify > , closing : mpsc:: Sender < ( ) > ) -> Client {
243
+ Client {
244
+ socket,
245
+ closed,
246
+ closing,
247
+ }
229
248
}
230
249
231
250
pub async fn wait_for_close ( self ) {
0 commit comments