下面列出了java.nio.channels.SocketChannel#keyFor() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* {@inheritDoc}
*/
@Override
protected ConnectionRequest getConnectionRequest(SocketChannel handle) {
SelectionKey key = handle.keyFor(selector);
if ((key == null) || (!key.isValid())) {
return null;
}
return (ConnectionRequest) key.attachment();
}
/**
* {@inheritDoc}
*/
@Override
protected void close(SocketChannel handle) throws Exception {
SelectionKey key = handle.keyFor(selector);
if (key != null) {
key.cancel();
}
handle.close();
}
/**
* {@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;
}
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();
}
synchronized void cancel(SocketChannel e) {
SelectionKey key = e.keyFor(selector);
if (key != null) {
key.cancel();
}
selector.wakeup();
}
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();
}
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;
}
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;
}
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;
}
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;
}
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();
}