DBusConnection class

Namespace Runic.Platform.Linux.DBus in Runic.Platform.Linux.DBus 0.23.0

public sealed class DBusConnection : IAsyncDisposable

Inherits or implements IAsyncDisposable.

A connection to a message bus or a D-Bus peer.

Remarks

Calls, signals and exported objects all use this connection. Its methods are safe to call from any thread; their continuations and every signal run on the thread pool, never on the caller's synchronization context, so a ViewModel moves results to its model context itself. A method call throws DBusException for an error reply, or with DBusException.Disconnected once the connection has closed.

Applications usually take IDBusConnections from dependency injection, which shares one connection per bus for the application. A connection created with DBusConnection.ConnectAsync belongs to the caller, which disposes it.

Properties

Disconnected

public CancellationToken Disconnected { get; }

Cancelled when the connection closes, because the bus went away or the connection was disposed.

UniqueName

public string UniqueName { get; }

The connection's unique bus name, such as :1.42; empty on a peer-to-peer connection.

Methods

CallMethodAsync

public ValueTask<ImmutableArray<DBusValue>> CallMethodAsync(DBusMethodCall call, CancellationToken cancellationToken = default)

Calls a method and returns its reply's values.

Exceptions

DBusException

The peer replied with an error, or the connection closed.

OperationCanceledException

cancellationToken was cancelled; the call's reply is ignored.

ConnectAsync

public static ValueTask<PlatformResult<DBusConnection>> ConnectAsync(DBusBus bus, CancellationToken cancellationToken = default)

Connects to the session or system bus.

Returns

The connection, which the caller disposes; PlatformUnavailableReason.BackendUnavailable when the bus is not running or refuses the connection; PlatformUnavailableReason.ProviderNotConfigured on Windows and macOS, which have no D-Bus.

ConnectToAddressAsync

public static ValueTask<PlatformResult<DBusConnection>> ConnectToAddressAsync(string address, CancellationToken cancellationToken = default)

Connects to the bus or peer at a D-Bus address, such as unix:path=/run/user/1000/bus.

Returns

The connection, which the caller disposes, or PlatformUnavailableReason.BackendUnavailable when nothing accepts the connection at the address.

Exceptions

ArgumentException

address is empty.

DisposeAsync

public ValueTask DisposeAsync()

Closes the connection. Pending calls fail with DBusException.Disconnected and subscriptions end.

Export

public IAsyncDisposable Export(DBusObjectPath path, DBusObject service)

Makes service answer calls at path on this connection.

Returns

The export; disposing it stops answering and lets the object be exported again.

Exceptions

InvalidOperationException

Another object is exported at the path, the object is exported elsewhere, or its DBusObject.MaxConcurrentCalls is less than 1.

ReleaseNameAsync

public ValueTask<bool> ReleaseNameAsync(string name, CancellationToken cancellationToken = default)

Gives up a well-known name or leaves its queue.

Returns

Whether the connection owned or queued for the name.

RequestNameAsync

public ValueTask<DBusRequestNameReply> RequestNameAsync(string name, DBusRequestNameOptions options = DBusRequestNameOptions.None, CancellationToken cancellationToken = default)

Asks the bus for a well-known name, such as org.example.Player.

Exceptions

ArgumentException

name is not a well-known bus name.

DBusException

The bus refused the request, for example because a policy forbids owning the name.

SubscribeAsync

public ValueTask<DBusSignalSubscription> SubscribeAsync(DBusSignalFilter filter, CancellationToken cancellationToken = default)

Starts receiving the signals that match filter.

Returns

A subscription that is active when this completes, so no signal emitted afterwards is missed. Enumerate it once, and dispose it to stop receiving.

Exceptions

ArgumentException

An argument position is outside 0–63, DBusSignalFilter.Arg0 and position 0 of DBusSignalFilter.Arguments differ, or DBusSignalFilter.Arg0Namespace is not a namespace of bus or interface names.

DBusException

The bus refused the subscription, or the connection closed.

Remarks

The bus applies the filter's keys, except DBusSignalFilter.Arguments and DBusSignalFilter.ArgumentPaths after the first argument, which the connection checks itself; the bus still sends those signals to the connection.

WatchNameOwnerAsync

public IAsyncEnumerable<string?> WatchNameOwnerAsync(string name, CancellationToken cancellationToken = default)

Reports the unique name that owns a bus name: the current owner first, then every change.

Remarks

null means nobody owns the name, for example while its service is not running.