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 concreteAFSocketAddressthat 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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAFSocket.Constructor<A extends AFSocketAddress>The reference to the constructor of anAFSocketsubclass. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAFSocket(AFSocketImpl<A> impl, AFSocketAddressFromHostname<A> afh) Creates a newAFSocketinstance. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddCloseable(Closeable closeable) Registers aCloseablethat should be closed when this socket is closed.final voidbind(SocketAddress bindpoint) Not supported, since it's not necessary for client sockets.booleanProbes the status of the socket connection.final voidclose()final voidconnect(SocketAddress endpoint) final voidconnect(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 newAFSocketand connects it to the givenAFSocketAddressusing 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 newAFSocketand connects it to the givenAFSocketAddress.final voidensureAncillaryReceiveBufferSize(int minSize) Ensures a minimum ancillary receive buffer size.static voidChecks ifAFSockets are supported by the current Java VM.static final voidChecks if the current environment (system platform, native library, etc.) supports "unsafe" operations (as controlled via theAFSocketCapability.CAPABILITY_UNSAFEcapability).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 intReturns the size of the receive buffer for ancillary messages (in bytes).final FileDescriptorReturns the correspondingFileDescriptor.protected final AFSocketImplExtensions<A> Returns the internal helper instance for address-specific extensions.final AFInputStreamstatic final StringReturns an identifier of the loaded native library, ornullif the library hasn't been loaded yet.final AReturns the socket's local socket address, ornullif unavailable or if there was a problem retrieving it.final AFOutputStreamfinal AReturns the socket's remote socket address, ornullif unavailable/not connected, or if there was a problem retrieving it.static final StringReturns the version of the junixsocket library, as a string, for debugging purposes.final booleanisBound()final booleanisClosed()final booleanstatic booleanChecks if we're running on Android (as far as junixsocket is concerned).static booleanReturnstrueiffAFSockets are supported by the current Java VM.protected abstract AFSocketChannel<A> Creates a newAFSocketChannelfor this socket.protected static final <A extends AFSocketAddress>
AFSocket<A> newInstance(AFSocket.Constructor<A> constr, AFSocketFactory<A> factory) Creates a new, unboundAFSocket.final voidremoveCloseable(Closeable closeable) Unregisters a previously registeredCloseable.final voidsetAncillaryReceiveBufferSize(int size) Sets the size of the receive buffer for ancillary messages (in bytes).voidsetShutdownOnClose(boolean enabled) Configures whether the socket should be shutdown uponCloseable.close(), which is the default.protected final Class<? extends AFSocketAddress> Returns theAFSocketAddresstype supported by this socket.static final booleansupports(AFSocketCapability capability) Checks if the current environment (system platform, native library, etc.) supports a given junixsocket capability.static final booleansupports(AFUNIXSocketCapability capability) Deprecated.final StringtoString()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 newAFSocketinstance.- Parameters:
impl- The correspondingSocketImplclass.afh- The conversion helper to get a socket address from an encoded hostname.- Throws:
SocketException- on error.
-
-
Method Details
-
socketAddressClass
Returns theAFSocketAddresstype supported by this socket.- Returns:
- The supported
AFSocketAddress.
-
newChannel
Creates a newAFSocketChannelfor 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'sAFSocketconstructorfactory- 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 newAFSocketand connects it to the givenAFSocketAddress.- Type Parameters:
A- The corresponding address type.- Parameters:
constr- The implementation'sAFSocketconstructoraddr- 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 newAFSocketand connects it to the givenAFSocketAddressusing 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:
bindin classSocket- Throws:
IOException- See Also:
-
isBound
-
isConnected
public final boolean isConnected()- Overrides:
isConnectedin classSocket
-
connect
- Overrides:
connectin classSocket- Throws:
IOException
-
connect
- Overrides:
connectin classSocket- Throws:
IOException
-
toString
-
isSupported
-
ensureSupported
Checks ifAFSockets are supported by the current Java VM. If not, anUnsupportedOperationExceptionis 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
nullif it could not be determined. - See Also:
-
getLoadedLibrary
Returns an identifier of the loaded native library, ornullif 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:AFSocketExtensionsReturns the size of the receive buffer for ancillary messages (in bytes).- Specified by:
getAncillaryReceiveBufferSizein interfaceAFSocketExtensions- Returns:
- The size.
-
setAncillaryReceiveBufferSize
public final void setAncillaryReceiveBufferSize(int size) Description copied from interface:AFSocketExtensionsSets the size of the receive buffer for ancillary messages (in bytes). To disable handling ancillary messages, set it to 0 (default).- Specified by:
setAncillaryReceiveBufferSizein interfaceAFSocketExtensions- Parameters:
size- The size.
-
ensureAncillaryReceiveBufferSize
public final void ensureAncillaryReceiveBufferSize(int minSize) Description copied from interface:AFSocketExtensionsEnsures a minimum ancillary receive buffer size.- Specified by:
ensureAncillaryReceiveBufferSizein 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_UNSAFEcapability). If supported, the method returns normally. If not supported, anIOExceptionis thrown.- Throws:
IOException- if "unsafe" operations are not supported.- See Also:
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classSocket- Throws:
IOException
-
addCloseable
-
removeCloseable
-
getChannel
- Overrides:
getChannelin classSocket
-
getRemoteSocketAddress
Description copied from interface:AFSomeSocketReturns the socket's remote socket address, ornullif unavailable/not connected, or if there was a problem retrieving it.- Specified by:
getRemoteSocketAddressin interfaceAFSomeSocket- Overrides:
getRemoteSocketAddressin classSocket- Returns:
- The remote socket address, or
null.
-
getLocalSocketAddress
Description copied from interface:AFSomeSocketThingReturns the socket's local socket address, ornullif unavailable or if there was a problem retrieving it.- Specified by:
getLocalSocketAddressin interfaceAFSomeSocketThing- Overrides:
getLocalSocketAddressin classSocket- Returns:
- The local socket address, or
null.
-
getFileDescriptor
Description copied from interface:FileDescriptorAccessReturns the correspondingFileDescriptor.- Specified by:
getFileDescriptorin interfaceFileDescriptorAccess- Returns:
- The corresponding
FileDescriptor. - Throws:
IOException- on error.
-
getInputStream
- Overrides:
getInputStreamin classSocket- Throws:
IOException
-
getOutputStream
- Overrides:
getOutputStreamin 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:
trueif the connection is known to be closed,falseif 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:
trueif running on Android.
-
setShutdownOnClose
public void setShutdownOnClose(boolean enabled) Description copied from interface:AFSomeSocketThingConfigures whether the socket should be shutdown uponCloseable.close(), which is the default.- Specified by:
setShutdownOnClosein interfaceAFSomeSocketThing- Parameters:
enabled-trueif enabled.
-