@@ -4,6 +4,7 @@ use futures::StreamExt;
44use tokio:: io:: AsyncRead ;
55use tokio:: io:: AsyncWrite ;
66use tokio:: sync:: mpsc;
7+ use tokio:: sync:: watch;
78use tokio_tungstenite:: WebSocketStream ;
89use tokio_tungstenite:: tungstenite:: Message ;
910
@@ -28,6 +29,7 @@ pub(crate) enum JsonRpcConnectionEvent {
2829pub ( crate ) struct JsonRpcConnection {
2930 outgoing_tx : mpsc:: Sender < JSONRPCMessage > ,
3031 incoming_rx : mpsc:: Receiver < JsonRpcConnectionEvent > ,
32+ disconnected_rx : watch:: Receiver < bool > ,
3133 task_handles : Vec < tokio:: task:: JoinHandle < ( ) > > ,
3234}
3335
@@ -40,9 +42,11 @@ impl JsonRpcConnection {
4042 {
4143 let ( outgoing_tx, mut outgoing_rx) = mpsc:: channel ( CHANNEL_CAPACITY ) ;
4244 let ( incoming_tx, incoming_rx) = mpsc:: channel ( CHANNEL_CAPACITY ) ;
45+ let ( disconnected_tx, disconnected_rx) = watch:: channel ( false ) ;
4346
4447 let reader_label = connection_label. clone ( ) ;
4548 let incoming_tx_for_reader = incoming_tx. clone ( ) ;
49+ let disconnected_tx_for_reader = disconnected_tx. clone ( ) ;
4650 let reader_task = tokio:: spawn ( async move {
4751 let mut lines = BufReader :: new ( reader) . lines ( ) ;
4852 loop {
@@ -73,12 +77,18 @@ impl JsonRpcConnection {
7377 }
7478 }
7579 Ok ( None ) => {
76- send_disconnected ( & incoming_tx_for_reader, /*reason*/ None ) . await ;
80+ send_disconnected (
81+ & incoming_tx_for_reader,
82+ & disconnected_tx_for_reader,
83+ /*reason*/ None ,
84+ )
85+ . await ;
7786 break ;
7887 }
7988 Err ( err) => {
8089 send_disconnected (
8190 & incoming_tx_for_reader,
91+ & disconnected_tx_for_reader,
8292 Some ( format ! (
8393 "failed to read JSON-RPC message from {reader_label}: {err}"
8494 ) ) ,
@@ -96,6 +106,7 @@ impl JsonRpcConnection {
96106 if let Err ( err) = write_jsonrpc_line_message ( & mut writer, & message) . await {
97107 send_disconnected (
98108 & incoming_tx,
109+ & disconnected_tx,
99110 Some ( format ! (
100111 "failed to write JSON-RPC message to {connection_label}: {err}"
101112 ) ) ,
@@ -109,6 +120,7 @@ impl JsonRpcConnection {
109120 Self {
110121 outgoing_tx,
111122 incoming_rx,
123+ disconnected_rx,
112124 task_handles : vec ! [ reader_task, writer_task] ,
113125 }
114126 }
@@ -119,10 +131,12 @@ impl JsonRpcConnection {
119131 {
120132 let ( outgoing_tx, mut outgoing_rx) = mpsc:: channel ( CHANNEL_CAPACITY ) ;
121133 let ( incoming_tx, incoming_rx) = mpsc:: channel ( CHANNEL_CAPACITY ) ;
134+ let ( disconnected_tx, disconnected_rx) = watch:: channel ( false ) ;
122135 let ( mut websocket_writer, mut websocket_reader) = stream. split ( ) ;
123136
124137 let reader_label = connection_label. clone ( ) ;
125138 let incoming_tx_for_reader = incoming_tx. clone ( ) ;
139+ let disconnected_tx_for_reader = disconnected_tx. clone ( ) ;
126140 let reader_task = tokio:: spawn ( async move {
127141 loop {
128142 match websocket_reader. next ( ) . await {
@@ -171,14 +185,20 @@ impl JsonRpcConnection {
171185 }
172186 }
173187 Some ( Ok ( Message :: Close ( _) ) ) => {
174- send_disconnected ( & incoming_tx_for_reader, /*reason*/ None ) . await ;
188+ send_disconnected (
189+ & incoming_tx_for_reader,
190+ & disconnected_tx_for_reader,
191+ /*reason*/ None ,
192+ )
193+ . await ;
175194 break ;
176195 }
177196 Some ( Ok ( Message :: Ping ( _) ) ) | Some ( Ok ( Message :: Pong ( _) ) ) => { }
178197 Some ( Ok ( _) ) => { }
179198 Some ( Err ( err) ) => {
180199 send_disconnected (
181200 & incoming_tx_for_reader,
201+ & disconnected_tx_for_reader,
182202 Some ( format ! (
183203 "failed to read websocket JSON-RPC message from {reader_label}: {err}"
184204 ) ) ,
@@ -187,7 +207,12 @@ impl JsonRpcConnection {
187207 break ;
188208 }
189209 None => {
190- send_disconnected ( & incoming_tx_for_reader, /*reason*/ None ) . await ;
210+ send_disconnected (
211+ & incoming_tx_for_reader,
212+ & disconnected_tx_for_reader,
213+ /*reason*/ None ,
214+ )
215+ . await ;
191216 break ;
192217 }
193218 }
@@ -202,6 +227,7 @@ impl JsonRpcConnection {
202227 {
203228 send_disconnected (
204229 & incoming_tx,
230+ & disconnected_tx,
205231 Some ( format ! (
206232 "failed to write websocket JSON-RPC message to {connection_label}: {err}"
207233 ) ) ,
@@ -213,6 +239,7 @@ impl JsonRpcConnection {
213239 Err ( err) => {
214240 send_disconnected (
215241 & incoming_tx,
242+ & disconnected_tx,
216243 Some ( format ! (
217244 "failed to serialize JSON-RPC message for {connection_label}: {err}"
218245 ) ) ,
@@ -227,6 +254,7 @@ impl JsonRpcConnection {
227254 Self {
228255 outgoing_tx,
229256 incoming_rx,
257+ disconnected_rx,
230258 task_handles : vec ! [ reader_task, writer_task] ,
231259 }
232260 }
@@ -236,16 +264,24 @@ impl JsonRpcConnection {
236264 ) -> (
237265 mpsc:: Sender < JSONRPCMessage > ,
238266 mpsc:: Receiver < JsonRpcConnectionEvent > ,
267+ watch:: Receiver < bool > ,
239268 Vec < tokio:: task:: JoinHandle < ( ) > > ,
240269 ) {
241- ( self . outgoing_tx , self . incoming_rx , self . task_handles )
270+ (
271+ self . outgoing_tx ,
272+ self . incoming_rx ,
273+ self . disconnected_rx ,
274+ self . task_handles ,
275+ )
242276 }
243277}
244278
245279async fn send_disconnected (
246280 incoming_tx : & mpsc:: Sender < JsonRpcConnectionEvent > ,
281+ disconnected_tx : & watch:: Sender < bool > ,
247282 reason : Option < String > ,
248283) {
284+ let _ = disconnected_tx. send ( true ) ;
249285 let _ = incoming_tx
250286 . send ( JsonRpcConnectionEvent :: Disconnected { reason } )
251287 . await ;
0 commit comments