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