Class SocketConnection
- All Implemented Interfaces:
Proxy
,AutoCloseable
,AutoCloseable
- Direct Known Subclasses:
TcpConnection
,UnixConnection
GSocketConnection
is a IOStream
for a connected socket. They
can be created either by SocketClient
when connecting to a host,
or by SocketListener
when accepting a new client.
The type of the GSocketConnection
object returned from these calls
depends on the type of the underlying socket that is in use. For
instance, for a TCP/IP connection it will be a TcpConnection
.
Choosing what type of object to construct is done with the socket
connection factory, and it is possible for third parties to register
custom socket connection types for specific combination of socket
family/type/protocol using factoryRegisterType(org.gnome.glib.Type, org.gnome.gio.SocketFamily, org.gnome.gio.SocketType, int)
.
To close a GSocketConnection
, use IOStream.close(org.gnome.gio.Cancellable)
. Closing both
substreams of the IOStream
separately will not close the
underlying Socket
.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
SocketConnection.Builder<B extends SocketConnection.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
Nested classes/interfaces inherited from class org.gnome.gio.IOStream
IOStream.IOStreamClass, IOStream.IOStreamImpl
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionSocketConnection
(MemorySegment address) Create a SocketConnection proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected SocketConnection
asParent()
Returns this instance as if it were its parent type.static SocketConnection.Builder
<? extends SocketConnection.Builder> builder()
ASocketConnection.Builder
object constructs aSocketConnection
with the specified properties.boolean
connect
(SocketAddress address, @Nullable Cancellable cancellable) Connect this SocketConnection to the specified remote address.void
connectAsync
(SocketAddress address, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Asynchronously connect this SocketConnection to the specified remote address.boolean
connectFinish
(AsyncResult result) Gets the result of a g_socket_connection_connect_async() call.static Type
factoryLookupType
(SocketFamily family, SocketType type, int protocolId) Looks up theGType
to be used when creating socket connections on sockets with the specifiedfamily
,type
andprotocolId
.static void
factoryRegisterType
(Type gType, SocketFamily family, SocketType type, int protocol) Looks up theGType
to be used when creating socket connections on sockets with the specifiedfamily
,type
andprotocol
.Try to get the local address of a socket connection.static MemoryLayout
The memory layout of the native struct.Try to get the remote address of a socket connection.Gets the underlyingGSocket
object of the connection.static Type
getType()
Get the GType of the SocketConnection classboolean
Checks if this SocketConnection is connected.Methods inherited from class org.gnome.gio.IOStream
clearPending, close, closeAsync, closeFinish, closeFn, getInputStream, getOutputStream, hasPending, isClosed, setPending, spliceAsync, spliceAsync, spliceFinish
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
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.github.jwharm.javagi.gio.AutoCloseable
close
-
Constructor Details
-
SocketConnection
Create a SocketConnection proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
-
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. -
factoryLookupType
Looks up theGType
to be used when creating socket connections on sockets with the specifiedfamily
,type
andprotocolId
.If no type is registered, the
GSocketConnection
base type is returned.- Parameters:
family
- aGSocketFamily
type
- aGSocketType
protocolId
- a protocol id- Returns:
- a
GType
-
factoryRegisterType
public static void factoryRegisterType(Type gType, SocketFamily family, SocketType type, int protocol) Looks up theGType
to be used when creating socket connections on sockets with the specifiedfamily
,type
andprotocol
.If no type is registered, the
GSocketConnection
base type is returned.- Parameters:
gType
- aGType
, inheriting fromG_TYPE_SOCKET_CONNECTION
family
- aGSocketFamily
type
- aGSocketType
protocol
- a protocol id
-
connect
public boolean connect(SocketAddress address, @Nullable @Nullable Cancellable cancellable) throws GErrorException Connect this SocketConnection to the specified remote address.- Parameters:
address
- aGSocketAddress
specifying the remote address.cancellable
- aGCancellable
ornull
- Returns:
true
if the connection succeeded,false
on error- Throws:
GErrorException
- seeGError
-
connectAsync
public void connectAsync(SocketAddress address, @Nullable @Nullable Cancellable cancellable, @Nullable @Nullable AsyncReadyCallback callback) Asynchronously connect this SocketConnection to the specified remote address.This clears the
GSocket
:blocking flag on this SocketConnection's underlying socket if it is currently set.If
GSocket
:timeout is set, the operation will time out and returnIOErrorEnum.TIMED_OUT
after that period. Otherwise, it will continue indefinitely until operating system timeouts (if any) are hit.Use g_socket_connection_connect_finish() to retrieve the result.
- Parameters:
address
- aGSocketAddress
specifying the remote address.cancellable
- aGCancellable
ornull
callback
- aGAsyncReadyCallback
-
connectFinish
Gets the result of a g_socket_connection_connect_async() call.- Parameters:
result
- theGAsyncResult
- Returns:
true
if the connection succeeded,false
on error- Throws:
GErrorException
- seeGError
-
getLocalAddress
Try to get the local address of a socket connection.- Returns:
- a
GSocketAddress
ornull
on error. Free the returned object with g_object_unref(). - Throws:
GErrorException
- seeGError
-
getRemoteAddress
Try to get the remote address of a socket connection.Since GLib 2.40, when used with g_socket_client_connect() or g_socket_client_connect_async(), during emission of
SocketClientEvent.CONNECTING
, this function will return the remote address that will be used for the connection. This allows applications to print e.g. "Connecting to example.com (10.42.77.3)...".- Returns:
- a
GSocketAddress
ornull
on error. Free the returned object with g_object_unref(). - Throws:
GErrorException
- seeGError
-
getSocket
Gets the underlyingGSocket
object of the connection. This can be useful if you want to do something unusual on it not supported by theGSocketConnection
APIs.- Returns:
- a
GSocket
ornull
on error.
-
isConnected
public boolean isConnected()Checks if this SocketConnection is connected. This is equivalent to calling g_socket_is_connected() on this SocketConnection's underlyingGSocket
.- Returns:
- whether this SocketConnection is connected
-
builder
ASocketConnection.Builder
object constructs aSocketConnection
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withSocketConnection.Builder.build()
.
-