第三篇中与JDBC握手,new了HandshakePacket对象,调用write方法,将报文发送给jdbc。所谓write方法就是最终调用AbstractConnection的write方法,将需要发送的数据写入writeQueue,再通过NIOSocketWR写出去。
@Override
public final void write(ByteBuffer buffer) {
if (isSupportCompress()) {
ByteBuffer newBuffer = CompressUtil.compressMysqlPacket(buffer, this, compressUnfinishedDataQueue);
writeQueue.offer(newBuffer);
} else {
writeQueue.offer(buffer);
}
// if ansyn write finishe event got lock before me ,then writing
// flag is set false but not start a write request
// so we check again
try {
this.socketWR.doNextWriteCheck();
} catch (Exception e) {
LOGGER.warn("write err:", e);
this.close("write err:" + e);
}
}
而asynRead()方法,依然是调用NIOSocketWR的asynRead方法,读取完毕后,通过AbstractConnection的onReadData方法通知connection,onReadData方法内调用handle方法对数据进行分发处理。
@Override
public void handle(byte[] data) {
if (isSupportCompress()) {
List<byte[]> packs = CompressUtil.decompressMysqlPacket(data, decompressUnfinishedDataQueue);
for (byte[] pack : packs) {
if (pack.length != 0) {
handler.handle(pack);
}
}
} else {
handler.handle(data);
}
}