gio/auto/socket.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::{
6 Cancellable, Credentials, DatagramBased, InetAddress, Initable, SocketAddress,
7 SocketConnection, SocketFamily, SocketProtocol, SocketType, ffi,
8};
9use glib::{
10 prelude::*,
11 signal::{SignalHandlerId, connect_raw},
12 translate::*,
13};
14use std::boxed::Box as Box_;
15
16glib::wrapper! {
17 /// local_error);
18 /// // handle error if needed
19 /// }
20 /// ```text
21 ///
22 ///
23 /// ## Properties
24 ///
25 ///
26 /// #### `blocking`
27 /// Whether I/O on this socket is blocking.
28 ///
29 /// Readable | Writable
30 ///
31 ///
32 /// #### `broadcast`
33 /// Whether the socket should allow sending to broadcast addresses.
34 ///
35 /// Readable | Writable
36 ///
37 ///
38 /// #### `family`
39 /// The socket’s address family.
40 ///
41 /// Readable | Writable | Construct Only
42 ///
43 ///
44 /// #### `fd`
45 /// The socket’s file descriptor.
46 ///
47 /// Readable | Writable | Construct Only
48 ///
49 ///
50 /// #### `keepalive`
51 /// Whether to keep the connection alive by sending periodic pings.
52 ///
53 /// Readable | Writable
54 ///
55 ///
56 /// #### `listen-backlog`
57 /// The number of outstanding connections in the listen queue.
58 ///
59 /// Readable | Writable
60 ///
61 ///
62 /// #### `local-address`
63 /// The local address the socket is bound to.
64 ///
65 /// Readable
66 ///
67 ///
68 /// #### `multicast-loopback`
69 /// Whether outgoing multicast packets loop back to the local host.
70 ///
71 /// Readable | Writable
72 ///
73 ///
74 /// #### `multicast-ttl`
75 /// Time-to-live out outgoing multicast packets
76 ///
77 /// Readable | Writable
78 ///
79 ///
80 /// #### `protocol`
81 /// The ID of the protocol to use, or `-1` for unknown.
82 ///
83 /// Readable | Writable | Construct Only
84 ///
85 ///
86 /// #### `remote-address`
87 /// The remote address the socket is connected to.
88 ///
89 /// Readable
90 ///
91 ///
92 /// #### `timeout`
93 /// The timeout in seconds on socket I/O
94 ///
95 /// Readable | Writable
96 ///
97 ///
98 /// #### `ttl`
99 /// Time-to-live for outgoing unicast packets
100 ///
101 /// Readable | Writable
102 ///
103 ///
104 /// #### `type`
105 /// The socket’s type.
106 ///
107 /// Readable | Writable | Construct Only
108 ///
109 /// # Implements
110 ///
111 /// [`SocketExt`][trait@crate::prelude::SocketExt], [`trait@glib::ObjectExt`], [`DatagramBasedExt`][trait@crate::prelude::DatagramBasedExt], [`InitableExt`][trait@crate::prelude::InitableExt], [`SocketExtManual`][trait@crate::prelude::SocketExtManual], [`DatagramBasedExtManual`][trait@crate::prelude::DatagramBasedExtManual]
112 #[doc(alias = "GSocket")]
113 pub struct Socket(Object<ffi::GSocket, ffi::GSocketClass>) @implements DatagramBased, Initable;
114
115 match fn {
116 type_ => || ffi::g_socket_get_type(),
117 }
118}
119
120impl Socket {
121 pub const NONE: Option<&'static Socket> = None;
122
123 /// Creates a new #GSocket with the defined family, type and protocol.
124 /// If @protocol is 0 ([`SocketProtocol::Default`][crate::SocketProtocol::Default]) the default protocol type
125 /// for the family and type is used.
126 ///
127 /// The @protocol is a family and type specific int that specifies what
128 /// kind of protocol to use. #GSocketProtocol lists several common ones.
129 /// Many families only support one protocol, and use 0 for this, others
130 /// support several and using 0 means to use the default protocol for
131 /// the family and type.
132 ///
133 /// The protocol id is passed directly to the operating
134 /// system, so you can use protocols not listed in #GSocketProtocol if you
135 /// know the protocol number used for it.
136 /// ## `family`
137 /// the socket family to use, e.g. [`SocketFamily::Ipv4`][crate::SocketFamily::Ipv4].
138 /// ## `type_`
139 /// the socket type to use.
140 /// ## `protocol`
141 /// the id of the protocol to use, or 0 for default.
142 ///
143 /// # Returns
144 ///
145 /// a #GSocket or [`None`] on error.
146 /// Free the returned object with g_object_unref().
147 #[doc(alias = "g_socket_new")]
148 pub fn new(
149 family: SocketFamily,
150 type_: SocketType,
151 protocol: SocketProtocol,
152 ) -> Result<Socket, glib::Error> {
153 unsafe {
154 let mut error = std::ptr::null_mut();
155 let ret = ffi::g_socket_new(
156 family.into_glib(),
157 type_.into_glib(),
158 protocol.into_glib(),
159 &mut error,
160 );
161 if error.is_null() {
162 Ok(from_glib_full(ret))
163 } else {
164 Err(from_glib_full(error))
165 }
166 }
167 }
168}
169
170/// Trait containing all [`struct@Socket`] methods.
171///
172/// # Implementors
173///
174/// [`Socket`][struct@crate::Socket]
175pub trait SocketExt: IsA<Socket> + 'static {
176 /// Accept incoming connections on a connection-based socket. This removes
177 /// the first outstanding connection request from the listening socket and
178 /// creates a #GSocket object for it.
179 ///
180 /// The @self must be bound to a local address with g_socket_bind() and
181 /// must be listening for incoming connections (g_socket_listen()).
182 ///
183 /// If there are no outstanding connections then the operation will block
184 /// or return [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock] if non-blocking I/O is enabled.
185 /// To be notified of an incoming connection, wait for the [`glib::IOCondition::IN`][crate::glib::IOCondition::IN] condition.
186 /// ## `cancellable`
187 /// a `GCancellable` or [`None`]
188 ///
189 /// # Returns
190 ///
191 /// a new #GSocket, or [`None`] on error.
192 /// Free the returned object with g_object_unref().
193 #[doc(alias = "g_socket_accept")]
194 fn accept(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<Socket, glib::Error> {
195 unsafe {
196 let mut error = std::ptr::null_mut();
197 let ret = ffi::g_socket_accept(
198 self.as_ref().to_glib_none().0,
199 cancellable.map(|p| p.as_ref()).to_glib_none().0,
200 &mut error,
201 );
202 if error.is_null() {
203 Ok(from_glib_full(ret))
204 } else {
205 Err(from_glib_full(error))
206 }
207 }
208 }
209
210 /// When a socket is created it is attached to an address family, but it
211 /// doesn't have an address in this family. g_socket_bind() assigns the
212 /// address (sometimes called name) of the socket.
213 ///
214 /// It is generally required to bind to a local address before you can
215 /// receive connections. (See g_socket_listen() and g_socket_accept() ).
216 /// In certain situations, you may also want to bind a socket that will be
217 /// used to initiate connections, though this is not normally required.
218 ///
219 /// If @self is a TCP socket, then @allow_reuse controls the setting
220 /// of the `SO_REUSEADDR` socket option; normally it should be [`true`] for
221 /// server sockets (sockets that you will eventually call
222 /// g_socket_accept() on), and [`false`] for client sockets. (Failing to
223 /// set this flag on a server socket may cause g_socket_bind() to return
224 /// [`IOErrorEnum::AddressInUse`][crate::IOErrorEnum::AddressInUse] if the server program is stopped and then
225 /// immediately restarted.)
226 ///
227 /// If @self is a UDP socket, then @allow_reuse determines whether or
228 /// not other UDP sockets can be bound to the same address at the same
229 /// time. In particular, you can have several UDP sockets bound to the
230 /// same address, and they will all receive all of the multicast and
231 /// broadcast packets sent to that address. (The behavior of unicast
232 /// UDP packets to an address with multiple listeners is not defined.)
233 /// ## `address`
234 /// a #GSocketAddress specifying the local address.
235 /// ## `allow_reuse`
236 /// whether to allow reusing this address
237 ///
238 /// # Returns
239 ///
240 /// [`true`] on success, [`false`] on error.
241 #[doc(alias = "g_socket_bind")]
242 fn bind(
243 &self,
244 address: &impl IsA<SocketAddress>,
245 allow_reuse: bool,
246 ) -> Result<(), glib::Error> {
247 unsafe {
248 let mut error = std::ptr::null_mut();
249 let is_ok = ffi::g_socket_bind(
250 self.as_ref().to_glib_none().0,
251 address.as_ref().to_glib_none().0,
252 allow_reuse.into_glib(),
253 &mut error,
254 );
255 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
256 if error.is_null() {
257 Ok(())
258 } else {
259 Err(from_glib_full(error))
260 }
261 }
262 }
263
264 /// Checks and resets the pending connect error for the socket.
265 /// This is used to check for errors when g_socket_connect() is
266 /// used in non-blocking mode.
267 ///
268 /// # Returns
269 ///
270 /// [`true`] if no error, [`false`] otherwise, setting @error to the error
271 #[doc(alias = "g_socket_check_connect_result")]
272 fn check_connect_result(&self) -> Result<(), glib::Error> {
273 unsafe {
274 let mut error = std::ptr::null_mut();
275 let is_ok =
276 ffi::g_socket_check_connect_result(self.as_ref().to_glib_none().0, &mut error);
277 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
278 if error.is_null() {
279 Ok(())
280 } else {
281 Err(from_glib_full(error))
282 }
283 }
284 }
285
286 /// Closes the socket, shutting down any active connection.
287 ///
288 /// Closing a socket does not wait for all outstanding I/O operations
289 /// to finish, so the caller should not rely on them to be guaranteed
290 /// to complete even if the close returns with no error.
291 ///
292 /// Once the socket is closed, all other operations will return
293 /// [`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed]. Closing a socket multiple times will not
294 /// return an error.
295 ///
296 /// Sockets will be automatically closed when the last reference
297 /// is dropped, but you might want to call this function to make sure
298 /// resources are released as early as possible.
299 ///
300 /// Beware that due to the way that TCP works, it is possible for
301 /// recently-sent data to be lost if either you close a socket while the
302 /// [`glib::IOCondition::IN`][crate::glib::IOCondition::IN] condition is set, or else if the remote connection tries to
303 /// send something to you after you close the socket but before it has
304 /// finished reading all of the data you sent. There is no easy generic
305 /// way to avoid this problem; the easiest fix is to design the network
306 /// protocol such that the client will never send data "out of turn".
307 /// Another solution is for the server to half-close the connection by
308 /// calling g_socket_shutdown() with only the @shutdown_write flag set,
309 /// and then wait for the client to notice this and close its side of the
310 /// connection, after which the server can safely call g_socket_close().
311 /// (This is what #GTcpConnection does if you call
312 /// g_tcp_connection_set_graceful_disconnect(). But of course, this
313 /// only works if the client will close its connection after the server
314 /// does.)
315 ///
316 /// # Returns
317 ///
318 /// [`true`] on success, [`false`] on error
319 #[doc(alias = "g_socket_close")]
320 fn close(&self) -> Result<(), glib::Error> {
321 unsafe {
322 let mut error = std::ptr::null_mut();
323 let is_ok = ffi::g_socket_close(self.as_ref().to_glib_none().0, &mut error);
324 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
325 if error.is_null() {
326 Ok(())
327 } else {
328 Err(from_glib_full(error))
329 }
330 }
331 }
332
333 /// Checks on the readiness of @self to perform operations.
334 /// The operations specified in @condition are checked for and masked
335 /// against the currently-satisfied conditions on @self. The result
336 /// is returned.
337 ///
338 /// Note that on Windows, it is possible for an operation to return
339 /// [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock] even immediately after
340 /// g_socket_condition_check() has claimed that the socket is ready for
341 /// writing. Rather than calling g_socket_condition_check() and then
342 /// writing to the socket if it succeeds, it is generally better to
343 /// simply try writing to the socket right away, and try again later if
344 /// the initial attempt returns [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock].
345 ///
346 /// It is meaningless to specify [`glib::IOCondition::ERR`][crate::glib::IOCondition::ERR] or [`glib::IOCondition::HUP`][crate::glib::IOCondition::HUP] in condition;
347 /// these conditions will always be set in the output if they are true.
348 ///
349 /// This call never blocks.
350 /// ## `condition`
351 /// a #GIOCondition mask to check
352 ///
353 /// # Returns
354 ///
355 /// the @GIOCondition mask of the current state
356 #[doc(alias = "g_socket_condition_check")]
357 fn condition_check(&self, condition: glib::IOCondition) -> glib::IOCondition {
358 unsafe {
359 from_glib(ffi::g_socket_condition_check(
360 self.as_ref().to_glib_none().0,
361 condition.into_glib(),
362 ))
363 }
364 }
365
366 /// Waits for up to @timeout_us microseconds for @condition to become true
367 /// on @self. If the condition is met, [`true`] is returned.
368 ///
369 /// If @cancellable is cancelled before the condition is met, or if
370 /// @timeout_us (or the socket's #GSocket:timeout) is reached before the
371 /// condition is met, then [`false`] is returned and @error, if non-[`None`],
372 /// is set to the appropriate value ([`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] or
373 /// [`IOErrorEnum::TimedOut`][crate::IOErrorEnum::TimedOut]).
374 ///
375 /// If you don't want a timeout, use g_socket_condition_wait().
376 /// (Alternatively, you can pass -1 for @timeout_us.)
377 ///
378 /// Note that although @timeout_us is in microseconds for consistency with
379 /// other GLib APIs, this function actually only has millisecond
380 /// resolution, and the behavior is undefined if @timeout_us is not an
381 /// exact number of milliseconds.
382 /// ## `condition`
383 /// a #GIOCondition mask to wait for
384 /// ## `timeout_us`
385 /// the maximum time (in microseconds) to wait, or -1
386 /// ## `cancellable`
387 /// a #GCancellable, or [`None`]
388 ///
389 /// # Returns
390 ///
391 /// [`true`] if the condition was met, [`false`] otherwise
392 #[doc(alias = "g_socket_condition_timed_wait")]
393 fn condition_timed_wait(
394 &self,
395 condition: glib::IOCondition,
396 timeout_us: i64,
397 cancellable: Option<&impl IsA<Cancellable>>,
398 ) -> Result<(), glib::Error> {
399 unsafe {
400 let mut error = std::ptr::null_mut();
401 let is_ok = ffi::g_socket_condition_timed_wait(
402 self.as_ref().to_glib_none().0,
403 condition.into_glib(),
404 timeout_us,
405 cancellable.map(|p| p.as_ref()).to_glib_none().0,
406 &mut error,
407 );
408 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
409 if error.is_null() {
410 Ok(())
411 } else {
412 Err(from_glib_full(error))
413 }
414 }
415 }
416
417 /// Waits for @condition to become true on @self. When the condition
418 /// is met, [`true`] is returned.
419 ///
420 /// If @cancellable is cancelled before the condition is met, or if the
421 /// socket has a timeout set and it is reached before the condition is
422 /// met, then [`false`] is returned and @error, if non-[`None`], is set to
423 /// the appropriate value ([`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] or
424 /// [`IOErrorEnum::TimedOut`][crate::IOErrorEnum::TimedOut]).
425 ///
426 /// See also g_socket_condition_timed_wait().
427 /// ## `condition`
428 /// a #GIOCondition mask to wait for
429 /// ## `cancellable`
430 /// a #GCancellable, or [`None`]
431 ///
432 /// # Returns
433 ///
434 /// [`true`] if the condition was met, [`false`] otherwise
435 #[doc(alias = "g_socket_condition_wait")]
436 fn condition_wait(
437 &self,
438 condition: glib::IOCondition,
439 cancellable: Option<&impl IsA<Cancellable>>,
440 ) -> Result<(), glib::Error> {
441 unsafe {
442 let mut error = std::ptr::null_mut();
443 let is_ok = ffi::g_socket_condition_wait(
444 self.as_ref().to_glib_none().0,
445 condition.into_glib(),
446 cancellable.map(|p| p.as_ref()).to_glib_none().0,
447 &mut error,
448 );
449 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
450 if error.is_null() {
451 Ok(())
452 } else {
453 Err(from_glib_full(error))
454 }
455 }
456 }
457
458 /// Connect the socket to the specified remote address.
459 ///
460 /// For connection oriented socket this generally means we attempt to make
461 /// a connection to the @address. For a connection-less socket it sets
462 /// the default address for g_socket_send() and discards all incoming datagrams
463 /// from other sources.
464 ///
465 /// Generally connection oriented sockets can only connect once, but
466 /// connection-less sockets can connect multiple times to change the
467 /// default address.
468 ///
469 /// If the connect call needs to do network I/O it will block, unless
470 /// non-blocking I/O is enabled. Then [`IOErrorEnum::Pending`][crate::IOErrorEnum::Pending] is returned
471 /// and the user can be notified of the connection finishing by waiting
472 /// for the G_IO_OUT condition. The result of the connection must then be
473 /// checked with g_socket_check_connect_result().
474 /// ## `address`
475 /// a #GSocketAddress specifying the remote address.
476 /// ## `cancellable`
477 /// a `GCancellable` or [`None`]
478 ///
479 /// # Returns
480 ///
481 /// [`true`] if connected, [`false`] on error.
482 #[doc(alias = "g_socket_connect")]
483 fn connect(
484 &self,
485 address: &impl IsA<SocketAddress>,
486 cancellable: Option<&impl IsA<Cancellable>>,
487 ) -> Result<(), glib::Error> {
488 unsafe {
489 let mut error = std::ptr::null_mut();
490 let is_ok = ffi::g_socket_connect(
491 self.as_ref().to_glib_none().0,
492 address.as_ref().to_glib_none().0,
493 cancellable.map(|p| p.as_ref()).to_glib_none().0,
494 &mut error,
495 );
496 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
497 if error.is_null() {
498 Ok(())
499 } else {
500 Err(from_glib_full(error))
501 }
502 }
503 }
504
505 /// Creates a #GSocketConnection subclass of the right type for
506 /// @self.
507 ///
508 /// # Returns
509 ///
510 /// a #GSocketConnection
511 #[doc(alias = "g_socket_connection_factory_create_connection")]
512 fn connection_factory_create_connection(&self) -> SocketConnection {
513 unsafe {
514 from_glib_full(ffi::g_socket_connection_factory_create_connection(
515 self.as_ref().to_glib_none().0,
516 ))
517 }
518 }
519
520 /// Get the amount of data pending in the OS input buffer, without blocking.
521 ///
522 /// If @self is a UDP or SCTP socket, this will return the size of
523 /// just the next packet, even if additional packets are buffered after
524 /// that one.
525 ///
526 /// Note that on Windows, this function is rather inefficient in the
527 /// UDP case, and so if you know any plausible upper bound on the size
528 /// of the incoming packet, it is better to just do a
529 /// g_socket_receive() with a buffer of that size, rather than calling
530 /// g_socket_get_available_bytes() first and then doing a receive of
531 /// exactly the right size.
532 ///
533 /// # Returns
534 ///
535 /// the number of bytes that can be read from the socket
536 /// without blocking or truncating, or -1 on error.
537 #[doc(alias = "g_socket_get_available_bytes")]
538 #[doc(alias = "get_available_bytes")]
539 fn available_bytes(&self) -> isize {
540 unsafe { ffi::g_socket_get_available_bytes(self.as_ref().to_glib_none().0) }
541 }
542
543 /// Gets the blocking mode of the socket. For details on blocking I/O,
544 /// see g_socket_set_blocking().
545 ///
546 /// # Returns
547 ///
548 /// [`true`] if blocking I/O is used, [`false`] otherwise.
549 #[doc(alias = "g_socket_get_blocking")]
550 #[doc(alias = "get_blocking")]
551 #[doc(alias = "blocking")]
552 fn is_blocking(&self) -> bool {
553 unsafe { from_glib(ffi::g_socket_get_blocking(self.as_ref().to_glib_none().0)) }
554 }
555
556 /// Gets the broadcast setting on @self; if [`true`],
557 /// it is possible to send packets to broadcast
558 /// addresses.
559 ///
560 /// # Returns
561 ///
562 /// the broadcast setting on @self
563 #[doc(alias = "g_socket_get_broadcast")]
564 #[doc(alias = "get_broadcast")]
565 #[doc(alias = "broadcast")]
566 fn is_broadcast(&self) -> bool {
567 unsafe { from_glib(ffi::g_socket_get_broadcast(self.as_ref().to_glib_none().0)) }
568 }
569
570 /// Returns the credentials of the foreign process connected to this
571 /// socket, if any (e.g. it is only supported for [`SocketFamily::Unix`][crate::SocketFamily::Unix]
572 /// sockets).
573 ///
574 /// If this operation isn't supported on the OS, the method fails with
575 /// the [`IOErrorEnum::NotSupported`][crate::IOErrorEnum::NotSupported] error. On Linux this is implemented
576 /// by reading the `SO_PEERCRED` option on the underlying socket.
577 ///
578 /// This method can be expected to be available on the following platforms:
579 ///
580 /// - Linux since GLib 2.26
581 /// - OpenBSD since GLib 2.30
582 /// - Solaris, Illumos and OpenSolaris since GLib 2.40
583 /// - NetBSD since GLib 2.42
584 /// - macOS, tvOS, iOS since GLib 2.66
585 ///
586 /// Other ways to obtain credentials from a foreign peer includes the
587 /// #GUnixCredentialsMessage type and
588 /// g_unix_connection_send_credentials() /
589 /// g_unix_connection_receive_credentials() functions.
590 ///
591 /// # Returns
592 ///
593 /// [`None`] if @error is set, otherwise a #GCredentials object
594 /// that must be freed with g_object_unref().
595 #[doc(alias = "g_socket_get_credentials")]
596 #[doc(alias = "get_credentials")]
597 fn credentials(&self) -> Result<Credentials, glib::Error> {
598 unsafe {
599 let mut error = std::ptr::null_mut();
600 let ret = ffi::g_socket_get_credentials(self.as_ref().to_glib_none().0, &mut error);
601 if error.is_null() {
602 Ok(from_glib_full(ret))
603 } else {
604 Err(from_glib_full(error))
605 }
606 }
607 }
608
609 /// Gets the socket family of the socket.
610 ///
611 /// # Returns
612 ///
613 /// a #GSocketFamily
614 #[doc(alias = "g_socket_get_family")]
615 #[doc(alias = "get_family")]
616 fn family(&self) -> SocketFamily {
617 unsafe { from_glib(ffi::g_socket_get_family(self.as_ref().to_glib_none().0)) }
618 }
619
620 /// Gets the keepalive mode of the socket. For details on this,
621 /// see g_socket_set_keepalive().
622 ///
623 /// # Returns
624 ///
625 /// [`true`] if keepalive is active, [`false`] otherwise.
626 #[doc(alias = "g_socket_get_keepalive")]
627 #[doc(alias = "get_keepalive")]
628 #[doc(alias = "keepalive")]
629 fn is_keepalive(&self) -> bool {
630 unsafe { from_glib(ffi::g_socket_get_keepalive(self.as_ref().to_glib_none().0)) }
631 }
632
633 /// Gets the listen backlog setting of the socket. For details on this,
634 /// see g_socket_set_listen_backlog().
635 ///
636 /// # Returns
637 ///
638 /// the maximum number of pending connections.
639 #[doc(alias = "g_socket_get_listen_backlog")]
640 #[doc(alias = "get_listen_backlog")]
641 #[doc(alias = "listen-backlog")]
642 fn listen_backlog(&self) -> i32 {
643 unsafe { ffi::g_socket_get_listen_backlog(self.as_ref().to_glib_none().0) }
644 }
645
646 /// Try to get the local address of a bound socket. This is only
647 /// useful if the socket has been bound to a local address,
648 /// either explicitly or implicitly when connecting.
649 ///
650 /// # Returns
651 ///
652 /// a #GSocketAddress or [`None`] on error.
653 /// Free the returned object with g_object_unref().
654 #[doc(alias = "g_socket_get_local_address")]
655 #[doc(alias = "get_local_address")]
656 #[doc(alias = "local-address")]
657 fn local_address(&self) -> Result<SocketAddress, glib::Error> {
658 unsafe {
659 let mut error = std::ptr::null_mut();
660 let ret = ffi::g_socket_get_local_address(self.as_ref().to_glib_none().0, &mut error);
661 if error.is_null() {
662 Ok(from_glib_full(ret))
663 } else {
664 Err(from_glib_full(error))
665 }
666 }
667 }
668
669 /// Gets the multicast loopback setting on @self; if [`true`] (the
670 /// default), outgoing multicast packets will be looped back to
671 /// multicast listeners on the same host.
672 ///
673 /// # Returns
674 ///
675 /// the multicast loopback setting on @self
676 #[doc(alias = "g_socket_get_multicast_loopback")]
677 #[doc(alias = "get_multicast_loopback")]
678 #[doc(alias = "multicast-loopback")]
679 fn is_multicast_loopback(&self) -> bool {
680 unsafe {
681 from_glib(ffi::g_socket_get_multicast_loopback(
682 self.as_ref().to_glib_none().0,
683 ))
684 }
685 }
686
687 /// Gets the multicast time-to-live setting on @self; see
688 /// g_socket_set_multicast_ttl() for more details.
689 ///
690 /// # Returns
691 ///
692 /// the multicast time-to-live setting on @self
693 #[doc(alias = "g_socket_get_multicast_ttl")]
694 #[doc(alias = "get_multicast_ttl")]
695 #[doc(alias = "multicast-ttl")]
696 fn multicast_ttl(&self) -> u32 {
697 unsafe { ffi::g_socket_get_multicast_ttl(self.as_ref().to_glib_none().0) }
698 }
699
700 /// `](networking.html)
701 /// header pulls in system headers that will define most of the
702 /// standard/portable socket options. For unusual socket protocols or
703 /// platform-dependent options, you may need to include additional
704 /// headers.
705 ///
706 /// Note that even for socket options that are a single byte in size,
707 /// @value is still a pointer to a #gint variable, not a #guchar;
708 /// g_socket_get_option() will handle the conversion internally.
709 /// ## `level`
710 /// the "API level" of the option (eg, `SOL_SOCKET`)
711 /// ## `optname`
712 /// the "name" of the option (eg, `SO_BROADCAST`)
713 ///
714 /// # Returns
715 ///
716 /// success or failure. On failure, @error will be set, and
717 /// the system error value (`errno` or WSAGetLastError()) will still
718 /// be set to the result of the getsockopt() call.
719 ///
720 /// ## `value`
721 /// return location for the option value
722 #[doc(alias = "g_socket_get_option")]
723 #[doc(alias = "get_option")]
724 fn option(&self, level: i32, optname: i32) -> Result<i32, glib::Error> {
725 unsafe {
726 let mut value = std::mem::MaybeUninit::uninit();
727 let mut error = std::ptr::null_mut();
728 let is_ok = ffi::g_socket_get_option(
729 self.as_ref().to_glib_none().0,
730 level,
731 optname,
732 value.as_mut_ptr(),
733 &mut error,
734 );
735 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
736 if error.is_null() {
737 Ok(value.assume_init())
738 } else {
739 Err(from_glib_full(error))
740 }
741 }
742 }
743
744 /// Gets the socket protocol id the socket was created with.
745 /// In case the protocol is unknown, -1 is returned.
746 ///
747 /// # Returns
748 ///
749 /// a protocol id, or -1 if unknown
750 #[doc(alias = "g_socket_get_protocol")]
751 #[doc(alias = "get_protocol")]
752 fn protocol(&self) -> SocketProtocol {
753 unsafe { from_glib(ffi::g_socket_get_protocol(self.as_ref().to_glib_none().0)) }
754 }
755
756 /// Try to get the remote address of a connected socket. This is only
757 /// useful for connection oriented sockets that have been connected.
758 ///
759 /// # Returns
760 ///
761 /// a #GSocketAddress or [`None`] on error.
762 /// Free the returned object with g_object_unref().
763 #[doc(alias = "g_socket_get_remote_address")]
764 #[doc(alias = "get_remote_address")]
765 #[doc(alias = "remote-address")]
766 fn remote_address(&self) -> Result<SocketAddress, glib::Error> {
767 unsafe {
768 let mut error = std::ptr::null_mut();
769 let ret = ffi::g_socket_get_remote_address(self.as_ref().to_glib_none().0, &mut error);
770 if error.is_null() {
771 Ok(from_glib_full(ret))
772 } else {
773 Err(from_glib_full(error))
774 }
775 }
776 }
777
778 /// Gets the socket type of the socket.
779 ///
780 /// # Returns
781 ///
782 /// a #GSocketType
783 #[doc(alias = "g_socket_get_socket_type")]
784 #[doc(alias = "get_socket_type")]
785 fn socket_type(&self) -> SocketType {
786 unsafe {
787 from_glib(ffi::g_socket_get_socket_type(
788 self.as_ref().to_glib_none().0,
789 ))
790 }
791 }
792
793 /// Gets the timeout setting of the socket. For details on this, see
794 /// g_socket_set_timeout().
795 ///
796 /// # Returns
797 ///
798 /// the timeout in seconds
799 #[doc(alias = "g_socket_get_timeout")]
800 #[doc(alias = "get_timeout")]
801 fn timeout(&self) -> u32 {
802 unsafe { ffi::g_socket_get_timeout(self.as_ref().to_glib_none().0) }
803 }
804
805 /// Gets the unicast time-to-live setting on @self; see
806 /// g_socket_set_ttl() for more details.
807 ///
808 /// # Returns
809 ///
810 /// the time-to-live setting on @self
811 #[doc(alias = "g_socket_get_ttl")]
812 #[doc(alias = "get_ttl")]
813 fn ttl(&self) -> u32 {
814 unsafe { ffi::g_socket_get_ttl(self.as_ref().to_glib_none().0) }
815 }
816
817 /// Checks whether a socket is closed.
818 ///
819 /// # Returns
820 ///
821 /// [`true`] if socket is closed, [`false`] otherwise
822 #[doc(alias = "g_socket_is_closed")]
823 fn is_closed(&self) -> bool {
824 unsafe { from_glib(ffi::g_socket_is_closed(self.as_ref().to_glib_none().0)) }
825 }
826
827 /// Check whether the socket is connected. This is only useful for
828 /// connection-oriented sockets.
829 ///
830 /// If using g_socket_shutdown(), this function will return [`true`] until the
831 /// socket has been shut down for reading and writing. If you do a non-blocking
832 /// connect, this function will not return [`true`] until after you call
833 /// g_socket_check_connect_result().
834 ///
835 /// # Returns
836 ///
837 /// [`true`] if socket is connected, [`false`] otherwise.
838 #[doc(alias = "g_socket_is_connected")]
839 fn is_connected(&self) -> bool {
840 unsafe { from_glib(ffi::g_socket_is_connected(self.as_ref().to_glib_none().0)) }
841 }
842
843 /// Registers @self to receive multicast messages sent to @group.
844 /// @self must be a [`SocketType::Datagram`][crate::SocketType::Datagram] socket, and must have
845 /// been bound to an appropriate interface and port with
846 /// g_socket_bind().
847 ///
848 /// If @iface is [`None`], the system will automatically pick an interface
849 /// to bind to based on @group.
850 ///
851 /// If @source_specific is [`true`], source-specific multicast as defined
852 /// in RFC 4604 is used. Note that on older platforms this may fail
853 /// with a [`IOErrorEnum::NotSupported`][crate::IOErrorEnum::NotSupported] error.
854 ///
855 /// To bind to a given source-specific multicast address, use
856 /// g_socket_join_multicast_group_ssm() instead.
857 /// ## `group`
858 /// a #GInetAddress specifying the group address to join.
859 /// ## `source_specific`
860 /// [`true`] if source-specific multicast should be used
861 /// ## `iface`
862 /// Name of the interface to use, or [`None`]
863 ///
864 /// # Returns
865 ///
866 /// [`true`] on success, [`false`] on error.
867 #[doc(alias = "g_socket_join_multicast_group")]
868 fn join_multicast_group(
869 &self,
870 group: &impl IsA<InetAddress>,
871 source_specific: bool,
872 iface: Option<&str>,
873 ) -> Result<(), glib::Error> {
874 unsafe {
875 let mut error = std::ptr::null_mut();
876 let is_ok = ffi::g_socket_join_multicast_group(
877 self.as_ref().to_glib_none().0,
878 group.as_ref().to_glib_none().0,
879 source_specific.into_glib(),
880 iface.to_glib_none().0,
881 &mut error,
882 );
883 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
884 if error.is_null() {
885 Ok(())
886 } else {
887 Err(from_glib_full(error))
888 }
889 }
890 }
891
892 /// Registers @self to receive multicast messages sent to @group.
893 /// @self must be a [`SocketType::Datagram`][crate::SocketType::Datagram] socket, and must have
894 /// been bound to an appropriate interface and port with
895 /// g_socket_bind().
896 ///
897 /// If @iface is [`None`], the system will automatically pick an interface
898 /// to bind to based on @group.
899 ///
900 /// If @source_specific is not [`None`], use source-specific multicast as
901 /// defined in RFC 4604. Note that on older platforms this may fail
902 /// with a [`IOErrorEnum::NotSupported`][crate::IOErrorEnum::NotSupported] error.
903 ///
904 /// Note that this function can be called multiple times for the same
905 /// @group with different @source_specific in order to receive multicast
906 /// packets from more than one source.
907 /// ## `group`
908 /// a #GInetAddress specifying the group address to join.
909 /// ## `source_specific`
910 /// a #GInetAddress specifying the
911 /// source-specific multicast address or [`None`] to ignore.
912 /// ## `iface`
913 /// Name of the interface to use, or [`None`]
914 ///
915 /// # Returns
916 ///
917 /// [`true`] on success, [`false`] on error.
918 #[doc(alias = "g_socket_join_multicast_group_ssm")]
919 fn join_multicast_group_ssm(
920 &self,
921 group: &impl IsA<InetAddress>,
922 source_specific: Option<&impl IsA<InetAddress>>,
923 iface: Option<&str>,
924 ) -> Result<(), glib::Error> {
925 unsafe {
926 let mut error = std::ptr::null_mut();
927 let is_ok = ffi::g_socket_join_multicast_group_ssm(
928 self.as_ref().to_glib_none().0,
929 group.as_ref().to_glib_none().0,
930 source_specific.map(|p| p.as_ref()).to_glib_none().0,
931 iface.to_glib_none().0,
932 &mut error,
933 );
934 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
935 if error.is_null() {
936 Ok(())
937 } else {
938 Err(from_glib_full(error))
939 }
940 }
941 }
942
943 /// Removes @self from the multicast group defined by @group, @iface,
944 /// and @source_specific (which must all have the same values they had
945 /// when you joined the group).
946 ///
947 /// @self remains bound to its address and port, and can still receive
948 /// unicast messages after calling this.
949 ///
950 /// To unbind to a given source-specific multicast address, use
951 /// g_socket_leave_multicast_group_ssm() instead.
952 /// ## `group`
953 /// a #GInetAddress specifying the group address to leave.
954 /// ## `source_specific`
955 /// [`true`] if source-specific multicast was used
956 /// ## `iface`
957 /// Interface used
958 ///
959 /// # Returns
960 ///
961 /// [`true`] on success, [`false`] on error.
962 #[doc(alias = "g_socket_leave_multicast_group")]
963 fn leave_multicast_group(
964 &self,
965 group: &impl IsA<InetAddress>,
966 source_specific: bool,
967 iface: Option<&str>,
968 ) -> Result<(), glib::Error> {
969 unsafe {
970 let mut error = std::ptr::null_mut();
971 let is_ok = ffi::g_socket_leave_multicast_group(
972 self.as_ref().to_glib_none().0,
973 group.as_ref().to_glib_none().0,
974 source_specific.into_glib(),
975 iface.to_glib_none().0,
976 &mut error,
977 );
978 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
979 if error.is_null() {
980 Ok(())
981 } else {
982 Err(from_glib_full(error))
983 }
984 }
985 }
986
987 /// Removes @self from the multicast group defined by @group, @iface,
988 /// and @source_specific (which must all have the same values they had
989 /// when you joined the group).
990 ///
991 /// @self remains bound to its address and port, and can still receive
992 /// unicast messages after calling this.
993 /// ## `group`
994 /// a #GInetAddress specifying the group address to leave.
995 /// ## `source_specific`
996 /// a #GInetAddress specifying the
997 /// source-specific multicast address or [`None`] to ignore.
998 /// ## `iface`
999 /// Name of the interface to use, or [`None`]
1000 ///
1001 /// # Returns
1002 ///
1003 /// [`true`] on success, [`false`] on error.
1004 #[doc(alias = "g_socket_leave_multicast_group_ssm")]
1005 fn leave_multicast_group_ssm(
1006 &self,
1007 group: &impl IsA<InetAddress>,
1008 source_specific: Option<&impl IsA<InetAddress>>,
1009 iface: Option<&str>,
1010 ) -> Result<(), glib::Error> {
1011 unsafe {
1012 let mut error = std::ptr::null_mut();
1013 let is_ok = ffi::g_socket_leave_multicast_group_ssm(
1014 self.as_ref().to_glib_none().0,
1015 group.as_ref().to_glib_none().0,
1016 source_specific.map(|p| p.as_ref()).to_glib_none().0,
1017 iface.to_glib_none().0,
1018 &mut error,
1019 );
1020 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1021 if error.is_null() {
1022 Ok(())
1023 } else {
1024 Err(from_glib_full(error))
1025 }
1026 }
1027 }
1028
1029 /// Marks the socket as a server socket, i.e. a socket that is used
1030 /// to accept incoming requests using g_socket_accept().
1031 ///
1032 /// Before calling this the socket must be bound to a local address using
1033 /// g_socket_bind().
1034 ///
1035 /// To set the maximum amount of outstanding clients, use
1036 /// g_socket_set_listen_backlog().
1037 ///
1038 /// # Returns
1039 ///
1040 /// [`true`] on success, [`false`] on error.
1041 #[doc(alias = "g_socket_listen")]
1042 fn listen(&self) -> Result<(), glib::Error> {
1043 unsafe {
1044 let mut error = std::ptr::null_mut();
1045 let is_ok = ffi::g_socket_listen(self.as_ref().to_glib_none().0, &mut error);
1046 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1047 if error.is_null() {
1048 Ok(())
1049 } else {
1050 Err(from_glib_full(error))
1051 }
1052 }
1053 }
1054
1055 /// Sets the blocking mode of the socket. In blocking mode
1056 /// all operations (which don’t take an explicit blocking parameter) block until
1057 /// they succeed or there is an error. In
1058 /// non-blocking mode all functions return results immediately or
1059 /// with a [`IOErrorEnum::WouldBlock`][crate::IOErrorEnum::WouldBlock] error.
1060 ///
1061 /// All sockets are created in blocking mode. However, note that the
1062 /// platform level socket is always non-blocking, and blocking mode
1063 /// is a GSocket level feature.
1064 /// ## `blocking`
1065 /// Whether to use blocking I/O or not.
1066 #[doc(alias = "g_socket_set_blocking")]
1067 #[doc(alias = "blocking")]
1068 fn set_blocking(&self, blocking: bool) {
1069 unsafe {
1070 ffi::g_socket_set_blocking(self.as_ref().to_glib_none().0, blocking.into_glib());
1071 }
1072 }
1073
1074 /// Sets whether @self should allow sending to broadcast addresses.
1075 /// This is [`false`] by default.
1076 /// ## `broadcast`
1077 /// whether @self should allow sending to broadcast
1078 /// addresses
1079 #[doc(alias = "g_socket_set_broadcast")]
1080 #[doc(alias = "broadcast")]
1081 fn set_broadcast(&self, broadcast: bool) {
1082 unsafe {
1083 ffi::g_socket_set_broadcast(self.as_ref().to_glib_none().0, broadcast.into_glib());
1084 }
1085 }
1086
1087 /// Sets or unsets the `SO_KEEPALIVE` flag on the underlying socket. When
1088 /// this flag is set on a socket, the system will attempt to verify that the
1089 /// remote socket endpoint is still present if a sufficiently long period of
1090 /// time passes with no data being exchanged. If the system is unable to
1091 /// verify the presence of the remote endpoint, it will automatically close
1092 /// the connection.
1093 ///
1094 /// This option is only functional on certain kinds of sockets. (Notably,
1095 /// [`SocketProtocol::Tcp`][crate::SocketProtocol::Tcp] sockets.)
1096 ///
1097 /// The exact time between pings is system- and protocol-dependent, but will
1098 /// normally be at least two hours. Most commonly, you would set this flag
1099 /// on a server socket if you want to allow clients to remain idle for long
1100 /// periods of time, but also want to ensure that connections are eventually
1101 /// garbage-collected if clients crash or become unreachable.
1102 /// ## `keepalive`
1103 /// Value for the keepalive flag
1104 #[doc(alias = "g_socket_set_keepalive")]
1105 #[doc(alias = "keepalive")]
1106 fn set_keepalive(&self, keepalive: bool) {
1107 unsafe {
1108 ffi::g_socket_set_keepalive(self.as_ref().to_glib_none().0, keepalive.into_glib());
1109 }
1110 }
1111
1112 /// Sets the maximum number of outstanding connections allowed
1113 /// when listening on this socket. If more clients than this are
1114 /// connecting to the socket and the application is not handling them
1115 /// on time then the new connections will be refused.
1116 ///
1117 /// Note that this must be called before g_socket_listen() and has no
1118 /// effect if called after that.
1119 /// ## `backlog`
1120 /// the maximum number of pending connections.
1121 #[doc(alias = "g_socket_set_listen_backlog")]
1122 #[doc(alias = "listen-backlog")]
1123 fn set_listen_backlog(&self, backlog: i32) {
1124 unsafe {
1125 ffi::g_socket_set_listen_backlog(self.as_ref().to_glib_none().0, backlog);
1126 }
1127 }
1128
1129 /// Sets whether outgoing multicast packets will be received by sockets
1130 /// listening on that multicast address on the same host. This is [`true`]
1131 /// by default.
1132 /// ## `loopback`
1133 /// whether @self should receive messages sent to its
1134 /// multicast groups from the local host
1135 #[doc(alias = "g_socket_set_multicast_loopback")]
1136 #[doc(alias = "multicast-loopback")]
1137 fn set_multicast_loopback(&self, loopback: bool) {
1138 unsafe {
1139 ffi::g_socket_set_multicast_loopback(
1140 self.as_ref().to_glib_none().0,
1141 loopback.into_glib(),
1142 );
1143 }
1144 }
1145
1146 /// Sets the time-to-live for outgoing multicast datagrams on @self.
1147 /// By default, this is 1, meaning that multicast packets will not leave
1148 /// the local network.
1149 /// ## `ttl`
1150 /// the time-to-live value for all multicast datagrams on @self
1151 #[doc(alias = "g_socket_set_multicast_ttl")]
1152 #[doc(alias = "multicast-ttl")]
1153 fn set_multicast_ttl(&self, ttl: u32) {
1154 unsafe {
1155 ffi::g_socket_set_multicast_ttl(self.as_ref().to_glib_none().0, ttl);
1156 }
1157 }
1158
1159 /// `](networking.html)
1160 /// header pulls in system headers that will define most of the
1161 /// standard/portable socket options. For unusual socket protocols or
1162 /// platform-dependent options, you may need to include additional
1163 /// headers.
1164 /// ## `level`
1165 /// the "API level" of the option (eg, `SOL_SOCKET`)
1166 /// ## `optname`
1167 /// the "name" of the option (eg, `SO_BROADCAST`)
1168 /// ## `value`
1169 /// the value to set the option to
1170 ///
1171 /// # Returns
1172 ///
1173 /// success or failure. On failure, @error will be set, and
1174 /// the system error value (`errno` or WSAGetLastError()) will still
1175 /// be set to the result of the setsockopt() call.
1176 #[doc(alias = "g_socket_set_option")]
1177 fn set_option(&self, level: i32, optname: i32, value: i32) -> Result<(), glib::Error> {
1178 unsafe {
1179 let mut error = std::ptr::null_mut();
1180 let is_ok = ffi::g_socket_set_option(
1181 self.as_ref().to_glib_none().0,
1182 level,
1183 optname,
1184 value,
1185 &mut error,
1186 );
1187 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1188 if error.is_null() {
1189 Ok(())
1190 } else {
1191 Err(from_glib_full(error))
1192 }
1193 }
1194 }
1195
1196 /// Sets the time in seconds after which I/O operations on @self will
1197 /// time out if they have not yet completed.
1198 ///
1199 /// On a blocking socket, this means that any blocking #GSocket
1200 /// operation will time out after @timeout seconds of inactivity,
1201 /// returning [`IOErrorEnum::TimedOut`][crate::IOErrorEnum::TimedOut].
1202 ///
1203 /// On a non-blocking socket, calls to g_socket_condition_wait() will
1204 /// also fail with [`IOErrorEnum::TimedOut`][crate::IOErrorEnum::TimedOut] after the given time. Sources
1205 /// created with g_socket_create_source() will trigger after
1206 /// @timeout seconds of inactivity, with the requested condition
1207 /// set, at which point calling g_socket_receive(), g_socket_send(),
1208 /// g_socket_check_connect_result(), etc, will fail with
1209 /// [`IOErrorEnum::TimedOut`][crate::IOErrorEnum::TimedOut].
1210 ///
1211 /// If @timeout is 0 (the default), operations will never time out
1212 /// on their own.
1213 ///
1214 /// Note that if an I/O operation is interrupted by a signal, this may
1215 /// cause the timeout to be reset.
1216 /// ## `timeout`
1217 /// the timeout for @self, in seconds, or 0 for none
1218 #[doc(alias = "g_socket_set_timeout")]
1219 #[doc(alias = "timeout")]
1220 fn set_timeout(&self, timeout: u32) {
1221 unsafe {
1222 ffi::g_socket_set_timeout(self.as_ref().to_glib_none().0, timeout);
1223 }
1224 }
1225
1226 /// Sets the time-to-live for outgoing unicast packets on @self.
1227 /// By default the platform-specific default value is used.
1228 /// ## `ttl`
1229 /// the time-to-live value for all unicast packets on @self
1230 #[doc(alias = "g_socket_set_ttl")]
1231 #[doc(alias = "ttl")]
1232 fn set_ttl(&self, ttl: u32) {
1233 unsafe {
1234 ffi::g_socket_set_ttl(self.as_ref().to_glib_none().0, ttl);
1235 }
1236 }
1237
1238 /// Shut down part or all of a full-duplex connection.
1239 ///
1240 /// If @shutdown_read is [`true`] then the receiving side of the connection
1241 /// is shut down, and further reading is disallowed.
1242 ///
1243 /// If @shutdown_write is [`true`] then the sending side of the connection
1244 /// is shut down, and further writing is disallowed.
1245 ///
1246 /// It is allowed for both @shutdown_read and @shutdown_write to be [`true`].
1247 ///
1248 /// One example where it is useful to shut down only one side of a connection is
1249 /// graceful disconnect for TCP connections where you close the sending side,
1250 /// then wait for the other side to close the connection, thus ensuring that the
1251 /// other side saw all sent data.
1252 /// ## `shutdown_read`
1253 /// whether to shut down the read side
1254 /// ## `shutdown_write`
1255 /// whether to shut down the write side
1256 ///
1257 /// # Returns
1258 ///
1259 /// [`true`] on success, [`false`] on error
1260 #[doc(alias = "g_socket_shutdown")]
1261 fn shutdown(&self, shutdown_read: bool, shutdown_write: bool) -> Result<(), glib::Error> {
1262 unsafe {
1263 let mut error = std::ptr::null_mut();
1264 let is_ok = ffi::g_socket_shutdown(
1265 self.as_ref().to_glib_none().0,
1266 shutdown_read.into_glib(),
1267 shutdown_write.into_glib(),
1268 &mut error,
1269 );
1270 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
1271 if error.is_null() {
1272 Ok(())
1273 } else {
1274 Err(from_glib_full(error))
1275 }
1276 }
1277 }
1278
1279 /// Checks if a socket is capable of speaking IPv4.
1280 ///
1281 /// IPv4 sockets are capable of speaking IPv4. On some operating systems
1282 /// and under some combinations of circumstances IPv6 sockets are also
1283 /// capable of speaking IPv4. See RFC 3493 section 3.7 for more
1284 /// information.
1285 ///
1286 /// No other types of sockets are currently considered as being capable
1287 /// of speaking IPv4.
1288 ///
1289 /// # Returns
1290 ///
1291 /// [`true`] if this socket can be used with IPv4.
1292 #[doc(alias = "g_socket_speaks_ipv4")]
1293 fn speaks_ipv4(&self) -> bool {
1294 unsafe { from_glib(ffi::g_socket_speaks_ipv4(self.as_ref().to_glib_none().0)) }
1295 }
1296
1297 /// The socket’s type.
1298 #[doc(alias = "type")]
1299 fn type_(&self) -> SocketType {
1300 ObjectExt::property(self.as_ref(), "type")
1301 }
1302
1303 #[doc(alias = "blocking")]
1304 fn connect_blocking_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1305 unsafe extern "C" fn notify_blocking_trampoline<P: IsA<Socket>, F: Fn(&P) + 'static>(
1306 this: *mut ffi::GSocket,
1307 _param_spec: glib::ffi::gpointer,
1308 f: glib::ffi::gpointer,
1309 ) {
1310 unsafe {
1311 let f: &F = &*(f as *const F);
1312 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1313 }
1314 }
1315 unsafe {
1316 let f: Box_<F> = Box_::new(f);
1317 connect_raw(
1318 self.as_ptr() as *mut _,
1319 c"notify::blocking".as_ptr(),
1320 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1321 notify_blocking_trampoline::<Self, F> as *const (),
1322 )),
1323 Box_::into_raw(f),
1324 )
1325 }
1326 }
1327
1328 #[doc(alias = "broadcast")]
1329 fn connect_broadcast_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1330 unsafe extern "C" fn notify_broadcast_trampoline<P: IsA<Socket>, F: Fn(&P) + 'static>(
1331 this: *mut ffi::GSocket,
1332 _param_spec: glib::ffi::gpointer,
1333 f: glib::ffi::gpointer,
1334 ) {
1335 unsafe {
1336 let f: &F = &*(f as *const F);
1337 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1338 }
1339 }
1340 unsafe {
1341 let f: Box_<F> = Box_::new(f);
1342 connect_raw(
1343 self.as_ptr() as *mut _,
1344 c"notify::broadcast".as_ptr(),
1345 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1346 notify_broadcast_trampoline::<Self, F> as *const (),
1347 )),
1348 Box_::into_raw(f),
1349 )
1350 }
1351 }
1352
1353 #[doc(alias = "keepalive")]
1354 fn connect_keepalive_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1355 unsafe extern "C" fn notify_keepalive_trampoline<P: IsA<Socket>, F: Fn(&P) + 'static>(
1356 this: *mut ffi::GSocket,
1357 _param_spec: glib::ffi::gpointer,
1358 f: glib::ffi::gpointer,
1359 ) {
1360 unsafe {
1361 let f: &F = &*(f as *const F);
1362 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1363 }
1364 }
1365 unsafe {
1366 let f: Box_<F> = Box_::new(f);
1367 connect_raw(
1368 self.as_ptr() as *mut _,
1369 c"notify::keepalive".as_ptr(),
1370 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1371 notify_keepalive_trampoline::<Self, F> as *const (),
1372 )),
1373 Box_::into_raw(f),
1374 )
1375 }
1376 }
1377
1378 #[doc(alias = "listen-backlog")]
1379 fn connect_listen_backlog_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1380 unsafe extern "C" fn notify_listen_backlog_trampoline<
1381 P: IsA<Socket>,
1382 F: Fn(&P) + 'static,
1383 >(
1384 this: *mut ffi::GSocket,
1385 _param_spec: glib::ffi::gpointer,
1386 f: glib::ffi::gpointer,
1387 ) {
1388 unsafe {
1389 let f: &F = &*(f as *const F);
1390 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1391 }
1392 }
1393 unsafe {
1394 let f: Box_<F> = Box_::new(f);
1395 connect_raw(
1396 self.as_ptr() as *mut _,
1397 c"notify::listen-backlog".as_ptr(),
1398 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1399 notify_listen_backlog_trampoline::<Self, F> as *const (),
1400 )),
1401 Box_::into_raw(f),
1402 )
1403 }
1404 }
1405
1406 #[doc(alias = "local-address")]
1407 fn connect_local_address_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1408 unsafe extern "C" fn notify_local_address_trampoline<
1409 P: IsA<Socket>,
1410 F: Fn(&P) + 'static,
1411 >(
1412 this: *mut ffi::GSocket,
1413 _param_spec: glib::ffi::gpointer,
1414 f: glib::ffi::gpointer,
1415 ) {
1416 unsafe {
1417 let f: &F = &*(f as *const F);
1418 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1419 }
1420 }
1421 unsafe {
1422 let f: Box_<F> = Box_::new(f);
1423 connect_raw(
1424 self.as_ptr() as *mut _,
1425 c"notify::local-address".as_ptr(),
1426 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1427 notify_local_address_trampoline::<Self, F> as *const (),
1428 )),
1429 Box_::into_raw(f),
1430 )
1431 }
1432 }
1433
1434 #[doc(alias = "multicast-loopback")]
1435 fn connect_multicast_loopback_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1436 unsafe extern "C" fn notify_multicast_loopback_trampoline<
1437 P: IsA<Socket>,
1438 F: Fn(&P) + 'static,
1439 >(
1440 this: *mut ffi::GSocket,
1441 _param_spec: glib::ffi::gpointer,
1442 f: glib::ffi::gpointer,
1443 ) {
1444 unsafe {
1445 let f: &F = &*(f as *const F);
1446 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1447 }
1448 }
1449 unsafe {
1450 let f: Box_<F> = Box_::new(f);
1451 connect_raw(
1452 self.as_ptr() as *mut _,
1453 c"notify::multicast-loopback".as_ptr(),
1454 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1455 notify_multicast_loopback_trampoline::<Self, F> as *const (),
1456 )),
1457 Box_::into_raw(f),
1458 )
1459 }
1460 }
1461
1462 #[doc(alias = "multicast-ttl")]
1463 fn connect_multicast_ttl_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1464 unsafe extern "C" fn notify_multicast_ttl_trampoline<
1465 P: IsA<Socket>,
1466 F: Fn(&P) + 'static,
1467 >(
1468 this: *mut ffi::GSocket,
1469 _param_spec: glib::ffi::gpointer,
1470 f: glib::ffi::gpointer,
1471 ) {
1472 unsafe {
1473 let f: &F = &*(f as *const F);
1474 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1475 }
1476 }
1477 unsafe {
1478 let f: Box_<F> = Box_::new(f);
1479 connect_raw(
1480 self.as_ptr() as *mut _,
1481 c"notify::multicast-ttl".as_ptr(),
1482 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1483 notify_multicast_ttl_trampoline::<Self, F> as *const (),
1484 )),
1485 Box_::into_raw(f),
1486 )
1487 }
1488 }
1489
1490 #[doc(alias = "remote-address")]
1491 fn connect_remote_address_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1492 unsafe extern "C" fn notify_remote_address_trampoline<
1493 P: IsA<Socket>,
1494 F: Fn(&P) + 'static,
1495 >(
1496 this: *mut ffi::GSocket,
1497 _param_spec: glib::ffi::gpointer,
1498 f: glib::ffi::gpointer,
1499 ) {
1500 unsafe {
1501 let f: &F = &*(f as *const F);
1502 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1503 }
1504 }
1505 unsafe {
1506 let f: Box_<F> = Box_::new(f);
1507 connect_raw(
1508 self.as_ptr() as *mut _,
1509 c"notify::remote-address".as_ptr(),
1510 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1511 notify_remote_address_trampoline::<Self, F> as *const (),
1512 )),
1513 Box_::into_raw(f),
1514 )
1515 }
1516 }
1517
1518 #[doc(alias = "timeout")]
1519 fn connect_timeout_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1520 unsafe extern "C" fn notify_timeout_trampoline<P: IsA<Socket>, F: Fn(&P) + 'static>(
1521 this: *mut ffi::GSocket,
1522 _param_spec: glib::ffi::gpointer,
1523 f: glib::ffi::gpointer,
1524 ) {
1525 unsafe {
1526 let f: &F = &*(f as *const F);
1527 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1528 }
1529 }
1530 unsafe {
1531 let f: Box_<F> = Box_::new(f);
1532 connect_raw(
1533 self.as_ptr() as *mut _,
1534 c"notify::timeout".as_ptr(),
1535 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1536 notify_timeout_trampoline::<Self, F> as *const (),
1537 )),
1538 Box_::into_raw(f),
1539 )
1540 }
1541 }
1542
1543 #[doc(alias = "ttl")]
1544 fn connect_ttl_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1545 unsafe extern "C" fn notify_ttl_trampoline<P: IsA<Socket>, F: Fn(&P) + 'static>(
1546 this: *mut ffi::GSocket,
1547 _param_spec: glib::ffi::gpointer,
1548 f: glib::ffi::gpointer,
1549 ) {
1550 unsafe {
1551 let f: &F = &*(f as *const F);
1552 f(Socket::from_glib_borrow(this).unsafe_cast_ref())
1553 }
1554 }
1555 unsafe {
1556 let f: Box_<F> = Box_::new(f);
1557 connect_raw(
1558 self.as_ptr() as *mut _,
1559 c"notify::ttl".as_ptr(),
1560 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1561 notify_ttl_trampoline::<Self, F> as *const (),
1562 )),
1563 Box_::into_raw(f),
1564 )
1565 }
1566 }
1567}
1568
1569impl<O: IsA<Socket>> SocketExt for O {}