2012-02-18 23:02:56 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2012-02-18 23:02:56 +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:
|
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-02-18 23:02:56 +01:00
|
|
|
*
|
|
|
|
* 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.AbstractChannel;
|
|
|
|
import io.netty.channel.Channel;
|
2012-05-27 07:48:48 +02:00
|
|
|
import io.netty.channel.ChannelException;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
2012-05-12 17:40:28 +02:00
|
|
|
import io.netty.channel.EventLoop;
|
2012-05-27 07:48:48 +02:00
|
|
|
import io.netty.logging.InternalLogger;
|
|
|
|
import io.netty.logging.InternalLoggerFactory;
|
2012-02-18 23:02:56 +01:00
|
|
|
|
2012-05-27 07:48:48 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.ConnectException;
|
2012-05-24 17:57:10 +02:00
|
|
|
import java.net.InetSocketAddress;
|
2012-05-27 07:48:48 +02:00
|
|
|
import java.net.SocketAddress;
|
2012-05-02 08:01:58 +02:00
|
|
|
import java.nio.channels.SelectableChannel;
|
2012-05-02 14:05:53 +02:00
|
|
|
import java.nio.channels.SelectionKey;
|
2012-10-24 18:27:26 +02:00
|
|
|
import java.util.Queue;
|
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2012-05-27 07:48:48 +02:00
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2012-02-18 23:02:56 +01:00
|
|
|
|
2012-12-21 17:59:37 +01:00
|
|
|
/**
|
|
|
|
* Abstract base class for {@link Channel} implementations which use a Selector based approach.
|
|
|
|
*/
|
2012-05-02 08:01:58 +02:00
|
|
|
public abstract class AbstractNioChannel extends AbstractChannel {
|
2012-02-18 23:02:56 +01:00
|
|
|
|
2012-05-27 07:48:48 +02:00
|
|
|
private static final InternalLogger logger =
|
|
|
|
InternalLoggerFactory.getInstance(AbstractNioChannel.class);
|
|
|
|
|
2012-05-02 14:05:53 +02:00
|
|
|
private final SelectableChannel ch;
|
2012-08-28 08:55:51 +02:00
|
|
|
private final int readInterestOp;
|
2012-05-02 14:05:53 +02:00
|
|
|
private volatile SelectionKey selectionKey;
|
2012-08-29 14:49:39 +02:00
|
|
|
private volatile boolean inputShutdown;
|
2012-10-24 18:27:26 +02:00
|
|
|
final Queue<NioTask<SelectableChannel>> writableTasks = new ConcurrentLinkedQueue<NioTask<SelectableChannel>>();
|
2012-05-02 08:01:58 +02:00
|
|
|
|
2012-08-28 08:55:51 +02:00
|
|
|
final Runnable suspendReadTask = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
selectionKey().interestOps(selectionKey().interestOps() & ~readInterestOp);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
final Runnable resumeReadTask = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
selectionKey().interestOps(selectionKey().interestOps() | readInterestOp);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-27 07:48:48 +02:00
|
|
|
/**
|
|
|
|
* The future of the current connection attempt. If not null, subsequent
|
|
|
|
* connection attempts will fail.
|
|
|
|
*/
|
|
|
|
private ChannelFuture connectFuture;
|
|
|
|
private ScheduledFuture<?> connectTimeoutFuture;
|
|
|
|
private ConnectException connectTimeoutException;
|
|
|
|
|
2012-12-21 17:59:37 +01:00
|
|
|
/**
|
|
|
|
* Create a new instance
|
|
|
|
*
|
|
|
|
* @param parent the parent {@link Channel} by which this instance was created. May be {@code null}
|
|
|
|
* @param id the id of this instance or {@code null} if one should be generated
|
|
|
|
* @param ch the underlying {@link SelectableChannel} on which it operates
|
|
|
|
* @param readInterestOp the ops to set to receive data from the {@link SelectableChannel}
|
|
|
|
*/
|
2012-05-30 12:58:14 +02:00
|
|
|
protected AbstractNioChannel(
|
2012-08-28 08:55:51 +02:00
|
|
|
Channel parent, Integer id, SelectableChannel ch, int readInterestOp) {
|
2012-06-07 14:06:56 +02:00
|
|
|
super(parent, id);
|
2012-05-02 08:01:58 +02:00
|
|
|
this.ch = ch;
|
2012-08-28 08:55:51 +02:00
|
|
|
this.readInterestOp = readInterestOp;
|
2012-05-27 07:48:48 +02:00
|
|
|
try {
|
|
|
|
ch.configureBlocking(false);
|
|
|
|
} catch (IOException e) {
|
|
|
|
try {
|
|
|
|
ch.close();
|
|
|
|
} catch (IOException e2) {
|
|
|
|
if (logger.isWarnEnabled()) {
|
|
|
|
logger.warn(
|
|
|
|
"Failed to close a partially initialized socket.", e2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ChannelException("Failed to enter non-blocking mode.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isOpen() {
|
|
|
|
return ch.isOpen();
|
2012-03-29 17:07:19 +02:00
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
|
2012-05-24 17:57:10 +02:00
|
|
|
@Override
|
|
|
|
public InetSocketAddress localAddress() {
|
|
|
|
return (InetSocketAddress) super.localAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InetSocketAddress remoteAddress() {
|
|
|
|
return (InetSocketAddress) super.remoteAddress();
|
|
|
|
}
|
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
@Override
|
2012-05-27 07:48:48 +02:00
|
|
|
public NioUnsafe unsafe() {
|
|
|
|
return (NioUnsafe) super.unsafe();
|
|
|
|
}
|
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
protected SelectableChannel javaChannel() {
|
|
|
|
return ch;
|
2012-03-06 19:26:32 +01:00
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
|
2012-10-24 18:27:26 +02:00
|
|
|
@Override
|
|
|
|
public NioEventLoop eventLoop() {
|
|
|
|
return (NioEventLoop) super.eventLoop();
|
|
|
|
}
|
|
|
|
|
2012-12-21 17:59:37 +01:00
|
|
|
/**
|
|
|
|
* Return the current {@link SelectionKey}
|
|
|
|
*/
|
2012-05-02 14:05:53 +02:00
|
|
|
protected SelectionKey selectionKey() {
|
|
|
|
assert selectionKey != null;
|
|
|
|
return selectionKey;
|
|
|
|
}
|
|
|
|
|
2012-12-21 17:59:37 +01:00
|
|
|
/**
|
|
|
|
* Return {@code true} if the input of this {@link Channel} is shutdown
|
|
|
|
*/
|
2012-08-29 14:49:39 +02:00
|
|
|
boolean isInputShutdown() {
|
|
|
|
return inputShutdown;
|
|
|
|
}
|
|
|
|
|
2012-12-21 17:59:37 +01:00
|
|
|
/**
|
|
|
|
* Shutdown the input of this {@link Channel}.
|
|
|
|
*/
|
2012-08-29 14:49:39 +02:00
|
|
|
void setInputShutdown() {
|
|
|
|
inputShutdown = true;
|
|
|
|
}
|
|
|
|
|
2012-12-21 17:59:37 +01:00
|
|
|
/**
|
|
|
|
* Special {@link Unsafe} sub-type which allows to access the underlying {@link SelectableChannel}
|
|
|
|
*/
|
2012-05-27 07:48:48 +02:00
|
|
|
public interface NioUnsafe extends Unsafe {
|
2012-10-24 20:37:41 +02:00
|
|
|
SelectableChannel ch();
|
2012-05-27 07:48:48 +02:00
|
|
|
void finishConnect();
|
|
|
|
void read();
|
|
|
|
}
|
|
|
|
|
2012-05-27 14:07:23 +02:00
|
|
|
protected abstract class AbstractNioUnsafe extends AbstractUnsafe implements NioUnsafe {
|
2012-08-28 08:55:51 +02:00
|
|
|
|
2012-05-27 07:48:48 +02:00
|
|
|
@Override
|
2012-10-24 20:37:41 +02:00
|
|
|
public SelectableChannel ch() {
|
2012-05-27 07:48:48 +02:00
|
|
|
return javaChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-08 12:28:12 +02:00
|
|
|
public void connect(
|
|
|
|
final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelFuture future) {
|
2012-05-27 07:48:48 +02:00
|
|
|
if (eventLoop().inEventLoop()) {
|
|
|
|
if (!ensureOpen(future)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (connectFuture != null) {
|
|
|
|
throw new IllegalStateException("connection attempt already made");
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean wasActive = isActive();
|
|
|
|
if (doConnect(remoteAddress, localAddress)) {
|
|
|
|
future.setSuccess();
|
|
|
|
if (!wasActive && isActive()) {
|
|
|
|
pipeline().fireChannelActive();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
connectFuture = future;
|
|
|
|
|
|
|
|
// Schedule connect timeout.
|
|
|
|
int connectTimeoutMillis = config().getConnectTimeoutMillis();
|
|
|
|
if (connectTimeoutMillis > 0) {
|
|
|
|
connectTimeoutFuture = eventLoop().schedule(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (connectTimeoutException == null) {
|
|
|
|
connectTimeoutException = new ConnectException("connection timed out");
|
|
|
|
}
|
|
|
|
ChannelFuture connectFuture = AbstractNioChannel.this.connectFuture;
|
2012-06-11 15:54:28 +02:00
|
|
|
if (connectFuture != null && connectFuture.setFailure(connectTimeoutException)) {
|
|
|
|
close(voidFuture());
|
2012-05-27 07:48:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, connectTimeoutMillis, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
future.setFailure(t);
|
|
|
|
closeIfClosed();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eventLoop().execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
connect(remoteAddress, localAddress, future);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void finishConnect() {
|
|
|
|
assert eventLoop().inEventLoop();
|
|
|
|
assert connectFuture != null;
|
|
|
|
try {
|
|
|
|
boolean wasActive = isActive();
|
|
|
|
doFinishConnect();
|
|
|
|
connectFuture.setSuccess();
|
|
|
|
if (!wasActive && isActive()) {
|
|
|
|
pipeline().fireChannelActive();
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
connectFuture.setFailure(t);
|
|
|
|
closeIfClosed();
|
|
|
|
} finally {
|
|
|
|
connectTimeoutFuture.cancel(false);
|
|
|
|
connectFuture = null;
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 08:55:51 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void suspendRead() {
|
|
|
|
EventLoop loop = eventLoop();
|
|
|
|
if (loop.inEventLoop()) {
|
|
|
|
suspendReadTask.run();
|
|
|
|
} else {
|
|
|
|
loop.execute(suspendReadTask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void resumeRead() {
|
2012-08-29 14:49:39 +02:00
|
|
|
if (inputShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-28 08:55:51 +02:00
|
|
|
EventLoop loop = eventLoop();
|
|
|
|
if (loop.inEventLoop()) {
|
|
|
|
resumeReadTask.run();
|
|
|
|
} else {
|
|
|
|
loop.execute(resumeReadTask);
|
|
|
|
}
|
|
|
|
}
|
2012-05-12 22:09:05 +02:00
|
|
|
}
|
2012-05-27 14:31:18 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isCompatible(EventLoop loop) {
|
2012-08-10 13:17:18 +02:00
|
|
|
return loop instanceof NioEventLoop;
|
2012-05-27 14:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isFlushPending() {
|
2012-06-02 07:33:53 +02:00
|
|
|
SelectionKey selectionKey = this.selectionKey;
|
|
|
|
return selectionKey.isValid() && (selectionKey.interestOps() & SelectionKey.OP_WRITE) != 0;
|
2012-05-27 14:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-03 21:54:26 +02:00
|
|
|
protected Runnable doRegister() throws Exception {
|
2012-12-21 17:59:37 +01:00
|
|
|
NioEventLoop loop = eventLoop();
|
2012-05-27 14:31:18 +02:00
|
|
|
selectionKey = javaChannel().register(
|
2012-08-29 14:49:39 +02:00
|
|
|
loop.selector, isActive() && !inputShutdown ? readInterestOp : 0, this);
|
2012-06-03 21:54:26 +02:00
|
|
|
return null;
|
2012-05-27 14:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doDeregister() throws Exception {
|
2012-12-21 17:59:37 +01:00
|
|
|
eventLoop().cancel(selectionKey());
|
2012-05-27 14:31:18 +02:00
|
|
|
}
|
|
|
|
|
2012-12-21 17:59:37 +01:00
|
|
|
/**
|
|
|
|
* Conect to the remote peer
|
|
|
|
*/
|
2012-05-27 14:31:18 +02:00
|
|
|
protected abstract boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception;
|
2012-12-21 17:59:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finish the connect
|
|
|
|
*/
|
2012-05-27 14:31:18 +02:00
|
|
|
protected abstract void doFinishConnect() throws Exception;
|
2012-02-18 23:02:56 +01:00
|
|
|
}
|