logo
pub struct Connection(_);
Expand description

A D-Bus connection.

A connection to a D-Bus bus, or a direct peer.

Once created, the connection is authenticated and negotiated and messages can be sent or received, such as method calls or signals.

For higher-level message handling (typed functions, introspection, documentation reasons etc), it is recommended to wrap the low-level D-Bus messages into Rust functions with the dbus_proxy and dbus_interface macros instead of doing it directly on a Connection.

For lower-level handling of the connection (such as nonblocking socket handling), see the documentation of the new_authenticated_unix constructor.

Typically, a connection is made to the session bus with new_session, or to the system bus with new_system. Then the connection is shared with the Proxy and ObjectServer instances.

Connection implements Clone and cloning it is a very cheap operation, as the underlying data is not cloned. This makes it very convenient to share the connection between different parts of your code. Connection also implements std::marker::Sync andstd::marker::Send so you can send and share a connection instance across threads as well.

NB: If you want to send and receive messages from multiple threads at the same time, it’s usually better to create unique connections for each thread. Otherwise you can end up with deadlocks. For example, if one thread tries to send a message on a connection, while another is waiting to receive a message on the bus, the former will block until the latter receives a message.

Since there are times when important messages arrive between a method call message is sent and its reply is received, Connection keeps an internal queue of incoming messages so that these messages are not lost and subsequent calls to receive_message will retreive messages from this queue first. The size of this queue is configurable through the set_max_queued method. The default size is 32. All messages that are received after the queue is full, are dropped.

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 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.

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.

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 = zbus::Connection::new_session()?.set_max_queued(30);
assert_eq!(conn.max_queued(), 30);

// Do something usefull with `conn`..

The server’s GUID.

The unique name as assigned by the message bus or None if not a message bus connection.

Fetch the next message from the connection.

Read from the connection until a message is received or an error is reached. Return the message on success. If the connection is in non-blocking mode, this will return a WouldBlock error instead of blocking. If there are pending messages in the queue, the first one from the queue is returned instead of attempting to read the connection.

If a default message handler has been registered on this connection through set_default_message_handler, it will first get to decide the fate of the received message.

Receive a specific message.

This is the same as Self::receive_message, except that it takes a predicate function that decides if the message received should be returned by this method or not. Message received during this call that are not returned by it, are pushed to the queue to be picked by the susubsequent call to receive_message] or this method.

Send msg to the peer.

The connection sets a unique serial number on the message before sending it off.

On successfully sending off msg, the assigned serial number is returned.

Note: if this connection is in non-blocking mode, the message may not actually have been sent when this method returns, and you need to call the flush method so that pending messages are written to the socket.

Flush pending outgoing messages to the server

This method is only useful if the connection is in non-blocking mode. It will write as many pending outgoing messages as possible to the socket.

It will return Ok(()) if all messages could be sent, and error otherwise. A WouldBlock error means that the internal buffer of the connection transport is full, and you need to wait for write-readiness before calling this method again.

If the connection is in blocking mode, this will return Ok(()) and do nothing.

Send a method call.

Create a method-call message, send it over the connection, then wait for the reply. Incoming messages are received through receive_message (and by the default message handler) until the matching method reply (error or return) is received.

On succesful reply, an Ok(Message) is returned. On error, an Err is returned. D-Bus error replies are returned as MethodError.

Note: This method will block until the response is received even if the connection is in non-blocking mode. If you don’t want to block like this, use Connection::send_message.

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.

👎 Deprecated since 1.4.0:

You shouldn’t need this anymore since Connection queues messages

Set a default handler for incoming messages on this connection.

This is the handler that will be called on all messages received during receive_message call. If the handler callback returns a message (which could be a different message than it was given), receive_message will return it to its caller.

👎 Deprecated since 1.4.0:

You shouldn’t need this anymore since Connection queues messages

Reset the default message handler.

Remove the previously set message handler from set_default_message_handler.

Create a Connection from an already authenticated unix socket

This method can be used in conjunction with ClientHandshake or ServerHandshake to handle the initial handshake of the D-Bus connection asynchronously. The Authenticated argument required by this method is the result provided by these handshake utilities.

If the aim is to initialize a client bus connection, you need to send the client hello and assign the resulting unique name using set_unique_name before doing anything else.

Sets the unique name for this connection

This method should only be used when initializing a client bus connection with new_authenticated_unix. Setting the unique name to anything other than the return value of the bus hello is a protocol violation.

Returns and error if the name has already been set.

Checks if self is a connection to a message bus.

This will return false for p2p connections.

Trait Implementations

Extracts the raw file descriptor. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.