fix(net): 优化数据包解析及日志记录逻辑

- 在 NewCurrentCollectorParser 中增加正则校验,提高数据包匹配准确性
- 修复 Packet 类中 length 属性的格式问题,确保正确访问
- 调整 PacketParserFactory,修复可能的空数据问题,增强健壮性
- TcpServer 中新增 logMessage 方法拆分日志记录逻辑
- 根据包匹配结果分别记录详细或简化日志,增强调试信息
- 修复 TcpServer 中不匹配数据包时的响应,避免无效处理
This commit is contained in:
zimoyin
2026-03-08 23:33:54 +08:00
parent 966ee0185b
commit bcb95d7772
4 changed files with 56 additions and 23 deletions
+39 -14
View File
@@ -82,21 +82,14 @@ class TcpServer
self::$connections[$ip] = $connection;
$packet = PacketParserFactory::new($data);
$context = new PacketContext(self::getConnections(), $connection, $packet);
// 格式化输出报文信息文本框
Logger::debug(Logger::generateTextBox([
"---------------------------- PACKET --------------------------------",
"IP Address : {$ip}",
"Packet Length: {$packet->length}",
"Hex Packet : {$packet->hexString}",
"Packet Type : {$packet->hexType->name}",
"---------------------------- DATA --------------------------------",
"Reader Info : {$packet->reader}",
"Card Info : {$packet->card}",
"Gateway Info : {$packet->gateway}",
]));
$this->logMessage($packet, $ip);
$result = FlowMain::getInstance()->main($context);
$connection->send($result->getFullVoice());
if ($packet->isMatched) {
$result = FlowMain::getInstance()->main($context);
$connection->send($result->getFullVoice());
} else {
$connection->send("Packet is not matched");
}
}
/**
@@ -107,4 +100,36 @@ class TcpServer
unset(self::$connections[$connection->getRemoteIp()]);
Logger::info("客户端已断开连接:{$connection->getRemoteIp()}");
}
/**
* @param \app\net\Packet $packet
* @param string $ip
* @return void
*/
public function logMessage(\app\net\Packet $packet, string $ip): void
{
if ($packet->isMatched) {
Logger::debug(Logger::generateTextBox([
"---------------------------- PACKET --------------------------------",
"IP Address : {$ip}",
"Packet Length: {$packet->length}",
"Hex Packet : {$packet->hexString}",
"Packet Type : {$packet->hexType->name}",
"---------------------------- DATA --------------------------------",
"Reader Info : {$packet->reader}",
"Card Info : {$packet->card}",
"Gateway Info : {$packet->gateway}",
]));
} else {
Logger::debug("Packet is not matched");
Logger::debug(Logger::generateTextBox([
"---------------------------- PACKET --------------------------------",
"IP Address : {$ip}",
"Packet Length: {$packet->length}",
"Packet Type : {$packet->hexType->name}",
"---------------------------- DATA --------------------------------",
"Hex Packet : {$packet->hexString}"
]));
}
}
}