2009-06-11 02:48:49 +02:00
|
|
|
/*
|
2011-12-09 06:18:34 +01:00
|
|
|
* Copyright 2011 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
|
|
|
*
|
2011-12-09 06:18:34 +01: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;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.channel.socket.DefaultDatagramChannelConfig;
|
2012-04-02 11:57:32 +02:00
|
|
|
import io.netty.util.internal.DetectionUtil;
|
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
import java.lang.reflect.Method;
|
2012-04-02 11:57:32 +02:00
|
|
|
import java.net.NetworkInterface;
|
|
|
|
import java.nio.channels.DatagramChannel;
|
2009-06-11 02:48:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The default {@link NioSocketChannelConfig} implementation.
|
|
|
|
*/
|
2012-05-26 00:51:22 +02:00
|
|
|
class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
|
2009-06-11 02:48:49 +02:00
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
private static final Object IP_MULTICAST_IF;
|
|
|
|
private static final Method GET_OPTION;
|
|
|
|
private static final Method SET_OPTION;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ClassLoader classLoader = DatagramChannel.class.getClassLoader();
|
|
|
|
Class<?> socketOptionType = null;
|
|
|
|
try {
|
|
|
|
socketOptionType = Class.forName("java.net.SocketOption", true, classLoader);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Not Java 7+
|
|
|
|
}
|
2009-06-11 02:48:49 +02:00
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
Object ipMulticastIf = 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) {
|
|
|
|
try {
|
|
|
|
ipMulticastIf = Class.forName("java.net.StandardSocketOptions", true, classLoader).getDeclaredField("IP_MULTICAST_IF").get(null);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new Error("cannot locate the IP_MULTICAST_IF field", e);
|
|
|
|
}
|
|
|
|
|
2012-05-24 18:32:14 +02:00
|
|
|
try {
|
|
|
|
getOption = DatagramChannel.class.getDeclaredMethod("getOption", socketOptionType);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new Error("cannot locate the getOption() method", e);
|
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
|
2012-05-24 18:32:14 +02:00
|
|
|
try {
|
|
|
|
setOption = DatagramChannel.class.getDeclaredMethod("setOption", socketOptionType, Object.class);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new Error("cannot locate the setOption() method", e);
|
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
}
|
2012-05-24 18:32:14 +02:00
|
|
|
IP_MULTICAST_IF = ipMulticastIf;
|
|
|
|
GET_OPTION = getOption;
|
2012-05-02 08:01:58 +02:00
|
|
|
SET_OPTION = setOption;
|
|
|
|
}
|
2009-06-11 02:48:49 +02:00
|
|
|
|
2012-04-02 11:57:32 +02:00
|
|
|
private final DatagramChannel channel;
|
|
|
|
|
2012-05-26 00:51:22 +02:00
|
|
|
NioDatagramChannelConfig(DatagramChannel channel) {
|
2012-04-02 11:57:32 +02:00
|
|
|
super(channel.socket());
|
|
|
|
this.channel = channel;
|
2009-06-11 02:48:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 11:57:32 +02:00
|
|
|
@Override
|
|
|
|
public void setNetworkInterface(NetworkInterface networkInterface) {
|
|
|
|
if (DetectionUtil.javaVersion() < 7) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
} else {
|
|
|
|
try {
|
2012-05-02 08:01:58 +02:00
|
|
|
SET_OPTION.invoke(channel, IP_MULTICAST_IF, networkInterface);
|
|
|
|
} catch (Exception e) {
|
2012-04-02 11:57:32 +02:00
|
|
|
throw new ChannelException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NetworkInterface getNetworkInterface() {
|
|
|
|
if (DetectionUtil.javaVersion() < 7) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
} else {
|
|
|
|
try {
|
2012-05-02 08:01:58 +02:00
|
|
|
return (NetworkInterface) GET_OPTION.invoke(channel, IP_MULTICAST_IF);
|
|
|
|
} catch (Exception e) {
|
2012-04-02 11:57:32 +02:00
|
|
|
throw new ChannelException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-11 02:48:49 +02:00
|
|
|
}
|