gio/auto/unix_connection.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{AsyncResult, Cancellable, Credentials, IOStream, SocketConnection, ffi};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 /// ` belonged to the UNIX-specific GIO
11 /// interfaces, thus you had to use the `gio-unix-2.0.pc` pkg-config file when
12 /// using it. This is no longer necessary since GLib 2.72.
13 ///
14 /// # Implements
15 ///
16 /// [`UnixConnectionExt`][trait@crate::prelude::UnixConnectionExt], [`SocketConnectionExt`][trait@crate::prelude::SocketConnectionExt], [`IOStreamExt`][trait@crate::prelude::IOStreamExt], [`trait@glib::ObjectExt`], [`IOStreamExtManual`][trait@crate::prelude::IOStreamExtManual]
17 #[doc(alias = "GUnixConnection")]
18 pub struct UnixConnection(Object<ffi::GUnixConnection, ffi::GUnixConnectionClass>) @extends SocketConnection, IOStream;
19
20 match fn {
21 type_ => || ffi::g_unix_connection_get_type(),
22 }
23}
24
25impl UnixConnection {
26 pub const NONE: Option<&'static UnixConnection> = None;
27}
28
29/// Trait containing all [`struct@UnixConnection`] methods.
30///
31/// # Implementors
32///
33/// [`UnixConnection`][struct@crate::UnixConnection]
34pub trait UnixConnectionExt: IsA<UnixConnection> + 'static {
35 /// Receives credentials from the sending end of the connection. The
36 /// sending end has to call g_unix_connection_send_credentials() (or
37 /// similar) for this to work.
38 ///
39 /// As well as reading the credentials this also reads (and discards) a
40 /// single byte from the stream, as this is required for credentials
41 /// passing to work on some implementations.
42 ///
43 /// This method can be expected to be available on the following platforms:
44 ///
45 /// - Linux since GLib 2.26
46 /// - FreeBSD since GLib 2.26
47 /// - GNU/kFreeBSD since GLib 2.36
48 /// - Solaris, Illumos and OpenSolaris since GLib 2.40
49 /// - GNU/Hurd since GLib 2.40
50 ///
51 /// Other ways to exchange credentials with a foreign peer includes the
52 /// #GUnixCredentialsMessage type and g_socket_get_credentials() function.
53 /// ## `cancellable`
54 /// A #GCancellable or [`None`].
55 ///
56 /// # Returns
57 ///
58 /// Received credentials on success (free with
59 /// g_object_unref()), [`None`] if @error is set.
60 #[doc(alias = "g_unix_connection_receive_credentials")]
61 fn receive_credentials(
62 &self,
63 cancellable: Option<&impl IsA<Cancellable>>,
64 ) -> Result<Credentials, glib::Error> {
65 unsafe {
66 let mut error = std::ptr::null_mut();
67 let ret = ffi::g_unix_connection_receive_credentials(
68 self.as_ref().to_glib_none().0,
69 cancellable.map(|p| p.as_ref()).to_glib_none().0,
70 &mut error,
71 );
72 if error.is_null() {
73 Ok(from_glib_full(ret))
74 } else {
75 Err(from_glib_full(error))
76 }
77 }
78 }
79
80 /// Asynchronously receive credentials.
81 ///
82 /// For more details, see g_unix_connection_receive_credentials() which is
83 /// the synchronous version of this call.
84 ///
85 /// When the operation is finished, @callback will be called. You can then call
86 /// g_unix_connection_receive_credentials_finish() to get the result of the operation.
87 /// ## `cancellable`
88 /// optional #GCancellable object, [`None`] to ignore.
89 /// ## `callback`
90 /// a #GAsyncReadyCallback
91 /// to call when the request is satisfied
92 #[doc(alias = "g_unix_connection_receive_credentials_async")]
93 fn receive_credentials_async<P: FnOnce(Result<Credentials, glib::Error>) + 'static>(
94 &self,
95 cancellable: Option<&impl IsA<Cancellable>>,
96 callback: P,
97 ) {
98 let main_context = glib::MainContext::ref_thread_default();
99 let is_main_context_owner = main_context.is_owner();
100 let has_acquired_main_context = (!is_main_context_owner)
101 .then(|| main_context.acquire().ok())
102 .flatten();
103 assert!(
104 is_main_context_owner || has_acquired_main_context.is_some(),
105 "Async operations only allowed if the thread is owning the MainContext"
106 );
107
108 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
109 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
110 unsafe extern "C" fn receive_credentials_async_trampoline<
111 P: FnOnce(Result<Credentials, glib::Error>) + 'static,
112 >(
113 _source_object: *mut glib::gobject_ffi::GObject,
114 res: *mut crate::ffi::GAsyncResult,
115 user_data: glib::ffi::gpointer,
116 ) {
117 unsafe {
118 let mut error = std::ptr::null_mut();
119 let ret = ffi::g_unix_connection_receive_credentials_finish(
120 _source_object as *mut _,
121 res,
122 &mut error,
123 );
124 let result = if error.is_null() {
125 Ok(from_glib_full(ret))
126 } else {
127 Err(from_glib_full(error))
128 };
129 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
130 Box_::from_raw(user_data as *mut _);
131 let callback: P = callback.into_inner();
132 callback(result);
133 }
134 }
135 let callback = receive_credentials_async_trampoline::<P>;
136 unsafe {
137 ffi::g_unix_connection_receive_credentials_async(
138 self.as_ref().to_glib_none().0,
139 cancellable.map(|p| p.as_ref()).to_glib_none().0,
140 Some(callback),
141 Box_::into_raw(user_data) as *mut _,
142 );
143 }
144 }
145
146 fn receive_credentials_future(
147 &self,
148 ) -> Pin<Box_<dyn std::future::Future<Output = Result<Credentials, glib::Error>> + 'static>>
149 {
150 Box_::pin(crate::GioFuture::new(
151 self,
152 move |obj, cancellable, send| {
153 obj.receive_credentials_async(Some(cancellable), move |res| {
154 send.resolve(res);
155 });
156 },
157 ))
158 }
159
160 /// Receives a file descriptor from the sending end of the connection.
161 /// The sending end has to call g_unix_connection_send_fd() for this
162 /// to work.
163 ///
164 /// As well as reading the fd this also reads a single byte from the
165 /// stream, as this is required for fd passing to work on some
166 /// implementations.
167 /// ## `cancellable`
168 /// optional #GCancellable object, [`None`] to ignore
169 ///
170 /// # Returns
171 ///
172 /// a file descriptor on success, -1 on error.
173 #[doc(alias = "g_unix_connection_receive_fd")]
174 fn receive_fd(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<i32, glib::Error> {
175 unsafe {
176 let mut error = std::ptr::null_mut();
177 let ret = ffi::g_unix_connection_receive_fd(
178 self.as_ref().to_glib_none().0,
179 cancellable.map(|p| p.as_ref()).to_glib_none().0,
180 &mut error,
181 );
182 if error.is_null() {
183 Ok(ret)
184 } else {
185 Err(from_glib_full(error))
186 }
187 }
188 }
189
190 /// Passes the credentials of the current user the receiving side
191 /// of the connection. The receiving end has to call
192 /// g_unix_connection_receive_credentials() (or similar) to accept the
193 /// credentials.
194 ///
195 /// As well as sending the credentials this also writes a single NUL
196 /// byte to the stream, as this is required for credentials passing to
197 /// work on some implementations.
198 ///
199 /// This method can be expected to be available on the following platforms:
200 ///
201 /// - Linux since GLib 2.26
202 /// - FreeBSD since GLib 2.26
203 /// - GNU/kFreeBSD since GLib 2.36
204 /// - Solaris, Illumos and OpenSolaris since GLib 2.40
205 /// - GNU/Hurd since GLib 2.40
206 ///
207 /// Other ways to exchange credentials with a foreign peer includes the
208 /// #GUnixCredentialsMessage type and g_socket_get_credentials() function.
209 /// ## `cancellable`
210 /// A #GCancellable or [`None`].
211 ///
212 /// # Returns
213 ///
214 /// [`true`] on success, [`false`] if @error is set.
215 #[doc(alias = "g_unix_connection_send_credentials")]
216 fn send_credentials(
217 &self,
218 cancellable: Option<&impl IsA<Cancellable>>,
219 ) -> Result<(), glib::Error> {
220 unsafe {
221 let mut error = std::ptr::null_mut();
222 let is_ok = ffi::g_unix_connection_send_credentials(
223 self.as_ref().to_glib_none().0,
224 cancellable.map(|p| p.as_ref()).to_glib_none().0,
225 &mut error,
226 );
227 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
228 if error.is_null() {
229 Ok(())
230 } else {
231 Err(from_glib_full(error))
232 }
233 }
234 }
235
236 /// Asynchronously send credentials.
237 ///
238 /// For more details, see g_unix_connection_send_credentials() which is
239 /// the synchronous version of this call.
240 ///
241 /// When the operation is finished, @callback will be called. You can then call
242 /// g_unix_connection_send_credentials_finish() to get the result of the operation.
243 /// ## `cancellable`
244 /// optional #GCancellable object, [`None`] to ignore.
245 /// ## `callback`
246 /// a #GAsyncReadyCallback
247 /// to call when the request is satisfied
248 #[doc(alias = "g_unix_connection_send_credentials_async")]
249 fn send_credentials_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
250 &self,
251 cancellable: Option<&impl IsA<Cancellable>>,
252 callback: P,
253 ) {
254 let main_context = glib::MainContext::ref_thread_default();
255 let is_main_context_owner = main_context.is_owner();
256 let has_acquired_main_context = (!is_main_context_owner)
257 .then(|| main_context.acquire().ok())
258 .flatten();
259 assert!(
260 is_main_context_owner || has_acquired_main_context.is_some(),
261 "Async operations only allowed if the thread is owning the MainContext"
262 );
263
264 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
265 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
266 unsafe extern "C" fn send_credentials_async_trampoline<
267 P: FnOnce(Result<(), glib::Error>) + 'static,
268 >(
269 _source_object: *mut glib::gobject_ffi::GObject,
270 res: *mut crate::ffi::GAsyncResult,
271 user_data: glib::ffi::gpointer,
272 ) {
273 unsafe {
274 let mut error = std::ptr::null_mut();
275 ffi::g_unix_connection_send_credentials_finish(
276 _source_object as *mut _,
277 res,
278 &mut error,
279 );
280 let result = if error.is_null() {
281 Ok(())
282 } else {
283 Err(from_glib_full(error))
284 };
285 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
286 Box_::from_raw(user_data as *mut _);
287 let callback: P = callback.into_inner();
288 callback(result);
289 }
290 }
291 let callback = send_credentials_async_trampoline::<P>;
292 unsafe {
293 ffi::g_unix_connection_send_credentials_async(
294 self.as_ref().to_glib_none().0,
295 cancellable.map(|p| p.as_ref()).to_glib_none().0,
296 Some(callback),
297 Box_::into_raw(user_data) as *mut _,
298 );
299 }
300 }
301
302 fn send_credentials_future(
303 &self,
304 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
305 Box_::pin(crate::GioFuture::new(
306 self,
307 move |obj, cancellable, send| {
308 obj.send_credentials_async(Some(cancellable), move |res| {
309 send.resolve(res);
310 });
311 },
312 ))
313 }
314
315 /// Passes a file descriptor to the receiving side of the
316 /// connection. The receiving end has to call g_unix_connection_receive_fd()
317 /// to accept the file descriptor.
318 ///
319 /// As well as sending the fd this also writes a single byte to the
320 /// stream, as this is required for fd passing to work on some
321 /// implementations.
322 /// ## `fd`
323 /// a file descriptor
324 /// ## `cancellable`
325 /// optional #GCancellable object, [`None`] to ignore.
326 ///
327 /// # Returns
328 ///
329 /// a [`true`] on success, [`None`] on error.
330 #[doc(alias = "g_unix_connection_send_fd")]
331 fn send_fd(
332 &self,
333 fd: i32,
334 cancellable: Option<&impl IsA<Cancellable>>,
335 ) -> Result<(), glib::Error> {
336 unsafe {
337 let mut error = std::ptr::null_mut();
338 let is_ok = ffi::g_unix_connection_send_fd(
339 self.as_ref().to_glib_none().0,
340 fd,
341 cancellable.map(|p| p.as_ref()).to_glib_none().0,
342 &mut error,
343 );
344 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
345 if error.is_null() {
346 Ok(())
347 } else {
348 Err(from_glib_full(error))
349 }
350 }
351 }
352}
353
354impl<O: IsA<UnixConnection>> UnixConnectionExt for O {}