From 97a2303c2fe8bb7be7358de3a745f4ab89c21925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sun, 27 Aug 2017 00:39:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=89=A9=E8=81=94=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E6=88=90=E5=8A=9F=E5=90=91=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=AB=AF=E6=8F=90=E4=BA=A4=E8=8A=AF=E7=89=87=E5=94=AF=E4=B8=80?= =?UTF-8?q?=E6=A0=87=E8=AF=86=E3=80=81=E7=89=88=E6=9C=AC=E3=80=81=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E3=80=81=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Link/LinkClient.cpp | 83 +++++++++++++++------------------------------ Link/LinkClient.h | 7 ++-- Link/LinkConfig.cpp | 63 ++++++++++++++++++++++++++++++++++ Link/LinkConfig.h | 32 +++++++++++++++++ 4 files changed, 126 insertions(+), 59 deletions(-) create mode 100644 Link/LinkConfig.cpp create mode 100644 Link/LinkConfig.h diff --git a/Link/LinkClient.cpp b/Link/LinkClient.cpp index 44f80579..d31d09fb 100644 --- a/Link/LinkClient.cpp +++ b/Link/LinkClient.cpp @@ -13,14 +13,14 @@ LinkClient* LinkClient::Current = nullptr; -//static void BroadcastHelloTask(void* param); - LinkClient::LinkClient() : Routes(String::Compare) { Opened = false; Status = 0; + Cfg = LinkConfig::Current; + LoginTime = 0; LastSend = 0; LastActive = 0; @@ -36,6 +36,7 @@ void LinkClient::Open() if (Opened) return; TS("LinkClient::Open"); + assert(Cfg, "未指定物联配置Cfg"); // 令牌客户端定时任务 _task = Sys.AddTask(&LinkClient::LoopTask, this, 0, 1000, "物联客户"); @@ -62,7 +63,7 @@ void LinkClient::CheckNet() // 检测主链接 if (!mst) { - auto uri = Server; + auto uri = Cfg->Uri(); // 创建连接服务器的Socket auto socket = Socket::CreateRemote(uri); if (!socket) return; @@ -115,7 +116,6 @@ void LinkClient::LoopTask() // 登录成功后,心跳一次,把数据同步上去 Sys.Sleep(1000); if (Status >= 2) Ping(); - break; case 2: @@ -156,36 +156,6 @@ void LinkClient::OnReceive(LinkMessage& msg) auto js = msg.Create(); - /*// 带有code是响应 - auto jcode = js["code"]; - if (!jcode.IsNull()) { - // 错误处理 - int code = js["code"].AsInt(); - if (code) - { -#if DEBUG - debug_printf("<= code=%d error="); - js["data"].AsString().Show(true); -#endif // DEBUG - - return; - } - - // 正常响应 - auto act = js["action"].AsString(); - auto args = js["result"]; - - if (act == "login") - OnLogin(args); - else if (act == "ping") - OnLogin(args); - } - // 没有code是请求 - else { - auto action = js["action"]; - auto args = js["args"]; - }*/ - auto act = js["action"].AsString(); if (act == "login") @@ -198,22 +168,13 @@ void LinkClient::OnReceive(LinkMessage& msg) } bool LinkClient::Send(const LinkMessage& msg) { - + return Master->Send(msg.GetBuffer()); } bool LinkClient::Invoke(const String& action, const Json& args) { // 消息缓冲区,跳过头部 char cs[512]; - /*String str(&cs[sizeof(LinkMessage)], sizeof(cs) - sizeof(LinkMessage), false); - str.SetLength(0);*/ - - // 构造内容 - /*str += "{\"action\":\""; - str += action; - str += "\",\"args\":"; - str += args; - str += "}";*/ // 格式化消息 auto& msg = *(LinkMessage*)cs; @@ -250,19 +211,31 @@ void LinkClient::Login() Json json; - json.Add("user", User); + // 已有用户名则直接登录,否则用机器码注册 + auto user = Cfg->User(); + if (user) { + json.Add("user", user); - // 原始密码对盐值进行加密,得到登录密码 - auto now = DateTime::Now().TotalMs(); - auto arr = Buffer(&now, 8); - ByteArray bs; - bs = arr; - RC4::Encrypt(arr, Pass); - // 散列明文和密码连接在一起 - auto pass = bs.ToHex(); - pass += arr.ToHex(); + // 原始密码对盐值进行加密,得到登录密码 + auto now = DateTime::Now().TotalMs(); + auto arr = Buffer(&now, 8); + ByteArray bs; + bs = arr; + RC4::Encrypt(arr, Cfg->Pass()); + // 散列明文和密码连接在一起 + auto pass = bs.ToHex(); + pass += arr.ToHex(); - json.Add("pass", pass); + json.Add("pass", pass); + } + else { + json.Add("user", Buffer(Sys.ID, 16).ToHex()); + } + + ushort code = _REV16(Sys.Code); + json.Add("type", Buffer(&code, 2).ToHex()); + json.Add("agent", Sys.Name); + json.Add("version", Version(Sys.Ver).ToString()); Invoke("Device/Login", json); } diff --git a/Link/LinkClient.h b/Link/LinkClient.h index 552b8e84..fc2e6f95 100644 --- a/Link/LinkClient.h +++ b/Link/LinkClient.h @@ -7,6 +7,7 @@ #include "Message\Json.h" #include "LinkMessage.h" +#include "LinkConfig.h" // 物联客户端 class LinkClient @@ -21,11 +22,9 @@ public: int Delay; // 心跳延迟。一条心跳指令从发出到收到所花费的时间 int MaxNotActive; // 最大不活跃时间ms,超过该时间时重启系统。默认0 - NetUri Server; - String User; - String Pass; + LinkConfig* Cfg; - Socket* Master; // 主链接。服务器长连接 + Socket* Master; // 主链接。服务器长连接 DataStore Store; // 数据存储区 Dictionary Routes; // 路由集合 diff --git a/Link/LinkConfig.cpp b/Link/LinkConfig.cpp new file mode 100644 index 00000000..480a82e7 --- /dev/null +++ b/Link/LinkConfig.cpp @@ -0,0 +1,63 @@ +#include "Net\IPEndPoint.h" + +#include "LinkConfig.h" + +LinkConfig* LinkConfig::Current = nullptr; + +LinkConfig::LinkConfig() : ConfigBase() +{ + _Name = "LinkCfg"; + _Start = &Length; + _End = &TagEnd; + Init(); +} + +void LinkConfig::Init() +{ + ConfigBase::Init(); + + Length = Size(); +} + +void LinkConfig::Show() const +{ +#if DEBUG + debug_printf("LinkConfig::物联配置:\r\n"); + + debug_printf("\t服务: %s \r\n", _Server); + debug_printf("\t登录: %s \r\n", _User); + debug_printf("\t密码: %s \r\n", _Pass); +#endif +} + +LinkConfig* LinkConfig::Create(cstring server) +{ + // Flash最后一块作为配置区 + if (Config::Current == nullptr) Config::Current = &Config::CreateFlash(); + + static LinkConfig tc; + if (!Current) + { + LinkConfig::Current = &tc; + tc.Init(); + tc.Load(); + + auto uri = tc.Uri(); + + // 默认 UDP 不允许 unknown + if (uri.Type == NetType::Unknown) uri.Type = NetType::Udp; + + // 如果服务器配置为空,则使用外部地址 + auto svr = tc.Server(); + if (!svr) + { + svr = server; + + tc.Save(); + } + + tc.Show(); + } + + return &tc; +} diff --git a/Link/LinkConfig.h b/Link/LinkConfig.h new file mode 100644 index 00000000..7b2dc6b9 --- /dev/null +++ b/Link/LinkConfig.h @@ -0,0 +1,32 @@ +#ifndef __LinkConfig_H__ +#define __LinkConfig_H__ + +#include "..\Config.h" +#include "Net\NetUri.h" + +// 配置信息 +class LinkConfig : public ConfigBase +{ +public: + byte Length; // 数据长度 + + char _User[16]; // 登录名 + char _Pass[16]; // 登录密码 + char _Server[32]; // 服务器 + + byte TagEnd; // 数据区结束标识符 + + LinkConfig(); + virtual void Init(); + virtual void Show() const; + + String User() { return String(_User, sizeof(_User), false); } + String Pass() { return String(_Pass, sizeof(_Pass), false); } + String Server() { return String(_Server, sizeof(_Server), false); } + NetUri Uri() { return NetUri(Server()); } + + static LinkConfig* Current; + static LinkConfig* Create(cstring server); +}; + +#endif