增加读取写入的请求指令。

下发写入指令,测试成功。
This commit is contained in:
大石头 2017-08-29 21:00:26 +08:00
parent 32e48cb5e4
commit 466cdae9ad
4 changed files with 88 additions and 1 deletions

View File

@ -175,6 +175,10 @@ void LinkClient::OnReceive(LinkMessage& msg)
OnLogin(msg); OnLogin(msg);
else if (act == "Device/Ping") else if (act == "Device/Ping")
OnPing(msg); OnPing(msg);
else if (act == "Read")
OnRead(msg);
else if (act == "Write")
OnWrite(msg);
// 外部公共消息事件 // 外部公共消息事件
//Received(msg, *this); //Received(msg, *this);
@ -217,6 +221,37 @@ bool LinkClient::Invoke(const String& action, const Json& args) {
return Send(msg); return Send(msg);
} }
bool LinkClient::Reply(const String& action, int seq, int code, const Json& result) {
// 消息缓冲区,跳过头部
char cs[512];
// 格式化消息
auto& msg = *(LinkMessage*)cs;
msg.Init();
msg.Reply = true;
msg.Error = code != 0;
msg.Seq = seq;
auto js = msg.Create(sizeof(cs));
js.Add("action", action);
js.Add("code", code);
js.Add("result", result);
auto str = js.ToString();
// 长度
msg.Length = str.Length();
msg.Code = 1;
#if DEBUG
debug_printf("Link => ");
msg.Show(true);
#endif
// 发送
return Send(msg);
}
// 登录 // 登录
void LinkClient::Login() void LinkClient::Login()
{ {
@ -361,3 +396,48 @@ void LinkClient::OnPing(LinkMessage& msg)
int serverTime = js["ServerSeconds"].AsInt(); int serverTime = js["ServerSeconds"].AsInt();
if (serverTime > 1000) ((TTime&)Time).SetTime(serverTime); if (serverTime > 1000) ((TTime&)Time).SetTime(serverTime);
} }
void LinkClient::OnRead(LinkMessage& msg)
{
TS("LinkClient::OnRead");
if (msg.Reply) return;
auto js = msg.Create();
auto args = js["args"];
int start = args["start"].AsInt();
auto data = args["data"].AsString().ToHex();
Store.Write(start, data);
auto& bs = Store.Data;
// 响应
Json rs;
rs.Add("start", 0);
rs.Add("size", bs.Length());
rs.Add("data", bs.ToHex());
Reply(js["action"].AsString(), msg.Seq, 0, rs);
}
void LinkClient::OnWrite(LinkMessage& msg)
{
TS("LinkClient::OnWrite");
if (msg.Reply) return;
auto js = msg.Create();
auto args = js["args"];
int start = args["start"].AsInt();
int size = args["size"].AsInt();
ByteArray bs(size);
int len = Store.Read(start, bs);
bs.SetLength(len);
// 响应
Json rs;
rs.Add("start", 0);
rs.Add("size", bs.Length());
rs.Add("data", bs.ToHex());
Reply(js["action"].AsString(), msg.Seq, 0, rs);
}

View File

@ -36,7 +36,7 @@ public:
// 发送消息 // 发送消息
bool Invoke(const String& action, const Json& args); bool Invoke(const String& action, const Json& args);
bool Reply(String& action, int code, String& result, int seq); bool Reply(const String& action, int seq, int code, const Json& result);
// 收到功能消息时触发 // 收到功能消息时触发
//MessageHandler Received; //MessageHandler Received;
@ -57,6 +57,9 @@ private:
void OnLogin(LinkMessage& msg); void OnLogin(LinkMessage& msg);
void OnPing(LinkMessage& msg); void OnPing(LinkMessage& msg);
void OnRead(LinkMessage& msg);
void OnWrite(LinkMessage& msg);
private: private:
uint _task; uint _task;

View File

@ -107,6 +107,7 @@
<ClCompile Include="..\Kernel\Time.cpp" /> <ClCompile Include="..\Kernel\Time.cpp" />
<ClCompile Include="..\Kernel\WaitHandle.cpp" /> <ClCompile Include="..\Kernel\WaitHandle.cpp" />
<ClCompile Include="..\Link\LinkClient.cpp" /> <ClCompile Include="..\Link\LinkClient.cpp" />
<ClCompile Include="..\Link\LinkConfig.cpp" />
<ClCompile Include="..\Link\LinkMessage.cpp" /> <ClCompile Include="..\Link\LinkMessage.cpp" />
<ClCompile Include="..\Message\BinaryPair.cpp" /> <ClCompile Include="..\Message\BinaryPair.cpp" />
<ClCompile Include="..\Message\Controller.cpp" /> <ClCompile Include="..\Message\Controller.cpp" />

View File

@ -590,5 +590,8 @@
<ClCompile Include="..\Link\LinkMessage.cpp"> <ClCompile Include="..\Link\LinkMessage.cpp">
<Filter>Link</Filter> <Filter>Link</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\Link\LinkConfig.cpp">
<Filter>Link</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>