/** * 判断返回的数据是否是当前命令的结束包。 当前全部连接都全部返回以后则表示当前命令完全结束。 * * @param conn * @param buffer * @return */ protected CommandStatus checkResponseCompleted(Connection conn, byte[] buffer) { boolean isCompleted = false; ConnectionStatuts connStatus = connStatusMap.get(conn); if (connStatus == null) { CommandMessageHandler.logger.error( "connection Status not Found, byffer=" + StringUtil.dumpAsHex(buffer, buffer.length)); } try { connStatus.buffers.add(buffer); isCompleted = connStatus.isCompleted(buffer); /** * 如果是多个连接的,需要将数据缓存起来,等待命令全部完成以后,将数据进行组装,然后发送到客户端 {@link * #CommandMessageHandler.mergeMessageToClient} */ if (isCompleted) { // set last insert id to client connection; if (conn != source) { if (connStatus.packetIndex == 0 && MysqlPacketBuffer.isOkPacket(buffer)) { if (statment instanceof InsertStatement && currentCommand.isMain()) { OkPacket packet = new OkPacket(); packet.init(buffer, conn); if (packet.insertId > 0) { source.setLastInsertId(packet.insertId); } } } } boolean isAllCompleted = currentCommand.getCompletedCount().incrementAndGet() == connStatusMap.size(); if (isAllCompleted) { connStatus.isMerged = true; } if (isAllCompleted) { if (CommandMessageHandler.logger.isDebugEnabled()) { Packet packet = null; if (MysqlPacketBuffer.isErrorPacket(buffer)) { packet = new ErrorPacket(); } else if (MysqlPacketBuffer.isEofPacket(buffer)) { packet = new EOFPacket(); } else if (MysqlPacketBuffer.isOkPacket(buffer)) { packet = new OkPacket(); } packet.init(buffer, conn); CommandMessageHandler.logger.debug("returned Packet:" + packet); } return CommandStatus.AllCompleted; } else { return CommandStatus.ConnectionCompleted; } } else { return CommandStatus.ConnectionNotComplete; } } finally { connStatus.packetIndex++; } }
/** * 尝试下一个命令,如果返回false,表示队列中没有命令了。 * * @return */ boolean tryNextCommandTuple() { if (sessionInitQueryQueue == null) { return false; } else { if (sessionInitQueryQueue.size() > 0) { currentCommand = sessionInitQueryQueue.get(0); if (CommandMessageHandler.logger.isDebugEnabled()) { QueryCommandPacket command = new QueryCommandPacket(); command.init(currentCommand.getBuffer(), source); CommandMessageHandler.logger.debug(command); } return true; } return false; } }