From 692ca31fed106f39d40a960d8f45a9d498e6a848 Mon Sep 17 00:00:00 2001 From: nnhy Date: Sat, 22 Aug 2015 01:29:46 +0000 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E7=BD=91=E6=88=90=E5=8A=9F=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=B8=80=E6=AC=A1=E6=9C=80=E5=90=8E=E6=B4=BB=E8=B7=83?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Config.cpp | 3 +++ Config.h | 6 +++++- TinyNet/TinyClient.cpp | 25 ++++++++++++++----------- TinyNet/TinyServer.cpp | 2 +- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Config.cpp b/Config.cpp index 957eb321..2b8570df 100644 --- a/Config.cpp +++ b/Config.cpp @@ -15,4 +15,7 @@ void TConfig::LoadDefault() { Type = Sys.Code; //Server = 0x01; + + PingTime = 15; + OfflineTime = 60; } diff --git a/Config.h b/Config.h index c7d495c9..206bf102 100644 --- a/Config.h +++ b/Config.h @@ -22,7 +22,11 @@ public: byte Server; // 网关ID byte Channel; // 通道 byte Speed; // 传输速度 - ByteArray ServerKey; // 服务端组网密码,退网时使用。一般6字节 + byte ServerKey[16]; // 服务端组网密码,退网时使用。一般6字节 + + ushort PingTime; // 心跳时间。秒 + ushort OfflineTime; // 离线阀值时间。秒 + ushort SleepTime; // 睡眠时间。秒 // 初始化,各字段为0 void Init(); diff --git a/TinyNet/TinyClient.cpp b/TinyNet/TinyClient.cpp index acbd0e8f..bae6de93 100644 --- a/TinyNet/TinyClient.cpp +++ b/TinyNet/TinyClient.cpp @@ -241,30 +241,33 @@ bool TinyClient::OnJoin(TinyMessage& msg) // 客户端只处理Discover响应 if(!msg.Reply || msg.Error) return true; - // 校验不对 - if(TranID != msg.Sequence) - { - debug_printf("发现响应序列号 %d 不等于内部序列号 %d \r\n", msg.Sequence, TranID); - //return true; - } - // 解析数据 JoinMessage dm; dm.ReadMessage(msg); dm.Show(true); + // 校验不对 + if(TranID != dm.TranID) + { + debug_printf("发现响应序列号 0x%08X 不等于内部序列号 0x%08X \r\n", dm.TranID, TranID); + //return true; + } + Control->Address = dm.Address; Password = dm.Password; // 记住服务端地址 Server = dm.Server; - debug_printf("组网成功!由网关 0x%02X 分配得到节点地址 0x%02X ,频道:%d,密码:", Server, dm.Address, 0); + debug_printf("组网成功!由网关 0x%02X 分配得到节点地址 0x%02X ,频道:%d,密码:", Server, dm.Address, dm.Channel); Password.Show(true); // 取消Join任务,启动Ping任务 Task* task = Scheduler[_TaskID]; - task->Period = 15000000; + task->Period = Config.PingTime; + + // 组网成功更新一次最后活跃时间 + LastActive = Time.Current(); return true; } @@ -278,11 +281,11 @@ bool TinyClient::OnDisjoin(TinyMessage& msg) // 心跳 void TinyClient::Ping() { - if(LastActive > 0 && LastActive + 60000000 < Time.Current()) + if(LastActive > 0 && LastActive + Config.OfflineTime * 1000000 < Time.Current()) { if(Server == 0) return; - debug_printf("30秒无法联系,服务端可能已经掉线,重启Join任务,关闭Ping任务\r\n"); + debug_printf("%d 秒无法联系,服务端可能已经掉线,重启Join任务,关闭Ping任务\r\n", Config.OfflineTime); Task* task = Scheduler[_TaskID]; task->Period = 5000000; diff --git a/TinyNet/TinyServer.cpp b/TinyNet/TinyServer.cpp index bd86f03f..b33cfc9f 100644 --- a/TinyNet/TinyServer.cpp +++ b/TinyNet/TinyServer.cpp @@ -167,7 +167,7 @@ bool TinyServer::OnJoin(TinyMessage& msg) } dv->LastTime = Time.Current(); - debug_printf("\r\nTinyServer::新设备 "); + debug_printf("\r\nTinyServer::新设备组网 0x%08X \r\n", dm.TranID); dv->Show(true); // 对于已注册的设备,再来发现消息不做处理