1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT
use crate::{
AsyncResult, Cancellable, Socket, SocketAddress, SocketConnection, SocketListenerEvent,
SocketProtocol, SocketType,
};
use glib::{
prelude::*,
signal::{connect_raw, SignalHandlerId},
translate::*,
};
use std::{boxed::Box as Box_, pin::Pin};
glib::wrapper! {
/// A `GSocketListener` is an object that keeps track of a set
/// of server sockets and helps you accept sockets from any of the
/// socket, either sync or async.
///
/// Add addresses and ports to listen on using
/// [`SocketListenerExt::add_address()`][crate::prelude::SocketListenerExt::add_address()] and
/// [`SocketListenerExt::add_inet_port()`][crate::prelude::SocketListenerExt::add_inet_port()]. These will be listened on until
/// [`SocketListenerExt::close()`][crate::prelude::SocketListenerExt::close()] is called. Dropping your final reference to
/// the `GSocketListener` will not cause [`SocketListenerExt::close()`][crate::prelude::SocketListenerExt::close()] to be
/// called implicitly, as some references to the `GSocketListener` may be held
/// internally.
///
/// If you want to implement a network server, also look at
/// [`SocketService`][crate::SocketService] and [`ThreadedSocketService`][crate::ThreadedSocketService] which are
/// subclasses of `GSocketListener` that make this even easier.
///
/// ## Properties
///
///
/// #### `listen-backlog`
/// The number of outstanding connections in the listen queue.
///
/// Readable | Writeable | Construct
///
/// ## Signals
///
///
/// #### `event`
/// Emitted when @listener's activity on @socket changes state.
/// Note that when @listener is used to listen on both IPv4 and
/// IPv6, a separate set of signals will be emitted for each, and
/// the order they happen in is undefined.
///
///
///
/// # Implements
///
/// [`SocketListenerExt`][trait@crate::prelude::SocketListenerExt], [`trait@glib::ObjectExt`]
#[doc(alias = "GSocketListener")]
pub struct SocketListener(Object<ffi::GSocketListener, ffi::GSocketListenerClass>);
match fn {
type_ => || ffi::g_socket_listener_get_type(),
}
}
impl SocketListener {
pub const NONE: Option<&'static SocketListener> = None;
/// Creates a new #GSocketListener with no sockets to listen for.
/// New listeners can be added with e.g. g_socket_listener_add_address()
/// or g_socket_listener_add_inet_port().
///
/// # Returns
///
/// a new #GSocketListener.
#[doc(alias = "g_socket_listener_new")]
pub fn new() -> SocketListener {
unsafe { from_glib_full(ffi::g_socket_listener_new()) }
}
}
impl Default for SocketListener {
fn default() -> Self {
Self::new()
}
}
mod sealed {
pub trait Sealed {}
impl<T: super::IsA<super::SocketListener>> Sealed for T {}
}
/// Trait containing all [`struct@SocketListener`] methods.
///
/// # Implementors
///
/// [`SocketListener`][struct@crate::SocketListener], [`SocketService`][struct@crate::SocketService]
pub trait SocketListenerExt: IsA<SocketListener> + sealed::Sealed + 'static {
/// Blocks waiting for a client to connect to any of the sockets added
/// to the listener. Returns a #GSocketConnection for the socket that was
/// accepted.
///
/// If @source_object is not [`None`] it will be filled out with the source
/// object specified when the corresponding socket or address was added
/// to the listener.
///
/// If @cancellable is not [`None`], then the operation can be cancelled by
/// triggering the cancellable object from another thread. If the operation
/// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned.
/// ## `cancellable`
/// optional #GCancellable object, [`None`] to ignore.
///
/// # Returns
///
/// a #GSocketConnection on success, [`None`] on error.
///
/// ## `source_object`
/// location where #GObject pointer will be stored, or [`None`]
#[doc(alias = "g_socket_listener_accept")]
fn accept(
&self,
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<(SocketConnection, Option<glib::Object>), glib::Error> {
unsafe {
let mut source_object = std::ptr::null_mut();
let mut error = std::ptr::null_mut();
let ret = ffi::g_socket_listener_accept(
self.as_ref().to_glib_none().0,
&mut source_object,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok((from_glib_full(ret), from_glib_none(source_object)))
} else {
Err(from_glib_full(error))
}
}
}
/// This is the asynchronous version of g_socket_listener_accept().
///
/// When the operation is finished @callback will be
/// called. You can then call g_socket_listener_accept_finish()
/// to get the result of the operation.
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// a #GAsyncReadyCallback
#[doc(alias = "g_socket_listener_accept_async")]
fn accept_async<
P: FnOnce(Result<(SocketConnection, Option<glib::Object>), glib::Error>) + 'static,
>(
&self,
cancellable: Option<&impl IsA<Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn accept_async_trampoline<
P: FnOnce(Result<(SocketConnection, Option<glib::Object>), glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut crate::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = std::ptr::null_mut();
let mut source_object = std::ptr::null_mut();
let ret = ffi::g_socket_listener_accept_finish(
_source_object as *mut _,
res,
&mut source_object,
&mut error,
);
let result = if error.is_null() {
Ok((from_glib_full(ret), from_glib_none(source_object)))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = accept_async_trampoline::<P>;
unsafe {
ffi::g_socket_listener_accept_async(
self.as_ref().to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn accept_future(
&self,
) -> Pin<
Box_<
dyn std::future::Future<
Output = Result<(SocketConnection, Option<glib::Object>), glib::Error>,
> + 'static,
>,
> {
Box_::pin(crate::GioFuture::new(
self,
move |obj, cancellable, send| {
obj.accept_async(Some(cancellable), move |res| {
send.resolve(res);
});
},
))
}
/// Blocks waiting for a client to connect to any of the sockets added
/// to the listener. Returns the #GSocket that was accepted.
///
/// If you want to accept the high-level #GSocketConnection, not a #GSocket,
/// which is often the case, then you should use g_socket_listener_accept()
/// instead.
///
/// If @source_object is not [`None`] it will be filled out with the source
/// object specified when the corresponding socket or address was added
/// to the listener.
///
/// If @cancellable is not [`None`], then the operation can be cancelled by
/// triggering the cancellable object from another thread. If the operation
/// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned.
/// ## `cancellable`
/// optional #GCancellable object, [`None`] to ignore.
///
/// # Returns
///
/// a #GSocket on success, [`None`] on error.
///
/// ## `source_object`
/// location where #GObject pointer will be stored, or [`None`].
#[doc(alias = "g_socket_listener_accept_socket")]
fn accept_socket(
&self,
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<(Socket, Option<glib::Object>), glib::Error> {
unsafe {
let mut source_object = std::ptr::null_mut();
let mut error = std::ptr::null_mut();
let ret = ffi::g_socket_listener_accept_socket(
self.as_ref().to_glib_none().0,
&mut source_object,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok((from_glib_full(ret), from_glib_none(source_object)))
} else {
Err(from_glib_full(error))
}
}
}
/// This is the asynchronous version of g_socket_listener_accept_socket().
///
/// When the operation is finished @callback will be
/// called. You can then call g_socket_listener_accept_socket_finish()
/// to get the result of the operation.
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// a #GAsyncReadyCallback
#[doc(alias = "g_socket_listener_accept_socket_async")]
fn accept_socket_async<
P: FnOnce(Result<(Socket, Option<glib::Object>), glib::Error>) + 'static,
>(
&self,
cancellable: Option<&impl IsA<Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn accept_socket_async_trampoline<
P: FnOnce(Result<(Socket, Option<glib::Object>), glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut crate::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = std::ptr::null_mut();
let mut source_object = std::ptr::null_mut();
let ret = ffi::g_socket_listener_accept_socket_finish(
_source_object as *mut _,
res,
&mut source_object,
&mut error,
);
let result = if error.is_null() {
Ok((from_glib_full(ret), from_glib_none(source_object)))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = accept_socket_async_trampoline::<P>;
unsafe {
ffi::g_socket_listener_accept_socket_async(
self.as_ref().to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn accept_socket_future(
&self,
) -> Pin<
Box_<
dyn std::future::Future<Output = Result<(Socket, Option<glib::Object>), glib::Error>>
+ 'static,
>,
> {
Box_::pin(crate::GioFuture::new(
self,
move |obj, cancellable, send| {
obj.accept_socket_async(Some(cancellable), move |res| {
send.resolve(res);
});
},
))
}
/// Creates a socket of type @type_ and protocol @protocol, binds
/// it to @address and adds it to the set of sockets we're accepting
/// sockets from.
///
/// Note that adding an IPv6 address, depending on the platform,
/// may or may not result in a listener that also accepts IPv4
/// connections. For more deterministic behavior, see
/// g_socket_listener_add_inet_port().
///
/// @source_object will be passed out in the various calls
/// to accept to identify this particular source, which is
/// useful if you're listening on multiple addresses and do
/// different things depending on what address is connected to.
///
/// If successful and @effective_address is non-[`None`] then it will
/// be set to the address that the binding actually occurred at. This
/// is helpful for determining the port number that was used for when
/// requesting a binding to port 0 (ie: "any port"). This address, if
/// requested, belongs to the caller and must be freed.
///
/// Call g_socket_listener_close() to stop listening on @address; this will not
/// be done automatically when you drop your final reference to @self, as
/// references may be held internally.
/// ## `address`
/// a #GSocketAddress
/// ## `type_`
/// a #GSocketType
/// ## `protocol`
/// a #GSocketProtocol
/// ## `source_object`
/// Optional #GObject identifying this source
///
/// # Returns
///
/// [`true`] on success, [`false`] on error.
///
/// ## `effective_address`
/// location to store the address that was bound to, or [`None`].
#[doc(alias = "g_socket_listener_add_address")]
fn add_address(
&self,
address: &impl IsA<SocketAddress>,
type_: SocketType,
protocol: SocketProtocol,
source_object: Option<&impl IsA<glib::Object>>,
) -> Result<SocketAddress, glib::Error> {
unsafe {
let mut effective_address = std::ptr::null_mut();
let mut error = std::ptr::null_mut();
let is_ok = ffi::g_socket_listener_add_address(
self.as_ref().to_glib_none().0,
address.as_ref().to_glib_none().0,
type_.into_glib(),
protocol.into_glib(),
source_object.map(|p| p.as_ref()).to_glib_none().0,
&mut effective_address,
&mut error,
);
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
if error.is_null() {
Ok(from_glib_full(effective_address))
} else {
Err(from_glib_full(error))
}
}
}
/// Listens for TCP connections on any available port number for both
/// IPv6 and IPv4 (if each is available).
///
/// This is useful if you need to have a socket for incoming connections
/// but don't care about the specific port number.
///
/// @source_object will be passed out in the various calls
/// to accept to identify this particular source, which is
/// useful if you're listening on multiple addresses and do
/// different things depending on what address is connected to.
/// ## `source_object`
/// Optional #GObject identifying this source
///
/// # Returns
///
/// the port number, or 0 in case of failure.
#[doc(alias = "g_socket_listener_add_any_inet_port")]
fn add_any_inet_port(
&self,
source_object: Option<&impl IsA<glib::Object>>,
) -> Result<u16, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::g_socket_listener_add_any_inet_port(
self.as_ref().to_glib_none().0,
source_object.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(ret)
} else {
Err(from_glib_full(error))
}
}
}
/// Helper function for g_socket_listener_add_address() that
/// creates a TCP/IP socket listening on IPv4 and IPv6 (if
/// supported) on the specified port on all interfaces.
///
/// @source_object will be passed out in the various calls
/// to accept to identify this particular source, which is
/// useful if you're listening on multiple addresses and do
/// different things depending on what address is connected to.
///
/// Call g_socket_listener_close() to stop listening on @port; this will not
/// be done automatically when you drop your final reference to @self, as
/// references may be held internally.
/// ## `port`
/// an IP port number (non-zero)
/// ## `source_object`
/// Optional #GObject identifying this source
///
/// # Returns
///
/// [`true`] on success, [`false`] on error.
#[doc(alias = "g_socket_listener_add_inet_port")]
fn add_inet_port(
&self,
port: u16,
source_object: Option<&impl IsA<glib::Object>>,
) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::g_socket_listener_add_inet_port(
self.as_ref().to_glib_none().0,
port,
source_object.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
}
}
}
/// Adds @socket to the set of sockets that we try to accept
/// new clients from. The socket must be bound to a local
/// address and listened to.
///
/// @source_object will be passed out in the various calls
/// to accept to identify this particular source, which is
/// useful if you're listening on multiple addresses and do
/// different things depending on what address is connected to.
///
/// The @socket will not be automatically closed when the @self is finalized
/// unless the listener held the final reference to the socket. Before GLib 2.42,
/// the @socket was automatically closed on finalization of the @self, even
/// if references to it were held elsewhere.
/// ## `socket`
/// a listening #GSocket
/// ## `source_object`
/// Optional #GObject identifying this source
///
/// # Returns
///
/// [`true`] on success, [`false`] on error.
#[doc(alias = "g_socket_listener_add_socket")]
fn add_socket(
&self,
socket: &impl IsA<Socket>,
source_object: Option<&impl IsA<glib::Object>>,
) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::g_socket_listener_add_socket(
self.as_ref().to_glib_none().0,
socket.as_ref().to_glib_none().0,
source_object.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
}
}
}
/// Closes all the sockets in the listener.
#[doc(alias = "g_socket_listener_close")]
fn close(&self) {
unsafe {
ffi::g_socket_listener_close(self.as_ref().to_glib_none().0);
}
}
/// Sets the listen backlog on the sockets in the listener. This must be called
/// before adding any sockets, addresses or ports to the #GSocketListener (for
/// example, by calling g_socket_listener_add_inet_port()) to be effective.
///
/// See g_socket_set_listen_backlog() for details
/// ## `listen_backlog`
/// an integer
#[doc(alias = "g_socket_listener_set_backlog")]
fn set_backlog(&self, listen_backlog: i32) {
unsafe {
ffi::g_socket_listener_set_backlog(self.as_ref().to_glib_none().0, listen_backlog);
}
}
/// The number of outstanding connections in the listen queue.
#[doc(alias = "listen-backlog")]
fn listen_backlog(&self) -> i32 {
ObjectExt::property(self.as_ref(), "listen-backlog")
}
/// The number of outstanding connections in the listen queue.
#[doc(alias = "listen-backlog")]
fn set_listen_backlog(&self, listen_backlog: i32) {
ObjectExt::set_property(self.as_ref(), "listen-backlog", listen_backlog)
}
/// Emitted when @listener's activity on @socket changes state.
/// Note that when @listener is used to listen on both IPv4 and
/// IPv6, a separate set of signals will be emitted for each, and
/// the order they happen in is undefined.
/// ## `event`
/// the event that is occurring
/// ## `socket`
/// the #GSocket the event is occurring on
#[doc(alias = "event")]
fn connect_event<F: Fn(&Self, SocketListenerEvent, &Socket) + 'static>(
&self,
f: F,
) -> SignalHandlerId {
unsafe extern "C" fn event_trampoline<
P: IsA<SocketListener>,
F: Fn(&P, SocketListenerEvent, &Socket) + 'static,
>(
this: *mut ffi::GSocketListener,
event: ffi::GSocketListenerEvent,
socket: *mut ffi::GSocket,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(
SocketListener::from_glib_borrow(this).unsafe_cast_ref(),
from_glib(event),
&from_glib_borrow(socket),
)
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"event\0".as_ptr() as *const _,
Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
event_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[doc(alias = "listen-backlog")]
fn connect_listen_backlog_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_listen_backlog_trampoline<
P: IsA<SocketListener>,
F: Fn(&P) + 'static,
>(
this: *mut ffi::GSocketListener,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(SocketListener::from_glib_borrow(this).unsafe_cast_ref())
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::listen-backlog\0".as_ptr() as *const _,
Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
notify_listen_backlog_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
}
impl<O: IsA<SocketListener>> SocketListenerExt for O {}