diff --git a/Config.h b/Config.h index fb591cb8..2e1f6c24 100644 --- a/Config.h +++ b/Config.h @@ -19,6 +19,7 @@ public: byte SoftVer; // 软件版本 ushort Type; // 类型 byte Address; // 分配得到的设备地址 + byte Password[16]; // 通信密码 byte Server; // 网关ID byte Channel; // 通道 ushort Speed; // 传输速度 diff --git a/TinyNet/TinyClient.cpp b/TinyNet/TinyClient.cpp index 543bdb65..29bafe84 100644 --- a/TinyNet/TinyClient.cpp +++ b/TinyNet/TinyClient.cpp @@ -40,8 +40,13 @@ void TinyClient::Open() _TaskID = Sys.AddTask(TinyClientTask, this, 0, 5000000, "客户端服务"); - if(Config.Address > 0) Control->Address = Config.Address; - if(Config.Server > 0) Server = Config.Server; + if(Config.Address > 0 && Config.Server > 0) + { + Control->Address = Config.Address; + Server = Config.Server; + + Password.Load(Config.Password, ArrayLength(Config.Password)); + } } void TinyClient::Close() @@ -259,11 +264,16 @@ bool TinyClient::OnJoin(TinyMessage& msg) Control->Address = dm.Address; Password = dm.Password; + Password.Save(Config.Password, ArrayLength(Config.Password)); // 记住服务端地址 Server = dm.Server; Config.Channel = dm.Channel; - Config.Speed = dm.Speed == 0 ? 250 : (dm.Speed == 1 ? 1000 : 2000);; + Config.Speed = dm.Speed == 0 ? 250 : (dm.Speed == 1 ? 1000 : 2000); + + // 服务端组网密码,退网使用 + Config.ServerKey[0] = dm.HardID.Length(); + dm.HardID.Save(Config.ServerKey, ArrayLength(Config.ServerKey)); debug_printf("组网成功!由网关 0x%02X 分配得到节点地址 0x%02X ,频道:%d,传输速率:%dkbps,密码:", Server, dm.Address, dm.Channel, Config.Speed); Password.Show(true); diff --git a/Type.cpp b/Type.cpp index 479d366a..0f4cc0a9 100644 --- a/Type.cpp +++ b/Type.cpp @@ -117,6 +117,37 @@ String ByteArray::ToHex(char sep, int newLine) const return ToHex(str, sep, newLine); } +// 保存到普通字节数组,首字节为长度 +int ByteArray::Load(const byte* data, int maxsize) +{ + /*// 压缩编码整数最大4字节 + Stream ms(data, 4); + _Length = ms.ReadEncodeInt(); + + return Copy(data + ms.Position(), _Length);*/ + + _Length = data[0] <= maxsize ? data[0] : maxsize; + + return Copy(data + 1, _Length); +} + +// 从普通字节数据组加载,首字节为长度 +int ByteArray::Save(byte* data, int maxsize) +{ + /*// 压缩编码整数最大4字节 + Stream ms(data, 4); + ms.WriteEncodeInt(_Length); + + return CopyTo(data + ms.Position(), _Length);*/ + + assert_param(_Length <= 0xFF); + + int len = _Length <= maxsize ? _Length : maxsize; + data[0] = len; + + return CopyTo(data + 1, len); +} + String& ByteArray::ToStr(String& str) const { return ToHex(str, '-', 0x20); diff --git a/Type.h b/Type.h index 5f903e96..2382e39c 100644 --- a/Type.h +++ b/Type.h @@ -192,7 +192,7 @@ public: { // 不要自己拷贝给自己 if(&arr == this) return *this; - + _Length = arr.Length(); Copy(arr); @@ -268,7 +268,7 @@ public: { // 不要自己拷贝给自己 if(&arr == this) return 0; - + int len = arr.Length(); if(len == 0) return 0; @@ -395,6 +395,11 @@ public: // 显示十六进制数据,指定分隔字符和换行长度 String ToHex(char sep = '-', int newLine = 0x10) const; + // 保存到普通字节数组,首字节为长度 + int Load(const byte* data, int maxsize = -1); + // 从普通字节数据组加载,首字节为长度 + int Save(byte* data, int maxsize = -1); + // 输出对象的字符串表示方式 virtual String& ToStr(String& str) const; // 显示对象。默认显示ToString