Search in sources :

Example 16 with CancelledKeyException

use of java.nio.channels.CancelledKeyException in project netty by netty.

the class NioEventLoop method processSelectedKey.

private void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
    final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe();
    if (!k.isValid()) {
        final EventLoop eventLoop;
        try {
            eventLoop = ch.eventLoop();
        } catch (Throwable ignored) {
            // to close ch.
            return;
        }
        // See https://github.com/netty/netty/issues/5125
        if (eventLoop != this || eventLoop == null) {
            return;
        }
        // close the channel if the key is not valid anymore
        unsafe.close(unsafe.voidPromise());
        return;
    }
    try {
        int readyOps = k.readyOps();
        // the NIO JDK channel implementation may throw a NotYetConnectedException.
        if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
            // remove OP_CONNECT as otherwise Selector.select(..) will always return without blocking
            // See https://github.com/netty/netty/issues/924
            int ops = k.interestOps();
            ops &= ~SelectionKey.OP_CONNECT;
            k.interestOps(ops);
            unsafe.finishConnect();
        }
        // Process OP_WRITE first as we may be able to write some queued buffers and so free memory.
        if ((readyOps & SelectionKey.OP_WRITE) != 0) {
            // Call forceFlush which will also take care of clear the OP_WRITE once there is nothing left to write
            ch.unsafe().forceFlush();
        }
        // to a spin loop
        if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
            unsafe.read();
        }
    } catch (CancelledKeyException ignored) {
        unsafe.close(unsafe.voidPromise());
    }
}
Also used : EventLoop(io.netty.channel.EventLoop) SingleThreadEventLoop(io.netty.channel.SingleThreadEventLoop) CancelledKeyException(java.nio.channels.CancelledKeyException)

Example 17 with CancelledKeyException

use of java.nio.channels.CancelledKeyException in project robovm by robovm.

the class CancelledKeyExceptionTest method test_Constructor.

/**
     * @tests {@link java.nio.channels.CancelledKeyException#CancelledKeyException()}
     */
public void test_Constructor() {
    CancelledKeyException e = new CancelledKeyException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : CancelledKeyException(java.nio.channels.CancelledKeyException)

Example 18 with CancelledKeyException

use of java.nio.channels.CancelledKeyException in project XobotOS by xamarin.

the class AbstractSelectableChannel method register.

/**
     * Registers this channel with the specified selector for the specified
     * interest set. If the channel is already registered with the selector, the
     * {@link SelectionKey interest set} is updated to {@code interestSet} and
     * the corresponding selection key is returned. If the channel is not yet
     * registered, this method calls the {@code register} method of
     * {@code selector} and adds the selection key to this channel's key set.
     *
     * @param selector
     *            the selector with which to register this channel.
     * @param interestSet
     *            this channel's {@link SelectionKey interest set}.
     * @param attachment
     *            the object to attach, can be {@code null}.
     * @return the selection key for this registration.
     * @throws CancelledKeyException
     *             if this channel is registered but its key has been canceled.
     * @throws ClosedChannelException
     *             if this channel is closed.
     * @throws IllegalArgumentException
     *             if {@code interestSet} is not supported by this channel.
     * @throws IllegalBlockingModeException
     *             if this channel is in blocking mode.
     * @throws IllegalSelectorException
     *             if this channel does not have the same provider as the given
     *             selector.
     */
@Override
public final SelectionKey register(Selector selector, int interestSet, Object attachment) throws ClosedChannelException {
    if (!isOpen()) {
        throw new ClosedChannelException();
    }
    if (!((interestSet & ~validOps()) == 0)) {
        throw new IllegalArgumentException();
    }
    synchronized (blockingLock) {
        if (isBlocking) {
            throw new IllegalBlockingModeException();
        }
        if (!selector.isOpen()) {
            if (interestSet == 0) {
                // throw ISE exactly to keep consistency
                throw new IllegalSelectorException();
            }
            // throw NPE exactly to keep consistency
            throw new NullPointerException();
        }
        SelectionKey key = keyFor(selector);
        if (key == null) {
            key = ((AbstractSelector) selector).register(this, interestSet, attachment);
            keyList.add(key);
        } else {
            if (!key.isValid()) {
                throw new CancelledKeyException();
            }
            key.interestOps(interestSet);
            key.attach(attachment);
        }
        return key;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) SelectionKey(java.nio.channels.SelectionKey) CancelledKeyException(java.nio.channels.CancelledKeyException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) IllegalSelectorException(java.nio.channels.IllegalSelectorException)

Example 19 with CancelledKeyException

use of java.nio.channels.CancelledKeyException in project jdk8u_jdk by JetBrains.

the class WindowsSelectorImpl method putEventOps.

public void putEventOps(SelectionKeyImpl sk, int ops) {
    synchronized (closeLock) {
        if (pollWrapper == null)
            throw new ClosedSelectorException();
        // make sure this sk has not been removed yet
        int index = sk.getIndex();
        if (index == -1)
            throw new CancelledKeyException();
        pollWrapper.putEventOps(index, ops);
    }
}
Also used : CancelledKeyException(java.nio.channels.CancelledKeyException) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 20 with CancelledKeyException

use of java.nio.channels.CancelledKeyException in project cloudstack by apache.

the class NioConnection method processTodos.

protected void processTodos() {
    List<ChangeRequest> todos;
    if (_todos.size() == 0) {
        // Nothing to do.
        return;
    }
    synchronized (this) {
        todos = _todos;
        _todos = new ArrayList<ChangeRequest>();
    }
    if (s_logger.isTraceEnabled()) {
        s_logger.trace("Todos Processing: " + todos.size());
    }
    SelectionKey key;
    for (final ChangeRequest todo : todos) {
        switch(todo.type) {
            case ChangeRequest.CHANGEOPS:
                try {
                    key = (SelectionKey) todo.key;
                    if (key != null && key.isValid()) {
                        if (todo.att != null) {
                            key.attach(todo.att);
                            final Link link = (Link) todo.att;
                            link.setKey(key);
                        }
                        key.interestOps(todo.ops);
                    }
                } catch (final CancelledKeyException e) {
                    s_logger.debug("key has been cancelled");
                }
                break;
            case ChangeRequest.REGISTER:
                try {
                    key = ((SocketChannel) todo.key).register(_selector, todo.ops, todo.att);
                    if (todo.att != null) {
                        final Link link = (Link) todo.att;
                        link.setKey(key);
                    }
                } catch (final ClosedChannelException e) {
                    s_logger.warn("Couldn't register socket: " + todo.key);
                    try {
                        ((SocketChannel) todo.key).close();
                    } catch (final IOException ignore) {
                        s_logger.info("[ignored] socket channel");
                    } finally {
                        final Link link = (Link) todo.att;
                        link.terminated();
                    }
                }
                break;
            case ChangeRequest.CLOSE:
                if (s_logger.isTraceEnabled()) {
                    s_logger.trace("Trying to close " + todo.key);
                }
                key = (SelectionKey) todo.key;
                closeConnection(key);
                if (key != null) {
                    final Link link = (Link) key.attachment();
                    if (link != null) {
                        link.terminated();
                    }
                }
                break;
            default:
                s_logger.warn("Shouldn't be here");
                throw new RuntimeException("Shouldn't be here");
        }
    }
    s_logger.trace("Todos Done processing");
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ClosedChannelException(java.nio.channels.ClosedChannelException) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException)

Aggregations

CancelledKeyException (java.nio.channels.CancelledKeyException)22 SelectionKey (java.nio.channels.SelectionKey)14 IOException (java.io.IOException)12 ClosedChannelException (java.nio.channels.ClosedChannelException)11 ClosedSelectorException (java.nio.channels.ClosedSelectorException)3 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)3 IllegalSelectorException (java.nio.channels.IllegalSelectorException)3 Selector (java.nio.channels.Selector)3 ByteBuffer (java.nio.ByteBuffer)2 SelectableChannel (java.nio.channels.SelectableChannel)2 ServerSocketChannel (java.nio.channels.ServerSocketChannel)2 SocketChannel (java.nio.channels.SocketChannel)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 ObjectReader (org.apache.catalina.tribes.io.ObjectReader)2 WebSocketImpl (org.java_websocket.WebSocketImpl)2 ListenCallback (com.koushikdutta.async.callback.ListenCallback)1 EventLoop (io.netty.channel.EventLoop)1