*
*/
@Override
protected void doParse() throws Exception {
//
final XInputStream is = this.transport.getInputStream();
final Context context = new Context(this.binlogFileName);
while(isRunning()) {
try {
// Parse packet
final int packetLength = is.readInt(3);
final int packetSequence = is.readInt(1);
is.setReadLimit(packetLength); // Ensure the packet boundary
//
final int packetMarker = is.readInt(1);
if(packetMarker != OKPacket.PACKET_MARKER) { // 0x00
if((byte)packetMarker == ErrorPacket.PACKET_MARKER) {
final ErrorPacket packet = ErrorPacket.valueOf(packetLength, packetSequence, packetMarker, is);
throw new NestableRuntimeException(packet.toString());
} else if((byte)packetMarker == EOFPacket.PACKET_MARKER) {
final EOFPacket packet = EOFPacket.valueOf(packetLength, packetSequence, packetMarker, is);
throw new NestableRuntimeException(packet.toString());
} else {
throw new NestableRuntimeException("assertion failed, invalid packet marker: " + packetMarker);
}
}
// Parse the event header
final BinlogEventV4HeaderImpl header = new BinlogEventV4HeaderImpl();
header.setTimestamp(is.readLong(4) * 1000L);
header.setEventType(is.readInt(1));
header.setServerId(is.readLong(4));
header.setEventLength(is.readInt(4));
header.setNextPosition(is.readLong(4));
header.setFlags(is.readInt(2));
header.setTimestampOfReceipt(System.currentTimeMillis());
if(isVerbose() && LOGGER.isInfoEnabled()) {
LOGGER.info("received an event, sequence: {}, header: {}", packetSequence, header);
}
// Parse the event body
if(this.eventFilter != null && !this.eventFilter.accepts(header, context)) {
this.defaultParser.parse(is, header, context);
} else {
BinlogEventParser parser = getEventParser(header.getEventType());
if(parser == null) parser = this.defaultParser;
parser.parse(is, header, context);
}
// Ensure the packet boundary
if(is.available() != 0) {
throw new NestableRuntimeException("assertion failed, available: " + is.available() + ", event type: " + header.getEventType());
}
} finally {
is.setReadLimit(0);
}
}
}