1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#[cfg(not(feature = "tokio"))]
use std::net::TcpStream;
#[cfg(all(unix, not(feature = "tokio")))]
use std::os::unix::net::UnixStream;
#[cfg(feature = "tokio")]
use tokio::net::TcpStream;
#[cfg(all(unix, feature = "tokio"))]
use tokio::net::UnixStream;
#[cfg(all(windows, not(feature = "tokio")))]
use uds_windows::UnixStream;
use zvariant::ObjectPath;
#[cfg(feature = "p2p")]
use crate::Guid;
use crate::{
Error, Result, address::Address, blocking::Connection, conn::AuthMechanism,
connection::socket::BoxedSplit, names::WellKnownName, object_server::Interface,
utils::block_on,
};
/// A builder for [`zbus::blocking::Connection`].
#[derive(Debug)]
#[must_use]
pub struct Builder<'a>(crate::connection::Builder<'a>);
impl<'a> Builder<'a> {
/// Create a builder for the session/user message bus connection.
pub fn session() -> Result<Self> {
crate::connection::Builder::session().map(Self)
}
/// Create a builder for the system-wide message bus connection.
pub fn system() -> Result<Self> {
crate::connection::Builder::system().map(Self)
}
/// Create a builder for an IBus connection.
///
/// IBus (Intelligent Input Bus) is an input method framework. This method creates a builder
/// that will query the IBus daemon for its D-Bus address using the `ibus address` command.
///
/// # Platform Support
///
/// This method is available on Unix-like systems where IBus is installed.
///
/// # Errors
///
/// Returns an error if:
/// - The `ibus` command is not found or fails to execute
/// - The IBus daemon is not running
/// - The command output cannot be parsed as a valid D-Bus address
///
/// # Example
///
/// ```no_run
/// # use std::error::Error;
/// # use zbus::blocking::connection;
/// #
/// let _conn = connection::Builder::ibus()?
/// .build()?;
///
/// // Use the connection to interact with IBus services.
/// # Ok::<_, Box<dyn Error + Send + Sync>>(())
/// ```
#[cfg(unix)]
pub fn ibus() -> Result<Self> {
crate::connection::Builder::ibus().map(Self)
}
/// Create a builder for a connection that will use the given [D-Bus bus address].
///
/// [D-Bus bus address]: https://dbus.freedesktop.org/doc/dbus-specification.html#addresses
pub fn address<A>(address: A) -> Result<Self>
where
A: TryInto<Address>,
A::Error: Into<Error>,
{
crate::connection::Builder::address(address).map(Self)
}
/// Create a builder for a connection that will use the given unix stream.
///
/// If the default `async-io` feature is disabled, this method will expect a
/// [`tokio::net::UnixStream`](https://docs.rs/tokio/latest/tokio/net/struct.UnixStream.html)
/// argument.
///
/// Since tokio currently [does not support Unix domain sockets][tuds] on Windows, this method
/// is not available when the `tokio` feature is enabled and building for Windows target.
///
/// [tuds]: https://github.com/tokio-rs/tokio/issues/2201
#[cfg(any(unix, not(feature = "tokio")))]
pub fn unix_stream(stream: UnixStream) -> Self {
Self(crate::connection::Builder::unix_stream(stream))
}
/// Create a builder for a connection that will use the given TCP stream.
///
/// If the default `async-io` feature is disabled, this method will expect a
/// [`tokio::net::TcpStream`](https://docs.rs/tokio/latest/tokio/net/struct.TcpStream.html)
/// argument.
pub fn tcp_stream(stream: TcpStream) -> Self {
Self(crate::connection::Builder::tcp_stream(stream))
}
/// Create a builder for a connection that will use the given pre-authenticated socket.
///
/// This is similar to [`Builder::socket`], except that the socket is either already
/// authenticated or does not require authentication.
pub fn authenticated_socket<S, G>(socket: S, guid: G) -> Result<Self>
where
S: Into<BoxedSplit>,
G: TryInto<crate::Guid<'a>>,
G::Error: Into<Error>,
{
crate::connection::Builder::authenticated_socket(socket, guid).map(Self)
}
/// Create a builder for a connection that will use the given socket.
pub fn socket<S: Into<BoxedSplit>>(socket: S) -> Self {
Self(crate::connection::Builder::socket(socket))
}
/// Specify the mechanism to use during authentication.
pub fn auth_mechanism(self, auth_mechanism: AuthMechanism) -> Self {
Self(self.0.auth_mechanism(auth_mechanism))
}
/// Specify the user id during authentication.
///
/// This can be useful when using [`AuthMechanism::External`] with `socat`
/// to avoid the host decide what uid to use and instead provide one
/// known to have access rights.
#[cfg(unix)]
pub fn user_id(self, id: u32) -> Self {
Self(self.0.user_id(id))
}
/// The to-be-created connection will be a peer-to-peer connection.
///
/// This method is only available when the `p2p` feature is enabled.
#[cfg(feature = "p2p")]
pub fn p2p(self) -> Self {
Self(self.0.p2p())
}
/// The to-be-created connection will be a server using the given GUID.
///
/// The to-be-created connection will wait for incoming client authentication handshake and
/// negotiation messages, for peer-to-peer communications after successful creation.
///
/// This method is only available when the `p2p` feature is enabled.
///
/// **NOTE:** This method is redundant when using [`Builder::authenticated_socket`] since the
/// latter already sets the GUID for the connection and zbus doesn't differentiate between a
/// server and a client connection, except for authentication.
#[cfg(feature = "p2p")]
pub fn server<G>(self, guid: G) -> Result<Self>
where
G: TryInto<Guid<'a>>,
G::Error: Into<Error>,
{
self.0.server(guid).map(Self)
}
/// Set the capacity of the main (unfiltered) queue.
///
/// Since typically you'd want to set this at instantiation time, you can set it through the
/// builder.
///
/// # Example
///
/// ```
/// # use std::error::Error;
/// # use zbus::blocking::connection;
/// #
/// let conn = connection::Builder::session()?
/// .max_queued(30)
/// .build()?;
/// assert_eq!(conn.max_queued(), 30);
///
/// // Do something useful with `conn`..
/// # Ok::<_, Box<dyn Error + Send + Sync>>(())
/// ```
pub fn max_queued(self, max: usize) -> Self {
Self(self.0.max_queued(max))
}
/// Register a D-Bus [`Interface`] to be served at a given path.
///
/// This is similar to [`zbus::blocking::ObjectServer::at`], except that it allows you to have
/// your interfaces available immediately after the connection is established. Typically, this
/// is exactly what you'd want. Also in contrast to [`zbus::blocking::ObjectServer::at`], this
/// method will replace any previously added interface with the same name at the same path.
pub fn serve_at<P, I>(self, path: P, iface: I) -> Result<Self>
where
I: Interface,
P: TryInto<ObjectPath<'a>>,
P::Error: Into<Error>,
{
self.0.serve_at(path, iface).map(Self)
}
/// Register a well-known name for this connection on the bus.
///
/// This is similar to [`zbus::blocking::Connection::request_name`], except the name is
/// requested as part of the connection setup ([`Builder::build`]), immediately after
/// interfaces registered (through [`Builder::serve_at`]) are advertised. Typically
/// this is exactly what you want.
pub fn name<W>(self, well_known_name: W) -> Result<Self>
where
W: TryInto<WellKnownName<'a>>,
W::Error: Into<Error>,
{
self.0.name(well_known_name).map(Self)
}
/// Whether the [`zbus::fdo::RequestNameFlags::AllowReplacement`] flag will be set when
/// requesting names.
pub fn allow_name_replacements(self, allow_replacement: bool) -> Self {
Self(self.0.allow_name_replacements(allow_replacement))
}
/// Whether the [`zbus::fdo::RequestNameFlags::ReplaceExisting`] flag will be set when
/// requesting names.
pub fn replace_existing_names(self, replace_existing: bool) -> Self {
Self(self.0.replace_existing_names(replace_existing))
}
/// Set the unique name of the connection.
///
/// This method is only available when the `bus-impl` feature is enabled.
///
/// # Panics
///
/// This method panics if the to-be-created connection is not a peer-to-peer connection.
/// It will always panic if the connection is to a message bus as it's the bus that assigns
/// peers their unique names. This is mainly provided for bus implementations. All other users
/// should not need to use this method.
#[cfg(feature = "bus-impl")]
pub fn unique_name<U>(self, unique_name: U) -> Result<Self>
where
U: TryInto<crate::names::UniqueName<'a>>,
U::Error: Into<Error>,
{
self.0.unique_name(unique_name).map(Self)
}
/// Set a timeout for method calls.
///
/// Method calls will return
/// `zbus::Error::InputOutput(std::io::Error(kind: ErrorKind::TimedOut))` if a client does not
/// receive an answer from a service in time.
pub fn method_timeout(self, timeout: std::time::Duration) -> Self {
Self(self.0.method_timeout(timeout))
}
/// Build the connection, consuming the builder.
///
/// # Errors
///
/// Until server-side bus connection is supported, attempting to build such a connection will
/// result in a [`Error::Unsupported`] error.
pub fn build(self) -> Result<Connection> {
block_on(self.0.build()).map(Into::into)
}
/// Build the connection and return a [`MessageIterator`] to receive messages from it.
///
/// This is the blocking counterpart of [`crate::connection::Builder::build_message_stream`].
/// The iterator is set up **before** the socket-reader task is started, so no messages can
/// be lost in the window between the connection being built and the iterator being created.
/// Use this when the peer may pipeline traffic right after authentication — e.g. a bus
/// implementation reading a `Hello` method call from a just-connected client.
///
/// To get the [`Connection`] out of the returned iterator, use `Connection::from(&iter)`.
///
/// This method is only available when the `bus-impl` feature is enabled.
///
/// [`MessageIterator`]: crate::blocking::MessageIterator
#[cfg(feature = "bus-impl")]
pub fn build_message_iterator(self) -> Result<crate::blocking::MessageIterator> {
block_on(self.0.build_message_stream())
.map(|azync| crate::blocking::MessageIterator { azync: Some(azync) })
}
}