完成令牌数据区读写框架,编译通过,未测试
This commit is contained in:
parent
d01f047a3b
commit
5e16201566
10
Config.h
10
Config.h
|
@ -63,17 +63,17 @@ public:
|
|||
void Write(Stream& ms) const;
|
||||
void Read(Stream& ms);
|
||||
|
||||
uint Size() const;
|
||||
|
||||
Buffer ToArray();
|
||||
const Buffer ToArray() const;
|
||||
|
||||
protected:
|
||||
const Config& Cfg;
|
||||
const char* _Name;
|
||||
|
||||
void* _Start;
|
||||
void* _End;
|
||||
|
||||
uint Size() const;
|
||||
|
||||
Buffer ToArray();
|
||||
const Buffer ToArray() const;
|
||||
};
|
||||
|
||||
// 必须设定为1字节对齐,否则offsetof会得到错误的位置
|
||||
|
|
|
@ -27,6 +27,7 @@ TokenClient::TokenClient()
|
|||
Delay = 0;
|
||||
|
||||
Control = nullptr;
|
||||
Cfg = nullptr;
|
||||
|
||||
Received = nullptr;
|
||||
Param = nullptr;
|
||||
|
@ -159,9 +160,7 @@ void LoopTask(void* param)
|
|||
break;
|
||||
case 1:
|
||||
{
|
||||
auto cfg = TokenConfig::Current;
|
||||
|
||||
if(!cfg->User)
|
||||
if(!client->Cfg->User)
|
||||
client->Register();
|
||||
else
|
||||
client->Login();
|
||||
|
@ -272,7 +271,7 @@ bool TokenClient::OnHello(TokenMessage& msg, Controller* ctrl)
|
|||
HelloMessage ext2(Hello);
|
||||
ext2.Reply = true;
|
||||
// 使用系统ID作为Name
|
||||
ext2.Name = TokenConfig::Current->User;
|
||||
ext2.Name = Cfg->User;
|
||||
// 使用系统ID作为Key
|
||||
ext2.Key.Copy(0, Sys.ID, 16);
|
||||
//ext2.Key = Sys.ID;
|
||||
|
@ -297,7 +296,7 @@ bool TokenClient::OnRedirect(HelloMessage& msg)
|
|||
|
||||
if(!(msg.ErrCode == 0xFE || msg.ErrCode == 0xFD)) return false;
|
||||
|
||||
auto cfg = TokenConfig::Current;
|
||||
auto cfg = Cfg;
|
||||
cfg->Protocol = (ProtocolType)msg.Protocol;
|
||||
|
||||
cfg->Show();
|
||||
|
@ -360,7 +359,7 @@ void TokenClient::OnRegister(TokenMessage& msg ,Controller* ctrl)
|
|||
return;
|
||||
}
|
||||
|
||||
auto cfg = TokenConfig::Current;
|
||||
auto cfg = Cfg;
|
||||
|
||||
RegisterMessage rm;
|
||||
rm.ReadMessage(msg);
|
||||
|
@ -385,7 +384,7 @@ void TokenClient::Login()
|
|||
|
||||
LoginMessage login;
|
||||
|
||||
auto cfg = TokenConfig::Current;
|
||||
auto cfg = Cfg;
|
||||
login.User = cfg->User;
|
||||
login.Pass = MD5::Hash(cfg->Pass);
|
||||
|
||||
|
@ -541,12 +540,13 @@ bool TokenClient::ChangeIPEndPoint(const String& domain, ushort port)
|
|||
socket->Remote.Port = port;
|
||||
Control->Port->Open();
|
||||
|
||||
auto cfg = TokenConfig::Current;
|
||||
cfg->ServerIP = ip.Value;
|
||||
Cfg->ServerIP = ip.Value;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/******************************** 数据区 ********************************/
|
||||
|
||||
void TokenClient::Read(int start, int size)
|
||||
{
|
||||
TokenDataMessage dm;
|
||||
|
@ -573,6 +573,87 @@ void TokenClient::Write(int start, const Buffer& bs)
|
|||
Control->Send(msg);
|
||||
}
|
||||
|
||||
/*
|
||||
请求:起始 + 大小
|
||||
响应:起始 + 数据
|
||||
*/
|
||||
void TokenClient::OnRead(const TokenMessage& msg, Controller* ctrl)
|
||||
{
|
||||
if(msg.Reply) return;
|
||||
if(msg.Length < 2) return;
|
||||
|
||||
auto rs = msg.CreateReply();
|
||||
auto ms = rs.ToStream();
|
||||
|
||||
TokenDataMessage dm;
|
||||
dm.ReadMessage(msg);
|
||||
|
||||
bool rt = true;
|
||||
if(dm.Start < 64)
|
||||
rt = dm.ReadData(Store);
|
||||
else if(dm.Start < 128)
|
||||
{
|
||||
dm.Start -= 64;
|
||||
rt = dm.ReadData(Cfg->ToArray());
|
||||
dm.Start += 64;
|
||||
}
|
||||
|
||||
if(!rt)
|
||||
rs.Error = true;
|
||||
else
|
||||
dm.WriteMessage(rs);
|
||||
|
||||
Reply(rs, ctrl);
|
||||
}
|
||||
|
||||
/*
|
||||
请求:1起始 + N数据
|
||||
响应:1起始 + 1大小 + N数据
|
||||
错误:错误码2 + 1起始 + 1大小
|
||||
*/
|
||||
void TokenClient::OnWrite(const TokenMessage& msg, Controller* ctrl)
|
||||
{
|
||||
if(msg.Reply) return;
|
||||
if(msg.Length < 2) return;
|
||||
|
||||
auto rs = msg.CreateReply();
|
||||
auto ms = rs.ToStream();
|
||||
|
||||
TokenDataMessage dm;
|
||||
dm.ReadMessage(msg);
|
||||
|
||||
bool rt = true;
|
||||
if(dm.Start < 64)
|
||||
{
|
||||
rt = dm.WriteData(Store, true);
|
||||
}
|
||||
else if(dm.Start < 128)
|
||||
{
|
||||
dm.Start -= 64;
|
||||
auto bs = Cfg->ToArray();
|
||||
rt = dm.WriteData(bs, true);
|
||||
dm.Start += 64;
|
||||
|
||||
Cfg->Save();
|
||||
}
|
||||
|
||||
if(!rt)
|
||||
rs.Error = true;
|
||||
else
|
||||
dm.WriteMessage(rs);
|
||||
|
||||
Reply(rs, ctrl);
|
||||
|
||||
if(dm.Start >= 64 && dm.Start < 128)
|
||||
{
|
||||
debug_printf("\r\n 配置区被修改,200ms后重启\r\n");
|
||||
Sys.Sleep(200);
|
||||
Sys.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
/******************************** 远程调用 ********************************/
|
||||
|
||||
void TokenClient::Invoke(const String& action, const Buffer& bs)
|
||||
{
|
||||
TokenMessage msg;
|
||||
|
@ -587,7 +668,7 @@ void TokenClient::Invoke(const String& action, const Buffer& bs)
|
|||
Control->Send(msg);
|
||||
}
|
||||
|
||||
void TokenClient::OnInvoke(TokenMessage& msg, Controller* ctrl)
|
||||
void TokenClient::OnInvoke(const TokenMessage& msg, Controller* ctrl)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
#include "TokenNet\TokenController.h"
|
||||
|
||||
#include "Message\DataStore.h"
|
||||
|
||||
class TokenSession;
|
||||
|
||||
// 微网客户端
|
||||
|
@ -26,6 +28,8 @@ public:
|
|||
int Delay; // 心跳延迟。一条心跳指令从发出到收到所花费的时间
|
||||
|
||||
Controller* Control;
|
||||
TokenConfig* Cfg;
|
||||
DataStore Store; // 数据存储区
|
||||
|
||||
TokenClient();
|
||||
|
||||
|
@ -78,10 +82,10 @@ private:
|
|||
bool OnPing(TokenMessage& msg, Controller* ctrl);
|
||||
bool ChangeIPEndPoint(const String& domain, ushort port);
|
||||
|
||||
void OnRead(TokenMessage& msg, Controller* ctrl);
|
||||
void OnWrite(TokenMessage& msg, Controller* ctrl);
|
||||
void OnRead(const TokenMessage& msg, Controller* ctrl);
|
||||
void OnWrite(const TokenMessage& msg, Controller* ctrl);
|
||||
|
||||
void OnInvoke(TokenMessage& msg, Controller* ctrl);
|
||||
void OnInvoke(const TokenMessage& msg, Controller* ctrl);
|
||||
};
|
||||
|
||||
// 令牌会话
|
||||
|
|
|
@ -39,6 +39,49 @@ void TokenDataMessage::Write(Stream& ms) const
|
|||
if(Data.Length()) bp.Set("Data", Data);
|
||||
}
|
||||
|
||||
// 读取数据
|
||||
bool TokenDataMessage::ReadData(const DataStore& ds)
|
||||
{
|
||||
return ReadData(ds.Data);
|
||||
}
|
||||
|
||||
// 读取数据
|
||||
bool TokenDataMessage::ReadData(const Buffer& bs)
|
||||
{
|
||||
if(!Size) return false;
|
||||
|
||||
TS("TokenDataMessage::ReadData");
|
||||
|
||||
int remain = bs.Length() - Start;
|
||||
if(remain < 0) return false;
|
||||
|
||||
int len = Size;
|
||||
if(len > remain) len = remain;
|
||||
if(len > 0) Data = bs.Sub(Start, len);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 写入数据
|
||||
bool TokenDataMessage::WriteData(DataStore& ds, bool withData)
|
||||
{
|
||||
TS("TokenDataMessage::WriteData");
|
||||
|
||||
ds.Write(Start, Data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 写入数据
|
||||
bool TokenDataMessage::WriteData(Buffer& bs, bool withData)
|
||||
{
|
||||
TS("TokenDataMessage::WriteData");
|
||||
|
||||
bs.Copy(Data, Start);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
// 显示消息内容
|
||||
String& TokenDataMessage::ToStr(String& str) const
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "Message\Message.h"
|
||||
#include "Message\MessageBase.h"
|
||||
#include "Message\DataStore.h"
|
||||
|
||||
// 令牌消息
|
||||
class TokenDataMessage : public MessageBase
|
||||
|
@ -24,6 +25,12 @@ public:
|
|||
// 把消息写入数据流中
|
||||
virtual void Write(Stream& ms) const;
|
||||
|
||||
bool ReadData(const DataStore& ds);
|
||||
bool WriteData(DataStore& ds, bool withData);
|
||||
|
||||
bool ReadData(const Buffer& bs);
|
||||
bool WriteData(Buffer& bs, bool withData);
|
||||
|
||||
// 显示消息内容
|
||||
#if DEBUG
|
||||
virtual String& ToStr(String& str) const;
|
||||
|
|
|
@ -779,6 +779,8 @@ namespace NewLife.Reflection
|
|||
ss.Add("not const-qualified", "非常量约束");
|
||||
ss.Add("no instance of constructor", "没有构造函数");
|
||||
ss.Add("is undefined", "未定义");
|
||||
ss.Add("declaration is incompatible with", "声明不兼容");
|
||||
ss.Add("is inaccessible", "不可访问");
|
||||
}
|
||||
|
||||
ss = Words;
|
||||
|
|
Loading…
Reference in New Issue