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