Module org.newsclub.net.unix
Package org.newsclub.net.unix
Class AFDatagramSocket<A extends AFSocketAddress>
java.lang.Object
java.net.DatagramSocket
org.newsclub.net.unix.AFDatagramSocket<A>
- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.
- All Implemented Interfaces:
Closeable,AutoCloseable,AFSocketExtensions,AFSomeSocket,AFSomeSocketThing,FileDescriptorAccess
- Direct Known Subclasses:
AFUNIXDatagramSocket
public abstract class AFDatagramSocket<A extends AFSocketAddress>
extends DatagramSocket
implements AFSomeSocket, AFSocketExtensions
A
DatagramSocket implementation that works with junixsocket.- Author:
- Christian Kohlschütter
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAFDatagramSocket.Constructor<A extends AFSocketAddress>A reference to the constructor of anAFDatagramSocketsubclass. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaccept()Accepts a connection to this socket.protected final AFAddressFamily<A> Returns the address family supported by this implementation.final voidbind(SocketAddress addr) final voidclose()final voidconnect(InetAddress address, int port) final voidconnect(SocketAddress addr) final voidensureAncillaryReceiveBufferSize(int minSize) Ensures a minimum ancillary receive buffer size.final intReturns the size of the receive buffer for ancillary messages (in bytes).final FileDescriptorReturns the correspondingFileDescriptor.protected AFSocketImplExtensions<A> Returns the internal helper instance for address-specific extensions.final @Nullable AReturns the socket's local socket address, ornullif unavailable or if there was a problem retrieving it.<T> TgetOption(SocketOption<T> name) <T> TgetOption(AFSocketOption<T> name) Returns the value of a junixsocket socket option.final @Nullable AReturns the socket's remote socket address, ornullif unavailable/not connected, or if there was a problem retrieving it.final booleanisBound()final booleanisClosed()final booleanfinal booleanChecks if thisAFDatagramSocket's bound filename should be removed uponclose().final voidlisten(int backlog) Sets this socket into "listen" state, which allows subsequent calls toaccept()receive any connection attempt.protected abstract AFDatagramChannel<A> Creates a newDatagramChannelthat is associated with this socket.protected abstract AFDatagramSocket<A> Returns a newAFDatagramSocketinstance to be used foraccept(), i.e., noFileDescriptoris associated.protected static final <A extends AFSocketAddress>
AFDatagramSocket<A> newInstance(AFDatagramSocket.Constructor<A> constructor) Returns a newAFDatagramSocketinstance.protected static final <A extends AFSocketAddress>
AFDatagramSocket<A> newInstance(AFDatagramSocket.Constructor<A> constructor, FileDescriptor fdObj, int localPort, int remotePort) Creates a newAFDatagramSocket.final voidReads the next received packet without actually removing it from the queue.final voidfinal voidfinal voidsetAncillaryReceiveBufferSize(int size) Sets the size of the receive buffer for ancillary messages (in bytes).final voidsetDeleteOnClose(boolean b) Enables/disables deleting thisAFDatagramSocket's bound filename uponclose().<T> DatagramSocketsetOption(SocketOption<T> name, T value) <T> DatagramSocketsetOption(AFSocketOption<T> name, T value) Sets the value of a socket option.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.Methods inherited from class java.net.DatagramSocket
disconnect, getBroadcast, getInetAddress, getLocalAddress, getLocalPort, getPort, getReceiveBufferSize, getReuseAddress, getSendBufferSize, getSoTimeout, getTrafficClass, joinGroup, leaveGroup, setBroadcast, setDatagramSocketImplFactory, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSoTimeout, setTrafficClass, supportedOptions
-
Constructor Details
-
AFDatagramSocket
Creates a newAFDatagramSocketinstance.- Parameters:
impl- The correspondingSocketImplclass.
-
-
Method Details
-
newChannel
Creates a newDatagramChannelthat is associated with this socket.- Returns:
- The channel.
-
socketAddressClass
Returns theAFSocketAddresstype supported by this socket.- Returns:
- The supported
AFSocketAddress.
-
newInstance
protected static final <A extends AFSocketAddress> AFDatagramSocket<A> newInstance(AFDatagramSocket.Constructor<A> constructor) throws IOException Returns a newAFDatagramSocketinstance.- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.- Parameters:
constructor- The supplying constructor.- Returns:
- The new instance.
- Throws:
IOException- on error.
-
newInstance
protected static final <A extends AFSocketAddress> AFDatagramSocket<A> newInstance(AFDatagramSocket.Constructor<A> constructor, FileDescriptor fdObj, int localPort, int remotePort) throws IOException Creates a newAFDatagramSocket.- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.- Parameters:
constructor- The supplying constructor.fdObj- The file descriptor.localPort- The local port.remotePort- The remote port.- Returns:
- The new instance.
- Throws:
IOException- on error.
-
connect
- Overrides:
connectin classDatagramSocket
-
peek
Reads the next received packet without actually removing it from the queue. In other words, once a packet is received, calling this method multiple times in a row will not have further effects on the packet contents. This call still blocks until at least one packet has been received and added to the queue.- Parameters:
p- The packet.- Throws:
IOException- on error.
-
send
- Overrides:
sendin classDatagramSocket- Throws:
IOException
-
connect
- Overrides:
connectin classDatagramSocket- Throws:
SocketException
-
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 classDatagramSocket- Returns:
- The remote socket address, or
null.
-
isConnected
public final boolean isConnected()- Overrides:
isConnectedin classDatagramSocket
-
isBound
public final boolean isBound()- Overrides:
isBoundin classDatagramSocket
-
close
public final void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classDatagramSocket
-
bind
- Overrides:
bindin classDatagramSocket- Throws:
SocketException
-
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 classDatagramSocket- Returns:
- The local socket address, or
null.
-
isDeleteOnClose
public final boolean isDeleteOnClose()Checks if thisAFDatagramSocket's bound filename should be removed uponclose(). Deletion is not guaranteed, especially when not supported (e.g., addresses in the abstract namespace).- Returns:
trueif an attempt is made to delete the socket file uponclose().
-
setDeleteOnClose
public final void setDeleteOnClose(boolean b) Enables/disables deleting thisAFDatagramSocket's bound filename uponclose(). Deletion is not guaranteed, especially when not supported (e.g., addresses in the abstract namespace).- Parameters:
b- Enabled iftrue.
-
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).
-
isClosed
public final boolean isClosed()- Overrides:
isClosedin classDatagramSocket
-
getChannel
- Overrides:
getChannelin classDatagramSocket
-
getFileDescriptor
Description copied from interface:FileDescriptorAccessReturns the correspondingFileDescriptor.- Specified by:
getFileDescriptorin interfaceFileDescriptorAccess- Returns:
- The corresponding
FileDescriptor. - Throws:
IOException- on error.
-
receive
- Overrides:
receivein classDatagramSocket- Throws:
IOException
-
addressFamily
Returns the address family supported by this implementation.- Returns:
- The family.
-
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.
-
getOption
Returns the value of a junixsocket socket option.- Type Parameters:
T- The type of the socket option value.- Parameters:
name- The socket option.- Returns:
- The value of the socket option.
- Throws:
IOException- on error.
-
setOption
Sets the value of a socket option.- Type Parameters:
T- The type of the socket option value.- Parameters:
name- The socket option.value- The value of the socket option.- Returns:
- this DatagramSocket.
- Throws:
IOException- on error.
-
accept
Accepts a connection to this socket. Note that 1., the socket must be inlistenstate by callingbind(SocketAddress), followed bylisten(int), and 2., the socket type must allow listen/accept. This is true forAFSocketType.SOCK_SEQPACKETAF_UNIX sockets, for example.- Returns:
- The accepted datagram socket.
- Throws:
IOException- on error.- See Also:
-
listen
Sets this socket into "listen" state, which allows subsequent calls toaccept()receive any connection attempt. Note that 1., the socket must be bound to a local address usingbind(SocketAddress), and 2., the socket type must allow listen/accept. This is true forAFSocketType.SOCK_SEQPACKETAF_UNIX sockets, for example.- Parameters:
backlog- The backlog, or0for default.- Throws:
IOException- on error.
-
newDatagramSocketInstance
Returns a newAFDatagramSocketinstance to be used foraccept(), i.e., noFileDescriptoris associated.- Returns:
- The new instance.
- Throws:
IOException- on error.
-
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.
-
getOption
- Overrides:
getOptionin classDatagramSocket- Throws:
IOException
-
setOption
- Overrides:
setOptionin classDatagramSocket- Throws:
IOException
-