### Class

# Gio DBusConnection

since: 2.26

#### Description [src]

    final class Gio.DBusConnection : GObject.Object
      implements Gio.AsyncInitable, Gio.Initable {
      /* No available fields */
    }

The `GDBusConnection` type is used for D-Bus connections to remote peers such as a message buses.

It is a low-level API that offers a lot of flexibility. For instance, it lets you establish a connection over any transport that can by represented as a `GIOStream`.

This class is rarely used directly in D-Bus clients. If you are writing a D-Bus client, it is often easier to use the `g_bus_own_name()`, `g_bus_watch_name()` or `g_dbus_proxy_new_for_bus()` APIs.

As an exception to the usual GLib rule that a particular object must not be used by two threads at the same time, `GDBusConnection`s methods may be called from any thread. This is so that `g_bus_get()` and `g_bus_get_sync()` can safely return the same `GDBusConnection` when called from any thread.

Most of the ways to obtain a `GDBusConnection` automatically initialize it (i.e. connect to D-Bus): for instance, `g_dbus_connection_new()` and `g_bus_get()`, and the synchronous versions of those methods, give you an initialized connection. Language bindings for GIO should use `g_initable_new()` or `g_async_initable_new_async()`, which also initialize the connection.

If you construct an uninitialized `GDBusConnection`, such as via `g_object_new()`, you must initialize it via `g_initable_init()` or `g_async_initable_init_async()` before using its methods or properties. Calling methods or accessing properties on a `GDBusConnection` that has not completed initialization successfully is considered to be invalid, and leads to undefined behaviour. In particular, if initialization fails with a `GError`, the only valid thing you can do with that `GDBusConnection` is to free it with `g_object_unref()`.

## An example D-Bus server

Here is an example for a D-Bus server: gdbus-example-server.c

## An example for exporting a subtree

Here is an example for exporting a subtree: gdbus-example-subtree.c

## An example for file descriptor passing

Here is an example for passing UNIX file descriptors: gdbus-unix-fd-client.c

## An example for exporting a GObject

Here is an example for exporting a `GObject`: gdbus-example-export.c.

Available since: 2.26

#### Ancestors

- GObject

#### Implements

- GAsyncInitable
- GInitable

#### Constructors

###### g_dbus_connection_new_finish

Finishes an operation started with g_dbus_connection_new().

since: 2.26

###### g_dbus_connection_new_for_address_finish

Finishes an operation started with g_dbus_connection_new_for_address().

since: 2.26

###### g_dbus_connection_new_for_address_sync

Synchronously connects and sets up a D-Bus client connection for exchanging D-Bus messages with an endpoint specified by `address` which must be in the D-Bus address format.

since: 2.26

###### g_dbus_connection_new_sync

Synchronously sets up a D-Bus connection for exchanging D-Bus messages with the end represented by `stream`.

since: 2.26

#### Functions

###### g_dbus_connection_new

Asynchronously sets up a D-Bus connection for exchanging D-Bus messages with the end represented by `stream`.

since: 2.26

###### g_dbus_connection_new_for_address

Asynchronously connects and sets up a D-Bus client connection for exchanging D-Bus messages with an endpoint specified by `address` which must be in the D-Bus address format.

since: 2.26

#### Instance methods

###### g_dbus_connection_add_filter

Adds a message filter. Filters are handlers that are run on all incoming and outgoing messages, prior to standard dispatch. Filters are run in the order that they were added. The same handler can be added as a filter more than once, in which case it will be run more than once. Filters added during a filter callback won’t be run on the message being processed. Filter functions are allowed to modify and even drop messages.

since: 2.26

###### g_dbus_connection_call

Asynchronously invokes the `method_name` method on the `interface_name` D-Bus interface on the remote object at `object_path` owned by `bus_name`.

since: 2.26

###### g_dbus_connection_call_finish

Finishes an operation started with g_dbus_connection_call().

since: 2.26

###### g_dbus_connection_call_sync

Synchronously invokes the `method_name` method on the `interface_name` D-Bus interface on the remote object at `object_path` owned by `bus_name`.

since: 2.26

###### g_dbus_connection_call_with_unix_fd_list

Like `g_dbus_connection_call()` but also takes a `GUnixFDList` object.

since: 2.30

###### g_dbus_connection_call_with_unix_fd_list_finish

Finishes an operation started with g_dbus_connection_call_with_unix_fd_list().

since: 2.30

###### g_dbus_connection_call_with_unix_fd_list_sync

Like `g_dbus_connection_call_sync()` but also takes and returns `GUnixFDList` objects. See `g_dbus_connection_call_with_unix_fd_list()` and `g_dbus_connection_call_with_unix_fd_list_finish()` for more details.

since: 2.30

###### g_dbus_connection_close

Closes `connection`. Note that this never causes the process to exit (this might only happen if the other end of a shared message bus connection disconnects, see `GDBusConnection:exit-on-close`).

since: 2.26

###### g_dbus_connection_close_finish

Finishes an operation started with g_dbus_connection_close().

since: 2.26

###### g_dbus_connection_close_sync

Synchronously closes `connection`. The calling thread is blocked until this is done. See `g_dbus_connection_close()` for the asynchronous version of this method and more details about what it does.

since: 2.26

###### g_dbus_connection_emit_signal

Emits a signal.

since: 2.26

###### g_dbus_connection_export_action_group

Exports `action_group` on `connection` at `object_path`.

since: 2.32

###### g_dbus_connection_export_menu_model

Exports `menu` on `connection` at `object_path`.

since: 2.32

###### g_dbus_connection_flush

Asynchronously flushes `connection`, that is, writes all queued outgoing messages to the transport and then flushes the transport (using g_output_stream_flush_async()). This is useful in programs that want to emit a D-Bus signal and then exit immediately. Without flushing the connection, there is no guarantee that the message has been sent to the networking buffers in the OS kernel.

since: 2.26

###### g_dbus_connection_flush_finish

Finishes an operation started with g_dbus_connection_flush().

since: 2.26

###### g_dbus_connection_flush_sync

Synchronously flushes `connection`. The calling thread is blocked until this is done. See `g_dbus_connection_flush()` for the asynchronous version of this method and more details about what it does.

since: 2.26

###### g_dbus_connection_get_capabilities

Gets the capabilities negotiated with the remote peer.

since: 2.26

###### g_dbus_connection_get_exit_on_close

Gets whether the process is terminated when `connection` is closed by the remote peer. See `GDBusConnection:exit-on-close` for more details.

since: 2.26

###### g_dbus_connection_get_flags

Gets the flags used to construct this connection.

since: 2.60

###### g_dbus_connection_get_guid

The GUID of the peer performing the role of server when authenticating. See `GDBusConnection:guid` for more details.

since: 2.26

###### g_dbus_connection_get_last_serial

Retrieves the last serial number assigned to a `GDBusMessage` on the current thread. This includes messages sent via both low-level API such as `g_dbus_connection_send_message()` as well as high-level API such as g_dbus_connection_emit_signal(), `g_dbus_connection_call()` or g_dbus_proxy_call().

since: 2.34

###### g_dbus_connection_get_peer_credentials

Gets the credentials of the authenticated peer. This will always return `NULL` unless `connection` acted as a server (e.g. `G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER` was passed) when set up and the client passed credentials as part of the authentication process.

since: 2.26

###### g_dbus_connection_get_stream

Gets the underlying stream used for IO.

since: 2.26

###### g_dbus_connection_get_unique_name

Gets the unique name of `connection` as assigned by the message bus. This can also be used to figure out if `connection` is a message bus connection.

since: 2.26

###### g_dbus_connection_is_closed

Gets whether `connection` is closed.

since: 2.26

###### g_dbus_connection_register_object

Registers callbacks for exported objects at `object_path` with the D-Bus interface that is described in `interface_info`.

since: 2.26

###### g_dbus_connection_register_object_with_closures

Version of `g_dbus_connection_register_object()` using closures instead of a `GDBusInterfaceVTable` for easier binding in other languages.

deprecated: 2.84 since: 2.46

###### g_dbus_connection_register_object_with_closures2

Version of `g_dbus_connection_register_object()` using closures instead of a `GDBusInterfaceVTable` for easier binding in other languages.

since: 2.84

###### g_dbus_connection_register_subtree

Registers a whole subtree of dynamic objects.

since: 2.26

###### g_dbus_connection_remove_filter

Removes a filter.

since: 2.26

###### g_dbus_connection_send_message

Asynchronously sends `message` to the peer represented by `connection`.

since: 2.26

###### g_dbus_connection_send_message_with_reply

Asynchronously sends `message` to the peer represented by `connection`.

since: 2.26

###### g_dbus_connection_send_message_with_reply_finish

Finishes an operation started with g_dbus_connection_send_message_with_reply().

since: 2.26

###### g_dbus_connection_send_message_with_reply_sync

Synchronously sends `message` to the peer represented by `connection` and blocks the calling thread until a reply is received or the timeout is reached. See `g_dbus_connection_send_message_with_reply()` for the asynchronous version of this method.

since: 2.26

###### g_dbus_connection_set_exit_on_close

Sets whether the process should be terminated when `connection` is closed by the remote peer. See `GDBusConnection:exit-on-close` for more details.

since: 2.26

###### g_dbus_connection_signal_subscribe

Subscribes to signals on `connection` and invokes `callback` whenever the signal is received. Note that `callback` will be invoked in the thread-default main context (see `g_main_context_push_thread_default()`) of the thread you are calling this method from.

since: 2.26

###### g_dbus_connection_signal_unsubscribe

Unsubscribes from signals.

since: 2.26

###### g_dbus_connection_start_message_processing

If `connection` was created with `G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING`, this method starts processing messages. Does nothing on if `connection` wasn’t created with this flag or if the method has already been called.

since: 2.26

###### g_dbus_connection_unexport_action_group

Reverses the effect of a previous call to `g_dbus_connection_export_action_group()`.

since: 2.32

###### g_dbus_connection_unexport_menu_model

Reverses the effect of a previous call to g_dbus_connection_export_menu_model().

since: 2.32

###### g_dbus_connection_unregister_object

Unregisters an object.

since: 2.26

###### g_dbus_connection_unregister_subtree

Unregisters a subtree.

since: 2.26

##### Methods inherited from GObject (43)

Please see GObject for a full list of methods.

##### Methods inherited from GAsyncInitable (3)

###### g_async_initable_init_async

Starts asynchronous initialization of the object implementing the interface. This must be done before any real use of the object after initial construction. If the object also implements `GInitable` you can optionally call `g_initable_init()` instead.

since: 2.22

###### g_async_initable_init_finish

Finishes asynchronous initialization and returns the result. See g_async_initable_init_async().

since: 2.22

###### g_async_initable_new_finish

Finishes the async construction for the various g_async_initable_new calls, returning the created object or `NULL` on error.

since: 2.22

##### Methods inherited from GInitable (1)

###### g_initable_init

Initializes the object implementing the interface.

since: 2.22

#### Properties

###### Gio.DBusConnection:address

A D-Bus address specifying potential endpoints that can be used when establishing the connection.

since: 2.26

###### Gio.DBusConnection:authentication-observer

A `GDBusAuthObserver` object to assist in the authentication process or `NULL`.

since: 2.26

###### Gio.DBusConnection:capabilities

Flags from the `GDBusCapabilityFlags` enumeration representing connection features negotiated with the other peer.

since: 2.26

###### Gio.DBusConnection:closed

A boolean specifying whether the connection has been closed.

since: 2.26

###### Gio.DBusConnection:exit-on-close

A boolean specifying whether the process will be terminated (by calling `raise(SIGTERM)`) if the connection is closed by the remote peer.

since: 2.26

###### Gio.DBusConnection:flags

Flags from the `GDBusConnectionFlags` enumeration.

since: 2.26

###### Gio.DBusConnection:guid

The GUID of the peer performing the role of server when authenticating.

since: 2.26

###### Gio.DBusConnection:stream

The underlying `GIOStream` used for I/O.

since: 2.26

###### Gio.DBusConnection:unique-name

The unique name as assigned by the message bus or `NULL` if the connection is not open or not a message bus connection.

since: 2.26

#### Signals

###### Gio.DBusConnection::closed

Emitted when the connection is closed.

since: 2.26

##### Signals inherited from GObject (1)

###### GObject::notify

The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.