优化TokenConfig,节省1k+内存

This commit is contained in:
Stone 2016-06-04 03:02:04 +00:00
parent 9597f9077d
commit 59de84ff65
7 changed files with 38 additions and 51 deletions

View File

@ -164,16 +164,17 @@ void AP0104::InitDHCP(Action onNetReady)
bool AP0104::QueryDNS(TokenConfig& tk) bool AP0104::QueryDNS(TokenConfig& tk)
{ {
if (tk.Server.Length() == 0) return false; auto svr = tk.Server();
if (svr.Length() == 0) return false;
// 根据DNS获取云端IP地址 // 根据DNS获取云端IP地址
auto ip = DNS::Query(*Host, tk.Server); auto ip = DNS::Query(*Host, svr);
if (ip == IPAddress::Any()) if (ip == IPAddress::Any())
{ {
debug_printf("DNS::Query %s 失败!\r\n", tk.Server.GetBuffer()); debug_printf("DNS::Query %s 失败!\r\n", svr.GetBuffer());
return false; return false;
} }
debug_printf("服务器地址 %s %s:%d \r\n", tk.Server.GetBuffer(), ip.ToString().GetBuffer(), tk.ServerPort); debug_printf("服务器地址 %s %s:%d \r\n", svr.GetBuffer(), ip.ToString().GetBuffer(), tk.ServerPort);
tk.ServerIP = ip.Value; tk.ServerIP = ip.Value;
tk.Save(); tk.Save();

View File

@ -172,7 +172,7 @@ TokenClient* AP0801::CreateClient()
auto socket = Host->CreateSocket(tk->Protocol); auto socket = Host->CreateSocket(tk->Protocol);
socket->Remote.Port = tk->ServerPort; socket->Remote.Port = tk->ServerPort;
socket->Remote.Address = IPAddress(tk->ServerIP); socket->Remote.Address = IPAddress(tk->ServerIP);
socket->Server = tk->Server; socket->Server = tk->Server();
// 创建连接服务器的控制器 // 创建连接服务器的控制器
auto ctrl = new TokenController(); auto ctrl = new TokenController();

View File

@ -101,7 +101,7 @@ TokenClient* IOK0203::CreateClient()
auto socket = Host->CreateSocket(tk->Protocol); auto socket = Host->CreateSocket(tk->Protocol);
socket->Remote.Port = tk->ServerPort; socket->Remote.Port = tk->ServerPort;
socket->Remote.Address = IPAddress(tk->ServerIP); socket->Remote.Address = IPAddress(tk->ServerIP);
socket->Server = tk->Server; socket->Server = tk->Server();
// 创建连接服务器的控制器 // 创建连接服务器的控制器
auto ctrl = new TokenController(); auto ctrl = new TokenController();

View File

@ -131,16 +131,17 @@ void PA0903::InitDHCP(Action onNetReady)
bool PA0903::QueryDNS(TokenConfig& tk) bool PA0903::QueryDNS(TokenConfig& tk)
{ {
if(tk.Server.Length() == 0) return false; auto svr = tk.Server();
if(svr.Length() == 0) return false;
// 根据DNS获取云端IP地址 // 根据DNS获取云端IP地址
auto ip = DNS::Query(*Host, tk.Server); auto ip = DNS::Query(*Host, svr);
if(ip == IPAddress::Any()) if(ip == IPAddress::Any())
{ {
debug_printf("DNS::Query %s 失败!\r\n", tk.Server.GetBuffer()); debug_printf("DNS::Query %s 失败!\r\n", svr.GetBuffer());
return false; return false;
} }
debug_printf("服务器地址 %s %s:%d \r\n", tk.Server.GetBuffer(), ip.ToString().GetBuffer(), tk.ServerPort); debug_printf("服务器地址 %s %s:%d \r\n", svr.GetBuffer(), ip.ToString().GetBuffer(), tk.ServerPort);
tk.ServerIP = ip.Value; tk.ServerIP = ip.Value;
tk.Save(); tk.Save();

View File

@ -190,7 +190,7 @@ void LoopTask(void* param)
break; break;
case 1: case 1:
{ {
if(!client->Cfg->User) if(!client->Cfg->User())
client->Register(); client->Register();
else else
client->Login(); client->Login();
@ -300,7 +300,7 @@ bool TokenClient::OnLocalHello(TokenMessage& msg, Controller* ctrl)
HelloMessage ext2(Hello); HelloMessage ext2(Hello);
ext2.Reply = true; ext2.Reply = true;
// 使用系统ID作为Name // 使用系统ID作为Name
ext2.Name = Cfg->User; ext2.Name = Cfg->User();
// 使用系统ID作为Key // 使用系统ID作为Key
ext2.Key = ctrl2->Key; ext2.Key = ctrl2->Key;
//ext2.Key = Sys.ID; //ext2.Key = Sys.ID;
@ -329,9 +329,9 @@ bool TokenClient::OnRedirect(HelloMessage& msg)
cfg->Show(); cfg->Show();
cfg->Server = msg.Server; cfg->Server() = msg.Server;
cfg->ServerPort = msg.Port; cfg->ServerPort = msg.Port;
cfg->VisitToken = msg.VisitToken; cfg->Token() = msg.VisitToken;
ChangeIPEndPoint(msg.Server, msg.Port); ChangeIPEndPoint(msg.Server, msg.Port);
@ -383,7 +383,7 @@ void TokenClient::Register()
reg.User = Buffer(Sys.ID, 16).ToHex(); reg.User = Buffer(Sys.ID, 16).ToHex();
// 原始密码作为注册密码 // 原始密码作为注册密码
reg.Pass = Cfg->Pass; reg.Pass = Cfg->Pass();
auto now = Sys.Ms(); auto now = Sys.Ms();
reg.Salt = Buffer(&now, 8); reg.Salt = Buffer(&now, 8);
@ -419,8 +419,8 @@ void TokenClient::OnRegister(TokenMessage& msg ,Controller* ctrl)
RegisterMessage reg; RegisterMessage reg;
reg.ReadMessage(msg); reg.ReadMessage(msg);
cfg->User = reg.User; cfg->User() = reg.User;
cfg->Pass = reg.Pass; cfg->Pass() = reg.Pass;
cfg->Show(); cfg->Show();
cfg->Save(); cfg->Save();
@ -441,14 +441,14 @@ void TokenClient::Login()
LoginMessage login; LoginMessage login;
auto cfg = Cfg; auto cfg = Cfg;
login.User = cfg->User; login.User = cfg->User();
//login.Pass = MD5::Hash(cfg->Pass); //login.Pass = MD5::Hash(cfg->Pass);
// 原始密码对盐值进行加密,得到登录密码 // 原始密码对盐值进行加密,得到登录密码
auto now = Sys.Ms(); auto now = Sys.Ms();
auto arr = Buffer(&now, 8); auto arr = Buffer(&now, 8);
login.Salt = arr; login.Salt = arr;
RC4::Encrypt(arr, cfg->Pass); RC4::Encrypt(arr, cfg->Pass());
login.Pass = arr.ToHex(); login.Pass = arr.ToHex();
TokenMessage msg(2); TokenMessage msg(2);

View File

@ -4,12 +4,7 @@
TokenConfig* TokenConfig::Current = nullptr; TokenConfig* TokenConfig::Current = nullptr;
TokenConfig::TokenConfig() : ConfigBase(), TokenConfig::TokenConfig() : ConfigBase()
User(_User, sizeof(_User)),
Pass(_Pass, sizeof(_Pass)),
VisitToken(_VisitToken, sizeof(_VisitToken)),
Server(_Server, sizeof(_Server)),
Vendor(_Vendor, sizeof(_Vendor))
{ {
_Name = "TokenCf"; _Name = "TokenCf";
_Start = &Length; _Start = &Length;
@ -32,17 +27,6 @@ void TokenConfig::Init()
Protocol = ProtocolType::Udp; Protocol = ProtocolType::Udp;
} }
void TokenConfig::Load()
{
ConfigBase::Load();
User = _User;
Pass = _Pass;
VisitToken = _VisitToken;
Server = _Server;
Vendor = _Vendor;
}
void TokenConfig::Show() const void TokenConfig::Show() const
{ {
#if DEBUG #if DEBUG
@ -54,10 +38,10 @@ void TokenConfig::Show() const
debug_printf("\t远程: "); debug_printf("\t远程: ");
IPEndPoint ep2(IPAddress(ServerIP), ServerPort); IPEndPoint ep2(IPAddress(ServerIP), ServerPort);
ep2.Show(true); ep2.Show(true);
debug_printf("\t服务: %s \r\n", Server.GetBuffer()); debug_printf("\t服务: %s \r\n", _Server);
debug_printf("\t厂商: %s \r\n", Vendor.GetBuffer()); debug_printf("\t厂商: %s \r\n", _Vendor);
debug_printf("\t登录: %s \r\n", User.GetBuffer()); debug_printf("\t登录: %s \r\n", _User);
debug_printf("\t密码: %s \r\n", Pass.GetBuffer()); debug_printf("\t密码: %s \r\n", _Pass);
#endif #endif
} }
@ -73,16 +57,18 @@ TokenConfig* TokenConfig::Create(cstring vendor, ProtocolType protocol, ushort s
if(tc.Protocol == 0x00)tc.Protocol = ProtocolType::Udp; // 默认 UDP 不允许 unknown if(tc.Protocol == 0x00)tc.Protocol = ProtocolType::Udp; // 默认 UDP 不允许 unknown
bool rs = tc.New; bool rs = tc.New;
if(!tc.Vendor) auto vnd = tc.Vendor();
if(!vnd)
{ {
tc.Vendor = vendor; vnd = vendor;
rs = false; rs = false;
} }
if(!tc.Server) auto svr = tc.Server();
if(!svr)
{ {
tc.Server = tc.Vendor; svr = vnd;
tc.ServerPort = sport; tc.ServerPort = sport;
tc.Port = port; tc.Port = port;

View File

@ -26,7 +26,7 @@ public:
ushort Port; // 本地端口 ushort Port; // 本地端口
uint ServerIP; // 服务器IP地址。服务器域名解析成功后覆盖 uint ServerIP; // 服务器IP地址。服务器域名解析成功后覆盖
ushort ServerPort; // 服务器端口 ushort ServerPort; // 服务器端口
char _VisitToken[16]; //访问服务器令牌 char _Token[16]; //访问服务器令牌
char _Server[32]; // 服务器域名。出厂为空,从厂商服务器覆盖,恢复出厂设置时清空 char _Server[32]; // 服务器域名。出厂为空,从厂商服务器覆盖,恢复出厂设置时清空
char _Vendor[32]; // 厂商服务器域名。原始厂商服务器地址 char _Vendor[32]; // 厂商服务器域名。原始厂商服务器地址
@ -34,14 +34,13 @@ public:
TokenConfig(); TokenConfig();
virtual void Init(); virtual void Init();
virtual void Load();
virtual void Show() const; virtual void Show() const;
String User; String User() { return String(_User, sizeof(_User)); }
String Pass; String Pass() { return String(_Pass, sizeof(_Pass)); }
String VisitToken; String Token() { return String(_Token, sizeof(_Token)); }
String Server; String Server() { return String(_Server, sizeof(_Server)); }
String Vendor; String Vendor() { return String(_Vendor, sizeof(_Vendor)); }
static TokenConfig* Current; static TokenConfig* Current;
static TokenConfig* Create(cstring vendor, ProtocolType protocol, ushort sport, ushort port); static TokenConfig* Create(cstring vendor, ProtocolType protocol, ushort sport, ushort port);