[][src]Struct zbus::Connection

pub struct Connection(_);

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.

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. Please note however, that sharing or sending of a connection instance across threads is not supported. If you've a valid use cas for that, please file an issue about it and we'll consider adding this feature.

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

impl Connection[src]

pub fn new_unix_client(stream: UnixStream, bus_connection: bool) -> Result<Self>[src]

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.

pub fn new_session() -> Result<Self>[src]

Create a Connection to the session/user message bus.

pub fn new_system() -> Result<Self>[src]

Create a Connection to the system-wide message bus.

pub fn new_for_address(address: &str, bus_connection: bool) -> Result<Self>[src]

Create a Connection for the given D-Bus address.

pub fn new_unix_server(stream: UnixStream, guid: &Guid) -> Result<Self>[src]

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.

pub fn max_queued(&self) -> usize[src]

Max number of messages to queue.

pub fn set_max_queued(self, max: usize) -> Self[src]

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

pub fn server_guid(&self) -> &str[src]

The server's GUID.

pub fn unique_name(&self) -> Option<&str>[src]

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

pub fn receive_message(&self) -> Result<Message>[src]

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 there are pending messages in the queue, the first one from the queue is returned instead.

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.

pub fn send_message(&self, msg: Message) -> Result<u32>[src]

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.

pub fn call_method<B>(
    &self,
    destination: Option<&str>,
    path: &str,
    iface: Option<&str>,
    method_name: &str,
    body: &B
) -> Result<Message> where
    B: Serialize + Type
[src]

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.

pub fn emit_signal<B>(
    &self,
    destination: Option<&str>,
    path: &str,
    iface: &str,
    signal_name: &str,
    body: &B
) -> Result<()> where
    B: Serialize + Type
[src]

Emit a signal.

Create a signal message, and send it over the connection.

pub fn reply<B>(&self, call: &Message, body: &B) -> Result<u32> where
    B: Serialize + Type
[src]

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.

pub fn reply_error<B>(
    &self,
    call: &Message,
    error_name: &str,
    body: &B
) -> Result<u32> where
    B: Serialize + Type
[src]

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.

pub fn set_default_message_handler(
    &mut self,
    handler: Box<dyn FnMut(Message) -> Option<Message>>
)
[src]

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.

pub fn reset_default_message_handler(&mut self)[src]

Reset the default message handler.

Remove the previously set message handler from set_default_message_handler.

Trait Implementations

impl AsRawFd for Connection[src]

impl Clone for Connection[src]

impl Debug for Connection[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.