Class SocketService

All Implemented Interfaces:
Proxy
Direct Known Subclasses:
ThreadedSocketService

@Generated("org.javagi.JavaGI") public class SocketService 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 Gio.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() and to connect to the Gio.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 MainContext#pushThreadDefault) 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.

Since:
2.22
  • Constructor Details

    • SocketService

      public SocketService(MemorySegment address)
      Create a SocketService instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • SocketService

      public SocketService()
      Create a new SocketService.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the SocketService class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected SocketService asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class SocketListener
      Returns:
      the instance as if it were its parent type
    • isActive

      public boolean isActive()
      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.
      Returns:
      true if the service is active, false otherwise
      Since:
      2.22
    • start

      public void start()

      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 g_socket_service_stop().

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

      Since:
      2.22
    • stop

      public void stop()

      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 g_socket_service_start() again later to begin listening again. To close the listening sockets, call g_socket_listener_close(). (This will happen automatically when the GSocketService is finalized.)

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

      Since:
      2.22
    • incoming

      protected boolean incoming(SocketConnection connection, GObject sourceObject)
      signal emitted when new connections are accepted
    • onIncoming

      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.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.22
      See Also:
    • emitIncoming

      public boolean emitIncoming(@Nullable SocketConnection connection, @Nullable GObject sourceObject)
      Emits the "incoming" signal. See onIncoming(SocketService.IncomingCallback).
    • builder

      public static SocketService.Builder<? extends SocketService.Builder> builder()
      A SocketService.Builder object constructs a SocketService with the specified properties. Use the various set...() methods to set properties, and finish construction with SocketService.Builder.build().
      Returns:
      the builder object