Add InternetProtocolFamily and use it with NioDatagramChannel

This commit is contained in:
Trustin Lee 2012-05-31 02:49:39 -07:00
parent 197f31c90e
commit 665777e6f9
3 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,24 @@
/*
* Copyright 2012 The Netty Project
*
* 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:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.socket;
/**
* Internet Protocol (IP) families
*/
public enum InternetProtocolFamily {
IPv4,
IPv6;
}

View File

@ -21,6 +21,7 @@ import io.netty.channel.ChannelException;
import io.netty.channel.ChannelFuture;
import io.netty.channel.socket.DatagramChannelConfig;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.util.internal.DetectionUtil;
import java.io.IOException;
@ -57,10 +58,30 @@ public final class NioDatagramChannel extends AbstractNioMessageChannel implemen
}
}
private static DatagramChannel newSocket(InternetProtocolFamily ipFamily) {
if (ipFamily == null) {
return newSocket();
}
if (DetectionUtil.javaVersion() < 7) {
throw new UnsupportedOperationException();
}
try {
return DatagramChannel.open(ProtocolFamilyConverter.convert(ipFamily));
} catch (IOException e) {
throw new ChannelException("Failed to open a socket.", e);
}
}
public NioDatagramChannel() {
this(newSocket());
}
public NioDatagramChannel(InternetProtocolFamily ipFamily) {
this(newSocket(ipFamily));
}
public NioDatagramChannel(DatagramChannel socket) {
this(null, socket);
}

View File

@ -0,0 +1,45 @@
/*
* Copyright 2012 The Netty Project
*
* 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:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.socket.nio;
import io.netty.channel.socket.InternetProtocolFamily;
/**
* Helper class which convert the {@link InternetProtocolFamily}.
*
*
*/
final class ProtocolFamilyConverter {
private ProtocolFamilyConverter() {
// Utility class
}
/**
* Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
*/
public static java.net.ProtocolFamily convert(InternetProtocolFamily family) {
switch (family) {
case IPv4:
return java.net.StandardProtocolFamily.INET;
case IPv6:
return java.net.StandardProtocolFamily.INET6;
default:
throw new IllegalArgumentException();
}
}
}