Java Code Examples for java.nio.channels.SocketChannel#keyFor()

The following examples show how to use java.nio.channels.SocketChannel#keyFor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: P2pMgr.java    From aion with MIT License 6 votes vote down vote up
@Override
public void closeSocket(SocketChannel _sc, String _reason, Exception e) {
    if (p2pLOG.isDebugEnabled()) {
        if (e != null) {
            p2pLOG.debug("close-socket reason=" + _reason, e);
        } else {
            p2pLOG.debug("close-socket reason={}", _reason);
        }
    }

    if (_sc != null) {
        SelectionKey sk = _sc.keyFor(selector);
        if (sk != null) {
            sk.cancel();
            sk.attach(null);
        }

        try {
            _sc.close();
        } catch (IOException ex) {
            p2pLOG.info("close-socket-io-exception.", ex);
        }
    }
}
 
Example 2
Source File: HandleSelector.java    From Blink with Artistic License 2.0 6 votes vote down vote up
/**
 * Cancel a SocketChannel register
 *
 * @param channel SocketChannel
 */
public void unRegister(SocketChannel channel) {
    if (channel.isRegistered()) {
        SelectionKey key = channel.keyFor(mReadSelector);
        if (key != null) {
            key.cancel();
            mReadRegisterMap.remove(key);
            mReadSelector.wakeup();
        }

        key = channel.keyFor(mWriteSelector);
        if (key != null) {
            key.cancel();
            mWriteRegisterMap.remove(key);
            mWriteSelector.wakeup();
        }

    }
}
 
Example 3
Source File: SelectorFactory.java    From Blink with Artistic License 2.0 6 votes vote down vote up
/**
 * Cancel a SocketChannel register
 *
 * @param channel SocketChannel
 */
public void unRegister(SocketChannel channel) {
    if (channel.isRegistered()) {
        SelectionKey key = channel.keyFor(mReadSelector);
        if (key != null) {
            key.cancel();
            mReadRegisterMap.remove(key);
            mReadSelector.wakeup();
        }

        key = channel.keyFor(mWriteSelector);
        if (key != null) {
            key.cancel();
            mWriteRegisterMap.remove(key);
            mWriteSelector.wakeup();
        }

    }
}
 
Example 4
Source File: NioSocketConnector.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected ConnectionRequest getConnectionRequest(SocketChannel handle) {
    SelectionKey key = handle.keyFor(selector);

    if ((key == null) || (!key.isValid())) {
        return null;
    }

    return (ConnectionRequest) key.attachment();
}
 
Example 5
Source File: NioSocketConnector.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void close(SocketChannel handle) throws Exception {
    SelectionKey key = handle.keyFor(selector);

    if (key != null) {
        key.cancel();
    }

    handle.close();
}
 
Example 6
Source File: NioSocketConnector.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected boolean finishConnect(SocketChannel handle) throws Exception {
    if (handle.finishConnect()) {
        SelectionKey key = handle.keyFor(selector);

        if (key != null) {
            key.cancel();
        }

        return true;
    }

    return false;
}
 
Example 7
Source File: NioBlockingSelector.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public void remove(final KeyAttachment key, final int ops) {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            if ( key == null ) return;
            NioChannel nch = key.getChannel();
            if ( nch == null ) return;
            SocketChannel ch = nch.getIOChannel();
            if ( ch == null ) return;
            SelectionKey sk = ch.keyFor(selector);
            try {
                if (sk == null) {
                    if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch());
                    if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch());
                } else {
                    if (sk.isValid()) {
                        sk.interestOps(sk.interestOps() & (~ops));
                        if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch());
                        if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch());
                        if (sk.interestOps()==0) {
                            sk.cancel();
                            sk.attach(null);
                        }
                    }else {
                        sk.cancel();
                        sk.attach(null);
                    }
                }
            }catch (CancelledKeyException cx) {
                if (sk!=null) {
                    sk.cancel();
                    sk.attach(null);
                }
            }
        }
    };
    events.offer(r);
    wakeup();
}
 
Example 8
Source File: HttpClientImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
synchronized void cancel(SocketChannel e) {
    SelectionKey key = e.keyFor(selector);
    if (key != null) {
        key.cancel();
    }
    selector.wakeup();
}
 
Example 9
Source File: NioBlockingSelector.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void remove(final KeyAttachment key, final int ops) {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            if ( key == null ) return;
            NioChannel nch = key.getChannel();
            if ( nch == null ) return;
            SocketChannel ch = nch.getIOChannel();
            if ( ch == null ) return;
            SelectionKey sk = ch.keyFor(selector);
            try {
                if (sk == null) {
                    if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch());
                    if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch());
                } else {
                    if (sk.isValid()) {
                        sk.interestOps(sk.interestOps() & (~ops));
                        if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch());
                        if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch());
                        if (sk.interestOps()==0) {
                            sk.cancel();
                            sk.attach(null);
                        }
                    }else {
                        sk.cancel();
                        sk.attach(null);
                    }
                }
            }catch (CancelledKeyException cx) {
                if (sk!=null) {
                    sk.cancel();
                    sk.attach(null);
                }
            }
        }
    };
    events.offer(r);
    wakeup();
}
 
Example 10
Source File: HandleSelector.java    From Blink with Artistic License 2.0 5 votes vote down vote up
public SelectionKey registerSend(SocketChannel channel, HandleCallback callback) throws ClosedChannelException {
    SelectionKey key = null;

    synchronized (mRegWrite) {
        mRegWrite.set(true);

        mWriteSelector.wakeup();


        if (channel.isRegistered()) {
            key = channel.keyFor(mWriteSelector);
            if (key != null)
                key.interestOps(key.readyOps() | SelectionKey.OP_WRITE);
        }

        if (key == null) {
            key = channel.register(mWriteSelector, SelectionKey.OP_WRITE);
            mWriteRegisterMap.put(key, callback);
        }

        mRegWrite.set(false);
        try {
            mRegWrite.notify();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return key;
}
 
Example 11
Source File: HandleSelector.java    From Blink with Artistic License 2.0 5 votes vote down vote up
public SelectionKey registerReceive(SocketChannel channel, HandleCallback callback) throws ClosedChannelException {
    SelectionKey key = null;

    synchronized (mRegRead) {
        mRegRead.set(true);

        mReadSelector.wakeup();


        if (channel.isRegistered()) {
            key = channel.keyFor(mReadSelector);
            if (key != null)
                key.interestOps(key.readyOps() | SelectionKey.OP_READ);
        }

        if (key == null) {
            key = channel.register(mReadSelector, SelectionKey.OP_READ);
            mReadRegisterMap.put(key, callback);
        }

        mRegRead.set(false);

        try {
            mRegRead.notify();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return key;
}
 
Example 12
Source File: SelectorFactory.java    From Blink with Artistic License 2.0 5 votes vote down vote up
public SelectionKey registerSend(SocketChannel channel, HandleCallback callback) throws ClosedChannelException {
    SelectionKey key = null;

    synchronized (mRegWrite) {
        mRegWrite.set(true);

        mWriteSelector.wakeup();


        if (channel.isRegistered()) {
            key = channel.keyFor(mWriteSelector);
            if (key != null)
                key.interestOps(key.readyOps() | SelectionKey.OP_WRITE);
        }

        if (key == null) {
            key = channel.register(mWriteSelector, SelectionKey.OP_WRITE);
            mWriteRegisterMap.put(key, callback);
        }

        mRegWrite.set(false);
        try {
            mRegWrite.notify();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return key;
}
 
Example 13
Source File: SelectorFactory.java    From Blink with Artistic License 2.0 5 votes vote down vote up
public SelectionKey registerReceive(SocketChannel channel, HandleCallback callback) throws ClosedChannelException {
    SelectionKey key = null;

    synchronized (mRegRead) {
        mRegRead.set(true);

        mReadSelector.wakeup();


        if (channel.isRegistered()) {
            key = channel.keyFor(mReadSelector);
            if (key != null)
                key.interestOps(key.readyOps() | SelectionKey.OP_READ);
        }

        if (key == null) {
            key = channel.register(mReadSelector, SelectionKey.OP_READ);
            mReadRegisterMap.put(key, callback);
        }

        mRegRead.set(false);

        try {
            mRegRead.notify();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return key;
}
 
Example 14
Source File: NioTcpConnector.java    From craft-atom with MIT License 5 votes vote down vote up
private void close(SocketChannel sc) throws IOException {
	LOG.debug("[CRAFT-ATOM-NIO] Close socket channel={}", sc);

	SelectionKey key = sc.keyFor(selector);

	if (key != null) {
		key.cancel();
	}

	sc.close();
}