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
// 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 gio_sys; use glib; use glib::object::Cast; use glib::object::IsA; use glib::signal::connect_raw; use glib::signal::SignalHandlerId; use glib::translate::*; use glib::StaticType; use glib::Value; use glib_sys; use gobject_sys; use std::boxed::Box as Box_; use std::fmt; use std::mem::transmute; use std::ptr; use Cancellable; use Socket; use SocketAddress; use SocketConnection; #[cfg(any(feature = "v2_46", feature = "dox"))] use SocketListenerEvent; use SocketProtocol; use SocketType; glib_wrapper! { /// A `SocketListener` 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` /// and `SocketListenerExt::add_inet_port`. These will be listened on until /// `SocketListenerExt::close` is called. Dropping your final reference to the /// `SocketListener` will not cause `SocketListenerExt::close` to be called /// implicitly, as some references to the `SocketListener` may be held /// internally. /// /// If you want to implement a network server, also look at `SocketService` /// and `ThreadedSocketService` which are subclasses of `SocketListener` /// that make this even easier. /// /// # Implements /// /// [`SocketListenerExt`](trait.SocketListenerExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html), [`SocketListenerExtManual`](prelude/trait.SocketListenerExtManual.html) pub struct SocketListener(Object<gio_sys::GSocketListener, gio_sys::GSocketListenerClass, SocketListenerClass>); match fn { get_type => || gio_sys::g_socket_listener_get_type(), } } impl SocketListener { /// Creates a new `SocketListener` with no sockets to listen for. /// New listeners can be added with e.g. `SocketListenerExt::add_address` /// or `SocketListenerExt::add_inet_port`. /// /// # Returns /// /// a new `SocketListener`. pub fn new() -> SocketListener { unsafe { from_glib_full(gio_sys::g_socket_listener_new()) } } } impl Default for SocketListener { fn default() -> Self { Self::new() } } pub const NONE_SOCKET_LISTENER: Option<&SocketListener> = None; /// Trait containing all `SocketListener` methods. /// /// # Implementors /// /// [`SocketListener`](struct.SocketListener.html), [`SocketService`](struct.SocketService.html) pub trait SocketListenerExt: 'static { /// Blocks waiting for a client to connect to any of the sockets added /// to the listener. Returns a `SocketConnection` 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` will be returned. /// ## `source_object` /// location where `gobject::Object` pointer will be stored, or `None` /// ## `cancellable` /// optional `Cancellable` object, `None` to ignore. /// /// # Returns /// /// a `SocketConnection` on success, `None` on error. fn accept<P: IsA<Cancellable>>( &self, cancellable: Option<&P>, ) -> Result<(SocketConnection, Option<glib::Object>), glib::Error>; /// Blocks waiting for a client to connect to any of the sockets added /// to the listener. Returns the `Socket` that was accepted. /// /// If you want to accept the high-level `SocketConnection`, not a `Socket`, /// which is often the case, then you should use `SocketListenerExt::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` will be returned. /// ## `source_object` /// location where `gobject::Object` pointer will be stored, or `None`. /// ## `cancellable` /// optional `Cancellable` object, `None` to ignore. /// /// # Returns /// /// a `Socket` on success, `None` on error. fn accept_socket<P: IsA<Cancellable>>( &self, cancellable: Option<&P>, ) -> Result<(Socket, Option<glib::Object>), glib::Error>; /// 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 /// `SocketListenerExt::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 `SocketListenerExt::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 `SocketAddress` /// ## `type_` /// a `SocketType` /// ## `protocol` /// a `SocketProtocol` /// ## `source_object` /// Optional `gobject::Object` identifying this source /// ## `effective_address` /// location to store the address that was bound to, or `None`. /// /// # Returns /// /// `true` on success, `false` on error. fn add_address<P: IsA<SocketAddress>, Q: IsA<glib::Object>>( &self, address: &P, type_: SocketType, protocol: SocketProtocol, source_object: Option<&Q>, ) -> Result<SocketAddress, glib::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::Object` identifying this source /// /// # Returns /// /// the port number, or 0 in case of failure. fn add_any_inet_port<P: IsA<glib::Object>>( &self, source_object: Option<&P>, ) -> Result<u16, glib::Error>; /// Helper function for `SocketListenerExt::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 `SocketListenerExt::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::Object` identifying this source /// /// # Returns /// /// `true` on success, `false` on error. fn add_inet_port<P: IsA<glib::Object>>( &self, port: u16, source_object: Option<&P>, ) -> Result<(), glib::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 `Socket` /// ## `source_object` /// Optional `gobject::Object` identifying this source /// /// # Returns /// /// `true` on success, `false` on error. fn add_socket<P: IsA<Socket>, Q: IsA<glib::Object>>( &self, socket: &P, source_object: Option<&Q>, ) -> Result<(), glib::Error>; /// Closes all the sockets in the listener. fn close(&self); /// Sets the listen backlog on the sockets in the listener. This must be called /// before adding any sockets, addresses or ports to the `SocketListener` (for /// example, by calling `SocketListenerExt::add_inet_port`) to be effective. /// /// See `SocketExt::set_listen_backlog` for details /// ## `listen_backlog` /// an integer fn set_backlog(&self, listen_backlog: i32); fn get_property_listen_backlog(&self) -> i32; fn set_property_listen_backlog(&self, listen_backlog: i32); /// 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. /// /// Feature: `v2_46` /// /// ## `event` /// the event that is occurring /// ## `socket` /// the `Socket` the event is occurring on #[cfg(any(feature = "v2_46", feature = "dox"))] fn connect_event<F: Fn(&Self, SocketListenerEvent, &Socket) + 'static>( &self, f: F, ) -> SignalHandlerId; fn connect_property_listen_backlog_notify<F: Fn(&Self) + 'static>( &self, f: F, ) -> SignalHandlerId; } impl<O: IsA<SocketListener>> SocketListenerExt for O { fn accept<P: IsA<Cancellable>>( &self, cancellable: Option<&P>, ) -> Result<(SocketConnection, Option<glib::Object>), glib::Error> { unsafe { let mut source_object = ptr::null_mut(); let mut error = ptr::null_mut(); let ret = gio_sys::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)) } } } fn accept_socket<P: IsA<Cancellable>>( &self, cancellable: Option<&P>, ) -> Result<(Socket, Option<glib::Object>), glib::Error> { unsafe { let mut source_object = ptr::null_mut(); let mut error = ptr::null_mut(); let ret = gio_sys::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)) } } } fn add_address<P: IsA<SocketAddress>, Q: IsA<glib::Object>>( &self, address: &P, type_: SocketType, protocol: SocketProtocol, source_object: Option<&Q>, ) -> Result<SocketAddress, glib::Error> { unsafe { let mut effective_address = ptr::null_mut(); let mut error = ptr::null_mut(); let _ = gio_sys::g_socket_listener_add_address( self.as_ref().to_glib_none().0, address.as_ref().to_glib_none().0, type_.to_glib(), protocol.to_glib(), source_object.map(|p| p.as_ref()).to_glib_none().0, &mut effective_address, &mut error, ); if error.is_null() { Ok(from_glib_full(effective_address)) } else { Err(from_glib_full(error)) } } } fn add_any_inet_port<P: IsA<glib::Object>>( &self, source_object: Option<&P>, ) -> Result<u16, glib::Error> { unsafe { let mut error = ptr::null_mut(); let ret = gio_sys::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)) } } } fn add_inet_port<P: IsA<glib::Object>>( &self, port: u16, source_object: Option<&P>, ) -> Result<(), glib::Error> { unsafe { let mut error = ptr::null_mut(); let _ = gio_sys::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, ); if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } } } fn add_socket<P: IsA<Socket>, Q: IsA<glib::Object>>( &self, socket: &P, source_object: Option<&Q>, ) -> Result<(), glib::Error> { unsafe { let mut error = ptr::null_mut(); let _ = gio_sys::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, ); if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) } } } fn close(&self) { unsafe { gio_sys::g_socket_listener_close(self.as_ref().to_glib_none().0); } } fn set_backlog(&self, listen_backlog: i32) { unsafe { gio_sys::g_socket_listener_set_backlog(self.as_ref().to_glib_none().0, listen_backlog); } } fn get_property_listen_backlog(&self) -> i32 { unsafe { let mut value = Value::from_type(<i32 as StaticType>::static_type()); gobject_sys::g_object_get_property( self.to_glib_none().0 as *mut gobject_sys::GObject, b"listen-backlog\0".as_ptr() as *const _, value.to_glib_none_mut().0, ); value .get() .expect("Return Value for property `listen-backlog` getter") .unwrap() } } fn set_property_listen_backlog(&self, listen_backlog: i32) { unsafe { gobject_sys::g_object_set_property( self.to_glib_none().0 as *mut gobject_sys::GObject, b"listen-backlog\0".as_ptr() as *const _, Value::from(&listen_backlog).to_glib_none().0, ); } } #[cfg(any(feature = "v2_46", feature = "dox"))] fn connect_event<F: Fn(&Self, SocketListenerEvent, &Socket) + 'static>( &self, f: F, ) -> SignalHandlerId { unsafe extern "C" fn event_trampoline< P, F: Fn(&P, SocketListenerEvent, &Socket) + 'static, >( this: *mut gio_sys::GSocketListener, event: gio_sys::GSocketListenerEvent, socket: *mut gio_sys::GSocket, f: glib_sys::gpointer, ) where P: IsA<SocketListener>, { let f: &F = &*(f as *const F); f( &SocketListener::from_glib_borrow(this).unsafe_cast(), 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(transmute(event_trampoline::<Self, F> as usize)), Box_::into_raw(f), ) } } fn connect_property_listen_backlog_notify<F: Fn(&Self) + 'static>( &self, f: F, ) -> SignalHandlerId { unsafe extern "C" fn notify_listen_backlog_trampoline<P, F: Fn(&P) + 'static>( this: *mut gio_sys::GSocketListener, _param_spec: glib_sys::gpointer, f: glib_sys::gpointer, ) where P: IsA<SocketListener>, { let f: &F = &*(f as *const F); f(&SocketListener::from_glib_borrow(this).unsafe_cast()) } 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(transmute( notify_listen_backlog_trampoline::<Self, F> as usize, )), Box_::into_raw(f), ) } } } impl fmt::Display for SocketListener { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "SocketListener") } }