30秒内发过数据,不再发送心跳

This commit is contained in:
Stone 2016-07-01 02:23:33 +00:00
parent cce95a003d
commit 30b952a49e
4 changed files with 22 additions and 13 deletions

View File

@ -27,6 +27,7 @@ TinyClient::TinyClient(TinyController* control)
Server = 0;
Type = Sys.Code;
LastSend = 0;
LastActive = 0;
Received = nullptr;
@ -256,7 +257,7 @@ void TinyClient::ReportAsync(uint offset,uint length)
if(offset + length >= Store.Data.Length()) return;
NextReport = offset;
NextReportLength = length;
ReportLength = length;
// 延迟200ms上报期间有其它上报任务到来将会覆盖
Sys.SetTask(_TaskID, true, 200);
}
@ -269,7 +270,7 @@ void TinyClientTask(void* param)
auto client = (TinyClient*)param;
uint offset = client->NextReport;
uint len = client->NextReportLength;
uint len = client->ReportLength;
assert(offset == 0 || offset < 0x10, "自动上报偏移量异常!");
if(offset)
@ -475,6 +476,9 @@ void TinyClient::Ping()
// 没有服务端时不要上报
if(!Server) return;
// 30秒内发过数据不再发送心跳
if(LastSend > 0 && LastSend + 30000 > Sys.Ms()) return;
TinyMessage msg;
msg.Code = 3;

View File

@ -21,9 +21,10 @@ public:
ushort Type; // 设备类型。两个字节可做二级分类
ByteArray Password; // 通讯密码
UInt64 LastSend; // 最后发送时间ms
UInt64 LastActive; // 最后活跃时间
ushort HardCrc; // 硬件ID校验
bool Encryption; // 是否加密
bool Encryption; // 是否加密
TinyClient(TinyController* control);
@ -49,7 +50,7 @@ public:
bool Report(uint offset, const Buffer& bs);
uint NextReport; // 下次上报偏移0不动
uint NextReportLength; // 下次上报数据长度
uint ReportLength; // 下次上报数据长度
void ReportAsync(uint offset,uint length = 1);
private:
@ -60,7 +61,7 @@ private:
void OnRead(const TinyMessage& msg);
void GetDeviceKey(byte id, Buffer& key);
// 常用系统级消息
public:
// 组网

View File

@ -26,7 +26,7 @@ TokenClient::TokenClient()
Status = 0;
LoginTime = 0;
LastSend = 0;
LastReceive = 0;
LastActive = 0;
Delay = 0;
MaxNotActive = 0;
@ -68,7 +68,8 @@ void TokenClient::Open()
// 令牌广播使用素数,避免跟别的任务重叠
if(cs.Count() > 0) _taskBroadcast = Sys.AddTask(BroadcastHelloTask, this, 7000, 37000, "令牌广播");
//LastReceive = Sys.Ms();
// 启动时记为为后一次活跃接收
LastActive = Sys.Ms();
Opened = true;
}
@ -132,7 +133,7 @@ void TokenClient::OnReceive(TokenMessage& msg, TokenController& ctrl)
{
TS("TokenClient::OnReceive");
LastReceive = Sys.Ms();
LastActive = Sys.Ms();
switch(msg.Code)
{
@ -210,7 +211,7 @@ void TokenClient::LoopTask()
// 最大不活跃时间ms超过该时间时重启系统
// WiFi触摸开关建议5~10分钟网关建议5分钟
// MaxNotActive 为零便不考虑重启
if(MaxNotActive != 0 && LastReceive + MaxNotActive < Sys.Ms()) Sys.Reset();
if(MaxNotActive != 0 && LastActive + MaxNotActive < Sys.Ms()) Sys.Reset();
}
void BroadcastHelloTask(void* param)
@ -580,7 +581,7 @@ void TokenClient::Ping()
{
TS("TokenClient::Ping");
if(LastReceive > 0 && LastReceive + 180000 < Sys.Ms())
if(LastActive > 0 && LastActive + 180000 < Sys.Ms())
{
// 30秒无法联系服务端可能已经掉线重启Hello任务
debug_printf("180秒无法联系服务端可能已经掉线重新开始握手\r\n");
@ -594,6 +595,9 @@ void TokenClient::Ping()
return;
}
// 30秒内发过数据不再发送心跳
if(LastSend > 0 && LastSend + 30000 > Sys.Ms()) return;
TokenPingMessage pinMsg;
TokenMessage msg(3);

View File

@ -23,7 +23,7 @@ public:
UInt64 LoginTime; // 登录时间ms
UInt64 LastSend; // 最后发送时间ms
UInt64 LastReceive;// 最后活跃时间ms
UInt64 LastActive; // 最后活跃时间ms
int Delay; // 心跳延迟。一条心跳指令从发出到收到所花费的时间
int MaxNotActive; // 最大不活跃时间ms超过该时间时重启系统。默认0
@ -107,7 +107,7 @@ private:
byte NextReport; // 下次上报偏移0不动
byte ReportLength; // 下次上报数据长度
void LoopTask();
bool CheckReport();
};
@ -127,7 +127,7 @@ public:
int Status; // 状态。0准备、1握手完成、2登录后
UInt64 LoginTime; // 登录时间ms
UInt64 LastReceive; // 最后活跃时间ms
UInt64 LastActive; // 最后活跃时间ms
};
#endif