pub struct Connection { /* private fields */ }Expand description
A connection to a message bus. Cloning is cheap and every clone shares the socket and the
reader thread; the socket closes when the last clone drops (or on close).
Exported handlers and signal callbacks that capture a clone keep the connection alive, so a
long-lived owner calls unexport / remove_signal_handler
or close when done. Handlers receive the connection in MethodCall and
signal callbacks as an argument, so they rarely need to capture one.
Implementations§
Source§impl Connection
impl Connection
Sourcepub fn session() -> Result<Connection, Error>
pub fn session() -> Result<Connection, Error>
Connects to the session bus named by DBUS_SESSION_BUS_ADDRESS, or to
$XDG_RUNTIME_DIR/bus when the variable is unset.
Sourcepub fn connect(address: &str) -> Result<Connection, Error>
pub fn connect(address: &str) -> Result<Connection, Error>
Connects to a bus at a D-Bus server address such as unix:path=/run/user/1000/bus.
Several ;-separated addresses are tried in order.
Sourcepub fn unique_name(&self) -> &str
pub fn unique_name(&self) -> &str
This connection’s unique bus name, such as :1.42.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Whether the connection has closed (the bus went away, or close).
Sourcepub fn close(&self)
pub fn close(&self)
Closes the socket. Pending and later calls fail with Error::Disconnected, and the
reader thread exits.
Sourcepub fn send(&self, msg: Message) -> Result<u32, Error>
pub fn send(&self, msg: Message) -> Result<u32, Error>
Sends msg with a fresh serial, returning that serial.
Sourcepub fn call(
&self,
destination: &str,
path: &str,
interface: &str,
member: &str,
args: &[Value],
) -> Result<Vec<Value>, Error>
pub fn call( &self, destination: &str, path: &str, interface: &str, member: &str, args: &[Value], ) -> Result<Vec<Value>, Error>
Calls a method and waits up to DEFAULT_TIMEOUT for its reply’s arguments.
Sourcepub fn call_with_timeout(
&self,
destination: &str,
path: &str,
interface: &str,
member: &str,
args: &[Value],
timeout: Duration,
) -> Result<Vec<Value>, Error>
pub fn call_with_timeout( &self, destination: &str, path: &str, interface: &str, member: &str, args: &[Value], timeout: Duration, ) -> Result<Vec<Value>, Error>
call with an explicit timeout.
Sourcepub fn call_no_reply(
&self,
destination: &str,
path: &str,
interface: &str,
member: &str,
args: &[Value],
) -> Result<(), Error>
pub fn call_no_reply( &self, destination: &str, path: &str, interface: &str, member: &str, args: &[Value], ) -> Result<(), Error>
Calls a method without waiting, asking the peer not to reply. Safe from handlers.
Sourcepub fn emit_signal(
&self,
path: &str,
interface: &str,
member: &str,
args: &[Value],
) -> Result<(), Error>
pub fn emit_signal( &self, path: &str, interface: &str, member: &str, args: &[Value], ) -> Result<(), Error>
Broadcasts a signal.
Sourcepub fn request_name(&self, name: &str, flags: u32) -> Result<u32, Error>
pub fn request_name(&self, name: &str, flags: u32) -> Result<u32, Error>
Asks the bus for a well-known name; returns a NAME_REPLY_* code.
Sourcepub fn release_name(&self, name: &str) -> Result<(), Error>
pub fn release_name(&self, name: &str) -> Result<(), Error>
Gives a well-known name back without waiting for the bus’s answer.
Sourcepub fn name_has_owner(&self, name: &str) -> bool
pub fn name_has_owner(&self, name: &str) -> bool
Whether some connection owns name; false when the bus cannot be asked.
Sourcepub fn add_match(&self, rule: &str) -> Result<(), Error>
pub fn add_match(&self, rule: &str) -> Result<(), Error>
Asks the bus to route messages matching rule (such as
type='signal',interface='org.example.Iface') to this connection.
Sourcepub fn remove_match(&self, rule: &str) -> Result<(), Error>
pub fn remove_match(&self, rule: &str) -> Result<(), Error>
Withdraws a rule added with add_match, without waiting.
Sourcepub fn on_signal<F>(&self, filter: SignalFilter, callback: F) -> SubscriptionId
pub fn on_signal<F>(&self, filter: SignalFilter, callback: F) -> SubscriptionId
Calls callback on the reader thread for every incoming signal that filter matches.
Sourcepub fn remove_signal_handler(&self, id: SubscriptionId)
pub fn remove_signal_handler(&self, id: SubscriptionId)
Removes a subscription made with on_signal.
Sourcepub fn export<F>(&self, path: &str, handler: F) -> Result<(), Error>
pub fn export<F>(&self, path: &str, handler: F) -> Result<(), Error>
Answers method calls on object path with handler, which runs on the reader thread.
The connection answers org.freedesktop.DBus.Peer itself. For
org.freedesktop.DBus.Introspectable.Introspect it asks the handler first and, when the
handler answers UnknownMethod, replies with a minimal document listing the standard
interfaces and any exported child objects (introspection_xml builds a handler’s own).