From 30b952a49ed4f611a937f08242fb147cfebef505 Mon Sep 17 00:00:00 2001 From: Stone Date: Fri, 1 Jul 2016 02:23:33 +0000 Subject: [PATCH] =?UTF-8?q?30=E7=A7=92=E5=86=85=E5=8F=91=E8=BF=87=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=EF=BC=8C=E4=B8=8D=E5=86=8D=E5=8F=91=E9=80=81=E5=BF=83?= =?UTF-8?q?=E8=B7=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinyNet/TinyClient.cpp | 8 ++++++-- TinyNet/TinyClient.h | 7 ++++--- TokenNet/TokenClient.cpp | 14 +++++++++----- TokenNet/TokenClient.h | 6 +++--- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/TinyNet/TinyClient.cpp b/TinyNet/TinyClient.cpp index d941e153..0a5e9084 100644 --- a/TinyNet/TinyClient.cpp +++ b/TinyNet/TinyClient.cpp @@ -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; diff --git a/TinyNet/TinyClient.h b/TinyNet/TinyClient.h index 9b9a8d48..71075f05 100644 --- a/TinyNet/TinyClient.h +++ b/TinyNet/TinyClient.h @@ -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: // 组网 diff --git a/TokenNet/TokenClient.cpp b/TokenNet/TokenClient.cpp index f9d81320..59c12921 100644 --- a/TokenNet/TokenClient.cpp +++ b/TokenNet/TokenClient.cpp @@ -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); diff --git a/TokenNet/TokenClient.h b/TokenNet/TokenClient.h index a94d8d4b..290d8bc2 100644 --- a/TokenNet/TokenClient.h +++ b/TokenNet/TokenClient.h @@ -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