java.nio.channels.SocketChannel#keyFor()源码实例Demo

下面列出了java.nio.channels.SocketChannel#keyFor() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: aion   文件: P2pMgr.java
@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);
        }
    }
}
 
源代码2 项目: Blink   文件: HandleSelector.java
/**
 * 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();
        }

    }
}
 
源代码3 项目: Blink   文件: SelectorFactory.java
/**
 * 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();
        }

    }
}
 
源代码4 项目: neoscada   文件: NioSocketConnector.java
/**
 * {@inheritDoc}
 */
@Override
protected ConnectionRequest getConnectionRequest(SocketChannel handle) {
    SelectionKey key = handle.keyFor(selector);

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

    return (ConnectionRequest) key.attachment();
}
 
源代码5 项目: neoscada   文件: NioSocketConnector.java
/**
 * {@inheritDoc}
 */
@Override
protected void close(SocketChannel handle) throws Exception {
    SelectionKey key = handle.keyFor(selector);

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

    handle.close();
}
 
源代码6 项目: neoscada   文件: NioSocketConnector.java
/**
 * {@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;
}
 
源代码7 项目: Tomcat7.0.67   文件: NioBlockingSelector.java
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();
}
 
源代码8 项目: openjdk-jdk9   文件: HttpClientImpl.java
synchronized void cancel(SocketChannel e) {
    SelectionKey key = e.keyFor(selector);
    if (key != null) {
        key.cancel();
    }
    selector.wakeup();
}
 
源代码9 项目: tomcatsrc   文件: NioBlockingSelector.java
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();
}
 
源代码10 项目: Blink   文件: HandleSelector.java
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;
}
 
源代码11 项目: Blink   文件: HandleSelector.java
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;
}
 
源代码12 项目: Blink   文件: SelectorFactory.java
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;
}
 
源代码13 项目: Blink   文件: SelectorFactory.java
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;
}
 
源代码14 项目: craft-atom   文件: NioTcpConnector.java
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();
}