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;
|
|
|
|
import io.netty.channel.ChannelMetadata;
|
|
|
|
import io.netty.channel.EventLoop;
|
2015-01-15 14:38:14 +01:00
|
|
|
import io.netty.channel.FileDescriptor;
|
2014-08-05 14:24:49 +02:00
|
|
|
import io.netty.util.ReferenceCountUtil;
|
2014-04-10 08:27:27 +02:00
|
|
|
import io.netty.util.internal.OneTimeTask;
|
2014-02-15 22:26:36 +01:00
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
2015-01-14 16:38:46 +01:00
|
|
|
import java.nio.ByteBuffer;
|
2014-03-16 16:29:45 +01:00
|
|
|
import java.nio.channels.UnresolvedAddressException;
|
2014-02-15 22:26:36 +01:00
|
|
|
|
|
|
|
abstract class AbstractEpollChannel extends AbstractChannel {
|
|
|
|
private static final ChannelMetadata DATA = new ChannelMetadata(false);
|
|
|
|
private final int readFlag;
|
2015-01-15 14:38:14 +01:00
|
|
|
private volatile FileDescriptor fileDescriptor;
|
2014-02-15 22:26:36 +01:00
|
|
|
protected int flags;
|
|
|
|
protected volatile boolean active;
|
|
|
|
int id;
|
|
|
|
|
2014-04-11 21:08:26 +02:00
|
|
|
AbstractEpollChannel(int fd, int flag) {
|
|
|
|
this(null, fd, flag, false);
|
2014-02-17 14:25:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AbstractEpollChannel(Channel parent, int fd, int flag, boolean active) {
|
2014-02-15 22:26:36 +01:00
|
|
|
super(parent);
|
|
|
|
readFlag = flag;
|
|
|
|
flags |= flag;
|
2014-02-17 14:25:17 +01:00
|
|
|
this.active = active;
|
2015-01-15 14:38:14 +01:00
|
|
|
fileDescriptor = new EpollFileDescriptor(fd);
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 14:38:14 +01:00
|
|
|
/**
|
|
|
|
* Returns the {@link FileDescriptor} that is used by this {@link Channel}.
|
|
|
|
*/
|
|
|
|
public final FileDescriptor fd() {
|
|
|
|
return fileDescriptor;
|
2015-01-14 16:38:46 +01:00
|
|
|
}
|
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
@Override
|
|
|
|
public boolean isActive() {
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ChannelMetadata metadata() {
|
|
|
|
return DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doClose() throws Exception {
|
|
|
|
active = false;
|
2014-02-19 11:57:41 +01:00
|
|
|
|
|
|
|
// deregister from epoll now
|
|
|
|
doDeregister();
|
|
|
|
|
2015-01-15 14:38:14 +01:00
|
|
|
FileDescriptor fd = fileDescriptor;
|
|
|
|
fileDescriptor = FileDescriptor.INVALID;
|
|
|
|
Native.close(fd.intValue());
|
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() {
|
2015-01-15 14:38:14 +01:00
|
|
|
return fileDescriptor != EpollFileDescriptor.INVALID;
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doDeregister() throws Exception {
|
|
|
|
((EpollEventLoop) eventLoop()).remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doBeginRead() throws Exception {
|
2014-08-05 14:24:49 +02:00
|
|
|
// Channel.read() or ChannelHandlerContext.read() was called
|
|
|
|
((AbstractEpollUnsafe) unsafe()).readPending = true;
|
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
if ((flags & readFlag) == 0) {
|
|
|
|
flags |= readFlag;
|
2014-04-21 11:16:07 +02:00
|
|
|
modifyEvents();
|
2014-02-15 22:26:36 +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
|
|
|
|
loop.execute(new OneTimeTask() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!config().isAutoRead() && !unsafe.readPending) {
|
|
|
|
// 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
|
|
|
|
flags &= ~readFlag;
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 21:08:26 +02:00
|
|
|
protected final void setEpollOut() {
|
|
|
|
if ((flags & Native.EPOLLOUT) == 0) {
|
|
|
|
flags |= Native.EPOLLOUT;
|
2014-04-21 11:16:07 +02:00
|
|
|
modifyEvents();
|
2014-04-11 21:08:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final void clearEpollOut() {
|
|
|
|
if ((flags & Native.EPOLLOUT) != 0) {
|
|
|
|
flags &= ~Native.EPOLLOUT;
|
2014-04-21 11:16:07 +02:00
|
|
|
modifyEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void modifyEvents() {
|
|
|
|
if (isOpen()) {
|
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 {
|
|
|
|
EpollEventLoop loop = (EpollEventLoop) eventLoop();
|
|
|
|
loop.add(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
|
|
|
ReferenceCountUtil.safeRelease(holder);
|
|
|
|
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;
|
|
|
|
if (byteBuf.hasMemoryAddress()) {
|
2015-01-15 14:38:14 +01:00
|
|
|
localReadAmount = Native.readAddress(
|
|
|
|
fileDescriptor.intValue(), byteBuf.memoryAddress(), writerIndex, byteBuf.capacity());
|
2015-01-14 16:38:46 +01:00
|
|
|
} else {
|
|
|
|
ByteBuffer buf = byteBuf.internalNioBuffer(writerIndex, byteBuf.writableBytes());
|
2015-01-15 14:38:14 +01:00
|
|
|
localReadAmount = Native.read(fileDescriptor.intValue(), buf, buf.position(), buf.limit());
|
2015-01-14 16:38:46 +01:00
|
|
|
}
|
|
|
|
if (localReadAmount > 0) {
|
|
|
|
byteBuf.writerIndex(writerIndex + localReadAmount);
|
|
|
|
}
|
|
|
|
return localReadAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final int doWriteBytes(ByteBuf buf) throws Exception {
|
|
|
|
int readableBytes = buf.readableBytes();
|
|
|
|
int writtenBytes = 0;
|
|
|
|
if (buf.hasMemoryAddress()) {
|
|
|
|
long memoryAddress = buf.memoryAddress();
|
|
|
|
int readerIndex = buf.readerIndex();
|
|
|
|
int writerIndex = buf.writerIndex();
|
|
|
|
for (;;) {
|
2015-01-15 14:38:14 +01:00
|
|
|
int localFlushedAmount = Native.writeAddress(
|
|
|
|
fileDescriptor.intValue(), memoryAddress, readerIndex, writerIndex);
|
2015-01-14 16:38:46 +01:00
|
|
|
if (localFlushedAmount > 0) {
|
|
|
|
writtenBytes += localFlushedAmount;
|
|
|
|
if (writtenBytes == readableBytes) {
|
|
|
|
return writtenBytes;
|
|
|
|
}
|
|
|
|
readerIndex += localFlushedAmount;
|
|
|
|
} else {
|
|
|
|
// Returned EAGAIN need to set EPOLLOUT
|
|
|
|
setEpollOut();
|
|
|
|
return writtenBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ByteBuffer nioBuf;
|
|
|
|
if (buf.nioBufferCount() == 1) {
|
|
|
|
nioBuf = buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes());
|
|
|
|
} else {
|
|
|
|
nioBuf = buf.nioBuffer();
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
int pos = nioBuf.position();
|
|
|
|
int limit = nioBuf.limit();
|
2015-01-15 14:38:14 +01:00
|
|
|
int localFlushedAmount = Native.write(fileDescriptor.intValue(), nioBuf, pos, limit);
|
2015-01-14 16:38:46 +01:00
|
|
|
if (localFlushedAmount > 0) {
|
|
|
|
nioBuf.position(pos + localFlushedAmount);
|
|
|
|
writtenBytes += localFlushedAmount;
|
|
|
|
if (writtenBytes == readableBytes) {
|
|
|
|
return writtenBytes;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Returned EAGAIN need to set EPOLLOUT
|
|
|
|
setEpollOut();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return writtenBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
protected abstract class AbstractEpollUnsafe extends AbstractUnsafe {
|
2014-02-21 21:42:13 +01:00
|
|
|
protected boolean readPending;
|
2014-02-15 22:26:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called once EPOLLIN event is ready to be processed
|
|
|
|
*/
|
|
|
|
abstract void epollInReady();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called once EPOLLRDHUP event is ready to be processed
|
|
|
|
*/
|
|
|
|
void epollRdHupReady() {
|
|
|
|
// NOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void flush0() {
|
|
|
|
// 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.
|
|
|
|
if (isFlushPending()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.flush0();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called once a EPOLLOUT event is ready to be processed
|
|
|
|
*/
|
|
|
|
void epollOutReady() {
|
|
|
|
// directly call super.flush0() to force a flush now
|
|
|
|
super.flush0();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isFlushPending() {
|
|
|
|
return (flags & Native.EPOLLOUT) != 0;
|
|
|
|
}
|
2014-04-10 08:27:27 +02:00
|
|
|
|
|
|
|
protected final void clearEpollIn0() {
|
|
|
|
if ((flags & readFlag) != 0) {
|
|
|
|
flags &= ~readFlag;
|
2014-04-21 11:16:07 +02:00
|
|
|
modifyEvents();
|
2014-04-10 08:27:27 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 22:26:36 +01:00
|
|
|
}
|
|
|
|
}
|