Skip to content

Gio.SocketService

class — extends SocketListener

A GSocketService is an object that represents a service that is provided to the network or over local sockets. When a new connection is made to the service the SocketService.incoming signal is emitted.

A GSocketService is a subclass of SocketListener and you need to add the addresses you want to accept connections on with the SocketListener APIs.

There are two options for implementing a network service based on GSocketService. The first is to create the service using SocketService.new and to connect to the SocketService.incoming signal. The second is to subclass GSocketService and override the default signal handler implementation.

In either case, the handler must immediately return, or else it will block additional incoming connections from being serviced. If you are interested in writing connection handlers that contain blocking code then see ThreadedSocketService.

The socket service runs on the main loop of the thread-default context (see GLib.MainContext.push_thread_default) of the thread it is created in, and is not threadsafe in general. However, the calls to start and stop the service are thread-safe so these can be used from threads that handle incoming clients.

Constructors

new

@classmethod
def new(cls) -> SocketService

Creates a new SocketService with no sockets to listen for. New listeners can be added with e.g. SocketListener.add_address or SocketListener.add_inet_port.

New services are created active, there is no need to call SocketService.start, unless SocketService.stop has been called before.

Methods

is_active

def is_active(self) -> bool

Check whether the service is active or not. An active service will accept new clients that connect, while a non-active service will let connecting clients queue up until the service is started.

start

def start(self) -> None

Restarts the service, i.e. start accepting connections from the added sockets when the mainloop runs. This only needs to be called after the service has been stopped from SocketService.stop.

This call is thread-safe, so it may be called from a thread handling an incoming client request.

stop

def stop(self) -> None

Stops the service, i.e. stops accepting connections from the added sockets when the mainloop runs.

This call is thread-safe, so it may be called from a thread handling an incoming client request.

Note that this only stops accepting new connections; it does not close the listening sockets, and you can call SocketService.start again later to begin listening again. To close the listening sockets, call SocketListener.close. (This will happen automatically when the SocketService is finalized.)

This must be called before calling SocketListener.close as the socket service will start accepting connections immediately when a new socket is added.

Virtual methods

do_incoming

def do_incoming(self, connection: SocketConnection, source_object: GObject.Object) -> bool

signal emitted when new connections are accepted

Properties

active

active: bool  # read/write

Whether the service is currently accepting connections.

Signals

incoming

def on_incoming(self, connection: SocketConnection, source_object: GObject.Object | None) -> bool: ...

The ::incoming signal is emitted when a new incoming connection to service needs to be handled. The handler must initiate the handling of connection, but may not block; in essence, asynchronous operations must be used.

connection will be unreffed once the signal handler returns, so you need to ref it yourself if you are planning to use it.