7ec12d327f
* The problem with the release(..) calls here was that it would have called release on an unsupported message and then throw an exception. This exception will trigger ChannelOutboundBuffer.fail(..), which will also try to release the message again. * Also use the same exception type for unsupported messages as in other channel impls.
545 lines
18 KiB
Java
Executable File
545 lines
18 KiB
Java
Executable File
/*
|
|
* 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.buffer.ByteBuf;
|
|
import io.netty.buffer.ByteBufHolder;
|
|
import io.netty.channel.AddressedEnvelope;
|
|
import io.netty.channel.Channel;
|
|
import io.netty.channel.ChannelException;
|
|
import io.netty.channel.ChannelFuture;
|
|
import io.netty.channel.ChannelMetadata;
|
|
import io.netty.channel.ChannelPromise;
|
|
import io.netty.channel.MessageList;
|
|
import io.netty.channel.RecvByteBufAllocator;
|
|
import io.netty.channel.nio.AbstractNioMessageChannel;
|
|
import io.netty.channel.socket.DatagramChannelConfig;
|
|
import io.netty.channel.socket.DatagramPacket;
|
|
import io.netty.channel.socket.InternetProtocolFamily;
|
|
import io.netty.util.ReferenceCountUtil;
|
|
import io.netty.util.internal.PlatformDependent;
|
|
import io.netty.util.internal.StringUtil;
|
|
|
|
import java.io.IOException;
|
|
import java.net.InetAddress;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.NetworkInterface;
|
|
import java.net.SocketAddress;
|
|
import java.net.SocketException;
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.channels.DatagramChannel;
|
|
import java.nio.channels.MembershipKey;
|
|
import java.nio.channels.SelectionKey;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* An NIO datagram {@link Channel} that sends and receives an
|
|
* {@link AddressedEnvelope AddressedEnvelope<ByteBuf, SocketAddress>}.
|
|
*
|
|
* @see AddressedEnvelope
|
|
* @see DatagramPacket
|
|
*/
|
|
public final class NioDatagramChannel
|
|
extends AbstractNioMessageChannel implements io.netty.channel.socket.DatagramChannel {
|
|
|
|
private static final ChannelMetadata METADATA = new ChannelMetadata(true);
|
|
|
|
private final DatagramChannelConfig config;
|
|
private final Map<InetAddress, List<MembershipKey>> memberships =
|
|
new HashMap<InetAddress, List<MembershipKey>>();
|
|
|
|
private RecvByteBufAllocator.Handle allocHandle;
|
|
|
|
private static DatagramChannel newSocket() {
|
|
try {
|
|
return DatagramChannel.open();
|
|
} catch (IOException e) {
|
|
throw new ChannelException("Failed to open a socket.", e);
|
|
}
|
|
}
|
|
|
|
private static DatagramChannel newSocket(InternetProtocolFamily ipFamily) {
|
|
if (ipFamily == null) {
|
|
return newSocket();
|
|
}
|
|
|
|
if (PlatformDependent.javaVersion() < 7) {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
try {
|
|
return DatagramChannel.open(ProtocolFamilyConverter.convert(ipFamily));
|
|
} catch (IOException e) {
|
|
throw new ChannelException("Failed to open a socket.", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a new instance which will use the Operation Systems default {@link InternetProtocolFamily}.
|
|
*/
|
|
public NioDatagramChannel() {
|
|
this(newSocket());
|
|
}
|
|
|
|
/**
|
|
* Create a new instance using the given {@link InternetProtocolFamily}. If {@code null} is used it will depend
|
|
* on the Operation Systems default which will be chosen.
|
|
*/
|
|
public NioDatagramChannel(InternetProtocolFamily ipFamily) {
|
|
this(newSocket(ipFamily));
|
|
}
|
|
|
|
/**
|
|
* Create a new instance from the given {@link DatagramChannel}.
|
|
*/
|
|
public NioDatagramChannel(DatagramChannel socket) {
|
|
this(null, socket);
|
|
}
|
|
|
|
/**
|
|
* Create a new instance from the given {@link DatagramChannel}.
|
|
*
|
|
* @param id the id to use for this instance or {@code null} if a new one should be generated.
|
|
* @param socket the {@link DatagramChannel} which will be used
|
|
*/
|
|
public NioDatagramChannel(Integer id, DatagramChannel socket) {
|
|
super(null, id, socket, SelectionKey.OP_READ);
|
|
config = new NioDatagramChannelConfig(this, socket);
|
|
}
|
|
|
|
@Override
|
|
public ChannelMetadata metadata() {
|
|
return METADATA;
|
|
}
|
|
|
|
@Override
|
|
public DatagramChannelConfig config() {
|
|
return config;
|
|
}
|
|
|
|
@Override
|
|
public boolean isActive() {
|
|
DatagramChannel ch = javaChannel();
|
|
return ch.isOpen() && ch.socket().isBound();
|
|
}
|
|
|
|
@Override
|
|
public boolean isConnected() {
|
|
return javaChannel().isConnected();
|
|
}
|
|
|
|
@Override
|
|
protected DatagramChannel javaChannel() {
|
|
return (DatagramChannel) super.javaChannel();
|
|
}
|
|
|
|
@Override
|
|
protected SocketAddress localAddress0() {
|
|
return javaChannel().socket().getLocalSocketAddress();
|
|
}
|
|
|
|
@Override
|
|
protected SocketAddress remoteAddress0() {
|
|
return javaChannel().socket().getRemoteSocketAddress();
|
|
}
|
|
|
|
@Override
|
|
protected void doBind(SocketAddress localAddress) throws Exception {
|
|
javaChannel().socket().bind(localAddress);
|
|
}
|
|
|
|
@Override
|
|
protected boolean doConnect(SocketAddress remoteAddress,
|
|
SocketAddress localAddress) throws Exception {
|
|
if (localAddress != null) {
|
|
javaChannel().socket().bind(localAddress);
|
|
}
|
|
|
|
boolean success = false;
|
|
try {
|
|
javaChannel().connect(remoteAddress);
|
|
success = true;
|
|
return true;
|
|
} finally {
|
|
if (!success) {
|
|
doClose();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void doFinishConnect() throws Exception {
|
|
throw new Error();
|
|
}
|
|
|
|
@Override
|
|
protected void doDisconnect() throws Exception {
|
|
javaChannel().disconnect();
|
|
}
|
|
|
|
@Override
|
|
protected void doClose() throws Exception {
|
|
javaChannel().close();
|
|
}
|
|
|
|
@Override
|
|
protected int doReadMessages(MessageList<Object> buf) throws Exception {
|
|
DatagramChannel ch = javaChannel();
|
|
DatagramChannelConfig config = config();
|
|
RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
|
|
if (allocHandle == null) {
|
|
this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle();
|
|
}
|
|
ByteBuf data = allocHandle.allocate(config.getAllocator());
|
|
boolean free = true;
|
|
try {
|
|
ByteBuffer nioData = data.nioBuffer(data.writerIndex(), data.writableBytes());
|
|
|
|
InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
|
|
if (remoteAddress == null) {
|
|
return 0;
|
|
}
|
|
|
|
int readBytes = nioData.position();
|
|
data.writerIndex(data.writerIndex() + readBytes);
|
|
allocHandle.record(readBytes);
|
|
|
|
buf.add(new DatagramPacket(data, localAddress(), remoteAddress));
|
|
free = false;
|
|
return 1;
|
|
} catch (Throwable cause) {
|
|
PlatformDependent.throwException(cause);
|
|
return -1;
|
|
} finally {
|
|
if (free) {
|
|
data.release();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected int doWriteMessages(MessageList<Object> msgs, int index, boolean lastSpin) throws Exception {
|
|
final Object o = msgs.get(index);
|
|
final Object m;
|
|
final ByteBuf data;
|
|
final SocketAddress remoteAddress;
|
|
if (o instanceof AddressedEnvelope) {
|
|
@SuppressWarnings("unchecked")
|
|
AddressedEnvelope<Object, SocketAddress> envelope = (AddressedEnvelope<Object, SocketAddress>) o;
|
|
remoteAddress = envelope.recipient();
|
|
m = envelope.content();
|
|
} else {
|
|
m = o;
|
|
remoteAddress = null;
|
|
}
|
|
|
|
if (m instanceof ByteBufHolder) {
|
|
data = ((ByteBufHolder) m).content();
|
|
} else if (m instanceof ByteBuf) {
|
|
data = (ByteBuf) m;
|
|
} else {
|
|
throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(0));
|
|
}
|
|
|
|
int dataLen = data.readableBytes();
|
|
ByteBuffer nioData;
|
|
if (data.nioBufferCount() == 1) {
|
|
nioData = data.nioBuffer();
|
|
} else {
|
|
nioData = ByteBuffer.allocate(dataLen);
|
|
data.getBytes(data.readerIndex(), nioData);
|
|
nioData.flip();
|
|
}
|
|
|
|
final int writtenBytes;
|
|
if (remoteAddress != null) {
|
|
writtenBytes = javaChannel().send(nioData, remoteAddress);
|
|
} else {
|
|
writtenBytes = javaChannel().write(nioData);
|
|
}
|
|
|
|
final SelectionKey key = selectionKey();
|
|
final int interestOps = key.interestOps();
|
|
if (writtenBytes <= 0 && dataLen > 0) {
|
|
// Did not write a packet.
|
|
// 1) If 'lastSpin' is false, the caller will call this method again real soon.
|
|
// - Do not update OP_WRITE.
|
|
// 2) If 'lastSpin' is true, the caller will not retry.
|
|
// - Set OP_WRITE so that the event loop calls flushForcibly() later.
|
|
if (lastSpin) {
|
|
if ((interestOps & SelectionKey.OP_WRITE) == 0) {
|
|
key.interestOps(interestOps | SelectionKey.OP_WRITE);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// Wrote a packet - free the message.
|
|
ReferenceCountUtil.release(o);
|
|
|
|
if (index + 1 == msgs.size()) {
|
|
// Wrote the outbound buffer completely - clear OP_WRITE.
|
|
if ((interestOps & SelectionKey.OP_WRITE) != 0) {
|
|
key.interestOps(interestOps & ~SelectionKey.OP_WRITE);
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
@Override
|
|
public InetSocketAddress localAddress() {
|
|
return (InetSocketAddress) super.localAddress();
|
|
}
|
|
|
|
@Override
|
|
public InetSocketAddress remoteAddress() {
|
|
return (InetSocketAddress) super.remoteAddress();
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture joinGroup(InetAddress multicastAddress) {
|
|
return joinGroup(multicastAddress, newPromise());
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) {
|
|
try {
|
|
return joinGroup(
|
|
multicastAddress,
|
|
NetworkInterface.getByInetAddress(localAddress().getAddress()),
|
|
null, promise);
|
|
} catch (SocketException e) {
|
|
promise.setFailure(e);
|
|
}
|
|
return promise;
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture joinGroup(
|
|
InetSocketAddress multicastAddress, NetworkInterface networkInterface) {
|
|
return joinGroup(multicastAddress, networkInterface, newPromise());
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture joinGroup(
|
|
InetSocketAddress multicastAddress, NetworkInterface networkInterface,
|
|
ChannelPromise promise) {
|
|
return joinGroup(multicastAddress.getAddress(), networkInterface, null, promise);
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture joinGroup(
|
|
InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source) {
|
|
return joinGroup(multicastAddress, networkInterface, source, newPromise());
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture joinGroup(
|
|
InetAddress multicastAddress, NetworkInterface networkInterface,
|
|
InetAddress source, ChannelPromise promise) {
|
|
if (PlatformDependent.javaVersion() >= 7) {
|
|
if (multicastAddress == null) {
|
|
throw new NullPointerException("multicastAddress");
|
|
}
|
|
|
|
if (networkInterface == null) {
|
|
throw new NullPointerException("networkInterface");
|
|
}
|
|
|
|
try {
|
|
MembershipKey key;
|
|
if (source == null) {
|
|
key = javaChannel().join(multicastAddress, networkInterface);
|
|
} else {
|
|
key = javaChannel().join(multicastAddress, networkInterface, source);
|
|
}
|
|
|
|
synchronized (this) {
|
|
List<MembershipKey> keys = memberships.get(multicastAddress);
|
|
if (keys == null) {
|
|
keys = new ArrayList<MembershipKey>();
|
|
memberships.put(multicastAddress, keys);
|
|
}
|
|
keys.add(key);
|
|
}
|
|
|
|
promise.setSuccess();
|
|
} catch (Throwable e) {
|
|
promise.setFailure(e);
|
|
}
|
|
} else {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
return promise;
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture leaveGroup(InetAddress multicastAddress) {
|
|
return leaveGroup(multicastAddress, newPromise());
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture leaveGroup(InetAddress multicastAddress, ChannelPromise promise) {
|
|
try {
|
|
return leaveGroup(
|
|
multicastAddress, NetworkInterface.getByInetAddress(localAddress().getAddress()), null, promise);
|
|
} catch (SocketException e) {
|
|
promise.setFailure(e);
|
|
}
|
|
return promise;
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture leaveGroup(
|
|
InetSocketAddress multicastAddress, NetworkInterface networkInterface) {
|
|
return leaveGroup(multicastAddress, networkInterface, newPromise());
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture leaveGroup(
|
|
InetSocketAddress multicastAddress,
|
|
NetworkInterface networkInterface, ChannelPromise promise) {
|
|
return leaveGroup(multicastAddress.getAddress(), networkInterface, null, promise);
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture leaveGroup(
|
|
InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source) {
|
|
return leaveGroup(multicastAddress, networkInterface, source, newPromise());
|
|
}
|
|
|
|
@Override
|
|
public ChannelFuture leaveGroup(
|
|
InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source,
|
|
ChannelPromise promise) {
|
|
if (PlatformDependent.javaVersion() < 7) {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
if (multicastAddress == null) {
|
|
throw new NullPointerException("multicastAddress");
|
|
}
|
|
if (networkInterface == null) {
|
|
throw new NullPointerException("networkInterface");
|
|
}
|
|
|
|
synchronized (this) {
|
|
if (memberships != null) {
|
|
List<MembershipKey> keys = memberships.get(multicastAddress);
|
|
if (keys != null) {
|
|
Iterator<MembershipKey> keyIt = keys.iterator();
|
|
|
|
while (keyIt.hasNext()) {
|
|
MembershipKey key = keyIt.next();
|
|
if (networkInterface.equals(key.networkInterface())) {
|
|
if (source == null && key.sourceAddress() == null ||
|
|
source != null && source.equals(key.sourceAddress())) {
|
|
key.drop();
|
|
keyIt.remove();
|
|
}
|
|
}
|
|
}
|
|
if (keys.isEmpty()) {
|
|
memberships.remove(multicastAddress);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
promise.setSuccess();
|
|
return promise;
|
|
}
|
|
|
|
/**
|
|
* Block the given sourceToBlock address for the given multicastAddress on the given networkInterface
|
|
*/
|
|
@Override
|
|
public ChannelFuture block(
|
|
InetAddress multicastAddress, NetworkInterface networkInterface,
|
|
InetAddress sourceToBlock) {
|
|
return block(multicastAddress, networkInterface, sourceToBlock, newPromise());
|
|
}
|
|
|
|
/**
|
|
* Block the given sourceToBlock address for the given multicastAddress on the given networkInterface
|
|
*/
|
|
@Override
|
|
public ChannelFuture block(
|
|
InetAddress multicastAddress, NetworkInterface networkInterface,
|
|
InetAddress sourceToBlock, ChannelPromise promise) {
|
|
if (PlatformDependent.javaVersion() < 7) {
|
|
throw new UnsupportedOperationException();
|
|
} else {
|
|
if (multicastAddress == null) {
|
|
throw new NullPointerException("multicastAddress");
|
|
}
|
|
if (sourceToBlock == null) {
|
|
throw new NullPointerException("sourceToBlock");
|
|
}
|
|
|
|
if (networkInterface == null) {
|
|
throw new NullPointerException("networkInterface");
|
|
}
|
|
synchronized (this) {
|
|
if (memberships != null) {
|
|
List<MembershipKey> keys = memberships.get(multicastAddress);
|
|
for (MembershipKey key: keys) {
|
|
if (networkInterface.equals(key.networkInterface())) {
|
|
try {
|
|
key.block(sourceToBlock);
|
|
} catch (IOException e) {
|
|
promise.setFailure(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
promise.setSuccess();
|
|
return promise;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Block the given sourceToBlock address for the given multicastAddress
|
|
*
|
|
*/
|
|
@Override
|
|
public ChannelFuture block(InetAddress multicastAddress, InetAddress sourceToBlock) {
|
|
return block(multicastAddress, sourceToBlock, newPromise());
|
|
}
|
|
|
|
/**
|
|
* Block the given sourceToBlock address for the given multicastAddress
|
|
*
|
|
*/
|
|
@Override
|
|
public ChannelFuture block(
|
|
InetAddress multicastAddress, InetAddress sourceToBlock, ChannelPromise promise) {
|
|
try {
|
|
return block(
|
|
multicastAddress,
|
|
NetworkInterface.getByInetAddress(localAddress().getAddress()),
|
|
sourceToBlock, promise);
|
|
} catch (SocketException e) {
|
|
promise.setFailure(e);
|
|
}
|
|
return promise;
|
|
}
|
|
}
|