@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.getCharset(charsetIndex); if (charset != null) { source.setCharsetIndex(charsetIndex); source.setCharset(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; } }
@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.getCharset(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"); } } }