+40
-12
@@ -40,6 +40,7 @@ pub struct DSP408 {
|
|||||||
state: types::DSPState,
|
state: types::DSPState,
|
||||||
input_meters: types::InputMeters,
|
input_meters: types::InputMeters,
|
||||||
output_meters: types::OutputMeters,
|
output_meters: types::OutputMeters,
|
||||||
|
auto_reconnect: bool,
|
||||||
reconnect_count: u32,
|
reconnect_count: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,6 +78,7 @@ impl DSP408 {
|
|||||||
input_meters: default_input_meters(),
|
input_meters: default_input_meters(),
|
||||||
output_meters: default_output_meters(),
|
output_meters: default_output_meters(),
|
||||||
reconnect_count: 0,
|
reconnect_count: 0,
|
||||||
|
auto_reconnect: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,6 +99,7 @@ impl DSP408 {
|
|||||||
input_meters: default_input_meters(),
|
input_meters: default_input_meters(),
|
||||||
output_meters: default_output_meters(),
|
output_meters: default_output_meters(),
|
||||||
reconnect_count: 0,
|
reconnect_count: 0,
|
||||||
|
auto_reconnect: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +129,9 @@ impl DSP408 {
|
|||||||
Ok(buffer)
|
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)),
|
Err(e) => Err(DSPError::Io(e)),
|
||||||
}
|
}
|
||||||
@@ -154,11 +159,12 @@ impl DSP408 {
|
|||||||
&& self.mode == types::DSPMode::Normal
|
&& self.mode == types::DSPMode::Normal
|
||||||
&& self.state.connection == types::ConnectionStatus::Connected =>
|
&& 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.connect()?;
|
||||||
|
|
||||||
self.reconnect_count += 1;
|
self.reconnect_count += 1;
|
||||||
|
|
||||||
self.send_recv_once(command)
|
self.send_recv_once(command)
|
||||||
}
|
}
|
||||||
other => other,
|
other => other,
|
||||||
@@ -199,6 +205,10 @@ impl DSP408 {
|
|||||||
self.reconnect_count
|
self.reconnect_count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_auto_reconnect(&mut self, enabled: bool) {
|
||||||
|
self.auto_reconnect = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- Helpers ----------
|
// ---------- Helpers ----------
|
||||||
|
|
||||||
fn initialize_state(&mut self) -> Result<(), DSPError> {
|
fn initialize_state(&mut self) -> Result<(), DSPError> {
|
||||||
@@ -236,9 +246,7 @@ impl DSP408 {
|
|||||||
(value / from_max) * to_max
|
(value / from_max) * to_max
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- Commands ----------
|
fn connect_inner(&mut self) -> Result<(), DSPError> {
|
||||||
// Command `0x10`
|
|
||||||
pub fn connect(&mut self) -> Result<(), DSPError> {
|
|
||||||
if self.mode == types::DSPMode::Demo {
|
if self.mode == types::DSPMode::Demo {
|
||||||
self.state.connection = types::ConnectionStatus::Connected;
|
self.state.connection = types::ConnectionStatus::Connected;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -265,6 +273,7 @@ impl DSP408 {
|
|||||||
let stream = TcpStream::connect_timeout(&addr, SOCKET_TIMEOUT)?;
|
let stream = TcpStream::connect_timeout(&addr, SOCKET_TIMEOUT)?;
|
||||||
|
|
||||||
stream.set_read_timeout(Some(SOCKET_TIMEOUT))?;
|
stream.set_read_timeout(Some(SOCKET_TIMEOUT))?;
|
||||||
|
stream.set_write_timeout(Some(SOCKET_TIMEOUT))?;
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut socket = self
|
let mut socket = self
|
||||||
@@ -327,6 +336,21 @@ impl DSP408 {
|
|||||||
Err(DSPError::HandshakeFailed)
|
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`
|
// Command `0x11`
|
||||||
pub fn disconnect(&mut self) -> Result<(), DSPError> {
|
pub fn disconnect(&mut self) -> Result<(), DSPError> {
|
||||||
if self.mode == types::DSPMode::Demo {
|
if self.mode == types::DSPMode::Demo {
|
||||||
@@ -334,6 +358,7 @@ impl DSP408 {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let was_connected = self.state.connection == types::ConnectionStatus::Connected;
|
||||||
let key = (self.connection.host, self.connection.device_id);
|
let key = (self.connection.host, self.connection.device_id);
|
||||||
|
|
||||||
let mut socket_guard = self
|
let mut socket_guard = self
|
||||||
@@ -345,14 +370,17 @@ impl DSP408 {
|
|||||||
let _ = socket.write_all(&commands::build_disconnect());
|
let _ = socket.write_all(&commands::build_disconnect());
|
||||||
let _ = socket.shutdown(std::net::Shutdown::Both);
|
let _ = socket.shutdown(std::net::Shutdown::Both);
|
||||||
}
|
}
|
||||||
|
drop(socket_guard);
|
||||||
|
|
||||||
instances()
|
// only unregister if THIS instance owned the registry entry
|
||||||
.lock()
|
if was_connected {
|
||||||
.map_err(|e| DSPError::MutexPoisoned(e.to_string()))?
|
instances()
|
||||||
.remove(&key);
|
.lock()
|
||||||
|
.map_err(|e| DSPError::MutexPoisoned(e.to_string()))?
|
||||||
|
.remove(&key);
|
||||||
|
}
|
||||||
|
|
||||||
self.state.connection = types::ConnectionStatus::Disconnected;
|
self.state.connection = types::ConnectionStatus::Disconnected;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user