Grizzly学习笔记(二)(二)

2014-11-24 02:29:11 · 作者: · 浏览: 1
// logger().log(Level.SEVERE, "onConnect", ex);
return;
}
// 注册Read事件
ioEvent.attachment().getSelectorHandler().register(
k, SelectionKey.OP_READ);

} finally {
if (waitLatch != null) {
waitLatch.countDown();
}
}
}

public void onRead(IOEvent ioEvent) {
ByteBuffer bb = ByteBuffer.allocate(100);
try {
tch.read(bb, false);
} catch (IOException e) {
e.printStackTrace();
}
bb.flip();
System.out.println(new String(bb.array(), 0, bb
.remaining()));

SelectionKey k = ioEvent.attachment().getSelectionKey();
// 重新注册read事件,原因参考TcpSelectorHandler的onReadInterest/onWriteInterest方法
ioEvent.attachment().getSelectionKey().interestOps(
k.interestOps() | SelectionKey.OP_READ);
}

public void onWrite(IOEvent ioEvent) {
}
});

System.out.println("current connection:" + tch.getUnderlyingChannel());

try {
waitLatch.await();
} catch (Exception e) {
// do nothing
}

ByteBuffer bb = ByteBuffer.wrap("xxxx".getBytes());
tch.write(bb, true);
}
}

本文出自 “天下无贼” 博客