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.
- Since:
- 2.22
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSocketService.Builder<B extends SocketService.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static interfaceFunctional interface declaration of theIncomingCallbackcallback.static classClass structure forGSocketService.Nested classes/interfaces inherited from class org.gnome.gio.SocketListener
SocketListener.EventCallback, SocketListener.SocketListenerClassNested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new SocketService.SocketService(MemorySegment address) Create a SocketService proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected SocketServiceasParent()Returns this instance as if it were its parent type.static SocketService.Builder<? extends SocketService.Builder> builder()ASocketService.Builderobject constructs aSocketServicewith the specified properties.booleanemitIncoming(@Nullable SocketConnection connection, @Nullable GObject sourceObject) Emits the "incoming" signal.static MemoryLayoutThe memory layout of the native struct.static @Nullable TypegetType()Get the GType of the SocketService classprotected booleanincoming(SocketConnection connection, GObject sourceObject) signal emitted when new connections are acceptedbooleanisActive()Check whether the service is active or not.onIncoming(SocketService.IncomingCallback handler) The ::incoming signal is emitted when a new incoming connection toserviceneeds to be handled.voidstart()Restarts the service, i.e.voidstop()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, setBacklogMethods 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, 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, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.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 new SocketService.
-
-
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 Javasuperkeyword, 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:
asParentin 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:
trueif the service is active,falseotherwise- 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
GSocketServiceis 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
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 toserviceneeds to be handled. The handler must initiate the handling ofconnection,but may not block; in essence, asynchronous operations must be used.connectionwill 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
Emits the "incoming" signal. SeeonIncoming(org.gnome.gio.SocketService.IncomingCallback). -
builder
ASocketService.Builderobject constructs aSocketServicewith the specified properties. Use the variousset...()methods to set properties, and finish construction withSocketService.Builder.build().- Returns:
- the builder object
-