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
impl ShmChannel
Sourcepub fn listen(
name: &str,
config: PubSubConfig,
timeout: Duration,
) -> Result<Self, IpcError>
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.
Sourcepub fn connect(
name: &str,
config: PubSubConfig,
timeout: Duration,
) -> Result<Self, IpcError>
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.
Sourcepub fn send(&mut self, data: &[u8]) -> Result<(), IpcError>
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.
Sourcepub fn loan(&mut self) -> Result<ShmLoan<'_>, IpcError>
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.
Sourcepub fn try_recv(&self) -> Option<SampleGuard<'_>>
pub fn try_recv(&self) -> Option<SampleGuard<'_>>
Non-blocking receive. Returns None if no new message.
Sourcepub fn recv(&self) -> Result<SampleGuard<'_>, IpcError>
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.
Sourcepub fn recv_with(
&self,
strategy: WaitStrategy,
) -> Result<SampleGuard<'_>, IpcError>
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.