2009-06-11 02:48:49 +02:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-06-11 02:48:49 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
2009-06-11 02:48:49 +02:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-11 02:48:49 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2009-06-11 02:48:49 +02:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.channel.socket.nio;
|
2009-06-11 02:48:49 +02:00
|
|
|
|
2012-04-02 11:57:32 +02:00
|
|
|
import io.netty.channel.ChannelException;
|
2012-12-14 16:59:23 +01:00
|
|
|
import io.netty.channel.socket.DatagramChannelConfig;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.channel.socket.DefaultDatagramChannelConfig;
|
2013-01-11 06:03:27 +01:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
2012-04-02 11:57:32 +02:00
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
import java.lang.reflect.Method;
|
2012-05-31 04:03:45 +02:00
|
|
|
import java.net.InetAddress;
|
2012-04-02 11:57:32 +02:00
|
|
|
import java.net.NetworkInterface;
|
2012-05-31 04:03:45 +02:00
|
|
|
import java.net.SocketException;
|
2012-04-02 11:57:32 +02:00
|
|
|
import java.nio.channels.DatagramChannel;
|
2012-05-31 04:03:45 +02:00
|
|
|
import java.util.Enumeration;
|
2009-06-11 02:48:49 +02:00
|
|
|
|
|
|
|
/**
|
2012-12-14 16:59:23 +01:00
|
|
|
* The default {@link NioDatagramChannelConfig} implementation.
|
2009-06-11 02:48:49 +02:00
|
|
|
*/
|
2012-05-26 00:51:22 +02:00
|
|
|
class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
|
2009-06-11 02:48:49 +02:00
|
|
|
|
2012-05-31 04:03:45 +02:00
|
|
|
private static final Object IP_MULTICAST_TTL;
|
2012-05-02 08:01:58 +02:00
|
|
|
private static final Object IP_MULTICAST_IF;
|
2012-05-31 04:03:45 +02:00
|
|
|
private static final Object IP_MULTICAST_LOOP;
|
2012-05-02 08:01:58 +02:00
|
|
|
private static final Method GET_OPTION;
|
|
|
|
private static final Method SET_OPTION;
|
|
|
|
|
|
|
|
static {
|
2014-04-08 13:56:14 +02:00
|
|
|
ClassLoader classLoader = PlatformDependent.getClassLoader(DatagramChannel.class);
|
2012-05-02 08:01:58 +02:00
|
|
|
Class<?> socketOptionType = null;
|
|
|
|
try {
|
|
|
|
socketOptionType = Class.forName("java.net.SocketOption", true, classLoader);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Not Java 7+
|
|
|
|
}
|
2012-06-08 12:28:12 +02:00
|
|
|
Class<?> stdSocketOptionType = null;
|
|
|
|
try {
|
|
|
|
stdSocketOptionType = Class.forName("java.net.StandardSocketOptions", true, classLoader);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Not Java 7+
|
|
|
|
}
|
2009-06-11 02:48:49 +02:00
|
|
|
|
2012-05-31 04:03:45 +02:00
|
|
|
Object ipMulticastTtl = null;
|
2012-05-02 08:01:58 +02:00
|
|
|
Object ipMulticastIf = null;
|
2012-05-31 04:03:45 +02:00
|
|
|
Object ipMulticastLoop = null;
|
2012-05-24 18:32:14 +02:00
|
|
|
Method getOption = null;
|
|
|
|
Method setOption = null;
|
2012-05-02 08:01:58 +02:00
|
|
|
if (socketOptionType != null) {
|
2012-05-31 04:03:45 +02:00
|
|
|
try {
|
2012-06-08 12:28:12 +02:00
|
|
|
ipMulticastTtl = stdSocketOptionType.getDeclaredField("IP_MULTICAST_TTL").get(null);
|
2012-05-31 04:03:45 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new Error("cannot locate the IP_MULTICAST_TTL field", e);
|
|
|
|
}
|
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
try {
|
2012-06-08 12:28:12 +02:00
|
|
|
ipMulticastIf = stdSocketOptionType.getDeclaredField("IP_MULTICAST_IF").get(null);
|
2012-05-02 08:01:58 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new Error("cannot locate the IP_MULTICAST_IF field", e);
|
|
|
|
}
|
|
|
|
|
2012-05-31 04:03:45 +02:00
|
|
|
try {
|
2012-06-08 12:28:12 +02:00
|
|
|
ipMulticastLoop = stdSocketOptionType.getDeclaredField("IP_MULTICAST_LOOP").get(null);
|
2012-05-31 04:03:45 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new Error("cannot locate the IP_MULTICAST_LOOP field", e);
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:33:47 +02:00
|
|
|
Class<?> networkChannelClass = null;
|
2012-05-24 18:32:14 +02:00
|
|
|
try {
|
2016-05-02 14:33:47 +02:00
|
|
|
networkChannelClass = Class.forName("java.nio.channels.NetworkChannel", true, classLoader);
|
|
|
|
} catch (Throwable ignore) {
|
|
|
|
// Not Java 7+
|
2012-05-24 18:32:14 +02:00
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
|
2016-05-02 14:33:47 +02:00
|
|
|
if (networkChannelClass == null) {
|
|
|
|
getOption = null;
|
|
|
|
setOption = null;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
getOption = networkChannelClass.getDeclaredMethod("getOption", socketOptionType);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new Error("cannot locate the getOption() method", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
setOption = networkChannelClass.getDeclaredMethod("setOption", socketOptionType, Object.class);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new Error("cannot locate the setOption() method", e);
|
|
|
|
}
|
2012-05-24 18:32:14 +02:00
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
}
|
2012-05-31 04:03:45 +02:00
|
|
|
IP_MULTICAST_TTL = ipMulticastTtl;
|
2012-05-24 18:32:14 +02:00
|
|
|
IP_MULTICAST_IF = ipMulticastIf;
|
2012-05-31 04:03:45 +02:00
|
|
|
IP_MULTICAST_LOOP = ipMulticastLoop;
|
2012-05-24 18:32:14 +02:00
|
|
|
GET_OPTION = getOption;
|
2012-05-02 08:01:58 +02:00
|
|
|
SET_OPTION = setOption;
|
|
|
|
}
|
2009-06-11 02:48:49 +02:00
|
|
|
|
2013-01-01 08:49:21 +01:00
|
|
|
private final DatagramChannel javaChannel;
|
2012-04-02 11:57:32 +02:00
|
|
|
|
2013-01-01 08:49:21 +01:00
|
|
|
NioDatagramChannelConfig(NioDatagramChannel channel, DatagramChannel javaChannel) {
|
|
|
|
super(channel, javaChannel.socket());
|
|
|
|
this.javaChannel = javaChannel;
|
2009-06-11 02:48:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-31 04:03:45 +02:00
|
|
|
@Override
|
|
|
|
public int getTimeToLive() {
|
|
|
|
return (Integer) getOption0(IP_MULTICAST_TTL);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-14 16:59:23 +01:00
|
|
|
public DatagramChannelConfig setTimeToLive(int ttl) {
|
2012-05-31 04:03:45 +02:00
|
|
|
setOption0(IP_MULTICAST_TTL, ttl);
|
2012-12-14 16:59:23 +01:00
|
|
|
return this;
|
2012-05-31 04:03:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InetAddress getInterface() {
|
|
|
|
NetworkInterface inf = getNetworkInterface();
|
|
|
|
if (inf == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
Enumeration<InetAddress> addresses = inf.getInetAddresses();
|
|
|
|
if (addresses.hasMoreElements()) {
|
|
|
|
return addresses.nextElement();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-14 16:59:23 +01:00
|
|
|
public DatagramChannelConfig setInterface(InetAddress interfaceAddress) {
|
2012-05-31 04:03:45 +02:00
|
|
|
try {
|
|
|
|
setNetworkInterface(NetworkInterface.getByInetAddress(interfaceAddress));
|
|
|
|
} catch (SocketException e) {
|
|
|
|
throw new ChannelException(e);
|
|
|
|
}
|
2012-12-14 16:59:23 +01:00
|
|
|
return this;
|
2012-05-31 04:03:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NetworkInterface getNetworkInterface() {
|
|
|
|
return (NetworkInterface) getOption0(IP_MULTICAST_IF);
|
|
|
|
}
|
|
|
|
|
2012-04-02 11:57:32 +02:00
|
|
|
@Override
|
2012-12-14 16:59:23 +01:00
|
|
|
public DatagramChannelConfig setNetworkInterface(NetworkInterface networkInterface) {
|
2012-05-31 04:03:45 +02:00
|
|
|
setOption0(IP_MULTICAST_IF, networkInterface);
|
2012-12-14 16:59:23 +01:00
|
|
|
return this;
|
2012-05-31 04:03:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isLoopbackModeDisabled() {
|
|
|
|
return (Boolean) getOption0(IP_MULTICAST_LOOP);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-12-14 16:59:23 +01:00
|
|
|
public DatagramChannelConfig setLoopbackModeDisabled(boolean loopbackModeDisabled) {
|
2012-05-31 04:03:45 +02:00
|
|
|
setOption0(IP_MULTICAST_LOOP, loopbackModeDisabled);
|
2012-12-14 16:59:23 +01:00
|
|
|
return this;
|
2012-05-31 04:03:45 +02:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:27:27 +02:00
|
|
|
@Override
|
|
|
|
public DatagramChannelConfig setAutoRead(boolean autoRead) {
|
|
|
|
super.setAutoRead(autoRead);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void autoReadCleared() {
|
2016-04-05 19:13:18 +02:00
|
|
|
((NioDatagramChannel) channel).clearReadPending0();
|
2014-04-10 08:27:27 +02:00
|
|
|
}
|
|
|
|
|
2012-05-31 04:03:45 +02:00
|
|
|
private Object getOption0(Object option) {
|
2016-05-02 14:33:47 +02:00
|
|
|
if (GET_OPTION == null) {
|
2012-04-02 11:57:32 +02:00
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
} else {
|
|
|
|
try {
|
2013-01-01 08:49:21 +01:00
|
|
|
return GET_OPTION.invoke(javaChannel, option);
|
2012-05-02 08:01:58 +02:00
|
|
|
} catch (Exception e) {
|
2012-04-02 11:57:32 +02:00
|
|
|
throw new ChannelException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 04:03:45 +02:00
|
|
|
private void setOption0(Object option, Object value) {
|
2016-05-02 14:33:47 +02:00
|
|
|
if (SET_OPTION == null) {
|
2012-04-02 11:57:32 +02:00
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
} else {
|
|
|
|
try {
|
2013-01-01 08:49:21 +01:00
|
|
|
SET_OPTION.invoke(javaChannel, option, value);
|
2012-05-02 08:01:58 +02:00
|
|
|
} catch (Exception e) {
|
2012-04-02 11:57:32 +02:00
|
|
|
throw new ChannelException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-11 02:48:49 +02:00
|
|
|
}
|