gio/auto/
socket_service.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, SocketConnection, SocketListener};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{connect_raw, SignalHandlerId},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// A `GSocketService` is an object that represents a service that
16    /// is provided to the network or over local sockets.  When a new
17    /// connection is made to the service the [`incoming`][struct@crate::SocketService#incoming]
18    /// signal is emitted.
19    ///
20    /// A `GSocketService` is a subclass of [`SocketListener`][crate::SocketListener] and you need
21    /// to add the addresses you want to accept connections on with the
22    /// [`SocketListener`][crate::SocketListener] APIs.
23    ///
24    /// There are two options for implementing a network service based on
25    /// `GSocketService`. The first is to create the service using
26    /// [`new()`][Self::new()] and to connect to the
27    /// [`incoming`][struct@crate::SocketService#incoming] signal. The second is to subclass
28    /// `GSocketService` and override the default signal handler implementation.
29    ///
30    /// In either case, the handler must immediately return, or else it
31    /// will block additional incoming connections from being serviced.
32    /// If you are interested in writing connection handlers that contain
33    /// blocking code then see [`ThreadedSocketService`][crate::ThreadedSocketService].
34    ///
35    /// The socket service runs on the main loop of the
36    /// thread-default context (see
37    /// [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()]) of the thread it is
38    /// created in, and is not threadsafe in general. However, the calls to start and
39    /// stop the service are thread-safe so these can be used from threads that
40    /// handle incoming clients.
41    ///
42    /// ## Properties
43    ///
44    ///
45    /// #### `active`
46    ///  Whether the service is currently accepting connections.
47    ///
48    /// Readable | Writeable | Construct
49    /// <details><summary><h4>SocketListener</h4></summary>
50    ///
51    ///
52    /// #### `listen-backlog`
53    ///  The number of outstanding connections in the listen queue.
54    ///
55    /// Readable | Writeable | Construct
56    /// </details>
57    ///
58    /// ## Signals
59    ///
60    ///
61    /// #### `incoming`
62    ///  The ::incoming signal is emitted when a new incoming connection
63    /// to @service needs to be handled. The handler must initiate the
64    /// handling of @connection, but may not block; in essence,
65    /// asynchronous operations must be used.
66    ///
67    /// @connection will be unreffed once the signal handler returns,
68    /// so you need to ref it yourself if you are planning to use it.
69    ///
70    ///
71    /// <details><summary><h4>SocketListener</h4></summary>
72    ///
73    ///
74    /// #### `event`
75    ///  Emitted when @listener's activity on @socket changes state.
76    /// Note that when @listener is used to listen on both IPv4 and
77    /// IPv6, a separate set of signals will be emitted for each, and
78    /// the order they happen in is undefined.
79    ///
80    ///
81    /// </details>
82    ///
83    /// # Implements
84    ///
85    /// [`SocketServiceExt`][trait@crate::prelude::SocketServiceExt], [`SocketListenerExt`][trait@crate::prelude::SocketListenerExt], [`trait@glib::ObjectExt`], [`SocketListenerExtManual`][trait@crate::prelude::SocketListenerExtManual]
86    #[doc(alias = "GSocketService")]
87    pub struct SocketService(Object<ffi::GSocketService, ffi::GSocketServiceClass>) @extends SocketListener;
88
89    match fn {
90        type_ => || ffi::g_socket_service_get_type(),
91    }
92}
93
94impl SocketService {
95    pub const NONE: Option<&'static SocketService> = None;
96
97    /// Creates a new #GSocketService with no sockets to listen for.
98    /// New listeners can be added with e.g. g_socket_listener_add_address()
99    /// or g_socket_listener_add_inet_port().
100    ///
101    /// New services are created active, there is no need to call
102    /// g_socket_service_start(), unless g_socket_service_stop() has been
103    /// called before.
104    ///
105    /// # Returns
106    ///
107    /// a new #GSocketService.
108    #[doc(alias = "g_socket_service_new")]
109    pub fn new() -> SocketService {
110        unsafe { from_glib_full(ffi::g_socket_service_new()) }
111    }
112}
113
114impl Default for SocketService {
115    fn default() -> Self {
116        Self::new()
117    }
118}
119
120/// Trait containing all [`struct@SocketService`] methods.
121///
122/// # Implementors
123///
124/// [`SocketService`][struct@crate::SocketService], [`ThreadedSocketService`][struct@crate::ThreadedSocketService]
125pub trait SocketServiceExt: IsA<SocketService> + 'static {
126    /// Check whether the service is active or not. An active
127    /// service will accept new clients that connect, while
128    /// a non-active service will let connecting clients queue
129    /// up until the service is started.
130    ///
131    /// # Returns
132    ///
133    /// [`true`] if the service is active, [`false`] otherwise
134    #[doc(alias = "g_socket_service_is_active")]
135    #[doc(alias = "active")]
136    fn is_active(&self) -> bool {
137        unsafe {
138            from_glib(ffi::g_socket_service_is_active(
139                self.as_ref().to_glib_none().0,
140            ))
141        }
142    }
143
144    /// Restarts the service, i.e. start accepting connections
145    /// from the added sockets when the mainloop runs. This only needs
146    /// to be called after the service has been stopped from
147    /// g_socket_service_stop().
148    ///
149    /// This call is thread-safe, so it may be called from a thread
150    /// handling an incoming client request.
151    #[doc(alias = "g_socket_service_start")]
152    fn start(&self) {
153        unsafe {
154            ffi::g_socket_service_start(self.as_ref().to_glib_none().0);
155        }
156    }
157
158    /// Stops the service, i.e. stops accepting connections
159    /// from the added sockets when the mainloop runs.
160    ///
161    /// This call is thread-safe, so it may be called from a thread
162    /// handling an incoming client request.
163    ///
164    /// Note that this only stops accepting new connections; it does not
165    /// close the listening sockets, and you can call
166    /// g_socket_service_start() again later to begin listening again. To
167    /// close the listening sockets, call g_socket_listener_close(). (This
168    /// will happen automatically when the #GSocketService is finalized.)
169    ///
170    /// This must be called before calling g_socket_listener_close() as
171    /// the socket service will start accepting connections immediately
172    /// when a new socket is added.
173    #[doc(alias = "g_socket_service_stop")]
174    fn stop(&self) {
175        unsafe {
176            ffi::g_socket_service_stop(self.as_ref().to_glib_none().0);
177        }
178    }
179
180    /// Whether the service is currently accepting connections.
181    fn set_active(&self, active: bool) {
182        ObjectExt::set_property(self.as_ref(), "active", active)
183    }
184
185    /// The ::incoming signal is emitted when a new incoming connection
186    /// to @service needs to be handled. The handler must initiate the
187    /// handling of @connection, but may not block; in essence,
188    /// asynchronous operations must be used.
189    ///
190    /// @connection will be unreffed once the signal handler returns,
191    /// so you need to ref it yourself if you are planning to use it.
192    /// ## `connection`
193    /// a new #GSocketConnection object
194    /// ## `source_object`
195    /// the source_object passed to
196    ///     g_socket_listener_add_address()
197    ///
198    /// # Returns
199    ///
200    /// [`true`] to stop other handlers from being called
201    #[doc(alias = "incoming")]
202    fn connect_incoming<
203        F: Fn(&Self, &SocketConnection, Option<&glib::Object>) -> bool + 'static,
204    >(
205        &self,
206        f: F,
207    ) -> SignalHandlerId {
208        unsafe extern "C" fn incoming_trampoline<
209            P: IsA<SocketService>,
210            F: Fn(&P, &SocketConnection, Option<&glib::Object>) -> bool + 'static,
211        >(
212            this: *mut ffi::GSocketService,
213            connection: *mut ffi::GSocketConnection,
214            source_object: *mut glib::gobject_ffi::GObject,
215            f: glib::ffi::gpointer,
216        ) -> glib::ffi::gboolean {
217            let f: &F = &*(f as *const F);
218            f(
219                SocketService::from_glib_borrow(this).unsafe_cast_ref(),
220                &from_glib_borrow(connection),
221                Option::<glib::Object>::from_glib_borrow(source_object)
222                    .as_ref()
223                    .as_ref(),
224            )
225            .into_glib()
226        }
227        unsafe {
228            let f: Box_<F> = Box_::new(f);
229            connect_raw(
230                self.as_ptr() as *mut _,
231                c"incoming".as_ptr() as *const _,
232                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
233                    incoming_trampoline::<Self, F> as *const (),
234                )),
235                Box_::into_raw(f),
236            )
237        }
238    }
239
240    #[doc(alias = "active")]
241    fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
242        unsafe extern "C" fn notify_active_trampoline<
243            P: IsA<SocketService>,
244            F: Fn(&P) + 'static,
245        >(
246            this: *mut ffi::GSocketService,
247            _param_spec: glib::ffi::gpointer,
248            f: glib::ffi::gpointer,
249        ) {
250            let f: &F = &*(f as *const F);
251            f(SocketService::from_glib_borrow(this).unsafe_cast_ref())
252        }
253        unsafe {
254            let f: Box_<F> = Box_::new(f);
255            connect_raw(
256                self.as_ptr() as *mut _,
257                c"notify::active".as_ptr() as *const _,
258                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
259                    notify_active_trampoline::<Self, F> as *const (),
260                )),
261                Box_::into_raw(f),
262            )
263        }
264    }
265}
266
267impl<O: IsA<SocketService>> SocketServiceExt for O {}