Class SocketService
- All Implemented Interfaces:
Proxy
- Direct Known Subclasses:
ThreadedSocketService
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
SocketService.Builder<B extends SocketService.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static interface
Functional interface declaration of theIncomingCallback
callback.static class
Class structure forGSocketService
.Nested classes/interfaces inherited from class org.gnome.gio.SocketListener
SocketListener.EventCallback, SocketListener.SocketListenerClass
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionCreates a newGSocketService
with no sockets to listen for.SocketService
(MemorySegment address) Create a SocketService proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected SocketService
asParent()
Returns this instance as if it were its parent type.static SocketService.Builder
<? extends SocketService.Builder> builder()
ASocketService.Builder
object constructs aSocketService
with the specified properties.boolean
emitIncoming
(SocketConnection connection, @Nullable GObject sourceObject) Emits the "incoming" signal.static MemoryLayout
The memory layout of the native struct.static Type
getType()
Get the GType of the SocketService classprotected boolean
incoming
(SocketConnection connection, GObject sourceObject) signal emitted when new connections are acceptedboolean
isActive()
Check whether the service is active or not.onIncoming
(SocketService.IncomingCallback handler) The ::incoming signal is emitted when a new incoming connection toservice
needs to be handled.void
start()
Restarts the service, i.e.void
stop()
Stops the service, i.e.Methods inherited from class org.gnome.gio.SocketListener
accept, acceptAsync, acceptFinish, acceptSocket, acceptSocketAsync, acceptSocketFinish, addAddress, addAnyInetPort, addInetPort, addSocket, changed, close, emitEvent, event, onEvent, setBacklog
Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
SocketService
Create a SocketService proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
SocketService
public SocketService()Creates a newGSocketService
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().New services are created active, there is no need to call g_socket_service_start(), unless g_socket_service_stop() has been called before.
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type.- Overrides:
asParent
in classSocketListener
-
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
-
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.
-
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.
-
incoming
signal emitted when new connections are accepted -
onIncoming
public SignalConnection<SocketService.IncomingCallback> onIncoming(SocketService.IncomingCallback handler) The ::incoming signal is emitted when a new incoming connection toservice
needs to be handled. The handler must initiate the handling ofconnection
, 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
- See Also:
-
emitIncoming
Emits the "incoming" signal. SeeonIncoming(org.gnome.gio.SocketService.IncomingCallback)
. -
builder
ASocketService.Builder
object constructs aSocketService
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withSocketService.Builder.build()
.
-