use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.
the class MySQLChannel method sendAutocommit.
/**
* 发送事务递交模式设置
*/
private void sendAutocommit(boolean autocommit) throws IOException {
CommandPacket cmd = getAutocommitCommand(autocommit);
cmd.write(out);
out.flush();
BinaryPacket bin = receive();
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
this.autocommit = autocommit;
break;
case ErrorPacket.FIELD_COUNT:
ErrorPacket err = new ErrorPacket();
err.read(bin);
throw new ErrorPacketException(new String(err.message, charset));
default:
throw new UnknownPacketException(bin.toString());
}
}
use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.
the class MySQLChannel method sendCharset.
/**
* 发送字符集设置
*/
private void sendCharset(int ci) throws IOException {
// 发送命令: 直接写入到out中即可
CommandPacket cmd = getCharsetCommand(ci);
cmd.write(out);
out.flush();
BinaryPacket bin = receive();
switch(bin.data[0]) {
case OkPacket.FIELD_COUNT:
this.charsetIndex = ci;
this.charset = CharsetUtil.getCharset(ci);
this.dbCharset = CharsetUtil.getCharset(ci);
break;
case ErrorPacket.FIELD_COUNT:
ErrorPacket err = new ErrorPacket();
err.read(bin);
throw new ErrorPacketException(new String(err.message, charset));
default:
throw new UnknownPacketException(bin.toString());
}
}
use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.
the class MySQLConnectionAuthenticator method handle.
@Override
public void handle(byte[] data) {
try {
HandshakePacket packet = source.getHandshake();
if (packet == null) {
// 设置握手数据包
packet = new HandshakePacket();
packet.read(data);
source.setHandshake(packet);
source.setThreadId(packet.threadId);
// 设置字符集编码
int charsetIndex = (packet.serverCharsetIndex & 0xff);
String charset = CharsetUtil.getDbCharset(charsetIndex);
if (charset != null) {
source.setCharsetIndex(charsetIndex);
source.setCharset(CharsetUtil.getCharset(charsetIndex));
source.setDbCharset(charset);
} else {
throw new RuntimeException("Unknown charsetIndex:" + charsetIndex);
}
// 发送认证数据包
source.authenticate();
} else {
// 处理认证结果
switch(data[4]) {
case OkPacket.FIELD_COUNT:
source.setHandler(new MySQLConnectionHandler(source));
source.setAuthenticated(true);
if (listener != null) {
listener.connectionAcquired(source);
}
break;
case ErrorPacket.FIELD_COUNT:
ErrorPacket err = new ErrorPacket();
err.read(data);
throw new RuntimeException(new String(err.message));
case EOFPacket.FIELD_COUNT:
auth323(data[3]);
break;
default:
throw new RuntimeException("Unknown Packet!");
}
}
} catch (RuntimeException e) {
if (listener != null) {
listener.connectionError(e, source);
}
throw e;
}
}
use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.
the class PacketUtil method getShutdown.
public static final ErrorPacket getShutdown() {
ErrorPacket error = new ErrorPacket();
error.packetId = 1;
error.errno = ErrorCode.ER_SERVER_SHUTDOWN;
error.message = "The server has been shutdown".getBytes();
return error;
}
use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.
the class CobarDetectorAuthenticator method handle.
@Override
public void handle(byte[] data) {
CobarDetector source = this.source;
HandshakePacket hsp = source.getHandshake();
if (hsp == null) {
// 设置握手数据包
hsp = new HandshakePacket();
hsp.read(data);
source.setHandshake(hsp);
// 设置字符集编码
int charsetIndex = (hsp.serverCharsetIndex & 0xff);
String charset = CharsetUtil.getDbCharset(charsetIndex);
if (charset != null) {
source.setCharsetIndex(charsetIndex);
} else {
throw new RuntimeException("Unknown charsetIndex:" + charsetIndex);
}
// 发送认证数据包
source.authenticate();
} else {
// 处理认证结果
switch(data[4]) {
case OkPacket.FIELD_COUNT:
source.setHandler(new CobarDetectorHandler(source));
source.setAuthenticated(true);
// 认证成功后,发起心跳。
source.heartbeat();
break;
case ErrorPacket.FIELD_COUNT:
ErrorPacket err = new ErrorPacket();
err.read(data);
throw new RuntimeException(new String(err.message));
default:
throw new RuntimeException("Unknown packet");
}
}
}
Aggregations