use of java.nio.channels.SelectionKey in project Mycat-Server by MyCATApache.
the class NIOSocketWR method clearSelectionKey.
private void clearSelectionKey() {
try {
SelectionKey key = this.processKey;
if (key != null && key.isValid()) {
key.attach(null);
key.cancel();
}
} catch (Exception e) {
AbstractConnection.LOGGER.warn("clear selector keys err:" + e);
}
}
use of java.nio.channels.SelectionKey in project netty by netty.
the class AbstractNioChannel method doBeginRead.
@Override
protected void doBeginRead() throws Exception {
// Channel.read() or ChannelHandlerContext.read() was called
final SelectionKey selectionKey = this.selectionKey;
if (!selectionKey.isValid()) {
return;
}
readPending = true;
final int interestOps = selectionKey.interestOps();
if ((interestOps & readInterestOp) == 0) {
selectionKey.interestOps(interestOps | readInterestOp);
}
}
use of java.nio.channels.SelectionKey in project netty by netty.
the class NioEventLoop method processSelectedKeysOptimized.
private void processSelectedKeysOptimized() {
for (int i = 0; i < selectedKeys.size; ++i) {
final SelectionKey k = selectedKeys.keys[i];
// null out entry in the array to allow to have it GC'ed once the Channel close
// See https://github.com/netty/netty/issues/2363
selectedKeys.keys[i] = null;
final Object a = k.attachment();
if (a instanceof AbstractNioChannel) {
processSelectedKey(k, (AbstractNioChannel) a);
} else {
@SuppressWarnings("unchecked") NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
processSelectedKey(k, task);
}
if (needsToSelectAgain) {
// null out entries in the array to allow to have it GC'ed once the Channel close
// See https://github.com/netty/netty/issues/2363
selectedKeys.reset(i + 1);
selectAgain();
i = -1;
}
}
}
use of java.nio.channels.SelectionKey in project netty by netty.
the class AbstractNioByteChannel method clearOpWrite.
protected final void clearOpWrite() {
final SelectionKey key = selectionKey();
// See https://github.com/netty/netty/issues/2104
if (!key.isValid()) {
return;
}
final int interestOps = key.interestOps();
if ((interestOps & SelectionKey.OP_WRITE) != 0) {
key.interestOps(interestOps & ~SelectionKey.OP_WRITE);
}
}
use of java.nio.channels.SelectionKey in project FishChatAndroid by oikomi.
the class Session method startIOThread.
public void startIOThread(final ArrayBlockingQueue<Cmd> queue) {
Runnable ioTask = new Runnable() {
@Override
public void run() {
while (true) {
try {
selector.select(1000);
} catch (IOException e) {
e.printStackTrace();
}
Iterator ite = selector.selectedKeys().iterator();
while (ite.hasNext()) {
SelectionKey key = (SelectionKey) ite.next();
ite.remove();
if (key.isConnectable()) {
Log.d("Session", "isConnectable");
SocketChannel channel = (SocketChannel) key.channel();
if (channel.isConnectionPending()) {
try {
channel.finishConnect();
} catch (IOException e) {
e.printStackTrace();
}
}
//try {
// channel.configureBlocking(false);
//} catch (IOException e) {
// e.printStackTrace();
//}
//try {
// channel.register(selector, SelectionKey.OP_WRITE);
//} catch (ClosedChannelException e) {
// e.printStackTrace();
//}
} else if (key.isReadable()) {
//Log.d("Session", "isReadable");
try {
handleRead(key, queue);
} catch (IOException e) {
e.printStackTrace();
}
} else if (key.isWritable()) {
Log.d("Session", "isWritable");
try {
handleSend();
} catch (IOException e) {
e.printStackTrace();
}
}
synchronized (this) {
if (closeFlag) {
break;
}
}
}
}
}
};
ioThread = new Thread(ioTask);
ioThread.start();
}
Aggregations