Class SocketListener
- All Implemented Interfaces:
Proxy
- Direct Known Subclasses:
SocketService
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
addAddress(org.gnome.gio.SocketAddress, org.gnome.gio.SocketType, org.gnome.gio.SocketProtocol, org.gnome.gobject.GObject, io.github.jwharm.javagi.base.Out<org.gnome.gio.SocketAddress>)
and
addInetPort(short, org.gnome.gobject.GObject)
. These will be listened on until
close()
is called. Dropping your final reference to
the GSocketListener
will not cause 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
SocketService
and ThreadedSocketService
which are
subclasses of GSocketListener
that make this even easier.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
SocketListener.Builder<B extends SocketListener.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static interface
Functional interface declaration of theEventCallback
callback.static class
Class structure forGSocketListener
.Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionCreates a newGSocketListener
with no sockets to listen for.SocketListener
(MemorySegment address) Create a SocketListener proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionaccept
(@Nullable Out<GObject> sourceObject, @Nullable Cancellable cancellable) Blocks waiting for a client to connect to any of the sockets added to the listener.void
acceptAsync
(@Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) This is the asynchronous version of g_socket_listener_accept().acceptFinish
(AsyncResult result, @Nullable Out<GObject> sourceObject) Finishes an async accept operation.acceptSocket
(@Nullable Out<GObject> sourceObject, @Nullable Cancellable cancellable) Blocks waiting for a client to connect to any of the sockets added to the listener.void
acceptSocketAsync
(@Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) This is the asynchronous version of g_socket_listener_accept_socket().acceptSocketFinish
(AsyncResult result, @Nullable Out<GObject> sourceObject) Finishes an async accept operation.boolean
addAddress
(SocketAddress address, SocketType type, SocketProtocol protocol, @Nullable GObject sourceObject, @Nullable Out<SocketAddress> effectiveAddress) Creates a socket of typetype
and protocolprotocol
, binds it toaddress
and adds it to the set of sockets we're accepting sockets from.short
addAnyInetPort
(@Nullable GObject sourceObject) Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each is available).boolean
addInetPort
(short port, @Nullable GObject sourceObject) Helper function for g_socket_listener_add_address() that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.boolean
Addssocket
to the set of sockets that we try to accept new clients from.protected SocketListener
asParent()
Returns this instance as if it were its parent type.static SocketListener.Builder
<? extends SocketListener.Builder> builder()
ASocketListener.Builder
object constructs aSocketListener
with the specified properties.protected void
changed()
virtual method called when the set of socket listened to changesvoid
close()
Closes all the sockets in the listener.void
emitEvent
(SocketListenerEvent event, Socket socket) Emits the "event" signal.protected void
event
(SocketListenerEvent event, Socket socket) static MemoryLayout
The memory layout of the native struct.static Type
getType()
Get the GType of the SocketListener classonEvent
(SocketListener.EventCallback handler) Emitted whenlistener
's activity onsocket
changes state.void
setBacklog
(int listenBacklog) Sets the listen backlog on the sockets in the listener.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
-
SocketListener
Create a SocketListener proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
SocketListener
public SocketListener()Creates a newGSocketListener
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().
-
-
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. -
accept
public SocketConnection accept(@Nullable @Nullable Out<GObject> sourceObject, @Nullable @Nullable Cancellable cancellable) throws GErrorException Blocks waiting for a client to connect to any of the sockets added to the listener. Returns aGSocketConnection
for the socket that was accepted.If
sourceObject
is notnull
it will be filled out with the source object specified when the corresponding socket or address was added to the listener.If
cancellable
is notnull
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLED
will be returned.- Parameters:
sourceObject
- location whereGObject
pointer will be stored, ornull
cancellable
- optionalGCancellable
object,null
to ignore.- Returns:
- a
GSocketConnection
on success,null
on error. - Throws:
GErrorException
- seeGError
-
acceptAsync
public void acceptAsync(@Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) This is the asynchronous version of g_socket_listener_accept().When the operation is finished
callback
will be called. You can then call g_socket_listener_accept_finish() to get the result of the operation.- Parameters:
cancellable
- aGCancellable
, ornull
callback
- aGAsyncReadyCallback
-
acceptFinish
public SocketConnection acceptFinish(AsyncResult result, @Nullable @Nullable Out<GObject> sourceObject) throws GErrorException Finishes an async accept operation. See g_socket_listener_accept_async()- Parameters:
result
- aGAsyncResult
.sourceObject
- OptionalGObject
identifying this source- Returns:
- a
GSocketConnection
on success,null
on error. - Throws:
GErrorException
- seeGError
-
acceptSocket
public Socket acceptSocket(@Nullable @Nullable Out<GObject> sourceObject, @Nullable @Nullable Cancellable cancellable) throws GErrorException Blocks waiting for a client to connect to any of the sockets added to the listener. Returns theGSocket
that was accepted.If you want to accept the high-level
GSocketConnection
, not aGSocket
, which is often the case, then you should use g_socket_listener_accept() instead.If
sourceObject
is notnull
it will be filled out with the source object specified when the corresponding socket or address was added to the listener.If
cancellable
is notnull
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLED
will be returned.- Parameters:
sourceObject
- location whereGObject
pointer will be stored, ornull
.cancellable
- optionalGCancellable
object,null
to ignore.- Returns:
- a
GSocket
on success,null
on error. - Throws:
GErrorException
- seeGError
-
acceptSocketAsync
public void acceptSocketAsync(@Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) This is the asynchronous version of g_socket_listener_accept_socket().When the operation is finished
callback
will be called. You can then call g_socket_listener_accept_socket_finish() to get the result of the operation.- Parameters:
cancellable
- aGCancellable
, ornull
callback
- aGAsyncReadyCallback
-
acceptSocketFinish
public Socket acceptSocketFinish(AsyncResult result, @Nullable @Nullable Out<GObject> sourceObject) throws GErrorException Finishes an async accept operation. See g_socket_listener_accept_socket_async()- Parameters:
result
- aGAsyncResult
.sourceObject
- OptionalGObject
identifying this source- Returns:
- a
GSocket
on success,null
on error. - Throws:
GErrorException
- seeGError
-
addAddress
public boolean addAddress(SocketAddress address, SocketType type, SocketProtocol protocol, @Nullable @Nullable GObject sourceObject, @Nullable @Nullable Out<SocketAddress> effectiveAddress) throws GErrorException Creates a socket of typetype
and protocolprotocol
, binds it toaddress
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 g_socket_listener_add_inet_port().
sourceObject
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
effectiveAddress
is non-null
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 g_socket_listener_close() to stop listening on
address
; this will not be done automatically when you drop your final reference to this SocketListener, as references may be held internally.- Parameters:
address
- aGSocketAddress
type
- aGSocketType
protocol
- aGSocketProtocol
sourceObject
- OptionalGObject
identifying this sourceeffectiveAddress
- location to store the address that was bound to, ornull
.- Returns:
true
on success,false
on error.- Throws:
GErrorException
- seeGError
-
addAnyInetPort
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.
sourceObject
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.- Parameters:
sourceObject
- OptionalGObject
identifying this source- Returns:
- the port number, or 0 in case of failure.
- Throws:
GErrorException
- seeGError
-
addInetPort
public boolean addInetPort(short port, @Nullable @Nullable GObject sourceObject) throws GErrorException Helper function for g_socket_listener_add_address() that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.sourceObject
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 g_socket_listener_close() to stop listening on
port
; this will not be done automatically when you drop your final reference to this SocketListener, as references may be held internally.- Parameters:
port
- an IP port number (non-zero)sourceObject
- OptionalGObject
identifying this source- Returns:
true
on success,false
on error.- Throws:
GErrorException
- seeGError
-
addSocket
public boolean addSocket(Socket socket, @Nullable @Nullable GObject sourceObject) throws GErrorException Addssocket
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.sourceObject
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 this SocketListener is finalized unless the listener held the final reference to the socket. Before GLib 2.42, thesocket
was automatically closed on finalization of the this SocketListener, even if references to it were held elsewhere.- Parameters:
socket
- a listeningGSocket
sourceObject
- OptionalGObject
identifying this source- Returns:
true
on success,false
on error.- Throws:
GErrorException
- seeGError
-
close
public void close()Closes all the sockets in the listener. -
setBacklog
public void setBacklog(int listenBacklog) Sets the listen backlog on the sockets in the listener. This must be called before adding any sockets, addresses or ports to theGSocketListener
(for example, by calling g_socket_listener_add_inet_port()) to be effective.See g_socket_set_listen_backlog() for details
- Parameters:
listenBacklog
- an integer
-
changed
protected void changed()virtual method called when the set of socket listened to changes -
event
-
onEvent
Emitted whenlistener
's activity onsocket
changes state. Note that whenlistener
is used to listen on both IPv4 and IPv6, a separate set of signals will be emitted for each, and the order they happen in is undefined.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitEvent
Emits the "event" signal. SeeonEvent(org.gnome.gio.SocketListener.EventCallback)
. -
builder
ASocketListener.Builder
object constructs aSocketListener
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withSocketListener.Builder.build()
.
-