fix: reconnection issues
CI / Rust CI (push) Failing after 1m5s

This commit is contained in:
Lucas
2026-11-04 11:09:11 +01:00
parent a01e4dca96
commit f030d98f1c
+40 -12
View File
@@ -40,6 +40,7 @@ pub struct DSP408 {
state: types::DSPState,
input_meters: types::InputMeters,
output_meters: types::OutputMeters,
auto_reconnect: bool,
reconnect_count: u32,
}
@@ -77,6 +78,7 @@ impl DSP408 {
input_meters: default_input_meters(),
output_meters: default_output_meters(),
reconnect_count: 0,
auto_reconnect: true,
})
}
@@ -97,6 +99,7 @@ impl DSP408 {
input_meters: default_input_meters(),
output_meters: default_output_meters(),
reconnect_count: 0,
auto_reconnect: true,
}
}
@@ -126,7 +129,9 @@ impl DSP408 {
Ok(buffer)
}
Err(e) if e.kind() == std::io::ErrorKind::TimedOut => Err(DSPError::Timeout),
Err(e) if matches!(e.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock) => {
Err(DSPError::Timeout)
}
Err(e) => Err(DSPError::Io(e)),
}
@@ -154,11 +159,12 @@ impl DSP408 {
&& self.mode == types::DSPMode::Normal
&& self.state.connection == types::ConnectionStatus::Connected =>
{
let _ = self.disconnect();
let _ = self.disconnect(); // state -> Disconnected, registry cleaned
if !self.auto_reconnect {
return Err(err); // no silent reconnect
}
self.connect()?;
self.reconnect_count += 1;
self.send_recv_once(command)
}
other => other,
@@ -199,6 +205,10 @@ impl DSP408 {
self.reconnect_count
}
pub fn set_auto_reconnect(&mut self, enabled: bool) {
self.auto_reconnect = enabled;
}
// ---------- Helpers ----------
fn initialize_state(&mut self) -> Result<(), DSPError> {
@@ -236,9 +246,7 @@ impl DSP408 {
(value / from_max) * to_max
}
// ---------- Commands ----------
// Command `0x10`
pub fn connect(&mut self) -> Result<(), DSPError> {
fn connect_inner(&mut self) -> Result<(), DSPError> {
if self.mode == types::DSPMode::Demo {
self.state.connection = types::ConnectionStatus::Connected;
return Ok(());
@@ -265,6 +273,7 @@ impl DSP408 {
let stream = TcpStream::connect_timeout(&addr, SOCKET_TIMEOUT)?;
stream.set_read_timeout(Some(SOCKET_TIMEOUT))?;
stream.set_write_timeout(Some(SOCKET_TIMEOUT))?;
{
let mut socket = self
@@ -327,6 +336,21 @@ impl DSP408 {
Err(DSPError::HandshakeFailed)
}
// ---------- Commands ----------
// Command `0x10`
pub fn connect(&mut self) -> Result<(), DSPError> {
let result = self.connect_inner();
// Any failure other than "already connected" may have left a half-open
// socket behind. AlreadyConnected is skipped because it means this
// instance (or another one) legitimately owns the connection.
if matches!(&result, Err(e) if !matches!(e, DSPError::AlreadyConnected)) {
let _ = self.disconnect();
}
result
}
// Command `0x11`
pub fn disconnect(&mut self) -> Result<(), DSPError> {
if self.mode == types::DSPMode::Demo {
@@ -334,6 +358,7 @@ impl DSP408 {
return Ok(());
}
let was_connected = self.state.connection == types::ConnectionStatus::Connected;
let key = (self.connection.host, self.connection.device_id);
let mut socket_guard = self
@@ -345,14 +370,17 @@ impl DSP408 {
let _ = socket.write_all(&commands::build_disconnect());
let _ = socket.shutdown(std::net::Shutdown::Both);
}
drop(socket_guard);
instances()
.lock()
.map_err(|e| DSPError::MutexPoisoned(e.to_string()))?
.remove(&key);
// only unregister if THIS instance owned the registry entry
if was_connected {
instances()
.lock()
.map_err(|e| DSPError::MutexPoisoned(e.to_string()))?
.remove(&key);
}
self.state.connection = types::ConnectionStatus::Disconnected;
Ok(())
}