private class MergingDatagramSocket.SocketContainer extends Object
MergingDatagramSocket
merges, and objects associated with the socket, including a thread
which loops reading from it.
The socket is either a DatagramSocket
or a
DelegatingSocket
instance, stored in datagramSocket
or
delegatingSocket
respectively. Exactly one of these fields must
be null.Modifier and Type | Class and Description |
---|---|
private class |
MergingDatagramSocket.SocketContainer.Buffer
Represents a
DatagramPacket for the purposes of MergingDatagramSocket.SocketContainer . |
Modifier and Type | Field and Description |
---|---|
private boolean |
closed
A flag used to signal to
thread to finish. |
private DatagramSocket |
datagramSocket
Either the socket represented by this instance, if it is a
DatagramSocket instance, or null if it is not. |
private DelegatingSocket |
delegatingSocket
Either the socket represented by this instance, if it is a
DelegatingSocket instance, or null if it is not. |
private ArrayBlockingQueue<MergingDatagramSocket.SocketContainer.Buffer> |
pool
A pool of unused
MergingDatagramSocket.SocketContainer.Buffer instances. |
private ArrayBlockingQueue<MergingDatagramSocket.SocketContainer.Buffer> |
queue
The queue to which packets received from this instance's socket are
added.
|
private SocketAddress |
remoteAddress
The remote address of the last received packet.
|
private Thread |
thread
The thread which reads packets from this instance's socket and adds
them to
queue . |
Constructor and Description |
---|
SocketContainer(DatagramSocket socket)
Initializes a
MergingDatagramSocket.SocketContainer instance using a DatagramSocket as its socket. |
SocketContainer(DelegatingSocket socket)
Initializes a
MergingDatagramSocket.SocketContainer instance using a DatagramSocket as its socket. |
Modifier and Type | Method and Description |
---|---|
private void |
accepted(DatagramPacket pkt)
Notifies this
MergingDatagramSocket.SocketContainer that a particular
DatagramPacket was received from it, and was accepted (as
opposed to e.g. |
private void |
close(boolean remove)
Closes this
MergingDatagramSocket.SocketContainer , stopping it's reading thread,
and, if necessary removing it from the merging socket. |
private boolean |
doReceive(MergingDatagramSocket.SocketContainer.Buffer buffer)
Tries to receive a packet from the underlying socket into
buffer . |
private long |
getFirstReceivedTime() |
private MergingDatagramSocket.SocketContainer.Buffer |
getFreeBuffer() |
private InetAddress |
getLocalAddress() |
private int |
getLocalPort() |
SocketAddress |
getLocalSocketAddress() |
private Object |
getSocket() |
private void |
init()
Initializes and starts the thread of this instance.
|
private void |
maybeUpdateActive()
Makes this
MergingDatagramSocket.SocketContainer the active socket container for
this MergingDatagramSocket , if it isn't already the active
socket. |
private void |
receive(DatagramPacket p)
Copies a packet from this
MergingDatagramSocket.SocketContainer 's queue into
p . |
private void |
runInReaderThread()
Reads from the underlying socket and adds the read packets to
queue . |
private void |
send(DatagramPacket pkt)
|
private void |
setTarget(DatagramPacket pkt)
Sets the
SocketAddress of pkt to the remote address
that this MergingDatagramSocket.SocketContainer should send packets to. |
String |
toString()
Returns a
String representation of this MergingDatagramSocket.SocketContainer . |
private final DatagramSocket datagramSocket
DatagramSocket
instance, or null
if it is not.private final DelegatingSocket delegatingSocket
DelegatingSocket
instance, or null
if it is not.private final ArrayBlockingQueue<MergingDatagramSocket.SocketContainer.Buffer> queue
private final ArrayBlockingQueue<MergingDatagramSocket.SocketContainer.Buffer> pool
MergingDatagramSocket.SocketContainer.Buffer
instances.private boolean closed
thread
to finish.private SocketAddress remoteAddress
MergingDatagramSocket.SocketContainer
via receive(DatagramPacket)
, and
not when a packet is received from the underlying socket by its
read thread. This is in order to prevent poisoning of the remote
address, since the verification of the address is performed by
the MergingDatagramSocket
after it invokes
receive(DatagramPacket)
.SocketContainer(DelegatingSocket socket)
MergingDatagramSocket.SocketContainer
instance using a DatagramSocket
as its socket.socket
- the socket.SocketContainer(DatagramSocket socket)
MergingDatagramSocket.SocketContainer
instance using a DatagramSocket
as its socket.socket
- the socket.private void init()
private MergingDatagramSocket.SocketContainer.Buffer getFreeBuffer()
MergingDatagramSocket.SocketContainer.Buffer
instance.private void runInReaderThread()
private boolean doReceive(MergingDatagramSocket.SocketContainer.Buffer buffer) throws IOException
buffer
.buffer
- the buffer into which to receive.true
if the method succeeded, or false
if the
thread was interrupted or this MergingDatagramSocket.SocketContainer
was closed.IOException
- if receiving failed due to an I/O error from the
underlying socket.private void maybeUpdateActive()
MergingDatagramSocket.SocketContainer
the active socket container for
this MergingDatagramSocket
, if it isn't already the active
socket.private void receive(DatagramPacket p)
MergingDatagramSocket.SocketContainer
's queue into
p
. Does not block.p
- the DatagramPacket
to receive into.private long getFirstReceivedTime()
-1
if the queue is empty.private InetAddress getLocalAddress()
Delegates to the underlying socket (either datagramSocket
or
delegatingSocket
).
private int getLocalPort()
Delegates to the underlying socket (either datagramSocket
or
delegatingSocket
).
public SocketAddress getLocalSocketAddress()
Delegates to the underlying socket (either datagramSocket
or
delegatingSocket
).
public String toString()
String
representation of this MergingDatagramSocket.SocketContainer
.private void send(DatagramPacket pkt) throws IOException
pkt
- the packet to send.IOException
private void setTarget(DatagramPacket pkt)
SocketAddress
of pkt
to the remote address
that this MergingDatagramSocket.SocketContainer
should send packets to.pkt
- the packet for which to set the socket address.private void accepted(DatagramPacket pkt)
MergingDatagramSocket.SocketContainer
that a particular
DatagramPacket
was received from it, and was accepted (as
opposed to e.g. having been discarded due to its remote address not
being authorized).pkt
- the accepted packet.private Object getSocket()
MergingDatagramSocket.SocketContainer
.private void close(boolean remove)
MergingDatagramSocket.SocketContainer
, stopping it's reading thread,
and, if necessary removing it from the merging socket.remove
- whether to remove this container from the merging
socket.Copyright © 2018. All rights reserved.