2012-02-18 23:02:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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.socket.nio;
|
|
|
|
|
|
|
|
import io.netty.channel.AbstractChannel;
|
|
|
|
import io.netty.channel.Channel;
|
2012-05-12 17:40:28 +02:00
|
|
|
import io.netty.channel.EventLoop;
|
2012-02-18 23:02:56 +01:00
|
|
|
|
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-02-18 23:02:56 +01:00
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
public abstract class AbstractNioChannel extends AbstractChannel {
|
2012-02-18 23:02:56 +01:00
|
|
|
|
2012-05-02 14:05:53 +02:00
|
|
|
private final SelectableChannel ch;
|
|
|
|
private volatile SelectionKey selectionKey;
|
2012-05-02 08:01:58 +02:00
|
|
|
|
2012-05-09 15:09:06 +02:00
|
|
|
protected AbstractNioChannel(Channel parent, Integer id, SelectableChannel ch) {
|
|
|
|
super(parent, id);
|
2012-05-02 08:01:58 +02:00
|
|
|
this.ch = ch;
|
2012-03-29 17:07:19 +02:00
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected SelectableChannel javaChannel() {
|
|
|
|
return ch;
|
2012-03-06 19:26:32 +01:00
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
|
2012-05-02 14:05:53 +02:00
|
|
|
protected SelectionKey selectionKey() {
|
|
|
|
assert selectionKey != null;
|
|
|
|
return selectionKey;
|
|
|
|
}
|
|
|
|
|
2012-02-18 23:02:56 +01:00
|
|
|
@Override
|
2012-05-12 17:40:28 +02:00
|
|
|
protected boolean isCompatible(EventLoop loop) {
|
2012-05-15 06:11:47 +02:00
|
|
|
return loop instanceof SingleThreadSelectorEventLoop;
|
2012-05-12 17:40:28 +02:00
|
|
|
}
|
2012-05-02 14:05:53 +02:00
|
|
|
|
|
|
|
@Override
|
2012-05-09 15:09:06 +02:00
|
|
|
protected void doRegister() throws Exception {
|
2012-05-15 06:11:47 +02:00
|
|
|
SingleThreadSelectorEventLoop loop = (SingleThreadSelectorEventLoop) eventLoop();
|
2012-05-11 14:19:19 +02:00
|
|
|
selectionKey = javaChannel().register(loop.selector, isActive()? SelectionKey.OP_READ : 0, this);
|
2012-05-02 14:05:53 +02:00
|
|
|
}
|
2012-05-12 22:09:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean inEventLoopDrivenFlush() {
|
|
|
|
return (selectionKey.interestOps() & SelectionKey.OP_WRITE) != 0;
|
|
|
|
}
|
2012-02-18 23:02:56 +01:00
|
|
|
}
|