fix(net): 优化数据包解析及日志记录逻辑
- 在 NewCurrentCollectorParser 中增加正则校验,提高数据包匹配准确性 - 修复 Packet 类中 length 属性的格式问题,确保正确访问 - 调整 PacketParserFactory,修复可能的空数据问题,增强健壮性 - TcpServer 中新增 logMessage 方法拆分日志记录逻辑 - 根据包匹配结果分别记录详细或简化日志,增强调试信息 - 修复 TcpServer 中不匹配数据包时的响应,避免无效处理
This commit is contained in:
+1
-1
@@ -177,7 +177,7 @@ class Packet
|
|||||||
get => $this->rawText;
|
get => $this->rawText;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int $length{
|
public int $length {
|
||||||
get => strlen($this->rawBytes);
|
get => strlen($this->rawBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ class NewCurrentCollectorParser implements PacketParserInterface
|
|||||||
|
|
||||||
public function parse(string $hexString): array
|
public function parse(string $hexString): array
|
||||||
{
|
{
|
||||||
|
$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 [
|
return [
|
||||||
'hexType' => 4,
|
'hexType' => 4,
|
||||||
'isMatched' => true,
|
'isMatched' => true,
|
||||||
@@ -22,4 +25,7 @@ class NewCurrentCollectorParser implements PacketParserInterface
|
|||||||
'channel1' => substr($hexString, 24, 2),
|
'channel1' => substr($hexString, 24, 2),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ['isMatched' => false];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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 ?? []);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+37
-12
@@ -82,7 +82,33 @@ class TcpServer
|
|||||||
self::$connections[$ip] = $connection;
|
self::$connections[$ip] = $connection;
|
||||||
$packet = PacketParserFactory::new($data);
|
$packet = PacketParserFactory::new($data);
|
||||||
$context = new PacketContext(self::getConnections(), $connection, $packet);
|
$context = new PacketContext(self::getConnections(), $connection, $packet);
|
||||||
// 格式化输出报文信息文本框
|
$this->logMessage($packet, $ip);
|
||||||
|
|
||||||
|
if ($packet->isMatched) {
|
||||||
|
$result = FlowMain::getInstance()->main($context);
|
||||||
|
$connection->send($result->getFullVoice());
|
||||||
|
} else {
|
||||||
|
$connection->send("Packet is not matched");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接关闭时触发
|
||||||
|
*/
|
||||||
|
public function onClose(TcpConnection $connection): void
|
||||||
|
{
|
||||||
|
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([
|
Logger::debug(Logger::generateTextBox([
|
||||||
"---------------------------- PACKET --------------------------------",
|
"---------------------------- PACKET --------------------------------",
|
||||||
"IP Address : {$ip}",
|
"IP Address : {$ip}",
|
||||||
@@ -94,17 +120,16 @@ class TcpServer
|
|||||||
"Card Info : {$packet->card}",
|
"Card Info : {$packet->card}",
|
||||||
"Gateway Info : {$packet->gateway}",
|
"Gateway Info : {$packet->gateway}",
|
||||||
]));
|
]));
|
||||||
|
} else {
|
||||||
$result = FlowMain::getInstance()->main($context);
|
Logger::debug("Packet is not matched");
|
||||||
$connection->send($result->getFullVoice());
|
Logger::debug(Logger::generateTextBox([
|
||||||
|
"---------------------------- PACKET --------------------------------",
|
||||||
|
"IP Address : {$ip}",
|
||||||
|
"Packet Length: {$packet->length}",
|
||||||
|
"Packet Type : {$packet->hexType->name}",
|
||||||
|
"---------------------------- DATA --------------------------------",
|
||||||
|
"Hex Packet : {$packet->hexString}"
|
||||||
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 连接关闭时触发
|
|
||||||
*/
|
|
||||||
public function onClose(TcpConnection $connection): void
|
|
||||||
{
|
|
||||||
unset(self::$connections[$connection->getRemoteIp()]);
|
|
||||||
Logger::info("客户端已断开连接:{$connection->getRemoteIp()}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user