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
120mod sealed {
121 pub trait Sealed {}
122 impl<T: super::IsA<super::SocketService>> Sealed for T {}
123}
124
125/// Trait containing all [`struct@SocketService`] methods.
126///
127/// # Implementors
128///
129/// [`SocketService`][struct@crate::SocketService], [`ThreadedSocketService`][struct@crate::ThreadedSocketService]
130pub trait SocketServiceExt: IsA<SocketService> + sealed::Sealed + 'static {
131 /// Check whether the service is active or not. An active
132 /// service will accept new clients that connect, while
133 /// a non-active service will let connecting clients queue
134 /// up until the service is started.
135 ///
136 /// # Returns
137 ///
138 /// [`true`] if the service is active, [`false`] otherwise
139 #[doc(alias = "g_socket_service_is_active")]
140 #[doc(alias = "active")]
141 fn is_active(&self) -> bool {
142 unsafe {
143 from_glib(ffi::g_socket_service_is_active(
144 self.as_ref().to_glib_none().0,
145 ))
146 }
147 }
148
149 /// Restarts the service, i.e. start accepting connections
150 /// from the added sockets when the mainloop runs. This only needs
151 /// to be called after the service has been stopped from
152 /// g_socket_service_stop().
153 ///
154 /// This call is thread-safe, so it may be called from a thread
155 /// handling an incoming client request.
156 #[doc(alias = "g_socket_service_start")]
157 fn start(&self) {
158 unsafe {
159 ffi::g_socket_service_start(self.as_ref().to_glib_none().0);
160 }
161 }
162
163 /// Stops the service, i.e. stops accepting connections
164 /// from the added sockets when the mainloop runs.
165 ///
166 /// This call is thread-safe, so it may be called from a thread
167 /// handling an incoming client request.
168 ///
169 /// Note that this only stops accepting new connections; it does not
170 /// close the listening sockets, and you can call
171 /// g_socket_service_start() again later to begin listening again. To
172 /// close the listening sockets, call g_socket_listener_close(). (This
173 /// will happen automatically when the #GSocketService is finalized.)
174 ///
175 /// This must be called before calling g_socket_listener_close() as
176 /// the socket service will start accepting connections immediately
177 /// when a new socket is added.
178 #[doc(alias = "g_socket_service_stop")]
179 fn stop(&self) {
180 unsafe {
181 ffi::g_socket_service_stop(self.as_ref().to_glib_none().0);
182 }
183 }
184
185 /// Whether the service is currently accepting connections.
186 fn set_active(&self, active: bool) {
187 ObjectExt::set_property(self.as_ref(), "active", active)
188 }
189
190 /// The ::incoming signal is emitted when a new incoming connection
191 /// to @service needs to be handled. The handler must initiate the
192 /// handling of @connection, but may not block; in essence,
193 /// asynchronous operations must be used.
194 ///
195 /// @connection will be unreffed once the signal handler returns,
196 /// so you need to ref it yourself if you are planning to use it.
197 /// ## `connection`
198 /// a new #GSocketConnection object
199 /// ## `source_object`
200 /// the source_object passed to
201 /// g_socket_listener_add_address()
202 ///
203 /// # Returns
204 ///
205 /// [`true`] to stop other handlers from being called
206 #[doc(alias = "incoming")]
207 fn connect_incoming<
208 F: Fn(&Self, &SocketConnection, Option<&glib::Object>) -> bool + 'static,
209 >(
210 &self,
211 f: F,
212 ) -> SignalHandlerId {
213 unsafe extern "C" fn incoming_trampoline<
214 P: IsA<SocketService>,
215 F: Fn(&P, &SocketConnection, Option<&glib::Object>) -> bool + 'static,
216 >(
217 this: *mut ffi::GSocketService,
218 connection: *mut ffi::GSocketConnection,
219 source_object: *mut glib::gobject_ffi::GObject,
220 f: glib::ffi::gpointer,
221 ) -> glib::ffi::gboolean {
222 let f: &F = &*(f as *const F);
223 f(
224 SocketService::from_glib_borrow(this).unsafe_cast_ref(),
225 &from_glib_borrow(connection),
226 Option::<glib::Object>::from_glib_borrow(source_object)
227 .as_ref()
228 .as_ref(),
229 )
230 .into_glib()
231 }
232 unsafe {
233 let f: Box_<F> = Box_::new(f);
234 connect_raw(
235 self.as_ptr() as *mut _,
236 b"incoming\0".as_ptr() as *const _,
237 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
238 incoming_trampoline::<Self, F> as *const (),
239 )),
240 Box_::into_raw(f),
241 )
242 }
243 }
244
245 #[doc(alias = "active")]
246 fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
247 unsafe extern "C" fn notify_active_trampoline<
248 P: IsA<SocketService>,
249 F: Fn(&P) + 'static,
250 >(
251 this: *mut ffi::GSocketService,
252 _param_spec: glib::ffi::gpointer,
253 f: glib::ffi::gpointer,
254 ) {
255 let f: &F = &*(f as *const F);
256 f(SocketService::from_glib_borrow(this).unsafe_cast_ref())
257 }
258 unsafe {
259 let f: Box_<F> = Box_::new(f);
260 connect_raw(
261 self.as_ptr() as *mut _,
262 b"notify::active\0".as_ptr() as *const _,
263 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
264 notify_active_trampoline::<Self, F> as *const (),
265 )),
266 Box_::into_raw(f),
267 )
268 }
269 }
270}
271
272impl<O: IsA<SocketService>> SocketServiceExt for O {}