:right-sidebar: True SocketListener =================================================================== .. currentmodule:: gi.repository.Gio .. versionadded:: 2.22 .. class:: SocketListener(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.GObject.Object` Subclasses: :class:`~gi.repository.Gio.SocketService` 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 :obj:`~gi.repository.Gio.SocketListener.add_address` and :obj:`~gi.repository.Gio.SocketListener.add_inet_port`. These will be listened on until :obj:`~gi.repository.Gio.SocketListener.close` is called. Dropping your final reference to the ``GSocketListener`` will not cause :obj:`~gi.repository.Gio.SocketListener.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 :obj:`~gi.repository.Gio.SocketService` and :obj:`~gi.repository.Gio.ThreadedSocketService` which are subclasses of ``GSocketListener`` that make this even easier. Constructors ------------ .. rst-class:: interim-class .. class:: SocketListener :no-index: .. classmethod:: new() -> ~gi.repository.Gio.SocketListener Creates a new :obj:`~gi.repository.Gio.SocketListener` with no sockets to listen for. New listeners can be added with e.g. :func:`~gi.repository.Gio.SocketListener.add_address` or :func:`~gi.repository.Gio.SocketListener.add_inet_port`. .. versionadded:: 2.22 Methods ------- .. rst-class:: interim-class .. class:: SocketListener :no-index: .. method:: accept(cancellable: ~gi.repository.Gio.Cancellable | None = None) -> tuple[~gi.repository.Gio.SocketConnection, ~gi.repository.GObject.Object] Blocks waiting for a client to connect to any of the sockets added to the listener. Returns a :obj:`~gi.repository.Gio.SocketConnection` for the socket that was accepted. If ``source_object`` is not :const:`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 :const:`None`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error :const:`~gi.repository.Gio.IOErrorEnum.CANCELLED` will be returned. .. versionadded:: 2.22 :param cancellable: optional :obj:`~gi.repository.Gio.Cancellable` object, :const:`None` to ignore. .. method:: accept_async(cancellable: ~gi.repository.Gio.Cancellable | None = None, callback: ~typing.Callable[[...], None] | None = None, *user_data: ~typing.Any) -> None This is the asynchronous version of :func:`~gi.repository.Gio.SocketListener.accept`. When the operation is finished ``callback`` will be called. You can then call :func:`~gi.repository.Gio.SocketListener.accept_finish` to get the result of the operation. .. versionadded:: 2.22 :param cancellable: a :obj:`~gi.repository.Gio.Cancellable`, or :const:`None` :param callback: a :obj:`~gi.repository.Gio.AsyncReadyCallback` :param user_data: user data for the callback .. method:: accept_finish(result: ~gi.repository.Gio.AsyncResult) -> tuple[~gi.repository.Gio.SocketConnection, ~gi.repository.GObject.Object] Finishes an async accept operation. See :func:`~gi.repository.Gio.SocketListener.accept_async` .. versionadded:: 2.22 :param result: a :obj:`~gi.repository.Gio.AsyncResult`. .. method:: accept_socket(cancellable: ~gi.repository.Gio.Cancellable | None = None) -> tuple[~gi.repository.Gio.Socket, ~gi.repository.GObject.Object] Blocks waiting for a client to connect to any of the sockets added to the listener. Returns the :obj:`~gi.repository.Gio.Socket` that was accepted. If you want to accept the high-level :obj:`~gi.repository.Gio.SocketConnection`, not a :obj:`~gi.repository.Gio.Socket`, which is often the case, then you should use :func:`~gi.repository.Gio.SocketListener.accept` instead. If ``source_object`` is not :const:`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 :const:`None`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error :const:`~gi.repository.Gio.IOErrorEnum.CANCELLED` will be returned. .. versionadded:: 2.22 :param cancellable: optional :obj:`~gi.repository.Gio.Cancellable` object, :const:`None` to ignore. .. method:: accept_socket_async(cancellable: ~gi.repository.Gio.Cancellable | None = None, callback: ~typing.Callable[[...], None] | None = None, *user_data: ~typing.Any) -> None This is the asynchronous version of :func:`~gi.repository.Gio.SocketListener.accept_socket`. When the operation is finished ``callback`` will be called. You can then call :func:`~gi.repository.Gio.SocketListener.accept_socket_finish` to get the result of the operation. .. versionadded:: 2.22 :param cancellable: a :obj:`~gi.repository.Gio.Cancellable`, or :const:`None` :param callback: a :obj:`~gi.repository.Gio.AsyncReadyCallback` :param user_data: user data for the callback .. method:: accept_socket_finish(result: ~gi.repository.Gio.AsyncResult) -> tuple[~gi.repository.Gio.Socket, ~gi.repository.GObject.Object] Finishes an async accept operation. See :func:`~gi.repository.Gio.SocketListener.accept_socket_async` .. versionadded:: 2.22 :param result: a :obj:`~gi.repository.Gio.AsyncResult`. .. method:: add_address(address: ~gi.repository.Gio.SocketAddress, type: ~gi.repository.Gio.SocketType, protocol: ~gi.repository.Gio.SocketProtocol, source_object: ~gi.repository.GObject.Object | None = None) -> tuple[bool, ~gi.repository.Gio.SocketAddress] 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 :func:`~gi.repository.Gio.SocketListener.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-:const:`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 :func:`~gi.repository.Gio.SocketListener.close` to stop listening on ``address``; this will not be done automatically when you drop your final reference to ``listener``, as references may be held internally. .. versionadded:: 2.22 :param address: a :obj:`~gi.repository.Gio.SocketAddress` :param type: a :obj:`~gi.repository.Gio.SocketType` :param protocol: a :obj:`~gi.repository.Gio.SocketProtocol` :param source_object: Optional :obj:`~gi.repository.GObject.Object` identifying this source .. method:: add_any_inet_port(source_object: ~gi.repository.GObject.Object | None = None) -> int 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. .. versionadded:: 2.24 :param source_object: Optional :obj:`~gi.repository.GObject.Object` identifying this source .. method:: add_inet_port(port: int, source_object: ~gi.repository.GObject.Object | None = None) -> bool Helper function for :func:`~gi.repository.Gio.SocketListener.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 :func:`~gi.repository.Gio.SocketListener.close` to stop listening on ``port``; this will not be done automatically when you drop your final reference to ``listener``, as references may be held internally. .. versionadded:: 2.22 :param port: an IP port number (non-zero) :param source_object: Optional :obj:`~gi.repository.GObject.Object` identifying this source .. method:: add_socket(socket: ~gi.repository.Gio.Socket, source_object: ~gi.repository.GObject.Object | None = None) -> bool 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 ``listener`` 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 ``listener``, even if references to it were held elsewhere. .. versionadded:: 2.22 :param socket: a listening :obj:`~gi.repository.Gio.Socket` :param source_object: Optional :obj:`~gi.repository.GObject.Object` identifying this source .. method:: close() -> None Closes all the sockets in the listener. .. versionadded:: 2.22 .. method:: set_backlog(listen_backlog: int) -> None Sets the listen backlog on the sockets in the listener. This must be called before adding any sockets, addresses or ports to the :obj:`~gi.repository.Gio.SocketListener` (for example, by calling :func:`~gi.repository.Gio.SocketListener.add_inet_port`) to be effective. See :func:`~gi.repository.Gio.Socket.set_listen_backlog` for details .. versionadded:: 2.22 :param listen_backlog: an integer Properties ---------- .. rst-class:: interim-class .. class:: SocketListener :no-index: .. attribute:: props.listen_backlog :type: int The type of the None singleton. .. versionadded:: 2.22 Signals ------- .. rst-class:: interim-class .. class:: SocketListener.signals :no-index: .. method:: event(event: ~gi.repository.Gio.SocketListenerEvent, socket: ~gi.repository.Gio.Socket) -> None The type of the None singleton. .. versionadded:: 2.46 :param event: the event that is occurring :param socket: the :obj:`~gi.repository.Gio.Socket` the event is occurring on Virtual Methods --------------- .. rst-class:: interim-class .. class:: SocketListener :no-index: .. method:: do_changed() -> None The type of the None singleton. .. method:: do_event(event: ~gi.repository.Gio.SocketListenerEvent, socket: ~gi.repository.Gio.Socket) -> None The type of the None singleton. :param event: :param socket: Fields ------ .. rst-class:: interim-class .. class:: SocketListener :no-index: .. attribute:: parent_instance .. attribute:: priv