2014-02-15 22:26:36 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2014 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.epoll;
|
|
|
|
|
2014-08-05 14:24:49 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.ByteBufAllocator;
|
|
|
|
import io.netty.buffer.ByteBufUtil;
|
|
|
|
import io.netty.buffer.Unpooled;
|
2014-02-15 22:26:36 +01:00
|
|
|
import io.netty.channel.AbstractChannel;
|
|
|
|
import io.netty.channel.Channel;
|
2016-02-28 02:56:41 +01:00
|
|
|
import io.netty.channel.ChannelConfig;
|
2016-03-17 12:10:23 +01:00
|
|
|
import io.netty.channel.ChannelException;
|
2017-01-09 14:15:48 +01:00
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
2014-02-15 22:26:36 +01:00
|
|
|
import io.netty.channel.ChannelMetadata;
|
2017-12-08 01:00:52 +01:00
|
|
|
import io.netty.channel.ChannelOutboundBuffer;
|
2017-01-09 14:15:48 +01:00
|
|
|
import io.netty.channel.ChannelPromise;
|
|
|
|
import io.netty.channel.ConnectTimeoutException;
|
2014-02-15 22:26:36 +01:00
|
|
|
import io.netty.channel.EventLoop;
|
2015-06-05 03:36:55 +02:00
|
|
|
import io.netty.channel.RecvByteBufAllocator;
|
2015-08-04 02:58:37 +02:00
|
|
|
import io.netty.channel.socket.ChannelInputShutdownEvent;
|
2017-02-02 19:15:10 +01:00
|
|
|
import io.netty.channel.socket.ChannelInputShutdownReadComplete;
|
2018-03-28 20:02:57 +02:00
|
|
|
import io.netty.channel.socket.SocketChannelConfig;
|
2017-01-19 17:31:34 +01:00
|
|
|
import io.netty.channel.unix.FileDescriptor;
|
2015-10-07 04:00:59 +02:00
|
|
|
import io.netty.channel.unix.Socket;
|
2015-02-12 20:44:36 +01:00
|
|
|
import io.netty.channel.unix.UnixChannel;
|
2014-08-05 14:24:49 +02:00
|
|
|
import io.netty.util.ReferenceCountUtil;
|
2017-01-09 14:15:48 +01:00
|
|
|
import io.netty.util.internal.ThrowableUtil;
|
2014-02-15 22:26:36 +01:00
|
|
|
|
2015-04-30 09:38:35 +02:00
|
|
|
import java.io.IOException;
|
2014-02-15 22:26:36 +01:00
|
|
|
import java.net.InetSocketAddress;
|
2017-01-09 14:15:48 +01:00
|
|
|
import java.net.SocketAddress;
|
2015-01-14 16:38:46 +01:00
|
|
|
import java.nio.ByteBuffer;
|
2017-01-09 14:15:48 +01:00
|
|
|
import java.nio.channels.AlreadyConnectedException;
|
|
|
|
import java.nio.channels.ClosedChannelException;
|
|
|
|
import java.nio.channels.ConnectionPendingException;
|
2016-08-29 12:43:11 +02:00
|
|
|
import java.nio.channels.NotYetConnectedException;
|
2014-03-16 16:29:45 +01:00
|
|
|
import java.nio.channels.UnresolvedAddressException;
|
2017-01-09 14:15:48 +01:00
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2014-02-15 22:26:36 +01:00
|
|
|
|
2017-12-08 01:00:52 +01:00
|
|
|
import static io.netty.channel.internal.ChannelUtils.WRITE_STATUS_SNDBUF_FULL;
|
2017-01-09 14:15:48 +01:00
|
|
|
import static io.netty.channel.unix.UnixChannelUtil.computeRemoteAddr;
|
2015-10-07 04:00:59 +02:00
|
|
|
import static io.netty.util.internal.ObjectUtil.checkNotNull;
|
|
|
|
|
2015-02-12 20:44:36 +01:00
|
|
|
abstract class AbstractEpollChannel extends AbstractChannel implements UnixChannel {
|
2017-01-09 14:15:48 +01:00
|
|
|
private static final ClosedChannelException DO_CLOSE_CLOSED_CHANNEL_EXCEPTION = ThrowableUtil.unknownStackTrace(
|
|
|
|
new ClosedChannelException(), AbstractEpollChannel.class, "doClose()");
|
2015-06-05 03:36:55 +02:00
|
|
|
private static final ChannelMetadata METADATA = new ChannelMetadata(false);
|
2017-01-19 17:31:34 +01:00
|
|
|
final LinuxSocket socket;
|
2017-01-09 14:15:48 +01:00
|
|
|
/**
|
|
|
|
* The future of the current connection attempt. If not null, subsequent
|
|
|
|
* connection attempts will fail.
|
|
|
|
*/
|
|
|
|
private ChannelPromise connectPromise;
|
|
|
|
private ScheduledFuture<?> connectTimeoutFuture;
|
|
|
|
private SocketAddress requestedRemoteAddress;
|
|
|
|
|
|
|
|
private volatile SocketAddress local;
|
|
|
|
private volatile SocketAddress remote;
|
|
|
|
|
2015-01-29 06:49:14 +01:00
|
|
|
protected int flags = Native.EPOLLET;
|
2017-02-02 19:15:10 +01:00
|
|
|
boolean inputClosedSeenErrorOnRead;
|
|
|
|
boolean epollInReadyRunnablePending;
|
2015-01-29 06:49:14 +01:00
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
protected volatile boolean active;
|
|
|
|
|
2018-06-15 10:28:50 +02:00
|
|
|
AbstractEpollChannel(LinuxSocket fd) {
|
|
|
|
this(null, fd, false);
|
2014-02-17 14:25:17 +01:00
|
|
|
}
|
|
|
|
|
2018-06-15 10:28:50 +02:00
|
|
|
AbstractEpollChannel(Channel parent, LinuxSocket fd, boolean active) {
|
2014-02-15 22:26:36 +01:00
|
|
|
super(parent);
|
2017-01-19 17:31:34 +01:00
|
|
|
socket = checkNotNull(fd, "fd");
|
2014-02-17 14:25:17 +01:00
|
|
|
this.active = active;
|
2017-01-09 14:15:48 +01:00
|
|
|
if (active) {
|
|
|
|
// Directly cache the remote and local addresses
|
|
|
|
// See https://github.com/netty/netty/issues/2359
|
|
|
|
local = fd.localAddress();
|
|
|
|
remote = fd.remoteAddress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 10:28:50 +02:00
|
|
|
AbstractEpollChannel(Channel parent, LinuxSocket fd, SocketAddress remote) {
|
2017-01-09 14:15:48 +01:00
|
|
|
super(parent);
|
|
|
|
socket = checkNotNull(fd, "fd");
|
|
|
|
active = true;
|
|
|
|
// Directly cache the remote and local addresses
|
|
|
|
// See https://github.com/netty/netty/issues/2359
|
|
|
|
this.remote = remote;
|
|
|
|
local = fd.localAddress();
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 12:10:23 +01:00
|
|
|
static boolean isSoErrorZero(Socket fd) {
|
|
|
|
try {
|
|
|
|
return fd.getSoError() == 0;
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new ChannelException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 09:38:35 +02:00
|
|
|
void setFlag(int flag) throws IOException {
|
2015-01-29 06:49:14 +01:00
|
|
|
if (!isFlagSet(flag)) {
|
|
|
|
flags |= flag;
|
|
|
|
modifyEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 09:38:35 +02:00
|
|
|
void clearFlag(int flag) throws IOException {
|
2015-01-29 06:49:14 +01:00
|
|
|
if (isFlagSet(flag)) {
|
|
|
|
flags &= ~flag;
|
|
|
|
modifyEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isFlagSet(int flag) {
|
|
|
|
return (flags & flag) != 0;
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:44:36 +01:00
|
|
|
@Override
|
2017-01-19 17:31:34 +01:00
|
|
|
public final FileDescriptor fd() {
|
|
|
|
return socket;
|
2015-01-14 16:38:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-29 06:49:14 +01:00
|
|
|
@Override
|
|
|
|
public abstract EpollChannelConfig config();
|
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
@Override
|
|
|
|
public boolean isActive() {
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelMetadata metadata() {
|
2015-06-05 03:36:55 +02:00
|
|
|
return METADATA;
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doClose() throws Exception {
|
2016-03-10 03:16:29 +01:00
|
|
|
active = false;
|
2017-02-02 19:15:10 +01:00
|
|
|
// Even if we allow half closed sockets we should give up on reading. Otherwise we may allow a read attempt on a
|
|
|
|
// socket which has not even been connected yet. This has been observed to block during unit tests.
|
|
|
|
inputClosedSeenErrorOnRead = true;
|
2015-05-04 15:40:29 +02:00
|
|
|
try {
|
2017-01-09 14:15:48 +01:00
|
|
|
ChannelPromise promise = connectPromise;
|
|
|
|
if (promise != null) {
|
|
|
|
// Use tryFailure() instead of setFailure() to avoid the race against cancel().
|
|
|
|
promise.tryFailure(DO_CLOSE_CLOSED_CHANNEL_EXCEPTION);
|
|
|
|
connectPromise = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScheduledFuture<?> future = connectTimeoutFuture;
|
|
|
|
if (future != null) {
|
|
|
|
future.cancel(false);
|
|
|
|
connectTimeoutFuture = null;
|
|
|
|
}
|
|
|
|
|
2017-05-23 17:40:22 +02:00
|
|
|
if (isRegistered()) {
|
2017-09-17 02:20:01 +02:00
|
|
|
// Need to check if we are on the EventLoop as doClose() may be triggered by the GlobalEventExecutor
|
|
|
|
// if SO_LINGER is used.
|
|
|
|
//
|
|
|
|
// See https://github.com/netty/netty/issues/7159
|
|
|
|
EventLoop loop = eventLoop();
|
|
|
|
if (loop.inEventLoop()) {
|
|
|
|
doDeregister();
|
|
|
|
} else {
|
|
|
|
loop.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
doDeregister();
|
|
|
|
} catch (Throwable cause) {
|
|
|
|
pipeline().fireExceptionCaught(cause);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-23 17:40:22 +02:00
|
|
|
}
|
2015-05-04 15:40:29 +02:00
|
|
|
} finally {
|
2017-01-19 17:31:34 +01:00
|
|
|
socket.close();
|
2015-05-04 15:40:29 +02:00
|
|
|
}
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doDisconnect() throws Exception {
|
|
|
|
doClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isCompatible(EventLoop loop) {
|
|
|
|
return loop instanceof EpollEventLoop;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isOpen() {
|
2017-01-19 17:31:34 +01:00
|
|
|
return socket.isOpen();
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doDeregister() throws Exception {
|
|
|
|
((EpollEventLoop) eventLoop()).remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-02-28 02:56:41 +01:00
|
|
|
protected final void doBeginRead() throws Exception {
|
2014-08-05 14:24:49 +02:00
|
|
|
// Channel.read() or ChannelHandlerContext.read() was called
|
2016-04-01 09:02:15 +02:00
|
|
|
final AbstractEpollUnsafe unsafe = (AbstractEpollUnsafe) unsafe();
|
2016-02-28 02:56:41 +01:00
|
|
|
unsafe.readPending = true;
|
2014-08-05 14:24:49 +02:00
|
|
|
|
2016-04-07 23:49:19 +02:00
|
|
|
// We must set the read flag here as it is possible the user didn't read in the last read loop, the
|
|
|
|
// executeEpollInReadyRunnable could read nothing, and if the user doesn't explicitly call read they will
|
|
|
|
// never get data after this.
|
2018-06-15 10:28:50 +02:00
|
|
|
setFlag(Native.EPOLLIN);
|
2016-04-07 23:49:19 +02:00
|
|
|
|
2016-02-28 02:56:41 +01:00
|
|
|
// If EPOLL ET mode is enabled and auto read was toggled off on the last read loop then we may not be notified
|
|
|
|
// again if we didn't consume all the data. So we force a read operation here if there maybe more data.
|
|
|
|
if (unsafe.maybeMoreDataToRead) {
|
2017-02-02 19:15:10 +01:00
|
|
|
unsafe.executeEpollInReadyRunnable(config());
|
2016-02-28 02:56:41 +01:00
|
|
|
}
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 19:15:10 +01:00
|
|
|
final boolean shouldBreakEpollInReady(ChannelConfig config) {
|
2017-01-19 17:31:34 +01:00
|
|
|
return socket.isInputShutdown() && (inputClosedSeenErrorOnRead || !isAllowHalfClosure(config));
|
2017-02-02 19:15:10 +01:00
|
|
|
}
|
|
|
|
|
2017-10-14 15:49:45 +02:00
|
|
|
private static boolean isAllowHalfClosure(ChannelConfig config) {
|
2018-03-28 20:02:57 +02:00
|
|
|
return config instanceof SocketChannelConfig &&
|
|
|
|
((SocketChannelConfig) config).isAllowHalfClosure();
|
2017-02-02 19:15:10 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:27:27 +02:00
|
|
|
final void clearEpollIn() {
|
2014-04-22 10:14:31 +02:00
|
|
|
// Only clear if registered with an EventLoop as otherwise
|
|
|
|
if (isRegistered()) {
|
|
|
|
final EventLoop loop = eventLoop();
|
|
|
|
final AbstractEpollUnsafe unsafe = (AbstractEpollUnsafe) unsafe();
|
|
|
|
if (loop.inEventLoop()) {
|
|
|
|
unsafe.clearEpollIn0();
|
|
|
|
} else {
|
|
|
|
// schedule a task to clear the EPOLLIN as it is not safe to modify it directly
|
2016-03-27 14:25:39 +02:00
|
|
|
loop.execute(new Runnable() {
|
2014-04-22 10:14:31 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-04-01 09:02:15 +02:00
|
|
|
if (!unsafe.readPending && !config().isAutoRead()) {
|
2014-04-22 10:14:31 +02:00
|
|
|
// Still no read triggered so clear it now
|
|
|
|
unsafe.clearEpollIn0();
|
|
|
|
}
|
2014-04-10 08:27:27 +02:00
|
|
|
}
|
2014-04-22 10:14:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The EventLoop is not registered atm so just update the flags so the correct value
|
|
|
|
// will be used once the channel is registered
|
2018-06-15 10:28:50 +02:00
|
|
|
flags &= ~Native.EPOLLIN;
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 09:38:35 +02:00
|
|
|
private void modifyEvents() throws IOException {
|
2015-01-29 06:49:14 +01:00
|
|
|
if (isOpen() && isRegistered()) {
|
2014-04-11 21:08:26 +02:00
|
|
|
((EpollEventLoop) eventLoop()).modify(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
@Override
|
|
|
|
protected void doRegister() throws Exception {
|
2016-04-07 23:49:19 +02:00
|
|
|
// Just in case the previous EventLoop was shutdown abruptly, or an event is still pending on the old EventLoop
|
|
|
|
// make sure the epollInReadyRunnablePending variable is reset so we will be able to execute the Runnable on the
|
|
|
|
// new EventLoop.
|
2017-02-02 19:15:10 +01:00
|
|
|
epollInReadyRunnablePending = false;
|
|
|
|
((EpollEventLoop) eventLoop()).add(this);
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected abstract AbstractEpollUnsafe newUnsafe();
|
|
|
|
|
2014-08-05 14:24:49 +02:00
|
|
|
/**
|
|
|
|
* Returns an off-heap copy of the specified {@link ByteBuf}, and releases the original one.
|
|
|
|
*/
|
|
|
|
protected final ByteBuf newDirectBuffer(ByteBuf buf) {
|
|
|
|
return newDirectBuffer(buf, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an off-heap copy of the specified {@link ByteBuf}, and releases the specified holder.
|
|
|
|
* The caller must ensure that the holder releases the original {@link ByteBuf} when the holder is released by
|
|
|
|
* this method.
|
|
|
|
*/
|
|
|
|
protected final ByteBuf newDirectBuffer(Object holder, ByteBuf buf) {
|
|
|
|
final int readableBytes = buf.readableBytes();
|
|
|
|
if (readableBytes == 0) {
|
2017-11-25 21:10:29 +01:00
|
|
|
ReferenceCountUtil.release(holder);
|
2014-08-05 14:24:49 +02:00
|
|
|
return Unpooled.EMPTY_BUFFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
final ByteBufAllocator alloc = alloc();
|
|
|
|
if (alloc.isDirectBufferPooled()) {
|
|
|
|
return newDirectBuffer0(holder, buf, alloc, readableBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
final ByteBuf directBuf = ByteBufUtil.threadLocalDirectBuffer();
|
|
|
|
if (directBuf == null) {
|
|
|
|
return newDirectBuffer0(holder, buf, alloc, readableBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
|
|
|
|
ReferenceCountUtil.safeRelease(holder);
|
|
|
|
return directBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ByteBuf newDirectBuffer0(Object holder, ByteBuf buf, ByteBufAllocator alloc, int capacity) {
|
|
|
|
final ByteBuf directBuf = alloc.directBuffer(capacity);
|
|
|
|
directBuf.writeBytes(buf, buf.readerIndex(), capacity);
|
|
|
|
ReferenceCountUtil.safeRelease(holder);
|
|
|
|
return directBuf;
|
|
|
|
}
|
|
|
|
|
2014-03-16 16:29:45 +01:00
|
|
|
protected static void checkResolvable(InetSocketAddress addr) {
|
2014-03-16 14:25:52 +01:00
|
|
|
if (addr.isUnresolved()) {
|
2014-03-16 16:29:45 +01:00
|
|
|
throw new UnresolvedAddressException();
|
2014-03-16 14:25:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 16:38:46 +01:00
|
|
|
/**
|
|
|
|
* Read bytes into the given {@link ByteBuf} and return the amount.
|
|
|
|
*/
|
|
|
|
protected final int doReadBytes(ByteBuf byteBuf) throws Exception {
|
|
|
|
int writerIndex = byteBuf.writerIndex();
|
|
|
|
int localReadAmount;
|
2015-06-05 03:36:55 +02:00
|
|
|
unsafe().recvBufAllocHandle().attemptedBytesRead(byteBuf.writableBytes());
|
2015-01-14 16:38:46 +01:00
|
|
|
if (byteBuf.hasMemoryAddress()) {
|
2017-01-19 17:31:34 +01:00
|
|
|
localReadAmount = socket.readAddress(byteBuf.memoryAddress(), writerIndex, byteBuf.capacity());
|
2015-01-14 16:38:46 +01:00
|
|
|
} else {
|
|
|
|
ByteBuffer buf = byteBuf.internalNioBuffer(writerIndex, byteBuf.writableBytes());
|
2017-01-19 17:31:34 +01:00
|
|
|
localReadAmount = socket.read(buf, buf.position(), buf.limit());
|
2015-01-14 16:38:46 +01:00
|
|
|
}
|
|
|
|
if (localReadAmount > 0) {
|
|
|
|
byteBuf.writerIndex(writerIndex + localReadAmount);
|
|
|
|
}
|
|
|
|
return localReadAmount;
|
|
|
|
}
|
|
|
|
|
2017-12-08 01:00:52 +01:00
|
|
|
protected final int doWriteBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception {
|
2015-01-14 16:38:46 +01:00
|
|
|
if (buf.hasMemoryAddress()) {
|
2017-12-08 01:00:52 +01:00
|
|
|
int localFlushedAmount = socket.writeAddress(buf.memoryAddress(), buf.readerIndex(), buf.writerIndex());
|
|
|
|
if (localFlushedAmount > 0) {
|
|
|
|
in.removeBytes(localFlushedAmount);
|
|
|
|
return 1;
|
2015-01-14 16:38:46 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-12-08 01:00:52 +01:00
|
|
|
final ByteBuffer nioBuf = buf.nioBufferCount() == 1 ?
|
|
|
|
buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()) : buf.nioBuffer();
|
|
|
|
int localFlushedAmount = socket.write(nioBuf, nioBuf.position(), nioBuf.limit());
|
|
|
|
if (localFlushedAmount > 0) {
|
|
|
|
nioBuf.position(nioBuf.position() + localFlushedAmount);
|
|
|
|
in.removeBytes(localFlushedAmount);
|
|
|
|
return 1;
|
2015-01-14 16:38:46 +01:00
|
|
|
}
|
2015-02-06 16:29:15 +01:00
|
|
|
}
|
2017-12-08 01:00:52 +01:00
|
|
|
return WRITE_STATUS_SNDBUF_FULL;
|
2015-01-14 16:38:46 +01:00
|
|
|
}
|
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
protected abstract class AbstractEpollUnsafe extends AbstractUnsafe {
|
2016-04-01 09:02:15 +02:00
|
|
|
boolean readPending;
|
|
|
|
boolean maybeMoreDataToRead;
|
2015-06-05 03:36:55 +02:00
|
|
|
private EpollRecvByteAllocatorHandle allocHandle;
|
2017-02-02 19:15:10 +01:00
|
|
|
private final Runnable epollInReadyRunnable = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
epollInReadyRunnablePending = false;
|
|
|
|
epollInReady();
|
|
|
|
}
|
|
|
|
};
|
2014-02-15 22:26:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called once EPOLLIN event is ready to be processed
|
|
|
|
*/
|
|
|
|
abstract void epollInReady();
|
|
|
|
|
2016-04-03 01:48:07 +02:00
|
|
|
final void epollInBefore() { maybeMoreDataToRead = false; }
|
2016-02-28 02:56:41 +01:00
|
|
|
|
|
|
|
final void epollInFinally(ChannelConfig config) {
|
2018-05-24 20:29:29 +02:00
|
|
|
maybeMoreDataToRead = allocHandle.maybeMoreDataToRead();
|
|
|
|
|
|
|
|
if (allocHandle.isReceivedRdHup() || (readPending && maybeMoreDataToRead)) {
|
2016-04-01 09:02:15 +02:00
|
|
|
// trigger a read again as there may be something left to read and because of epoll ET we
|
|
|
|
// will not get notified again until we read everything from the socket
|
|
|
|
//
|
|
|
|
// It is possible the last fireChannelRead call could cause the user to call read() again, or if
|
|
|
|
// autoRead is true the call to channelReadComplete would also call read, but maybeMoreDataToRead is set
|
|
|
|
// to false before every read operation to prevent re-entry into epollInReady() we will not read from
|
|
|
|
// the underlying OS again unless the user happens to call read again.
|
2017-02-02 19:15:10 +01:00
|
|
|
executeEpollInReadyRunnable(config);
|
2018-05-24 20:29:29 +02:00
|
|
|
} else if (!readPending && !config.isAutoRead()) {
|
|
|
|
// Check if there is a readPending which was not processed yet.
|
|
|
|
// This could be for two reasons:
|
|
|
|
// * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
|
|
|
|
// * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
|
|
|
|
//
|
|
|
|
// See https://github.com/netty/netty/issues/2254
|
|
|
|
clearEpollIn();
|
2016-02-28 02:56:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:15:10 +01:00
|
|
|
final void executeEpollInReadyRunnable(ChannelConfig config) {
|
|
|
|
if (epollInReadyRunnablePending || !isActive() || shouldBreakEpollInReady(config)) {
|
2016-04-07 23:49:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
epollInReadyRunnablePending = true;
|
2016-04-01 09:02:15 +02:00
|
|
|
eventLoop().execute(epollInReadyRunnable);
|
2015-08-15 01:36:48 +02:00
|
|
|
}
|
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
/**
|
|
|
|
* Called once EPOLLRDHUP event is ready to be processed
|
|
|
|
*/
|
2015-08-04 02:58:37 +02:00
|
|
|
final void epollRdHupReady() {
|
2015-10-06 02:43:41 +02:00
|
|
|
// This must happen before we attempt to read. This will ensure reading continues until an error occurs.
|
|
|
|
recvBufAllocHandle().receivedRdHup();
|
|
|
|
|
2015-08-04 02:58:37 +02:00
|
|
|
if (isActive()) {
|
|
|
|
// If it is still active, we need to call epollInReady as otherwise we may miss to
|
|
|
|
// read pending data from the underlying file descriptor.
|
|
|
|
// See https://github.com/netty/netty/issues/3709
|
|
|
|
epollInReady();
|
2017-02-16 00:04:38 +01:00
|
|
|
} else {
|
|
|
|
// Just to be safe make sure the input marked as closed.
|
|
|
|
shutdownInput(true);
|
2015-08-04 02:58:37 +02:00
|
|
|
}
|
2017-02-16 00:04:38 +01:00
|
|
|
|
|
|
|
// Clear the EPOLLRDHUP flag to prevent continuously getting woken up on this event.
|
|
|
|
clearEpollRdHup();
|
2015-08-04 02:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the {@link Native#EPOLLRDHUP} flag from EPOLL, and close on failure.
|
|
|
|
*/
|
|
|
|
private void clearEpollRdHup() {
|
|
|
|
try {
|
|
|
|
clearFlag(Native.EPOLLRDHUP);
|
|
|
|
} catch (IOException e) {
|
|
|
|
pipeline().fireExceptionCaught(e);
|
|
|
|
close(voidPromise());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shutdown the input side of the channel.
|
|
|
|
*/
|
2017-02-02 19:15:10 +01:00
|
|
|
void shutdownInput(boolean rdHup) {
|
2017-01-19 17:31:34 +01:00
|
|
|
if (!socket.isInputShutdown()) {
|
2017-02-02 19:15:10 +01:00
|
|
|
if (isAllowHalfClosure(config())) {
|
2015-10-07 23:12:38 +02:00
|
|
|
try {
|
2017-01-19 17:31:34 +01:00
|
|
|
socket.shutdown(true, false);
|
2015-11-13 03:29:02 +01:00
|
|
|
} catch (IOException ignored) {
|
|
|
|
// We attempted to shutdown and failed, which means the input has already effectively been
|
|
|
|
// shutdown.
|
2016-08-29 12:43:11 +02:00
|
|
|
fireEventAndClose(ChannelInputShutdownEvent.INSTANCE);
|
|
|
|
return;
|
|
|
|
} catch (NotYetConnectedException ignore) {
|
|
|
|
// We attempted to shutdown and failed, which means the input has already effectively been
|
|
|
|
// shutdown.
|
2015-08-04 02:58:37 +02:00
|
|
|
}
|
2017-01-19 17:31:34 +01:00
|
|
|
clearEpollIn();
|
2016-08-29 12:43:11 +02:00
|
|
|
pipeline().fireUserEventTriggered(ChannelInputShutdownEvent.INSTANCE);
|
2015-10-07 23:12:38 +02:00
|
|
|
} else {
|
|
|
|
close(voidPromise());
|
2015-08-04 02:58:37 +02:00
|
|
|
}
|
2017-02-02 19:15:10 +01:00
|
|
|
} else if (!rdHup) {
|
|
|
|
inputClosedSeenErrorOnRead = true;
|
|
|
|
pipeline().fireUserEventTriggered(ChannelInputShutdownReadComplete.INSTANCE);
|
2015-08-04 02:58:37 +02:00
|
|
|
}
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
2016-08-29 12:43:11 +02:00
|
|
|
private void fireEventAndClose(Object evt) {
|
|
|
|
pipeline().fireUserEventTriggered(evt);
|
|
|
|
close(voidPromise());
|
|
|
|
}
|
|
|
|
|
2015-06-05 03:36:55 +02:00
|
|
|
@Override
|
|
|
|
public EpollRecvByteAllocatorHandle recvBufAllocHandle() {
|
|
|
|
if (allocHandle == null) {
|
2017-02-02 19:15:10 +01:00
|
|
|
allocHandle = newEpollHandle((RecvByteBufAllocator.ExtendedHandle) super.recvBufAllocHandle());
|
2015-06-05 03:36:55 +02:00
|
|
|
}
|
|
|
|
return allocHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-22 14:42:11 +01:00
|
|
|
* Create a new {@link EpollRecvByteAllocatorHandle} instance.
|
2015-06-05 03:36:55 +02:00
|
|
|
* @param handle The handle to wrap with EPOLL specific logic.
|
|
|
|
*/
|
2017-02-02 19:15:10 +01:00
|
|
|
EpollRecvByteAllocatorHandle newEpollHandle(RecvByteBufAllocator.ExtendedHandle handle) {
|
|
|
|
return new EpollRecvByteAllocatorHandle(handle);
|
2016-02-28 02:56:41 +01:00
|
|
|
}
|
2015-06-05 03:36:55 +02:00
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
@Override
|
2018-02-18 20:49:08 +01:00
|
|
|
protected final void flush0() {
|
2014-02-15 22:26:36 +01:00
|
|
|
// Flush immediately only when there's no pending flush.
|
|
|
|
// If there's a pending flush operation, event loop will call forceFlush() later,
|
|
|
|
// and thus there's no need to call it now.
|
2018-02-18 20:49:08 +01:00
|
|
|
if (!isFlagSet(Native.EPOLLOUT)) {
|
|
|
|
super.flush0();
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called once a EPOLLOUT event is ready to be processed
|
|
|
|
*/
|
2017-01-09 14:15:48 +01:00
|
|
|
final void epollOutReady() {
|
|
|
|
if (connectPromise != null) {
|
|
|
|
// pending connect which is now complete so handle it.
|
|
|
|
finishConnect();
|
|
|
|
} else if (!socket.isOutputShutdown()) {
|
|
|
|
// directly call super.flush0() to force a flush now
|
|
|
|
super.flush0();
|
2015-10-07 23:12:38 +02:00
|
|
|
}
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:27:27 +02:00
|
|
|
protected final void clearEpollIn0() {
|
2015-04-30 09:38:35 +02:00
|
|
|
assert eventLoop().inEventLoop();
|
|
|
|
try {
|
2016-04-03 01:48:07 +02:00
|
|
|
readPending = false;
|
2018-06-15 10:28:50 +02:00
|
|
|
clearFlag(Native.EPOLLIN);
|
2015-04-30 09:38:35 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
// When this happens there is something completely wrong with either the filedescriptor or epoll,
|
|
|
|
// so fire the exception through the pipeline and close the Channel.
|
|
|
|
pipeline().fireExceptionCaught(e);
|
|
|
|
unsafe().close(unsafe().voidPromise());
|
|
|
|
}
|
2014-04-10 08:27:27 +02:00
|
|
|
}
|
2017-01-09 14:15:48 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void connect(
|
|
|
|
final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
|
|
|
|
if (!promise.setUncancellable() || !ensureOpen(promise)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (connectPromise != null) {
|
|
|
|
throw new ConnectionPendingException();
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean wasActive = isActive();
|
|
|
|
if (doConnect(remoteAddress, localAddress)) {
|
|
|
|
fulfillConnectPromise(promise, wasActive);
|
|
|
|
} else {
|
|
|
|
connectPromise = promise;
|
|
|
|
requestedRemoteAddress = remoteAddress;
|
|
|
|
|
|
|
|
// Schedule connect timeout.
|
|
|
|
int connectTimeoutMillis = config().getConnectTimeoutMillis();
|
|
|
|
if (connectTimeoutMillis > 0) {
|
|
|
|
connectTimeoutFuture = eventLoop().schedule(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
ChannelPromise connectPromise = AbstractEpollChannel.this.connectPromise;
|
|
|
|
ConnectTimeoutException cause =
|
|
|
|
new ConnectTimeoutException("connection timed out: " + remoteAddress);
|
|
|
|
if (connectPromise != null && connectPromise.tryFailure(cause)) {
|
|
|
|
close(voidPromise());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, connectTimeoutMillis, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.addListener(new ChannelFutureListener() {
|
|
|
|
@Override
|
|
|
|
public void operationComplete(ChannelFuture future) throws Exception {
|
|
|
|
if (future.isCancelled()) {
|
|
|
|
if (connectTimeoutFuture != null) {
|
|
|
|
connectTimeoutFuture.cancel(false);
|
|
|
|
}
|
|
|
|
connectPromise = null;
|
|
|
|
close(voidPromise());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
closeIfClosed();
|
|
|
|
promise.tryFailure(annotateConnectException(t, remoteAddress));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fulfillConnectPromise(ChannelPromise promise, boolean wasActive) {
|
|
|
|
if (promise == null) {
|
|
|
|
// Closed via cancellation and the promise has been notified already.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
active = true;
|
|
|
|
|
|
|
|
// Get the state as trySuccess() may trigger an ChannelFutureListener that will close the Channel.
|
|
|
|
// We still need to ensure we call fireChannelActive() in this case.
|
|
|
|
boolean active = isActive();
|
|
|
|
|
|
|
|
// trySuccess() will return false if a user cancelled the connection attempt.
|
|
|
|
boolean promiseSet = promise.trySuccess();
|
|
|
|
|
|
|
|
// Regardless if the connection attempt was cancelled, channelActive() event should be triggered,
|
|
|
|
// because what happened is what happened.
|
|
|
|
if (!wasActive && active) {
|
|
|
|
pipeline().fireChannelActive();
|
2018-12-14 11:11:34 +01:00
|
|
|
readIfIsAutoRead();
|
2017-01-09 14:15:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a user cancelled the connection attempt, close the channel, which is followed by channelInactive().
|
|
|
|
if (!promiseSet) {
|
|
|
|
close(voidPromise());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fulfillConnectPromise(ChannelPromise promise, Throwable cause) {
|
|
|
|
if (promise == null) {
|
|
|
|
// Closed via cancellation and the promise has been notified already.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use tryFailure() instead of setFailure() to avoid the race against cancel().
|
|
|
|
promise.tryFailure(cause);
|
|
|
|
closeIfClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void finishConnect() {
|
|
|
|
// Note this method is invoked by the event loop only if the connection attempt was
|
|
|
|
// neither cancelled nor timed out.
|
|
|
|
|
|
|
|
assert eventLoop().inEventLoop();
|
|
|
|
|
|
|
|
boolean connectStillInProgress = false;
|
|
|
|
try {
|
|
|
|
boolean wasActive = isActive();
|
|
|
|
if (!doFinishConnect()) {
|
|
|
|
connectStillInProgress = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fulfillConnectPromise(connectPromise, wasActive);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
fulfillConnectPromise(connectPromise, annotateConnectException(t, requestedRemoteAddress));
|
|
|
|
} finally {
|
|
|
|
if (!connectStillInProgress) {
|
|
|
|
// Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used
|
|
|
|
// See https://github.com/netty/netty/issues/1770
|
|
|
|
if (connectTimeoutFuture != null) {
|
|
|
|
connectTimeoutFuture.cancel(false);
|
|
|
|
}
|
|
|
|
connectPromise = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finish the connect
|
|
|
|
*/
|
|
|
|
private boolean doFinishConnect() throws Exception {
|
|
|
|
if (socket.finishConnect()) {
|
|
|
|
clearFlag(Native.EPOLLOUT);
|
|
|
|
if (requestedRemoteAddress instanceof InetSocketAddress) {
|
|
|
|
remote = computeRemoteAddr((InetSocketAddress) requestedRemoteAddress, socket.remoteAddress());
|
|
|
|
}
|
|
|
|
requestedRemoteAddress = null;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
setFlag(Native.EPOLLOUT);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doBind(SocketAddress local) throws Exception {
|
|
|
|
if (local instanceof InetSocketAddress) {
|
|
|
|
checkResolvable((InetSocketAddress) local);
|
|
|
|
}
|
|
|
|
socket.bind(local);
|
|
|
|
this.local = socket.localAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to the remote peer
|
|
|
|
*/
|
|
|
|
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
|
|
|
|
if (localAddress instanceof InetSocketAddress) {
|
|
|
|
checkResolvable((InetSocketAddress) localAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
InetSocketAddress remoteSocketAddr = remoteAddress instanceof InetSocketAddress
|
|
|
|
? (InetSocketAddress) remoteAddress : null;
|
|
|
|
if (remoteSocketAddr != null) {
|
|
|
|
checkResolvable(remoteSocketAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remote != null) {
|
|
|
|
// Check if already connected before trying to connect. This is needed as connect(...) will not return -1
|
|
|
|
// and set errno to EISCONN if a previous connect(...) attempt was setting errno to EINPROGRESS and finished
|
|
|
|
// later.
|
|
|
|
throw new AlreadyConnectedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localAddress != null) {
|
|
|
|
socket.bind(localAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean connected = doConnect0(remoteAddress);
|
|
|
|
if (connected) {
|
|
|
|
remote = remoteSocketAddr == null ?
|
|
|
|
remoteAddress : computeRemoteAddr(remoteSocketAddr, socket.remoteAddress());
|
|
|
|
}
|
|
|
|
// We always need to set the localAddress even if not connected yet as the bind already took place.
|
|
|
|
//
|
|
|
|
// See https://github.com/netty/netty/issues/3463
|
|
|
|
local = socket.localAddress();
|
|
|
|
return connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean doConnect0(SocketAddress remote) throws Exception {
|
|
|
|
boolean success = false;
|
|
|
|
try {
|
|
|
|
boolean connected = socket.connect(remote);
|
|
|
|
if (!connected) {
|
|
|
|
setFlag(Native.EPOLLOUT);
|
|
|
|
}
|
|
|
|
success = true;
|
|
|
|
return connected;
|
|
|
|
} finally {
|
|
|
|
if (!success) {
|
|
|
|
doClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected SocketAddress localAddress0() {
|
|
|
|
return local;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected SocketAddress remoteAddress0() {
|
|
|
|
return remote;
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
}
|