实现心跳检测
This commit is contained in:
@@ -14,7 +14,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// 结构体引用已移至SharedStructures类
|
||||
|
||||
// 常量定义
|
||||
private const int SDK_PORT = 5000;
|
||||
private const int SDK_PORT = 8080;
|
||||
private const int BUFFER_SIZE = 4096;
|
||||
private const int TIMEOUT = 3000;
|
||||
private const string CMD_HEAD = "+CMD";
|
||||
@@ -1655,27 +1655,81 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
}
|
||||
|
||||
// 使用UDP协议发送心跳命令,与SDK保持一致
|
||||
public int Heartbeat()
|
||||
{
|
||||
// 关键修改:与SDK保持一致,使用UDP协议发送心跳命令
|
||||
// SDK中使用的是UDP协议(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
|
||||
// 之前的实现使用了TCP协议,这是心跳失败的根本原因
|
||||
// 端口修改为18890,与SDK中的CMD_SERVER_UDP_PORT保持一致
|
||||
try
|
||||
{
|
||||
// 根据SDK格式构建心跳命令 - SDK格式
|
||||
string command = $"{deviceIp}:{(int)CMD_TYPE.HEARTBEAT}$";
|
||||
// 根据SDK实际实现,心跳命令格式是 +CMD:24$
|
||||
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.HEARTBEAT}$";
|
||||
byte[] commandBytes = Encoding.ASCII.GetBytes(command);
|
||||
|
||||
if (SendCommand(command, out string response))
|
||||
// SDK实现中会尝试3次心跳,这里也采用相同的策略
|
||||
for (int retry = 0; retry < 3; retry++)
|
||||
{
|
||||
// 验证响应格式
|
||||
if (response != null)
|
||||
Console.WriteLine($"心跳检测...(尝试 {retry + 1}/3)");
|
||||
Console.WriteLine($"心跳命令: {command}");
|
||||
Console.WriteLine($"目标IP: {deviceIp}:18890");
|
||||
|
||||
using (UdpClient udpClient = new UdpClient())
|
||||
{
|
||||
Console.WriteLine("心跳成功");
|
||||
return 1; // 返回1表示成功,与DeviceManager中的判断一致
|
||||
try
|
||||
{
|
||||
// 设置接收超时,与SDK中的50毫秒保持一致
|
||||
udpClient.Client.ReceiveTimeout = 50;
|
||||
|
||||
// 发送UDP数据报,端口使用18890与SDK保持一致
|
||||
udpClient.Send(commandBytes, commandBytes.Length, deviceIp, 18890);
|
||||
Console.WriteLine("UDP心跳命令已发送");
|
||||
|
||||
// 尝试接收响应
|
||||
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
byte[] responseBytes = udpClient.Receive(ref remoteEndPoint);
|
||||
string response = Encoding.ASCII.GetString(responseBytes);
|
||||
|
||||
Console.WriteLine($"收到UDP心跳响应: {response}");
|
||||
|
||||
// SDK要求响应中必须包含 ":ok" 字符串才算成功
|
||||
if (!string.IsNullOrEmpty(response) && response.Contains(":ok"))
|
||||
{
|
||||
Console.WriteLine("心跳成功: 响应包含':ok'");
|
||||
return 1; // 返回1表示成功,与DeviceManager中的判断一致
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("心跳响应不包含':ok',验证失败");
|
||||
}
|
||||
}
|
||||
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.TimedOut)
|
||||
{
|
||||
// 超时异常,继续重试
|
||||
Console.WriteLine("UDP心跳接收超时");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"UDP心跳异常: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 如果不是最后一次尝试,短暂延迟后重试
|
||||
if (retry < 2)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
// 3次尝试都失败
|
||||
Console.WriteLine("三次心跳检测均失败");
|
||||
return -1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"心跳检测异常: {ex.Message}");
|
||||
Console.WriteLine($"异常堆栈: {ex.StackTrace}");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user