Module org.newsclub.net.unix
Package org.newsclub.net.unix
Class AFSocket<A extends AFSocketAddress>
java.lang.Object
java.net.Socket
org.newsclub.net.unix.AFSocket<A>
- Type Parameters:
A
- The concreteAFSocketAddress
that is supported by this type.
- All Implemented Interfaces:
Closeable
,AutoCloseable
,AFSocketExtensions
,AFSomeSocket
,AFSomeSocketThing
,FileDescriptorAccess
- Direct Known Subclasses:
AFUNIXSocket
public abstract class AFSocket<A extends AFSocketAddress>
extends Socket
implements AFSomeSocket, AFSocketExtensions
junixsocket's base implementation of a
Socket
.- Author:
- Christian Kohlschütter
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
AFSocket.Constructor<A extends AFSocketAddress>
The reference to the constructor of anAFSocket
subclass. -
Constructor Summary
ModifierConstructorDescriptionprotected
AFSocket
(AFSocketImpl<A> impl, AFSocketAddressFromHostname<A> afh) Creates a newAFSocket
instance. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
addCloseable
(Closeable closeable) Registers aCloseable
that should be closed when this socket is closed.final void
bind
(SocketAddress bindpoint) Not supported, since it's not necessary for client sockets.boolean
Probes the status of the socket connection.final void
close()
final void
connect
(SocketAddress endpoint) final void
connect
(SocketAddress endpoint, int timeout) Sets the hook for any subsequent call toconnect(SocketAddress)
orconnect(SocketAddress, int)
to be the given function.static final <A extends AFSocketAddress>
AFSocket<?> connectTo
(@NonNull A addr) Creates a newAFSocket
and connects it to the givenAFSocketAddress
using the default implementation suited for that address type.protected static final <A extends AFSocketAddress>
@NonNull AFSocket<A> connectTo
(AFSocket.Constructor<A> constr, A addr) Creates a newAFSocket
and connects it to the givenAFSocketAddress
.final void
ensureAncillaryReceiveBufferSize
(int minSize) Ensures a minimum ancillary receive buffer size.static void
Checks ifAFSocket
s are supported by the current Java VM.static final void
Checks if the current environment (system platform, native library, etc.) supports "unsafe" operations (as controlled via theAFSocketCapability.CAPABILITY_UNSAFE
capability).forceConnectAddress
(SocketAddress endpoint) Forces the address to be used for any subsequent call toconnect(SocketAddress)
to be the given one, regardless of what'll be passed there.final int
Returns the size of the receive buffer for ancillary messages (in bytes).final FileDescriptor
Returns the correspondingFileDescriptor
.protected final AFSocketImplExtensions
<A> Returns the internal helper instance for address-specific extensions.final AFInputStream
static final String
Returns an identifier of the loaded native library, ornull
if the library hasn't been loaded yet.final A
Returns the socket's local socket address, ornull
if unavailable or if there was a problem retrieving it.final AFOutputStream
final A
Returns the socket's remote socket address, ornull
if unavailable/not connected, or if there was a problem retrieving it.static final String
Returns the version of the junixsocket library, as a string, for debugging purposes.final boolean
isBound()
final boolean
isClosed()
final boolean
static boolean
Checks if we're running on Android (as far as junixsocket is concerned).static boolean
Returnstrue
iffAFSocket
s are supported by the current Java VM.protected abstract AFSocketChannel
<A> Creates a newAFSocketChannel
for this socket.protected static final <A extends AFSocketAddress>
AFSocket<A> newInstance
(AFSocket.Constructor<A> constr, AFSocketFactory<A> factory) Creates a new, unboundAFSocket
.final void
removeCloseable
(Closeable closeable) Unregisters a previously registeredCloseable
.final void
setAncillaryReceiveBufferSize
(int size) Sets the size of the receive buffer for ancillary messages (in bytes).void
setShutdownOnClose
(boolean enabled) Configures whether the socket should be shutdown uponCloseable.close()
, which is the default.protected final Class
<? extends AFSocketAddress> Returns theAFSocketAddress
type supported by this socket.static final boolean
supports
(AFSocketCapability capability) Checks if the current environment (system platform, native library, etc.) supports a given junixsocket capability.static final boolean
supports
(AFUNIXSocketCapability capability) Deprecated.final String
toString()
Methods inherited from class java.net.Socket
getInetAddress, getKeepAlive, getLocalAddress, getLocalPort, getOOBInline, getOption, getPort, getReceiveBufferSize, getReuseAddress, getSendBufferSize, getSoLinger, getSoTimeout, getTcpNoDelay, getTrafficClass, isInputShutdown, isOutputShutdown, sendUrgentData, setKeepAlive, setOOBInline, setOption, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSocketImplFactory, setSoLinger, setSoTimeout, setTcpNoDelay, setTrafficClass, shutdownInput, shutdownOutput, supportedOptions
-
Constructor Details
-
AFSocket
Creates a newAFSocket
instance.- Parameters:
impl
- The correspondingSocketImpl
class.afh
- The conversion helper to get a socket address from an encoded hostname.- Throws:
SocketException
- on error.
-
-
Method Details
-
socketAddressClass
Returns theAFSocketAddress
type supported by this socket.- Returns:
- The supported
AFSocketAddress
.
-
newChannel
Creates a newAFSocketChannel
for this socket.- Returns:
- The new instance.
-
newInstance
protected static final <A extends AFSocketAddress> AFSocket<A> newInstance(AFSocket.Constructor<A> constr, AFSocketFactory<A> factory) throws SocketException Creates a new, unboundAFSocket
. This "default" implementation is a bit "lenient" with respect to the specification. In particular, we may ignore calls toSocket.getTcpNoDelay()
andSocket.setTcpNoDelay(boolean)
.- Type Parameters:
A
- The corresponding address type.- Parameters:
constr
- The implementation'sAFSocket
constructorfactory
- The corresponding socket factory, ornull
.- Returns:
- A new, unbound socket.
- Throws:
SocketException
- if the operation fails.
-
connectTo
protected static final <A extends AFSocketAddress> @NonNull AFSocket<A> connectTo(AFSocket.Constructor<A> constr, A addr) throws IOException Creates a newAFSocket
and connects it to the givenAFSocketAddress
.- Type Parameters:
A
- The corresponding address type.- Parameters:
constr
- The implementation'sAFSocket
constructoraddr
- The address to connect to.- Returns:
- A new, connected socket.
- Throws:
IOException
- if the operation fails.
-
connectTo
public static final <A extends AFSocketAddress> AFSocket<?> connectTo(@NonNull A addr) throws IOException Creates a newAFSocket
and connects it to the givenAFSocketAddress
using the default implementation suited for that address type.- Type Parameters:
A
- The corresponding address type.- Parameters:
addr
- The address to connect to.- Returns:
- A new, connected socket.
- Throws:
IOException
- if the operation fails.
-
bind
Not supported, since it's not necessary for client sockets.- Overrides:
bind
in classSocket
- Throws:
IOException
- See Also:
-
isBound
-
isConnected
public final boolean isConnected()- Overrides:
isConnected
in classSocket
-
connect
- Overrides:
connect
in classSocket
- Throws:
IOException
-
connect
- Overrides:
connect
in classSocket
- Throws:
IOException
-
toString
-
isSupported
-
ensureSupported
Checks ifAFSocket
s are supported by the current Java VM. If not, anUnsupportedOperationException
is thrown.- Throws:
UnsupportedOperationException
- if not supported.
-
getVersion
Returns the version of the junixsocket library, as a string, for debugging purposes. NOTE: Do not rely on the format of the version identifier, use socket capabilities instead.- Returns:
- String The version identifier, or
null
if it could not be determined. - See Also:
-
getLoadedLibrary
Returns an identifier of the loaded native library, ornull
if the library hasn't been loaded yet. The identifier is useful mainly for debugging purposes.- Returns:
- The identifier of the loaded junixsocket-native library, or
null
.
-
isClosed
-
getAncillaryReceiveBufferSize
public final int getAncillaryReceiveBufferSize()Description copied from interface:AFSocketExtensions
Returns the size of the receive buffer for ancillary messages (in bytes).- Specified by:
getAncillaryReceiveBufferSize
in interfaceAFSocketExtensions
- Returns:
- The size.
-
setAncillaryReceiveBufferSize
public final void setAncillaryReceiveBufferSize(int size) Description copied from interface:AFSocketExtensions
Sets the size of the receive buffer for ancillary messages (in bytes). To disable handling ancillary messages, set it to 0 (default).- Specified by:
setAncillaryReceiveBufferSize
in interfaceAFSocketExtensions
- Parameters:
size
- The size.
-
ensureAncillaryReceiveBufferSize
public final void ensureAncillaryReceiveBufferSize(int minSize) Description copied from interface:AFSocketExtensions
Ensures a minimum ancillary receive buffer size.- Specified by:
ensureAncillaryReceiveBufferSize
in interfaceAFSocketExtensions
- Parameters:
minSize
- The minimum size (in bytes).
-
supports
Deprecated.Checks if the current environment (system platform, native library, etc.) supports a given junixsocket capability. Deprecated. Please usesupports(AFSocketCapability)
instead. NOTE: The result may or may not be cached from a previous call or from a check upon initialization.- Parameters:
capability
- The capability.- Returns:
- true if supported.
- See Also:
-
supports
Checks if the current environment (system platform, native library, etc.) supports a given junixsocket capability. NOTE: The result may or may not be cached from a previous call or from a check upon initialization.- Parameters:
capability
- The capability.- Returns:
- true if supported.
-
ensureUnsafeSupported
Checks if the current environment (system platform, native library, etc.) supports "unsafe" operations (as controlled via theAFSocketCapability.CAPABILITY_UNSAFE
capability). If supported, the method returns normally. If not supported, anIOException
is thrown.- Throws:
IOException
- if "unsafe" operations are not supported.- See Also:
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classSocket
- Throws:
IOException
-
addCloseable
-
removeCloseable
-
getChannel
- Overrides:
getChannel
in classSocket
-
getRemoteSocketAddress
Description copied from interface:AFSomeSocket
Returns the socket's remote socket address, ornull
if unavailable/not connected, or if there was a problem retrieving it.- Specified by:
getRemoteSocketAddress
in interfaceAFSomeSocket
- Overrides:
getRemoteSocketAddress
in classSocket
- Returns:
- The remote socket address, or
null
.
-
getLocalSocketAddress
Description copied from interface:AFSomeSocketThing
Returns the socket's local socket address, ornull
if unavailable or if there was a problem retrieving it.- Specified by:
getLocalSocketAddress
in interfaceAFSomeSocketThing
- Overrides:
getLocalSocketAddress
in classSocket
- Returns:
- The local socket address, or
null
.
-
getFileDescriptor
Description copied from interface:FileDescriptorAccess
Returns the correspondingFileDescriptor
.- Specified by:
getFileDescriptor
in interfaceFileDescriptorAccess
- Returns:
- The corresponding
FileDescriptor
. - Throws:
IOException
- on error.
-
getInputStream
- Overrides:
getInputStream
in classSocket
- Throws:
IOException
-
getOutputStream
- Overrides:
getOutputStream
in classSocket
- Throws:
IOException
-
getImplExtensions
Returns the internal helper instance for address-specific extensions.- Returns:
- The helper instance.
- Throws:
UnsupportedOperationException
- if such extensions are not supported for this address type.
-
forceConnectAddress
Forces the address to be used for any subsequent call toconnect(SocketAddress)
to be the given one, regardless of what'll be passed there.- Parameters:
endpoint
- The forced endpoint address.- Returns:
- This instance.
-
connectHook
Sets the hook for any subsequent call toconnect(SocketAddress)
orconnect(SocketAddress, int)
to be the given function. The function can monitor events or even alter the target address.- Parameters:
hook
- The function that gets called for each connect call.- Returns:
- This instance.
-
checkConnectionClosed
Probes the status of the socket connection. This usually involves checking forisConnected()
, and if assumed connected, also sending a zero-length message to the remote.- Returns:
true
if the connection is known to be closed,false
if the connection is open/not closed or the condition is unknown.- Throws:
IOException
- on an unexpected error.
-
isRunningOnAndroid
public static boolean isRunningOnAndroid()Checks if we're running on Android (as far as junixsocket is concerned).- Returns:
true
if running on Android.
-
setShutdownOnClose
public void setShutdownOnClose(boolean enabled) Description copied from interface:AFSomeSocketThing
Configures whether the socket should be shutdown uponCloseable.close()
, which is the default.- Specified by:
setShutdownOnClose
in interfaceAFSomeSocketThing
- Parameters:
enabled
-true
if enabled.
-