feat: try reconnecting once if dsp close the tcp connection
CI / Rust CI (push) Successful in 3m44s

This commit is contained in:
Lucas
2026-10-15 16:51:00 +02:00
parent 0339f99cc5
commit 066d3806eb
3 changed files with 45 additions and 9 deletions
Generated
+2 -2
View File
@@ -583,9 +583,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.151"
version = "1.0.154"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
dependencies = [
"itoa",
"memchr",
+1 -1
View File
@@ -11,7 +11,7 @@ thiserror = "2.0.21"
strum = "0.28.0"
strum_macros = "0.28.0"
serde = { version = "1", features = ["derive"], optional = true }
serde_json = { version = "1", optional = true }
serde_json = { version = "1.0.154", optional = true }
serde_with = "3.24.0"
rand = "0.10.3"
+42 -6
View File
@@ -40,9 +40,12 @@ pub struct DSP408 {
state: types::DSPState,
input_meters: types::InputMeters,
output_meters: types::OutputMeters,
reconnect_count: u32,
}
impl DSP408 {
// ---------- Initializers ----------
pub fn new(host: IpAddr, port: u16, device_id: u8) -> Result<Self, DSPError> {
if matches!(host, IpAddr::V6(_)) {
return Err(DSPError::InvalidHost("DSP doesn't support IPv6"));
@@ -73,6 +76,7 @@ impl DSP408 {
input_meters: default_input_meters(),
output_meters: default_output_meters(),
reconnect_count: 0,
})
}
@@ -92,9 +96,19 @@ impl DSP408 {
input_meters: default_input_meters(),
output_meters: default_output_meters(),
reconnect_count: 0,
}
}
// ---------- Network Helpers ----------
fn is_connection_error(err: &DSPError) -> bool {
matches!(
err,
DSPError::ConnectionClosed | DSPError::Timeout | DSPError::Io(_)
)
}
fn send_raw(socket: &mut TcpStream, data: &[u8]) -> Result<(), DSPError> {
socket.write_all(data)?;
@@ -118,7 +132,7 @@ impl DSP408 {
}
}
pub fn send_recv(&self, command: &[u8]) -> Result<types::DeviceResponseType, DSPError> {
fn send_recv_once(&self, command: &[u8]) -> Result<types::DeviceResponseType, DSPError> {
let mut socket_guard = self
.socket
.lock()
@@ -127,16 +141,32 @@ impl DSP408 {
let socket = socket_guard.as_mut().ok_or(DSPError::NotConnected)?;
Self::send_raw(socket, command)?;
let raw = Self::recv_raw(socket)?;
let frame = extract_frame(&raw)?;
let response = map_response(&frame)?;
Ok(response)
Ok(map_response(&frame)?)
}
pub fn send_recv(&mut self, command: &[u8]) -> Result<types::DeviceResponseType, DSPError> {
match self.send_recv_once(command) {
Err(err)
if DSP408::is_connection_error(&err)
&& self.mode == types::DSPMode::Normal
&& self.state.connection == types::ConnectionStatus::Connected =>
{
let _ = self.disconnect();
self.connect()?;
self.reconnect_count += 1;
self.send_recv_once(command)
}
other => other,
}
}
// ---------- Getters ----------
pub fn mode(&self) -> types::DSPMode {
self.mode
}
@@ -165,6 +195,12 @@ impl DSP408 {
&mut self.state
}
pub fn reconnect_count(&self) -> u32 {
self.reconnect_count
}
// ---------- Helpers ----------
fn initialize_state(&mut self) -> Result<(), DSPError> {
let name = self.get_device_name()?;
let flags = self.get_device_flags()?;