Top Description Inners Fields Constructors Methods
org.newsclub.net.unix

public abstract Class AFDatagramSocket<A extends AFSocketAddress>

extends DatagramSocketShim
implements AFSomeSocket, AFSocketExtensions
Class Inheritance
All Implemented Interfaces
org.newsclub.net.unix.AFSocketExtensions, org.newsclub.net.unix.AFSomeSocket, org.newsclub.net.unix.AFSomeSocketThing, org.newsclub.net.unix.FileDescriptorAccess, java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
org.newsclub.net.unix.AFGenericDatagramSocket, org.newsclub.net.unix.AFUNIXDatagramSocket
Type Parameters
<A>
The concrete AFSocketAddress that is supported by this type.
Imports
java.io.FileDescriptor, .IOException, java.net.DatagramPacket, .DatagramSocket, .InetAddress, .InetSocketAddress, .SocketAddress, .SocketException, .SocketImpl, java.nio.channels.AlreadyBoundException, .DatagramChannel, .IllegalBlockingModeException, java.util.concurrent.atomic.AtomicBoolean, org.eclipse.jdt.annotation.Nullable, com.kohlschutter.annotations.compiletime.SuppressFBWarnings

A DatagramSocket implementation that works with junixsocket.
Author
Christian Kohlschütter

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
AFDatagramSocket.Constructor<
The concrete AFSocketAddress that is supported by this type.
A extends AFSocketAddress
>

A reference to the constructor of an AFDatagramSocket subclass.

Field Summary

Modifier and TypeField and Description
private final AncillaryDataSupport
private final AFDatagramChannel<A>
private final AtomicBoolean
private final AtomicBoolean
private final AFDatagramSocketImpl<A>
private static final InetSocketAddress

Constructor Summary

AccessConstructor and Description
protected
AFDatagramSocket(final AFDatagramSocketImpl<A>
The corresponding SocketImpl class.
impl
)

Creates a new AFDatagramSocket instance.

Method Summary

Modifier and TypeMethod and Description
public AFDatagramSocket<A>

Returns:

The accepted datagram socket.
accept
()

Accepts a connection to this socket.

pack-priv AFDatagramSocket<A>
accept1(boolean throwOnFail)

protected final AFAddressFamily<A>

Returns:

The family.
addressFamily
()

Returns the address family supported by this implementation.

public final synchronized void
bind(SocketAddress
The address and port to bind to.
addr
)

Overrides java.net.DatagramSocket.bind.

Binds this DatagramSocket to a specific address and port.

public final void
close()

Overrides java.net.DatagramSocket.close.

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.

public final void
connect(InetAddress
the remote address for the socket
address
,
int
the remote port for the socket.
port
)

Overrides java.net.DatagramSocket.connect.

Connects the socket to a remote address for this socket.

public final synchronized void
connect(SocketAddress
The remote address.
addr
)

Overrides java.net.DatagramSocket.connect.

Connects this socket to a remote socket address (IP address + port number).

public final void
ensureAncillaryReceiveBufferSize(int
The minimum size (in bytes).
minSize
)

Implements org.newsclub.net.unix.AFSocketExtensions.ensureAncillaryReceiveBufferSize.

Ensures a minimum ancillary receive buffer size.

pack-priv final AFDatagramSocketImpl<A>
pack-priv final AFDatagramSocketImpl<A>
getAFImpl(boolean create)

pack-priv final AncillaryDataSupport

Returns:

The instance.
getAncillaryDataSupport
()

Returns the AncillaryDataSupport instance.

public final int
getAncillaryReceiveBufferSize()

Implements org.newsclub.net.unix.AFSocketExtensions.getAncillaryReceiveBufferSize.

Returns the size of the receive buffer for ancillary messages (in bytes).

public AFDatagramChannel<A>
getChannel()

Overrides java.net.DatagramSocket.getChannel.

Returns the unique java.nio.channels.DatagramChannel object associated with this datagram socket, if any.

public final FileDescriptor
protected AFSocketImplExtensions<A>

Returns:

The helper instance.
getImplExtensions
()

Returns the internal helper instance for address-specific extensions.

public final @Nullable A
getLocalSocketAddress()

Overrides java.net.DatagramSocket.getLocalSocketAddress.

Implements org.newsclub.net.unix.AFSomeSocketThing.getLocalSocketAddress.

Returns the socket's local socket address, or null if unavailable or if there was a problem retrieving it.

public <
The type of the socket option value.
T
>
T

Returns:

The value of the socket option.
getOption
(AFSocketOption<T>
The socket option.
name
)

Implements abstract org.newsclub.net.unix.DatagramSocketShim.getOption.

Returns the value of a junixsocket socket option.

public final synchronized @Nullable A
getRemoteSocketAddress()

Overrides java.net.DatagramSocket.getRemoteSocketAddress.

Implements org.newsclub.net.unix.AFSomeSocket.getRemoteSocketAddress.

Returns the socket's remote socket address, or null if unavailable/not connected, or if there was a problem retrieving it.

pack-priv final void
pack-priv final void
public final boolean
isBound()

Overrides java.net.DatagramSocket.isBound.

Returns the binding state of the socket.

public final boolean
isClosed()

Overrides java.net.DatagramSocket.isClosed.

Returns whether the socket is closed or not.

public final boolean
isConnected()

Overrides java.net.DatagramSocket.isConnected.

Returns the connection state of the socket.

public final boolean

Returns:

true if an attempt is made to delete the socket file upon close().
isDeleteOnClose
()

Checks if this AFDatagramSocket's bound filename should be removed upon close().

public final void
listen(int
The backlog, or 0 for default.
backlog
)

Sets this socket into "listen" state, which allows subsequent calls to accept() receive any connection attempt.

protected abstract AFDatagramChannel<A>

Returns:

The channel.
newChannel
()

Creates a new DatagramChannel that is associated with this socket.

protected abstract AFDatagramSocket<A>

Returns:

The new instance.
newDatagramSocketInstance
()

Returns a new AFDatagramSocket instance to be used for accept(), i.e., no FileDescriptor is associated.

protected static final <
The concrete AFSocketAddress that is supported by this type.
A extends AFSocketAddress
>
AFDatagramSocket<A>

Returns:

The new instance.
newInstance
(AFDatagramSocket.Constructor<A>
The supplying constructor.
constructor
)

Returns a new AFDatagramSocket instance.

protected static final <
The concrete AFSocketAddress that is supported by this type.
A extends AFSocketAddress
>
AFDatagramSocket<A>

Returns:

The new instance.
newInstance
(AFDatagramSocket.Constructor<A>
The supplying constructor.
constructor
,
FileDescriptor
The file descriptor.
fdObj
,
int
The local port.
localPort
,
int
The remote port.
remotePort
)

Creates a new AFDatagramSocket.

public final void
peek(DatagramPacket
The packet.
p
)

Reads the next received packet without actually removing it from the queue.

public final void
receive(DatagramPacket
the DatagramPacket into which to place the incoming data.
p
)

Overrides java.net.DatagramSocket.receive.

Receives a datagram packet from this socket.

public final void
send(DatagramPacket
the DatagramPacket to be sent.
p
)

Overrides java.net.DatagramSocket.send.

Sends a datagram packet from this socket.

public final void
setAncillaryReceiveBufferSize(int
The size.
size
)

Implements org.newsclub.net.unix.AFSocketExtensions.setAncillaryReceiveBufferSize.

Sets the size of the receive buffer for ancillary messages (in bytes).

public final void
setDeleteOnClose(boolean
Enabled if true.
b
)

Enables/disables deleting this AFDatagramSocket's bound filename upon close().

public <
The type of the socket option value.
T
>
DatagramSocket

Returns:

this DatagramSocket.
setOption
(AFSocketOption<T>
The socket option.
name
,
T
The value of the socket option.
value
)

Implements abstract org.newsclub.net.unix.DatagramSocketShim.setOption.

Sets the value of a socket option.

public void
setShutdownOnClose(boolean
true if enabled.
enabled
)

Implements org.newsclub.net.unix.AFSomeSocketThing.setShutdownOnClose.

Configures whether the socket should be shutdown upon close(), which is the default.

protected final Class<? extends AFSocketAddress>

Returns:

The supported AFSocketAddress.
socketAddressClass
()

Returns the AFSocketAddress type supported by this socket.

Inherited from org.newsclub.net.unix.DatagramSocketShim:
getOptionsetOption

Field Detail

ancillaryDataSupportback to summary
private final AncillaryDataSupport ancillaryDataSupport
channelback to summary
private final AFDatagramChannel<A> channel
Annotations
@SuppressWarnings:this-escape
createdback to summary
private final AtomicBoolean created
deleteOnCloseback to summary
private final AtomicBoolean deleteOnClose
implback to summary
private final AFDatagramSocketImpl<A> impl
WILDCARD_ADDRESSback to summary
private static final InetSocketAddress WILDCARD_ADDRESS

Constructor Detail

AFDatagramSocketback to summary
protected AFDatagramSocket(final AFDatagramSocketImpl<A> impl)

Creates a new AFDatagramSocket instance.

Parameters
impl:AFDatagramSocketImpl<A>

The corresponding SocketImpl class.

Method Detail

acceptback to summary
public AFDatagramSocket<A> accept() throws IOException

Accepts a connection to this socket. Note that 1., the socket must be in listen state by calling bind(SocketAddress), followed by listen(int), and 2., the socket type must allow listen/accept. This is true for AFSocketType#SOCK_SEQPACKET AF_UNIX sockets, for example.

Returns:AFDatagramSocket<A>

The accepted datagram socket.

Exceptions
IOException:
on error.
See Also
listen(int)
accept1back to summary
pack-priv AFDatagramSocket<A> accept1(boolean throwOnFail) throws IOException
addressFamilyback to summary
protected final AFAddressFamily<A> addressFamily()

Returns the address family supported by this implementation.

Returns:AFAddressFamily<A>

The family.

bindback to summary
public final synchronized void bind(SocketAddress addr) throws SocketException

Overrides java.net.DatagramSocket.bind.

Doc from java.net.DatagramSocket.bind.

Binds this DatagramSocket to a specific address and port.

If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.

Parameters
addr:SocketAddress

The address and port to bind to.

Annotations
@Override
@SuppressWarnings:PMD.CognitiveComplexity
Exceptions
SocketException:
if any error happens during the bind, or if the socket is already bound or is closed.
closeback to summary
public final void close()

Overrides java.net.DatagramSocket.close.

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
connectback to summary
public final void connect(InetAddress address, int port)

Overrides java.net.DatagramSocket.connect.

Doc from java.net.DatagramSocket.connect.

Connects the socket to a remote address for this socket. When a socket is connected to a remote address, packets may only be sent to or received from that address. By default a datagram socket is not connected. If the socket is already closed, then this method has no effect.

If this socket is not bound then this method will first cause the socket to be bound to an address that is assigned automatically, as if invoking the bind method with a parameter of null. If the remote destination to which the socket is connected does not exist, or is otherwise unreachable, and if an ICMP destination unreachable packet has been received for that address, then a subsequent call to send or receive may throw a PortUnreachableException. Note, there is no guarantee that the exception will be thrown.

If this socket is already connected, then this method will attempt to connect to the given address. If this connect fails then the state of this socket is unknown - it may or may not be connected to the address that it was previously connected to.

When the socket is connected, the send method checks that the packet's address matches the remote address that the socket is connected to. A socket connected to a multicast address may only be used to send packets. Datagrams in the socket's socket receive buffer, which have not been received before invoking this method, may be discarded.

Parameters
address:InetAddress

the remote address for the socket

port:int

the remote port for the socket.

Annotations
@Override
connectback to summary
public final synchronized void connect(SocketAddress addr) throws SocketException

Overrides java.net.DatagramSocket.connect.

Doc from java.net.DatagramSocket.connect.

Connects this socket to a remote socket address (IP address + port number).

If given an InetSocketAddress, this method behaves as if invoking connect(InetAddress,int) with the given socket addresses IP address and port number, except that the SocketException that may be raised is not wrapped in an UncheckedIOException. Datagrams in the socket's socket receive buffer, which have not been received before invoking this method, may be discarded.

If this socket is already connected, then this method will attempt to connect to the given address. If this connect fails then the state of this socket is unknown - it may or may not be connected to the address that it was previously connected to.

Parameters
addr:SocketAddress

The remote address.

Annotations
@Override
Exceptions
SocketException:
if the connect fails
ensureAncillaryReceiveBufferSizeback to summary
public final void ensureAncillaryReceiveBufferSize(int minSize)

Implements org.newsclub.net.unix.AFSocketExtensions.ensureAncillaryReceiveBufferSize.

Doc from org.newsclub.net.unix.AFSocketExtensions.ensureAncillaryReceiveBufferSize.

Ensures a minimum ancillary receive buffer size.

Parameters
minSize:int

The minimum size (in bytes).

Annotations
@Override
getAFImplback to summary
pack-priv final AFDatagramSocketImpl<A> getAFImpl()
getAFImplback to summary
pack-priv final AFDatagramSocketImpl<A> getAFImpl(boolean create)
getAncillaryDataSupportback to summary
pack-priv final AncillaryDataSupport getAncillaryDataSupport()

Returns the AncillaryDataSupport instance.

Returns:AncillaryDataSupport

The instance.

getAncillaryReceiveBufferSizeback to summary
public final int getAncillaryReceiveBufferSize()

Implements org.newsclub.net.unix.AFSocketExtensions.getAncillaryReceiveBufferSize.

Doc from org.newsclub.net.unix.AFSocketExtensions.getAncillaryReceiveBufferSize.

Returns the size of the receive buffer for ancillary messages (in bytes).

Returns:int

The size.

Annotations
@Override
getChannelback to summary
public AFDatagramChannel<A> getChannel()

Overrides java.net.DatagramSocket.getChannel.

Doc from java.net.DatagramSocket.getChannel.

Returns the unique java.nio.channels.DatagramChannel object associated with this datagram socket, if any.

A datagram socket will have a channel if, and only if, the channel itself was created via the DatagramChannel.open method.

Returns:AFDatagramChannel<A>

the datagram channel associated with this datagram socket, or null if this socket was not created for a channel

Annotations
@SuppressFBWarnings:EI_EXPOSE_REP
@Override
getFileDescriptorback to summary
public final FileDescriptor getFileDescriptor() throws IOException

Implements org.newsclub.net.unix.FileDescriptorAccess.getFileDescriptor.

Doc from org.newsclub.net.unix.FileDescriptorAccess.getFileDescriptor.

Returns the corresponding FileDescriptor.

Returns:FileDescriptor

The corresponding FileDescriptor.

Annotations
@Override
Exceptions
IOException:
on error.
getImplExtensionsback to summary
protected AFSocketImplExtensions<A> getImplExtensions()

Returns the internal helper instance for address-specific extensions.

Returns:AFSocketImplExtensions<A>

The helper instance.

Exceptions
UnsupportedOperationException:
if such extensions are not supported for this address type.
getLocalSocketAddressback to summary
public final @Nullable A getLocalSocketAddress()

Overrides java.net.DatagramSocket.getLocalSocketAddress.

Implements org.newsclub.net.unix.AFSomeSocketThing.getLocalSocketAddress.

Doc from org.newsclub.net.unix.AFSomeSocketThing.getLocalSocketAddress.

Returns the socket's local socket address, or null if unavailable or if there was a problem retrieving it.

Returns:@Nullable A

The local socket address, or null.

Annotations
@Override
getOptionback to summary
public <T> T getOption(AFSocketOption<T> name) throws IOException

Implements abstract org.newsclub.net.unix.DatagramSocketShim.getOption.

Returns the value of a junixsocket socket option.

Parameters
<T>
The type of the socket option value.
name:AFSocketOption<T>

The socket option.

Returns:T

The value of the socket option.

Annotations
@Override
Exceptions
IOException:
on error.
getRemoteSocketAddressback to summary
public final synchronized @Nullable A getRemoteSocketAddress()

Overrides java.net.DatagramSocket.getRemoteSocketAddress.

Implements org.newsclub.net.unix.AFSomeSocket.getRemoteSocketAddress.

Doc from org.newsclub.net.unix.AFSomeSocket.getRemoteSocketAddress.

Returns the socket's remote socket address, or null if unavailable/not connected, or if there was a problem retrieving it.

Returns:@Nullable A

The remote socket address, or null.

Annotations
@Override
internalDummyBindback to summary
pack-priv final void internalDummyBind() throws SocketException
internalDummyConnectback to summary
pack-priv final void internalDummyConnect() throws SocketException
isBoundback to summary
public final boolean isBound()

Overrides java.net.DatagramSocket.isBound.

Doc from java.net.DatagramSocket.isBound.

Returns the binding state of the socket.

If the socket was bound prior to being closed, then this method will continue to return true after the socket is closed.

Returns:boolean

true if the socket successfully bound to an address

Annotations
@Override
isClosedback to summary
public final boolean isClosed()

Overrides java.net.DatagramSocket.isClosed.

Doc from java.net.DatagramSocket.isClosed.

Returns whether the socket is closed or not.

Returns:boolean

true if the socket has been closed

Annotations
@Override
isConnectedback to summary
public final boolean isConnected()

Overrides java.net.DatagramSocket.isConnected.

Doc from java.net.DatagramSocket.isConnected.

Returns the connection state of the socket.

If the socket was connected prior to being closed, then this method will continue to return true after the socket is closed.

Returns:boolean

true if the socket successfully connected to a server

Annotations
@Override
isDeleteOnCloseback to summary
public final boolean isDeleteOnClose()

Checks if this AFDatagramSocket's bound filename should be removed upon close(). Deletion is not guaranteed, especially when not supported (e.g., addresses in the abstract namespace).

Returns:boolean

true if an attempt is made to delete the socket file upon close().

listenback to summary
public final void listen(int backlog) throws IOException

Sets this socket into "listen" state, which allows subsequent calls to accept() receive any connection attempt. Note that 1., the socket must be bound to a local address using bind(SocketAddress), and 2., the socket type must allow listen/accept. This is true for AFSocketType#SOCK_SEQPACKET AF_UNIX sockets, for example.

Parameters
backlog:int

The backlog, or 0 for default.

Exceptions
IOException:
on error.
newChannelback to summary
protected abstract AFDatagramChannel<A> newChannel()

Creates a new DatagramChannel that is associated with this socket.

Returns:AFDatagramChannel<A>

The channel.

newDatagramSocketInstanceback to summary
protected abstract AFDatagramSocket<A> newDatagramSocketInstance() throws IOException

Returns a new AFDatagramSocket instance to be used for accept(), i.e., no FileDescriptor is associated.

Returns:AFDatagramSocket<A>

The new instance.

Exceptions
IOException:
on error.
newInstanceback to summary
protected static final <A extends AFSocketAddress> AFDatagramSocket<A> newInstance(AFDatagramSocket.Constructor<A> constructor) throws IOException

Returns a new AFDatagramSocket instance.

Parameters
<A>
The concrete AFSocketAddress that is supported by this type.
constructor:AFDatagramSocket.Constructor<A>

The supplying constructor.

Returns:AFDatagramSocket<A>

The new instance.

Exceptions
IOException:
on error.
newInstanceback to summary
protected static final <A extends AFSocketAddress> AFDatagramSocket<A> newInstance(AFDatagramSocket.Constructor<A> constructor, FileDescriptor fdObj, int localPort, int remotePort) throws IOException

Creates a new AFDatagramSocket.

Parameters
<A>
The concrete AFSocketAddress that is supported by this type.
constructor:AFDatagramSocket.Constructor<A>

The supplying constructor.

fdObj:FileDescriptor

The file descriptor.

localPort:int

The local port.

remotePort:int

The remote port.

Returns:AFDatagramSocket<A>

The new instance.

Exceptions
IOException:
on error.
peekback to summary
public final void peek(DatagramPacket p) throws IOException

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:DatagramPacket

The packet.

Exceptions
IOException:
on error.
receiveback to summary
public final void receive(DatagramPacket p) throws IOException

Overrides java.net.DatagramSocket.receive.

Doc from java.net.DatagramSocket.receive.

Receives a datagram packet from this socket. This method blocks until a datagram is received. When this method returns, the DatagramPacket's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine. The length field of the datagram packet object contains the length of the received message. If the message is longer than the packet's length, the message is truncated.

This method is interruptible in the following circumstances:

  1. The datagram socket is associated with a DatagramChannel. In that case, interrupting a thread receiving a datagram packet will close the underlying channel and cause this method to throw java.nio.channels.ClosedByInterruptException with the interrupt status set.
  2. The datagram socket uses the system-default socket implementation and a virtual thread is receiving a datagram packet. In that case, interrupting the virtual thread will cause it to wakeup and close the socket. This method will then throw SocketException with the interrupt status set.
Parameters
p:DatagramPacket

the DatagramPacket into which to place the incoming data.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs, or the socket is closed.
sendback to summary
public final void send(DatagramPacket p) throws IOException

Overrides java.net.DatagramSocket.send.

Doc from java.net.DatagramSocket.send.

Sends a datagram packet from this socket. The DatagramPacket includes information indicating the data to be sent, its length, the IP address of the remote host, and the port number on the remote host.

Parameters
p:DatagramPacket

the DatagramPacket to be sent.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs, or the socket is closed.
setAncillaryReceiveBufferSizeback to summary
public final void setAncillaryReceiveBufferSize(int size)

Implements org.newsclub.net.unix.AFSocketExtensions.setAncillaryReceiveBufferSize.

Doc from org.newsclub.net.unix.AFSocketExtensions.setAncillaryReceiveBufferSize.

Sets the size of the receive buffer for ancillary messages (in bytes). To disable handling ancillary messages, set it to 0 (default).

Parameters
size:int

The size.

Annotations
@Override
setDeleteOnCloseback to summary
public final void setDeleteOnClose(boolean b)

Enables/disables deleting this AFDatagramSocket's bound filename upon close(). Deletion is not guaranteed, especially when not supported (e.g., addresses in the abstract namespace).

Parameters
b:boolean

Enabled if true.

setOptionback to summary
public <T> DatagramSocket setOption(AFSocketOption<T> name, T value) throws IOException

Implements abstract org.newsclub.net.unix.DatagramSocketShim.setOption.

Sets the value of a socket option.

Parameters
<T>
The type of the socket option value.
name:AFSocketOption<T>

The socket option.

value:T

The value of the socket option.

Returns:DatagramSocket

this DatagramSocket.

Annotations
@Override
Exceptions
IOException:
on error.
setShutdownOnCloseback to summary
public void setShutdownOnClose(boolean enabled)

Implements org.newsclub.net.unix.AFSomeSocketThing.setShutdownOnClose.

Doc from org.newsclub.net.unix.AFSomeSocketThing.setShutdownOnClose.

Configures whether the socket should be shutdown upon close(), which is the default.

Parameters
enabled:boolean

true if enabled.

Annotations
@Override
socketAddressClassback to summary
protected final Class<? extends AFSocketAddress> socketAddressClass()

Returns the AFSocketAddress type supported by this socket.

Returns:Class<? extends AFSocketAddress>

The supported AFSocketAddress.

org.newsclub.net.unix back to summary

public Interface AFDatagramSocket.Constructor<A extends AFSocketAddress>

Annotations
@FunctionalInterface
Type Parameters
<A>
The concrete AFSocketAddress that is supported by this type.

A reference to the constructor of an AFDatagramSocket subclass.

Method Summary

Modifier and TypeMethod and Description
public AFDatagramSocket<A>

Returns:

The new instance.
newSocket
(FileDescriptor
The file descriptor.
fd
)

Constructs a new DatagramSocket instance.

Method Detail

newSocketback to summary
public AFDatagramSocket<A> newSocket(FileDescriptor fd) throws IOException

Constructs a new DatagramSocket instance.

Parameters
fd:FileDescriptor

The file descriptor.

Returns:AFDatagramSocket<A>

The new instance.

Exceptions
IOException:
on error.