Skip to content

Commit 5a60ffd

Browse files
Fix reconnect bug on bad connection, add bad conn popup[CPP-520] (#310)
Add a popup to connection dialog if unable to connect to deviceCPP-520, popup has a 7000ms delay to debounce. Fix an issue where process messages loop would close normally without error and valid reconnect would not fire. Made an attempt at syncing backend and frontend interpretation of connection state. Adds functionality to catch "Carriage Return" when viewing the connection dialog and trigger Connect/Disconnect.
1 parent dbf4048 commit 5a60ffd

File tree

10 files changed

+218
-23
lines changed

10 files changed

+218
-23
lines changed

console_backend/src/common_constants.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ pub enum Keys {
287287
RECORDING_FILENAME,
288288
#[strum(serialize = "CONSOLE_VERSION")]
289289
CONSOLE_VERSION,
290+
#[strum(serialize = "CONNECTION_MESSAGE")]
291+
CONNECTION_MESSAGE,
290292
}
291293

292294
#[derive(Clone, Debug, Display, EnumString, EnumVariantNames, Eq, Hash, PartialEq)]
@@ -297,6 +299,8 @@ pub enum ConnectionState {
297299
CONNECTED,
298300
#[strum(serialize = "DISCONNECTED")]
299301
DISCONNECTED,
302+
#[strum(serialize = "CONNECTING")]
303+
CONNECTING,
300304
}
301305

302306
#[derive(Clone, Debug, Display, EnumString, EnumVariantNames, Eq, Hash, PartialEq)]

console_backend/src/connection.rs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::process_messages::{process_messages, Messages};
1919
use crate::shared_state::{ConnectionState, SharedState};
2020
use crate::status_bar::StatusBar;
2121
use crate::types::*;
22-
use crate::utils::{refresh_connection_frontend, refresh_loggingbar};
22+
use crate::utils::{refresh_connection_frontend, refresh_loggingbar, send_conn_notification};
2323
use 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
}

console_backend/src/shared_state.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,9 @@ pub enum ConnectionState {
844844
conn: Connection,
845845
stop_token: StopToken,
846846
},
847+
848+
/// Attempting to connect
849+
Connecting,
847850
}
848851

849852
impl ConnectionState {
@@ -859,10 +862,15 @@ impl ConnectionState {
859862
matches!(self, Self::Connected { .. })
860863
}
861864

865+
pub fn is_connecting(&self) -> bool {
866+
matches!(self, Self::Connecting)
867+
}
868+
862869
pub fn name(&self) -> String {
863870
match self {
864871
ConnectionState::Closed => "closed".into(),
865872
ConnectionState::Disconnected => "disconnected".into(),
873+
ConnectionState::Connecting => "connecting".into(),
866874
ConnectionState::Connected { conn, .. } => conn.name(),
867875
}
868876
}
@@ -873,6 +881,7 @@ impl std::fmt::Display for ConnectionState {
873881
match self {
874882
ConnectionState::Closed => write!(f, "{}", cc::ConnectionState::CLOSED),
875883
ConnectionState::Disconnected => write!(f, "{}", cc::ConnectionState::DISCONNECTED),
884+
ConnectionState::Connecting => write!(f, "{}", cc::ConnectionState::CONNECTING),
876885
ConnectionState::Connected { .. } => {
877886
write!(f, "{}", cc::ConnectionState::CONNECTED)
878887
}

console_backend/src/utils.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ pub fn fixed_sbp_string<T, const L: usize>(data: &str) -> SbpString<[u8; L], T>
2323
SbpString::new(arr)
2424
}
2525

26+
/// Notify the frontend of a Connection notification.
27+
pub fn send_conn_notification(client_sender: &BoxedClientSender, message: String) {
28+
let mut builder = Builder::new_default();
29+
let msg = builder.init_root::<crate::console_backend_capnp::message::Builder>();
30+
let mut status = msg.init_connection_notification();
31+
status.set_message(&message);
32+
client_sender.send_data(serialize_capnproto_builder(builder));
33+
}
34+
2635
/// Notify the frontend of an [ConnectionState] change.
2736
pub fn send_conn_state(app_state: ConnectionState, client_sender: &BoxedClientSender) {
2837
let mut builder = Builder::new_default();

0 commit comments

Comments
 (0)