use of com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket in project canal by alibaba.
the class MysqlConnector method negotiate.
private void negotiate(SocketChannel channel) throws IOException {
HeaderPacket header = PacketManager.readHeader(channel, 4);
byte[] body = PacketManager.readBytes(channel, header.getPacketBodyLength());
if (body[0] < 0) {
// check field_count
if (body[0] == -1) {
ErrorPacket error = new ErrorPacket();
error.fromBytes(body);
throw new IOException("handshake exception:\n" + error.toString());
} else if (body[0] == -2) {
throw new IOException("Unexpected EOF packet at handshake phase.");
} else {
throw new IOException("unpexpected packet with field_count=" + body[0]);
}
}
HandshakeInitializationPacket handshakePacket = new HandshakeInitializationPacket();
handshakePacket.fromBytes(body);
// 记录一下connection
connectionId = handshakePacket.threadId;
logger.info("handshake initialization packet received, prepare the client authentication packet to send");
ClientAuthenticationPacket clientAuth = new ClientAuthenticationPacket();
clientAuth.setCharsetNumber(charsetNumber);
clientAuth.setUsername(username);
clientAuth.setPassword(password);
clientAuth.setServerCapabilities(handshakePacket.serverCapabilities);
clientAuth.setDatabaseName(defaultSchema);
clientAuth.setScrumbleBuff(joinAndCreateScrumbleBuff(handshakePacket));
byte[] clientAuthPkgBody = clientAuth.toBytes();
HeaderPacket h = new HeaderPacket();
h.setPacketBodyLength(clientAuthPkgBody.length);
h.setPacketSequenceNumber((byte) (header.getPacketSequenceNumber() + 1));
PacketManager.write(channel, new ByteBuffer[] { ByteBuffer.wrap(h.toBytes()), ByteBuffer.wrap(clientAuthPkgBody) });
logger.info("client authentication packet is sent out.");
// check auth result
header = null;
header = PacketManager.readHeader(channel, 4);
body = null;
body = PacketManager.readBytes(channel, header.getPacketBodyLength());
assert body != null;
if (body[0] < 0) {
if (body[0] == -1) {
ErrorPacket err = new ErrorPacket();
err.fromBytes(body);
throw new IOException("Error When doing Client Authentication:" + err.toString());
} else if (body[0] == -2) {
auth323(channel, header.getPacketSequenceNumber(), handshakePacket.seed);
// throw new
// IOException("Unexpected EOF packet at Client Authentication.");
} else {
throw new IOException("unpexpected packet with field_count=" + body[0]);
}
}
}
use of com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket in project canal by alibaba.
the class ChannelBufferHelper method assembleHeaderPacket.
public final HeaderPacket assembleHeaderPacket(ChannelBuffer buffer) {
HeaderPacket header = new HeaderPacket();
byte[] headerBytes = new byte[MSC.HEADER_PACKET_LENGTH];
buffer.readBytes(headerBytes);
header.fromBytes(headerBytes);
return header;
}
use of com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket in project canal by alibaba.
the class ChannelBufferHelper method createHeaderWithPacketNumberPlusOne.
public final ChannelBuffer createHeaderWithPacketNumberPlusOne(int bodyLength, byte packetNumber) {
HeaderPacket header = new HeaderPacket();
header.setPacketBodyLength(bodyLength);
header.setPacketSequenceNumber((byte) (packetNumber + 1));
return ChannelBuffers.wrappedBuffer(header.toBytes());
}
use of com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket in project canal by alibaba.
the class PacketManager method write.
public static void write(SocketChannel ch, byte[] body, byte packetSeqNumber) throws IOException {
HeaderPacket header = new HeaderPacket();
header.setPacketBodyLength(body.length);
header.setPacketSequenceNumber(packetSeqNumber);
write(ch, new ByteBuffer[] { ByteBuffer.wrap(header.toBytes()), ByteBuffer.wrap(body) });
}
use of com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket in project canal by alibaba.
the class PacketManager method readHeader.
public static HeaderPacket readHeader(SocketChannel ch, int len) throws IOException {
HeaderPacket header = new HeaderPacket();
header.fromBytes(readBytesAsBuffer(ch, len).array());
return header;
}
Aggregations