Merge pull request #30 from robertoandrade/bugfix/tcp-harvester-interface-reuse
Fixing MuxServerSocketChannel when binding to "anyLocalAddress" and attempting to reuse socket by binding a particular interface
This commit is contained in:
commit
bfa3b487d1
@ -55,6 +55,18 @@ public class MuxServerSocketChannel
|
|||||||
*/
|
*/
|
||||||
private static final int SOCKET_CHANNEL_READ_TIMEOUT
|
private static final int SOCKET_CHANNEL_READ_TIMEOUT
|
||||||
= MuxServerSocketChannelFactory.SOCKET_CHANNEL_READ_TIMEOUT;
|
= MuxServerSocketChannelFactory.SOCKET_CHANNEL_READ_TIMEOUT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reference to 0.0.0.0 IPv4 or 0::0 IPv6 address for "wildcard" matching purposes
|
||||||
|
*/
|
||||||
|
private static final InetAddress ANY_LOCAL_ADDRESS;
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
ANY_LOCAL_ADDRESS = InetAddress.getByName("::");
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that <tt>t</tt> is not <tt>null</tt> by throwing a
|
* Asserts that <tt>t</tt> is not <tt>null</tt> by throwing a
|
||||||
@ -503,7 +515,21 @@ public class MuxServerSocketChannel
|
|||||||
{
|
{
|
||||||
aLocalAddr = null;
|
aLocalAddr = null;
|
||||||
}
|
}
|
||||||
if (aLocalAddr != null && aLocalAddr.equals(localAddr))
|
|
||||||
|
boolean matches = aLocalAddr != null && aLocalAddr.equals(localAddr);
|
||||||
|
|
||||||
|
// if not the same address, let's see if the cached one is an "anyLocalAddress" and if so let's consider it a match
|
||||||
|
if (!matches && aLocalAddr instanceof InetSocketAddress && localAddr instanceof InetSocketAddress)
|
||||||
|
{
|
||||||
|
InetSocketAddress aLocalInetAddr = (InetSocketAddress) aLocalAddr;
|
||||||
|
InetSocketAddress localInetAddr = (InetSocketAddress) localAddr;
|
||||||
|
|
||||||
|
matches =
|
||||||
|
aLocalInetAddr.getAddress().equals(ANY_LOCAL_ADDRESS) &&
|
||||||
|
aLocalInetAddr.getPort() == localInetAddr.getPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matches)
|
||||||
{
|
{
|
||||||
channel = aChannel;
|
channel = aChannel;
|
||||||
// The whole idea of using (1) a List for
|
// The whole idea of using (1) a List for
|
||||||
|
|||||||
44
test/org/ice4j/socket/jdk8/MuxServerSocketChannelTest.java
Normal file
44
test/org/ice4j/socket/jdk8/MuxServerSocketChannelTest.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package org.ice4j.socket.jdk8;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
|
||||||
|
import org.ice4j.socket.DatagramPacketFilter;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author robertoandrade
|
||||||
|
*/
|
||||||
|
public class MuxServerSocketChannelTest extends TestCase {
|
||||||
|
|
||||||
|
public void testOpenAndBindReuseWhenBindingToAllInterfacesFirst() throws IOException
|
||||||
|
{
|
||||||
|
SocketAddress allInterfaces = new InetSocketAddress("::", 4443);
|
||||||
|
SocketAddress oneInterface = new InetSocketAddress("localhost", 4443);
|
||||||
|
|
||||||
|
MuxServerSocketChannel channelForAllInterfaces =
|
||||||
|
MuxServerSocketChannel.openAndBind(null, allInterfaces, 0, new DatagramPacketFilter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean accept(DatagramPacket p)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
MuxServerSocketChannel channelForOneInterface =
|
||||||
|
MuxServerSocketChannel.openAndBind(null, oneInterface, 0, new DatagramPacketFilter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean accept(DatagramPacket p)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(channelForAllInterfaces.getFDVal(), channelForOneInterface.getFDVal());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user