Search in sources :

Example 46 with SelectionKey

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);
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException)

Example 47 with SelectionKey

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);
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey)

Example 48 with SelectionKey

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;
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SelectableChannel(java.nio.channels.SelectableChannel)

Example 49 with SelectionKey

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);
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey)

Example 50 with SelectionKey

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();
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) Iterator(java.util.Iterator) IOException(java.io.IOException)

Aggregations

SelectionKey (java.nio.channels.SelectionKey)202 IOException (java.io.IOException)93 SocketChannel (java.nio.channels.SocketChannel)59 Selector (java.nio.channels.Selector)44 ServerSocketChannel (java.nio.channels.ServerSocketChannel)41 ClosedChannelException (java.nio.channels.ClosedChannelException)30 InetSocketAddress (java.net.InetSocketAddress)27 CancelledKeyException (java.nio.channels.CancelledKeyException)25 ByteBuffer (java.nio.ByteBuffer)24 ClosedSelectorException (java.nio.channels.ClosedSelectorException)17 SelectableChannel (java.nio.channels.SelectableChannel)13 Test (org.junit.Test)13 Iterator (java.util.Iterator)9 Socket (java.net.Socket)7 ArrayList (java.util.ArrayList)7 SocketTimeoutException (java.net.SocketTimeoutException)6 AbstractSelectionKey (java.nio.channels.spi.AbstractSelectionKey)6 EOFException (java.io.EOFException)5 DatagramChannel (java.nio.channels.DatagramChannel)5 HashSet (java.util.HashSet)5