From bcb95d777216abeffa7e1aeca9b875830937f3c8 Mon Sep 17 00:00:00 2001 From: zimoyin <2556608754@qq.com> Date: Sun, 8 Mar 2026 23:33:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(net):=20=E4=BC=98=E5=8C=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=8C=85=E8=A7=A3=E6=9E=90=E5=8F=8A=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 NewCurrentCollectorParser 中增加正则校验,提高数据包匹配准确性 - 修复 Packet 类中 length 属性的格式问题,确保正确访问 - 调整 PacketParserFactory,修复可能的空数据问题,增强健壮性 - TcpServer 中新增 logMessage 方法拆分日志记录逻辑 - 根据包匹配结果分别记录详细或简化日志,增强调试信息 - 修复 TcpServer 中不匹配数据包时的响应,避免无效处理 --- app/net/Packet.php | 2 +- app/net/parsers/NewCurrentCollectorParser.php | 18 ++++--- app/net/parsers/PacketParserFactory.php | 6 ++- app/process/TcpServer.php | 53 ++++++++++++++----- 4 files changed, 56 insertions(+), 23 deletions(-) diff --git a/app/net/Packet.php b/app/net/Packet.php index 324f6cb..661838d 100644 --- a/app/net/Packet.php +++ b/app/net/Packet.php @@ -177,7 +177,7 @@ class Packet get => $this->rawText; } - public int $length{ + public int $length { get => strlen($this->rawBytes); } diff --git a/app/net/parsers/NewCurrentCollectorParser.php b/app/net/parsers/NewCurrentCollectorParser.php index dba7e0e..7442c07 100644 --- a/app/net/parsers/NewCurrentCollectorParser.php +++ b/app/net/parsers/NewCurrentCollectorParser.php @@ -15,11 +15,17 @@ class NewCurrentCollectorParser implements PacketParserInterface public function parse(string $hexString): array { - return [ - 'hexType' => 4, - 'isMatched' => true, - 'detectionDevice' => substr($hexString, 16, 2), - 'channel1' => substr($hexString, 24, 2), - ]; + $pattern = '/^BBBB([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{8})$/'; + + if (preg_match($pattern, $hexString, $matches)) { + return [ + 'hexType' => 4, + 'isMatched' => true, + 'detectionDevice' => substr($hexString, 16, 2), + 'channel1' => substr($hexString, 24, 2), + ]; + } + + return ['isMatched' => false]; } } \ No newline at end of file diff --git a/app/net/parsers/PacketParserFactory.php b/app/net/parsers/PacketParserFactory.php index 6113f15..d9e2ee5 100644 --- a/app/net/parsers/PacketParserFactory.php +++ b/app/net/parsers/PacketParserFactory.php @@ -30,7 +30,9 @@ class PacketParserFactory /** * 防止克隆(保障单例唯一性) */ - private function __clone() {} + private function __clone() + { + } /** * 防止反序列化(保障单例唯一性) @@ -91,7 +93,7 @@ class PacketParserFactory } } - return new Packet($rawBytes, $parsedData); + return new Packet($rawBytes, $parsedData ?? []); } /** diff --git a/app/process/TcpServer.php b/app/process/TcpServer.php index 57597dc..32fa9b5 100644 --- a/app/process/TcpServer.php +++ b/app/process/TcpServer.php @@ -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}" + ])); + } + } } \ No newline at end of file