Skip to main content

ShmChannel

Struct ShmChannel 

Source
pub struct ShmChannel { /* private fields */ }
Expand description

Bidirectional shared-memory channel.

Composed of two pub/sub regions – one per direction. The server creates "{name}-srv" and subscribes to "{name}-cli"; the client does the reverse. Both endpoints have identical capabilities after construction.

§Examples

use crossbar::*;
use std::time::Duration;

// Process A (server -- start first)
let mut srv = ShmChannel::listen("rpc", PubSubConfig::default(),
    Duration::from_secs(30)).unwrap();

// Process B (client)
let mut cli = ShmChannel::connect("rpc", PubSubConfig::default(),
    Duration::from_secs(5)).unwrap();

cli.send(b"request").unwrap();
let msg = srv.recv().unwrap();
assert_eq!(&*msg, b"request");
drop(msg);

srv.send(b"response").unwrap();
let reply = cli.recv().unwrap();
assert_eq!(&*reply, b"response");

Implementations§

Source§

impl ShmChannel

Source

pub fn listen( name: &str, config: PubSubConfig, timeout: Duration, ) -> Result<Self, IpcError>

Creates the server side of a bidirectional channel.

Creates the "{name}-srv" region immediately, then waits up to timeout for a client to create "{name}-cli".

§Errors

Returns an error if the server region cannot be created or the client does not appear before timeout.

Source

pub fn connect( name: &str, config: PubSubConfig, timeout: Duration, ) -> Result<Self, IpcError>

Creates the client side of a bidirectional channel.

Creates the "{name}-cli" region immediately, then connects to the server’s "{name}-srv" region. Retries up to timeout if the server has not started yet.

§Errors

Returns an error if the client region cannot be created or the server region does not appear before timeout.

Source

pub fn send(&mut self, data: &[u8]) -> Result<(), IpcError>

Copies data into a pool block and sends it to the other endpoint.

§Errors

Returns IpcError::PoolExhausted if all blocks are in use, or IpcError::DataTooLarge if data exceeds block capacity.

Source

pub fn loan(&mut self) -> Result<ShmLoan<'_>, IpcError>

Returns a mutable loan for born-in-SHM writes.

Write directly into the loan, then call ShmLoan::publish.

§Errors

Returns IpcError::PoolExhausted if all blocks are in use.

Source

pub fn try_recv(&self) -> Option<SampleGuard<'_>>

Non-blocking receive. Returns None if no new message.

Source

pub fn recv(&self) -> Result<SampleGuard<'_>, IpcError>

Blocking receive with the default wait strategy.

§Errors

Returns IpcError::PublisherDead if the other endpoint’s heartbeat goes stale.

Source

pub fn recv_with( &self, strategy: WaitStrategy, ) -> Result<SampleGuard<'_>, IpcError>

Blocking receive with a custom WaitStrategy.

§Errors

Returns IpcError::PublisherDead if the other endpoint’s heartbeat goes stale.

Source

pub fn heartbeat(&mut self) -> Result<(), IpcError>

Updates the publisher heartbeat. Call during idle periods when not sending to prevent the other side from reporting publisher dead.

§Errors

Returns IpcError::ClockError if the system clock is before UNIX epoch.

Trait Implementations§

Source§

impl Debug for ShmChannel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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.