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);
}
}
use of java.nio.channels.SelectionKey in project cobar by alibaba.
the class AbstractConnection method enableWrite.
/**
* 打开写事件
*/
private void enableWrite() {
final Lock lock = this.keyLock;
lock.lock();
try {
SelectionKey key = this.processKey;
key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
} finally {
lock.unlock();
}
processKey.selector().wakeup();
}
use of java.nio.channels.SelectionKey in project cobar by alibaba.
the class AbstractConnection method disableRead.
/**
* 关闭读事件
*/
public void disableRead() {
final Lock lock = this.keyLock;
lock.lock();
try {
SelectionKey key = this.processKey;
key.interestOps(key.interestOps() & OP_NOT_READ);
} finally {
lock.unlock();
}
}
use of java.nio.channels.SelectionKey in project http-kit by http-kit.
the class HttpClient method run.
public void run() {
while (running) {
try {
Request first = requests.peek();
long timeout = 2000;
if (first != null) {
timeout = Math.max(first.toTimeout(currentTimeMillis()), 200L);
}
int select = selector.select(timeout);
long now = currentTimeMillis();
if (select > 0) {
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> ite = selectedKeys.iterator();
while (ite.hasNext()) {
SelectionKey key = ite.next();
if (!key.isValid()) {
continue;
}
if (key.isConnectable()) {
finishConnect(key, now);
} else if (key.isReadable()) {
doRead(key, now);
} else if (key.isWritable()) {
doWrite(key);
}
ite.remove();
}
}
clearTimeout(now);
processPending();
} catch (Throwable e) {
// catch any exception (including OOM), print it: do not exits the loop
HttpUtils.printError("select exception, should not happen", e);
}
}
}
use of java.nio.channels.SelectionKey in project http-kit by http-kit.
the class NBlockingSSL method main.
public static void main(String[] args) throws IOException {
engine = CLIENT_CONTEXT.createSSLEngine();
engine.setUseClientMode(true);
selector = Selector.open();
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
key = socketChannel.register(selector, SelectionKey.OP_CONNECT);
socketChannel.connect(new InetSocketAddress(HOST, 443));
int i = 0;
// peerNetData.clear();
while (true) {
int select = selector.select(1000);
if (select > 0) {
Set<SelectionKey> selectionKeys = selector.selectedKeys();
final Iterator<SelectionKey> ite = selectionKeys.iterator();
while (ite.hasNext()) {
final SelectionKey key = ite.next();
if (key.isConnectable()) {
if (socketChannel.finishConnect()) {
key.interestOps(SelectionKey.OP_WRITE);
engine.beginHandshake();
}
} else if (key.isReadable()) {
if (!isHandshakeDone) {
doHandshake();
} else {
int read = socketChannel.read(peerNetData);
if (read > 0) {
peerNetData.flip();
SSLEngineResult res = engine.unwrap(peerNetData, peerAppData);
if (res.getStatus() == SSLEngineResult.Status.OK) {
peerAppData.flip();
byte[] data = new byte[peerAppData.remaining()];
peerAppData.get(data);
i++;
logger.info("get data length: " + new String(data).length());
key.interestOps(SelectionKey.OP_WRITE);
peerAppData.clear();
if (i > 5) {
return;
}
// peerNetData.clear();
}
logger.info("read unwrap, " + res);
peerNetData.compact();
}
}
} else if (key.isWritable()) {
if (!isHandshakeDone) {
doHandshake();
} else {
myNetData.clear();
ByteBuffer buffer = ByteBuffer.wrap(("GET / HTTP/1.1\r\nHost: " + HOST + "\r\n\r\n").getBytes());
SSLEngineResult res = engine.wrap(buffer, myNetData);
if (res.getStatus() == Status.OK) {
myNetData.flip();
socketChannel.write(myNetData);
if (!myNetData.hasRemaining()) {
key.interestOps(SelectionKey.OP_READ);
}
}
}
}
ite.remove();
}
} else {
logger.info("waiting");
}
}
}
Aggregations