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;
|
|
|
|
|
|
|
|
import io.netty.channel.AbstractChannel;
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelMetadata;
|
|
|
|
import io.netty.channel.EventLoop;
|
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;
|
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;
|
|
|
|
protected int flags;
|
|
|
|
protected volatile boolean active;
|
|
|
|
volatile int fd;
|
|
|
|
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);
|
|
|
|
this.fd = fd;
|
|
|
|
readFlag = flag;
|
|
|
|
flags |= flag;
|
2014-02-17 14:25:17 +01:00
|
|
|
this.active = active;
|
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();
|
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
int fd = this.fd;
|
|
|
|
this.fd = -1;
|
|
|
|
Native.close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InetSocketAddress remoteAddress() {
|
|
|
|
return (InetSocketAddress) super.remoteAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InetSocketAddress localAddress() {
|
|
|
|
return (InetSocketAddress) super.localAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doDisconnect() throws Exception {
|
|
|
|
doClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isCompatible(EventLoop loop) {
|
|
|
|
return loop instanceof EpollEventLoop;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isOpen() {
|
|
|
|
return fd != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doDeregister() throws Exception {
|
|
|
|
((EpollEventLoop) eventLoop()).remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doBeginRead() throws Exception {
|
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-02-21 21:42:13 +01:00
|
|
|
@Override
|
|
|
|
public void beginRead() {
|
|
|
|
// Channel.read() or ChannelHandlerContext.read() was called
|
|
|
|
readPending = true;
|
|
|
|
super.beginRead();
|
|
|
|
}
|
|
|
|
|
2014-02-15 22:26:36 +01:00
|
|
|
@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
|
|
|
}
|
|
|
|
}
|