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 concreteAFSocketAddress
that is supported by this type.
- All Implemented Interfaces:
Closeable
,AutoCloseable
,AFSocketExtensions
,AFSomeSocket
,AFSomeSocketThing
,FileDescriptorAccess
- Direct Known Subclasses:
AFSYSTEMDatagramSocket
,AFTIPCDatagramSocket
,AFUNIXDatagramSocket
,AFVSOCKDatagramSocket
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
Modifier and TypeClassDescriptionstatic interface
AFDatagramSocket.Constructor<A extends AFSocketAddress>
A reference to the constructor of anAFDatagramSocket
subclass. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaccept()
Accepts a connection to this socket.protected final AFAddressFamily
<A> Returns the address family supported by this implementation.final void
bind
(SocketAddress addr) final void
close()
final void
connect
(InetAddress address, int port) final void
connect
(SocketAddress addr) final void
ensureAncillaryReceiveBufferSize
(int minSize) Ensures a minimum ancillary receive buffer size.final int
Returns the size of the receive buffer for ancillary messages (in bytes).final FileDescriptor
Returns the correspondingFileDescriptor
.protected AFSocketImplExtensions
<A> Returns the internal helper instance for address-specific extensions.final @Nullable A
Returns the socket's local socket address, ornull
if unavailable or if there was a problem retrieving it.<T> T
getOption
(SocketOption<T> name) <T> T
getOption
(AFSocketOption<T> name) Returns the value of a junixsocket socket option.final @Nullable A
Returns the socket's remote socket address, ornull
if unavailable/not connected, or if there was a problem retrieving it.final boolean
isBound()
final boolean
isClosed()
final boolean
final boolean
Checks if thisAFDatagramSocket
's bound filename should be removed uponclose()
.final void
listen
(int backlog) Sets this socket into "listen" state, which allows subsequent calls toaccept()
receive any connection attempt.protected abstract AFDatagramChannel
<A> Creates a newDatagramChannel
that is associated with this socket.protected abstract AFDatagramSocket
<A> Returns a newAFDatagramSocket
instance to be used foraccept()
, i.e., noFileDescriptor
is associated.protected static final <A extends AFSocketAddress>
AFDatagramSocket<A> newInstance
(AFDatagramSocket.Constructor<A> constructor) Returns a newAFDatagramSocket
instance.protected static final <A extends AFSocketAddress>
AFDatagramSocket<A> newInstance
(AFDatagramSocket.Constructor<A> constructor, FileDescriptor fdObj, int localPort, int remotePort) Creates a newAFDatagramSocket
.final void
Reads the next received packet without actually removing it from the queue.final void
final void
final void
setAncillaryReceiveBufferSize
(int size) Sets the size of the receive buffer for ancillary messages (in bytes).final void
setDeleteOnClose
(boolean b) Enables/disables deleting thisAFDatagramSocket
's bound filename uponclose()
.<T> DatagramSocket
setOption
(SocketOption<T> name, T value) <T> DatagramSocket
setOption
(AFSocketOption<T> name, T value) Sets the value of a socket option.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.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 newAFDatagramSocket
instance.- Parameters:
impl
- The correspondingSocketImpl
class.
-
-
Method Details
-
newChannel
Creates a newDatagramChannel
that is associated with this socket.- Returns:
- The channel.
-
socketAddressClass
Returns theAFSocketAddress
type 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 newAFDatagramSocket
instance.- Type Parameters:
A
- The concreteAFSocketAddress
that 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 concreteAFSocketAddress
that 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:
connect
in 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:
send
in classDatagramSocket
- Throws:
IOException
-
connect
- Overrides:
connect
in classDatagramSocket
- Throws:
SocketException
-
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 classDatagramSocket
- Returns:
- The remote socket address, or
null
.
-
isConnected
public final boolean isConnected()- Overrides:
isConnected
in classDatagramSocket
-
isBound
public final boolean isBound()- Overrides:
isBound
in classDatagramSocket
-
close
public final void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classDatagramSocket
-
bind
- Overrides:
bind
in classDatagramSocket
- Throws:
SocketException
-
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 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:
true
if 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: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).
-
isClosed
public final boolean isClosed()- Overrides:
isClosed
in classDatagramSocket
-
getChannel
- Overrides:
getChannel
in classDatagramSocket
-
getFileDescriptor
Description copied from interface:FileDescriptorAccess
Returns the correspondingFileDescriptor
.- Specified by:
getFileDescriptor
in interfaceFileDescriptorAccess
- Returns:
- The corresponding
FileDescriptor
. - Throws:
IOException
- on error.
-
receive
- Overrides:
receive
in 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 inlisten
state by callingbind(SocketAddress)
, followed bylisten(int)
, and 2., the socket type must allow listen/accept. This is true forAFSocketType.SOCK_SEQPACKET
AF_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_SEQPACKET
AF_UNIX sockets, for example.- Parameters:
backlog
- The backlog, or0
for default.- Throws:
IOException
- on error.
-
newDatagramSocketInstance
Returns a newAFDatagramSocket
instance to be used foraccept()
, i.e., noFileDescriptor
is associated.- Returns:
- The new instance.
- Throws:
IOException
- on error.
-
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.
-
getOption
- Overrides:
getOption
in classDatagramSocket
- Throws:
IOException
-
setOption
- Overrides:
setOption
in classDatagramSocket
- Throws:
IOException
-