pub struct ReconnectingSession { /* private fields */ }Expand description
A reconnecting wrapper around the streaming API.
This helper reconnects to a device by ID and restarts streaming automatically when a disconnection occurs.
By default this uses open_device() internally. To use custom DAC
backends, call with_discovery with a factory
function that creates a configured DacDiscovery.
§Example
use laser_dac::{ChunkRequest, ChunkResult, LaserPoint, ReconnectingSession, StreamConfig};
use std::time::Duration;
let mut session = ReconnectingSession::new("my-device", StreamConfig::new(30_000))
.with_max_retries(5)
.with_backoff(Duration::from_secs(1))
.on_disconnect(|err| eprintln!("Lost connection: {}", err))
.on_reconnect(|info| println!("Reconnected to {}", info.name));
session.control().arm()?;
session.run(
|req: &ChunkRequest, buffer: &mut [LaserPoint]| {
let n = req.target_points;
for i in 0..n {
buffer[i] = LaserPoint::blanked(0.0, 0.0);
}
ChunkResult::Filled(n)
},
|err| eprintln!("Stream error: {}", err),
)?;Implementations§
Source§impl ReconnectingSession
impl ReconnectingSession
Sourcepub fn new(device_id: impl Into<String>, config: StreamConfig) -> Self
pub fn new(device_id: impl Into<String>, config: StreamConfig) -> Self
Create a new reconnecting session for a device ID.
Sourcepub fn with_max_retries(self, max_retries: u32) -> Self
pub fn with_max_retries(self, max_retries: u32) -> Self
Set the maximum number of reconnect attempts.
None (default) retries forever. Some(0) disables retries.
Sourcepub fn with_backoff(self, backoff: Duration) -> Self
pub fn with_backoff(self, backoff: Duration) -> Self
Set a fixed backoff duration between reconnect attempts.
Sourcepub fn on_disconnect<F>(self, f: F) -> Self
pub fn on_disconnect<F>(self, f: F) -> Self
Register a callback invoked when a disconnect is detected.
Sourcepub fn on_reconnect<F>(self, f: F) -> Self
pub fn on_reconnect<F>(self, f: F) -> Self
Register a callback invoked after a successful reconnect.
Sourcepub fn with_discovery<F>(self, factory: F) -> Self
pub fn with_discovery<F>(self, factory: F) -> Self
Use a custom discovery factory for opening devices.
This allows using custom DAC backends by providing a factory function
that creates a DacDiscovery with external discoverers registered.
§Example
use laser_dac::{DacDiscovery, EnabledDacTypes, ReconnectingSession, StreamConfig};
let session = ReconnectingSession::new("custom:my-device", StreamConfig::new(30_000))
.with_discovery(|| {
let mut discovery = DacDiscovery::new(EnabledDacTypes::all());
// discovery.register(my_custom_discoverer);
discovery
});Sourcepub fn control(&self) -> SessionControl
pub fn control(&self) -> SessionControl
Returns a control handle for arm/disarm/stop.
Sourcepub fn run<F, E>(&mut self, producer: F, on_error: E) -> Result<RunExit>where
F: FnMut(&ChunkRequest, &mut [LaserPoint]) -> ChunkResult + Send + 'static,
E: FnMut(Error) + Send + 'static,
pub fn run<F, E>(&mut self, producer: F, on_error: E) -> Result<RunExit>where
F: FnMut(&ChunkRequest, &mut [LaserPoint]) -> ChunkResult + Send + 'static,
E: FnMut(Error) + Send + 'static,
Run the stream, automatically reconnecting on disconnection.
Uses the zero-allocation callback API with buffer-driven timing.