Skip to main content

ReconnectingSession

Struct ReconnectingSession 

Source
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::{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| Some(vec![laser_dac::LaserPoint::blanked(0.0, 0.0); req.n_points]),
    |err| eprintln!("Stream error: {}", err),
)?;

Implementations§

Source§

impl ReconnectingSession

Source

pub fn new(device_id: impl Into<String>, config: StreamConfig) -> Self

Create a new reconnecting session for a device ID.

Source

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.

Source

pub fn with_backoff(self, backoff: Duration) -> Self

Set a fixed backoff duration between reconnect attempts.

Source

pub fn on_disconnect<F>(self, f: F) -> Self
where F: FnMut(&Error) + Send + 'static,

Register a callback invoked when a disconnect is detected.

Source

pub fn on_reconnect<F>(self, f: F) -> Self
where F: FnMut(&DacInfo) + Send + 'static,

Register a callback invoked after a successful reconnect.

Source

pub fn with_discovery<F>(self, factory: F) -> Self
where F: Fn() -> DacDiscovery + Send + 'static,

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
    });
Source

pub fn control(&self) -> SessionControl

Returns a control handle for arm/disarm/stop.

Source

pub fn run<F, E>(&mut self, producer: F, on_error: E) -> Result<RunExit>
where F: FnMut(ChunkRequest) -> Option<Vec<LaserPoint>> + Send + 'static, E: FnMut(Error) + Send + 'static,

Run the stream, automatically reconnecting on disconnection.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.