@@ -19,7 +19,7 @@ use crate::process_messages::{process_messages, Messages};
1919use crate :: shared_state:: { ConnectionState , SharedState } ;
2020use crate :: status_bar:: StatusBar ;
2121use crate :: types:: * ;
22- use crate :: utils:: { refresh_connection_frontend, refresh_loggingbar} ;
22+ use crate :: utils:: { refresh_connection_frontend, refresh_loggingbar, send_conn_notification } ;
2323use crate :: watch:: Watched ;
2424
2525#[ derive( Debug ) ]
@@ -115,13 +115,36 @@ fn conn_manager_thd(
115115 while let Ok ( msg) = recv. wait ( ) {
116116 match msg {
117117 ConnectionManagerMsg :: Connect ( conn) => {
118+ shared_state. set_connection ( ConnectionState :: Connecting , & client_sender) ;
118119 let ( reader, writer) = match conn. try_connect ( Some ( & shared_state) ) {
119120 Ok ( rw) => rw,
120121 Err ( e) => {
121- error ! ( "Unable to connect: {}" , e) ;
122+ let ( reconnect, message) = match e. kind ( ) {
123+ ErrorKind :: ConnectionRefused => {
124+ ( true , String :: from ( "Connection refused. Reconnecting..." ) )
125+ }
126+ ErrorKind :: ConnectionReset => {
127+ ( true , String :: from ( "Connection reset. Reconnecting..." ) )
128+ }
129+ ErrorKind :: TimedOut => {
130+ ( true , String :: from ( "Connection timed out. Reconnecting..." ) )
131+ }
132+ ErrorKind :: NotConnected => {
133+ ( true , String :: from ( "Not connected. Reconnecting..." ) )
134+ }
135+ _ => ( false , format ! ( "Unable to connect: {}" , e) ) ,
136+ } ;
137+ error ! ( "{}" , message) ;
122138 log:: logger ( ) . flush ( ) ;
123- if conn. is_tcp ( ) {
124- manager_msg. send ( ConnectionManagerMsg :: Reconnect ( conn) )
139+ send_conn_notification ( & client_sender, message. clone ( ) ) ;
140+ if !conn. is_file ( ) {
141+ if reconnect {
142+ manager_msg. send ( ConnectionManagerMsg :: Reconnect ( conn) )
143+ } else {
144+ manager_msg. send ( ConnectionManagerMsg :: Disconnect )
145+ }
146+ } else {
147+ manager_msg. send ( ConnectionManagerMsg :: Disconnect )
125148 }
126149 continue ;
127150 }
@@ -151,6 +174,7 @@ fn conn_manager_thd(
151174 }
152175 ConnectionManagerMsg :: Reconnect ( conn) => {
153176 join ( & mut reconnect_thd) ;
177+ shared_state. set_connection ( ConnectionState :: Connecting , & client_sender) ;
154178 reconnect_thd = Some ( start_reconnect_thd ( conn, manager_msg. clone ( ) ) ) ;
155179 }
156180 ConnectionManagerMsg :: Disconnect => {
@@ -161,6 +185,7 @@ fn conn_manager_thd(
161185 }
162186 refresh_loggingbar ( & client_sender, & shared_state) ;
163187 shared_state. set_connection ( ConnectionState :: Disconnected , & client_sender) ;
188+ refresh_connection_frontend ( & client_sender, & shared_state) ;
164189 join ( & mut pm_thd) ;
165190 info ! ( "Disconnected successfully." ) ;
166191 }
@@ -203,16 +228,20 @@ fn process_messages_thd(
203228 messages,
204229 msg_sender,
205230 conn. clone ( ) ,
206- shared_state,
231+ shared_state. clone ( ) ,
207232 client_sender,
208233 ) ;
209- if conn. close_when_done ( ) {
210- manager_msg. close ( ) ;
211- }
212- if let Err ( e) = res {
213- error ! ( "Connection error: {}" , e) ;
214- if conn. is_tcp ( ) {
215- manager_msg. send ( ConnectionManagerMsg :: Reconnect ( conn) )
234+ if conn. is_file ( ) {
235+ manager_msg. send ( ConnectionManagerMsg :: Disconnect ) ;
236+ if conn. close_when_done ( ) {
237+ manager_msg. close ( ) ;
238+ }
239+ } else {
240+ if let Err ( e) = res {
241+ error ! ( "Connection error: {}" , e) ;
242+ }
243+ if !matches ! ( shared_state. connection( ) , ConnectionState :: Disconnected ) {
244+ manager_msg. send ( ConnectionManagerMsg :: Reconnect ( conn) ) ;
216245 }
217246 }
218247 } )
@@ -269,6 +298,10 @@ impl Connection {
269298 matches ! ( self , Connection :: Tcp ( _) | Connection :: Serial ( _) )
270299 }
271300
301+ pub fn is_file ( & self ) -> bool {
302+ matches ! ( self , Connection :: File ( _) )
303+ }
304+
272305 pub fn is_serial ( & self ) -> bool {
273306 matches ! ( self , Connection :: Serial ( _) )
274307 }
0 commit comments