修复温度数据接收异常,添加心跳机制和连接管理
This commit is contained in:
@@ -1129,10 +1129,11 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
byte[] buffer = new byte[65536]; // 缓冲区大小
|
byte[] buffer = new byte[65536]; // 缓冲区大小
|
||||||
|
|
||||||
// 定义常量
|
// 定义常量
|
||||||
const int RECEIVE_TIMEOUT = 5000;
|
const int RECEIVE_TIMEOUT = 30000; // 增加到30秒,减少因超时导致的断开
|
||||||
|
const int HEARTBEAT_INTERVAL = 15000; // 心跳间隔15秒
|
||||||
const int MEDIUM_SLEEP_MS = 50;
|
const int MEDIUM_SLEEP_MS = 50;
|
||||||
const int LONG_SLEEP_MS = 1000;
|
const int LONG_SLEEP_MS = 2000; // 增加重连等待时间
|
||||||
const int ERROR_SLEEP_MS = 100;
|
const int ERROR_SLEEP_MS = 1000; // 增加异常恢复等待时间
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -1255,13 +1256,51 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
_isReceivingTemperatureData = true;
|
_isReceivingTemperatureData = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 记录上次心跳时间和上次接收数据时间
|
||||||
|
DateTime lastHeartbeatTime = DateTime.Now;
|
||||||
|
DateTime lastReceiveTime = DateTime.Now;
|
||||||
|
|
||||||
// 持续读取温度数据流
|
// 持续读取温度数据流
|
||||||
while (localTcpClient != null && localTcpClient.Connected && !isPaused)
|
while (localTcpClient != null && localTcpClient.Connected)
|
||||||
{
|
{
|
||||||
// 使用阻塞读取方式,等待数据到达
|
// 检查是否暂停
|
||||||
|
lock (_lockObject)
|
||||||
|
{
|
||||||
|
isPaused = _isTemperatureReceivingPaused;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isPaused)
|
||||||
|
{
|
||||||
|
Log("温度接收已暂停,等待恢复");
|
||||||
|
Thread.Sleep(MEDIUM_SLEEP_MS);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送心跳保持连接
|
||||||
|
if ((DateTime.Now - lastHeartbeatTime).TotalMilliseconds > HEARTBEAT_INTERVAL)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
byte[] heartbeatCommand = Encoding.ASCII.GetBytes("heartbeat\r\n");
|
||||||
|
localStream.Write(heartbeatCommand, 0, heartbeatCommand.Length);
|
||||||
|
localStream.Flush();
|
||||||
|
lastHeartbeatTime = DateTime.Now;
|
||||||
|
// 心跳不记录日志以减少日志量
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log($"发送心跳失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用非阻塞方式检查是否有数据可读
|
||||||
|
if (localStream.DataAvailable)
|
||||||
|
{
|
||||||
|
// 有数据可读时进行阻塞读取
|
||||||
int bytesRead = localStream.Read(buffer, 0, buffer.Length);
|
int bytesRead = localStream.Read(buffer, 0, buffer.Length);
|
||||||
if (bytesRead > 0)
|
if (bytesRead > 0)
|
||||||
{
|
{
|
||||||
|
lastReceiveTime = DateTime.Now; // 更新最后接收数据时间
|
||||||
byte[] receivedBytes = new byte[bytesRead];
|
byte[] receivedBytes = new byte[bytesRead];
|
||||||
Array.Copy(buffer, receivedBytes, bytesRead);
|
Array.Copy(buffer, receivedBytes, bytesRead);
|
||||||
Log($"接收到温度数据字节数: {bytesRead}");
|
Log($"接收到温度数据字节数: {bytesRead}");
|
||||||
@@ -1281,7 +1320,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else if (bytesRead == 0)
|
||||||
{
|
{
|
||||||
// 连接已关闭
|
// 连接已关闭
|
||||||
Log("远程主机关闭了连接");
|
Log("远程主机关闭了连接");
|
||||||
@@ -1305,6 +1344,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// while循环退出后的处理
|
// while循环退出后的处理
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
Reference in New Issue
Block a user