com.datastax.oss.driver.shaded.netty.channel.socket.oio.OioDatagramChannel Maven / Gradle / Ivy
The newest version!
/*
* 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:
*
* https://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 com.datastax.oss.driver.shaded.netty.channel.socket.oio;
import com.datastax.oss.driver.shaded.netty.buffer.ByteBuf;
import com.datastax.oss.driver.shaded.netty.buffer.ByteBufUtil;
import com.datastax.oss.driver.shaded.netty.channel.AddressedEnvelope;
import com.datastax.oss.driver.shaded.netty.channel.Channel;
import com.datastax.oss.driver.shaded.netty.channel.ChannelException;
import com.datastax.oss.driver.shaded.netty.channel.ChannelFuture;
import com.datastax.oss.driver.shaded.netty.channel.ChannelMetadata;
import com.datastax.oss.driver.shaded.netty.channel.ChannelOption;
import com.datastax.oss.driver.shaded.netty.channel.ChannelOutboundBuffer;
import com.datastax.oss.driver.shaded.netty.channel.ChannelPromise;
import com.datastax.oss.driver.shaded.netty.channel.RecvByteBufAllocator;
import com.datastax.oss.driver.shaded.netty.channel.oio.AbstractOioMessageChannel;
import com.datastax.oss.driver.shaded.netty.channel.socket.DatagramChannel;
import com.datastax.oss.driver.shaded.netty.channel.socket.DatagramChannelConfig;
import com.datastax.oss.driver.shaded.netty.channel.socket.DatagramPacket;
import com.datastax.oss.driver.shaded.netty.util.internal.EmptyArrays;
import com.datastax.oss.driver.shaded.netty.util.internal.PlatformDependent;
import com.datastax.oss.driver.shaded.netty.util.internal.StringUtil;
import com.datastax.oss.driver.shaded.netty.util.internal.logging.InternalLogger;
import com.datastax.oss.driver.shaded.netty.util.internal.logging.InternalLoggerFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.nio.channels.NotYetConnectedException;
import java.nio.channels.UnresolvedAddressException;
import java.util.List;
import java.util.Locale;
/**
* An OIO datagram {@link Channel} that sends and receives an
* {@link AddressedEnvelope AddressedEnvelope}.
*
* @see AddressedEnvelope
* @see DatagramPacket
* @deprecated use NIO / EPOLL / KQUEUE transport.
*/
@Deprecated
public class OioDatagramChannel extends AbstractOioMessageChannel
implements DatagramChannel {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(OioDatagramChannel.class);
private static final ChannelMetadata METADATA = new ChannelMetadata(true);
private static final String EXPECTED_TYPES =
" (expected: " + StringUtil.simpleClassName(DatagramPacket.class) + ", " +
StringUtil.simpleClassName(AddressedEnvelope.class) + '<' +
StringUtil.simpleClassName(ByteBuf.class) + ", " +
StringUtil.simpleClassName(SocketAddress.class) + ">, " +
StringUtil.simpleClassName(ByteBuf.class) + ')';
private final MulticastSocket socket;
private final OioDatagramChannelConfig config;
private final java.net.DatagramPacket tmpPacket = new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0);
private static MulticastSocket newSocket() {
try {
return new MulticastSocket(null);
} catch (Exception e) {
throw new ChannelException("failed to create a new socket", e);
}
}
/**
* Create a new instance with an new {@link MulticastSocket}.
*/
public OioDatagramChannel() {
this(newSocket());
}
/**
* Create a new instance from the given {@link MulticastSocket}.
*
* @param socket the {@link MulticastSocket} which is used by this instance
*/
public OioDatagramChannel(MulticastSocket socket) {
super(null);
boolean success = false;
try {
socket.setSoTimeout(SO_TIMEOUT);
socket.setBroadcast(false);
success = true;
} catch (SocketException e) {
throw new ChannelException(
"Failed to configure the datagram socket timeout.", e);
} finally {
if (!success) {
socket.close();
}
}
this.socket = socket;
config = new DefaultOioDatagramChannelConfig(this, socket);
}
@Override
public ChannelMetadata metadata() {
return METADATA;
}
/**
* {@inheritDoc}
*
* This can be safely cast to {@link OioDatagramChannelConfig}.
*/
@Override
// TODO: Change return type to OioDatagramChannelConfig in next major release
public DatagramChannelConfig config() {
return config;
}
@Override
public boolean isOpen() {
return !socket.isClosed();
}
@Override
@SuppressWarnings("deprecation")
public boolean isActive() {
return isOpen()
&& (config.getOption(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) && isRegistered()
|| socket.isBound());
}
@Override
public boolean isConnected() {
return socket.isConnected();
}
@Override
protected SocketAddress localAddress0() {
return socket.getLocalSocketAddress();
}
@Override
protected SocketAddress remoteAddress0() {
return socket.getRemoteSocketAddress();
}
@Override
protected void doBind(SocketAddress localAddress) throws Exception {
socket.bind(localAddress);
}
@Override
public InetSocketAddress localAddress() {
return (InetSocketAddress) super.localAddress();
}
@Override
public InetSocketAddress remoteAddress() {
return (InetSocketAddress) super.remoteAddress();
}
@Override
protected void doConnect(SocketAddress remoteAddress,
SocketAddress localAddress) throws Exception {
if (localAddress != null) {
socket.bind(localAddress);
}
boolean success = false;
try {
socket.connect(remoteAddress);
success = true;
} finally {
if (!success) {
try {
socket.close();
} catch (Throwable t) {
logger.warn("Failed to close a socket.", t);
}
}
}
}
@Override
protected void doDisconnect() throws Exception {
socket.disconnect();
}
@Override
protected void doClose() throws Exception {
socket.close();
}
@Override
protected int doReadMessages(List