Struct zbus::azync::Connection [−][src]
pub struct Connection(_);
Expand description
The asynchronous sibling of zbus::Connection.
Most of the API is very similar to zbus::Connection, except it’s asynchronous. However,
there are a few differences:
Sending Messages
For sending messages you can either use Connection::send_message method or make use of the
futures_sink::Sink implementation that is returned by Connection::sink method. For
latter, you might find SinkExt API very useful. Keep in mind that Connection will not
manage the serial numbers (cookies) on the messages for you when they are sent through the
MessageSink. You can manually assign unique serial numbers to them using the
Connection::assign_serial_num method before sending them off, if needed. Having said that,
MessageSink is mainly useful for sending out signals, as they do not expect a reply, and
serial numbers are not very useful for signals either for the same reason.
Receiving Messages
Unlike zbus::Connection, there is no direct async equivalent of
zbus::Connection::receive_message method provided. This is because the futures crate
already provides a nice rich API that makes use of the stream::Stream implementation that is
returned by Connection::stream method.
Examples
Get the session bus ID
use zbus::azync::Connection; let mut connection = Connection::new_session().await?; let reply = connection .call_method( Some("org.freedesktop.DBus"), "/org/freedesktop/DBus", Some("org.freedesktop.DBus"), "GetId", &(), ) .await?; let id: &str = reply.body()?; println!("Unique ID of the bus: {}", id);
Monitoring all messages
Let’s eavesdrop on the session bus 😈 using the Monitor interface:
use futures_util::stream::TryStreamExt; use zbus::azync::Connection; let mut connection = Connection::new_session().await?; connection .call_method( Some("org.freedesktop.DBus"), "/org/freedesktop/DBus", Some("org.freedesktop.DBus.Monitoring"), "BecomeMonitor", &(&[] as &[&str], 0u32), ) .await?; while let Some(msg) = connection.stream().await.try_next().await? { println!("Got message: {}", msg); }
This should print something like:
Got message: Signal NameAcquired from org.freedesktop.DBus
Got message: Signal NameLost from org.freedesktop.DBus
Got message: Method call GetConnectionUnixProcessID from :1.1324
Got message: Error org.freedesktop.DBus.Error.NameHasNoOwner:
Could not get PID of name ':1.1332': no such name from org.freedesktop.DBus
Got message: Method call AddMatch from :1.918
Got message: Method return from org.freedesktop.DBus
Implementations
Create and open a D-Bus connection from a UnixStream.
The connection may either be set up for a bus connection, or not (for peer-to-peer communications).
Upon successful return, the connection is fully established and negotiated: D-Bus messages can be sent and received.
Create a server Connection for the given UnixStream and the server guid.
The connection will wait for incoming client authentication handshake & negotiation messages, for peer-to-peer communications.
Upon successful return, the connection is fully established and negotiated: D-Bus messages can be sent and received.
Get a stream to receive incoming messages.
Get a sink to send out messages.
Send msg to the peer.
Unlike MessageSink, this method sets a unique (to this connection) serial number on the
message before sending it off, for you.
On successfully sending off msg, the assigned serial number is returned.
Send a method call.
Create a method-call message, send it over the connection, then wait for the reply.
On successful reply, an Ok(Message) is returned. On error, an Err is returned. D-Bus
error replies are returned as Error::MethodError.
pub async fn emit_signal<B, E>(
&self,
destination: Option<&str>,
path: impl TryInto<ObjectPath<'_>, Error = E>,
interface: &str,
signal_name: &str,
body: &B
) -> Result<()> where
B: Serialize + Type,
E: Into<MessageError>, [src]
pub async fn emit_signal<B, E>(
&self,
destination: Option<&str>,
path: impl TryInto<ObjectPath<'_>, Error = E>,
interface: &str,
signal_name: &str,
body: &B
) -> Result<()> where
B: Serialize + Type,
E: Into<MessageError>, [src]Emit a signal.
Create a signal message, and send it over the connection.
Reply to a message.
Given an existing message (likely a method call), send a reply back to the caller with the
given body.
Returns the message serial number.
Reply an error to a message.
Given an existing message (likely a method call), send an error reply back to the caller
with the given error_name and body.
Returns the message serial number.
Checks if self is a connection to a message bus.
This will return false for p2p connections.
Assigns a serial number to msg that is unique to this connection.
This method can fail if msg is corrupt.
The unique name as assigned by the message bus or None if not a message bus connection.
Max number of messages to queue.
Set the max number of messages to queue.
Since typically you’d want to set this at instantiation time, this method takes ownership
of self and returns an owned Connection instance so you can use the builder pattern to
set the value.
Example
let conn = Connection::new_session() .await? .set_max_queued(30); assert_eq!(conn.max_queued(), 30); // Do something useful with `conn`..
The server’s GUID.
The underlying executor.
This method is available when built with the default internal-executor feature disabled.
Since zbus will not spawn thread internally to run the executor in this case, you’re
responsible to continuously tick the executor. Failure to do so will result in hangs.
Examples
Here is how one would typically run the zbus executor through tokio’s single-threaded scheduler:
use zbus::azync::Connection; use tokio::runtime; runtime::Builder::new_current_thread() .build() .unwrap() .block_on(async { let conn = Connection::new_session().await.unwrap(); { let conn = conn.clone(); tokio::task::spawn(async move { loop { conn.executor().tick().await; } }); } // All your other async code goes here. });
Create a Connection to the session/user message bus.
Create a Connection to the system-wide message bus.
Create a Connection for the given D-Bus address.
Trait Implementations
Performs the conversion.
Performs the conversion.
Auto Trait Implementations
impl !RefUnwindSafe for Connectionimpl Send for Connectionimpl Sync for Connectionimpl Unpin for Connectionimpl !UnwindSafe for ConnectionBlanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V