我有一个传递字节数组的函数。这些字节可能包含 xml 消息的一部分、一个整体或一个整体和一部分。我已经成功地使用状态机尝试解析消息,直到今天我们意识到如果第二个来源正在向我们发送数据,我可以得到消息的前半部分,并且在第二个来源的另一条消息开始之后立即. 我希望能够通过丢弃出现故障的消息来纠正这一点。我认为我不应该存储它并保留它直到完成。
这是我的状态机:
for (int i = 0; i < bytes.length; i++)
{
byte data = bytes[i];
//based on the read state, perform an action. This is attempting to
//determine if we are reading the beginning of a message, or the end of the message.
if (bufferIndex == BmsConstants.MAX_DRAWING_LENGTH)
{
resetState();
}
switch (readState)
{
case SYNC:
//we've read: <
if (data == '<')
{
readState = DrawingDeviceParserState.START_LT;
buff[bufferIndex++] = (char) data;
} else
{
resetState();
}
break;
case START_LT:
//we've read: <?
if (data == '?')
{
readState = DrawingDeviceParserState.START_QM;
buff[bufferIndex++] = (char) data;
} else
{
resetState();
}
break;
case START_QM:
//we've read: <?x
if (data == 'x')
{
readState = DrawingDeviceParserState.START_X;
buff[bufferIndex++] = (char) data;
} else
{
resetState();
}
break;
case START_X:
//we've read: <, now just read until we get to the </event>
if (data == '<')
{
readState = DrawingDeviceParserState.END_LT;
buff[bufferIndex++] = (char) data;
}
else
{
buff[bufferIndex++] = (char) data;
}
break;
case END_LT:
//we've read: </
if (data == '/')
{
readState = DrawingDeviceParserState.END_SLASH;
buff[bufferIndex++] = (char) data;
}
else
{
readState = DrawingDeviceParserState.START_X;
buff[bufferIndex++] = (char) data;
}
break;
case END_SLASH:
//we've read: </e
if (data == 'e')
{
readState = DrawingDeviceParserState.END_E;
buff[bufferIndex++] = (char) data;
}
else
{
readState = DrawingDeviceParserState.START_X;
buff[bufferIndex++] = (char) data;
}
break;
case END_E:
//we've read: </ev
if (data == 'v')
{
readState = DrawingDeviceParserState.END_V;
buff[bufferIndex++] = (char) data;
}
else
{
readState = DrawingDeviceParserState.START_X;
buff[bufferIndex++] = (char) data;
}
break;
case END_V:
//we've read: </eve
if (data == 'e')
{
readState = DrawingDeviceParserState.END_E1;
buff[bufferIndex++] = (char) data;
}
else
{
readState = DrawingDeviceParserState.START_X;
buff[bufferIndex++] = (char) data;
}
break;
case END_E1:
//we've read: </even
if (data == 'n')
{
readState = DrawingDeviceParserState.END_N;
buff[bufferIndex++] = (char) data;
}
else
{
readState = DrawingDeviceParserState.START_X;
buff[bufferIndex++] = (char) data;
}
break;
case END_N:
//we've read: </event
if (data == 't')
{
readState = DrawingDeviceParserState.END_T;
buff[bufferIndex++] = (char) data;
}
else
{
readState = DrawingDeviceParserState.START_X;
buff[bufferIndex++] = (char) data;
}
break;
case END_T:
//we've read: </event>
//End of the message, now we can parse it.
if (data == '>')
{
//readState = DrawingDeviceParserState.END_GT;
buff[bufferIndex++] = (char) data;
parseXml(new String(buff, 0, bufferIndex));
resetState();
}
else
{
readState = DrawingDeviceParserState.START_X;
buff[bufferIndex++] = (char) data;
}
break;
}
}
弱点是此状态机位于案例 START_X 内。我将继续阅读,直到找到标签,在 2 条消息乱七八糟地塞在一起的情况下,该标签将位于第二条消息的末尾。我希望有人能做的是要么告诉我状态机不是我应该做的,要么修改我的代码/逻辑以更好地处理这种边缘情况。