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