Ensure we null out the previous set InetAddress on java.net.DatagramPacket when using OioDatagramChannel.

Motivation:

We need to ensure we always null out (or set) the address on the java.net.DatagramPacket when doing read or write operation as the same instance is used across different calls.

Modifications:

Null out the address if needed.

Result:

Ensure the correct remote address is used when connect / disconnect between calls and also mix these with calls that directly specify the remote address for adatagram packets.
This commit is contained in:
Norman Maurer 2017-08-08 09:19:03 +02:00
parent b30c4f899f
commit 85f5d6bf05

View File

@ -211,6 +211,8 @@ public class OioDatagramChannel extends AbstractOioMessageChannel
ByteBuf data = config.getAllocator().heapBuffer(allocHandle.guess()); ByteBuf data = config.getAllocator().heapBuffer(allocHandle.guess());
boolean free = true; boolean free = true;
try { try {
// Ensure we null out the address which may have been set before.
tmpPacket.setAddress(null);
tmpPacket.setData(data.array(), data.arrayOffset(), data.capacity()); tmpPacket.setData(data.array(), data.arrayOffset(), data.capacity());
socket.receive(tmpPacket); socket.receive(tmpPacket);
@ -268,6 +270,8 @@ public class OioDatagramChannel extends AbstractOioMessageChannel
// NioDatagramChannel // NioDatagramChannel
throw new NotYetConnectedException(); throw new NotYetConnectedException();
} }
// Ensure we null out the address which may have been set before.
tmpPacket.setAddress(null);
} }
if (data.hasArray()) { if (data.hasArray()) {
tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length); tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);