From 466cdae9ad028dae551b80c41dee043c91f1dd2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 29 Aug 2017 21:00:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=BB=E5=8F=96=E5=86=99?= =?UTF-8?q?=E5=85=A5=E7=9A=84=E8=AF=B7=E6=B1=82=E6=8C=87=E4=BB=A4=E3=80=82?= =?UTF-8?q?=20=E4=B8=8B=E5=8F=91=E5=86=99=E5=85=A5=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=EF=BC=8C=E6=B5=8B=E8=AF=95=E6=88=90=E5=8A=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Link/LinkClient.cpp | 80 ++++++++++++++++++++++++++++++++++++++ Link/LinkClient.h | 5 ++- vs/SmartOS.vcxproj | 1 + vs/SmartOS.vcxproj.filters | 3 ++ 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/Link/LinkClient.cpp b/Link/LinkClient.cpp index d71a8b1b..dccebc14 100644 --- a/Link/LinkClient.cpp +++ b/Link/LinkClient.cpp @@ -175,6 +175,10 @@ void LinkClient::OnReceive(LinkMessage& msg) OnLogin(msg); else if (act == "Device/Ping") OnPing(msg); + else if (act == "Read") + OnRead(msg); + else if (act == "Write") + OnWrite(msg); // 外部公共消息事件 //Received(msg, *this); @@ -217,6 +221,37 @@ bool LinkClient::Invoke(const String& action, const Json& args) { 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() { @@ -361,3 +396,48 @@ void LinkClient::OnPing(LinkMessage& msg) int serverTime = js["ServerSeconds"].AsInt(); 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); +} diff --git a/Link/LinkClient.h b/Link/LinkClient.h index 1e9253e6..9f715bc9 100644 --- a/Link/LinkClient.h +++ b/Link/LinkClient.h @@ -36,7 +36,7 @@ public: // 发送消息 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; @@ -57,6 +57,9 @@ private: void OnLogin(LinkMessage& msg); void OnPing(LinkMessage& msg); + void OnRead(LinkMessage& msg); + void OnWrite(LinkMessage& msg); + private: uint _task; diff --git a/vs/SmartOS.vcxproj b/vs/SmartOS.vcxproj index 27b7b17d..b6d97819 100644 --- a/vs/SmartOS.vcxproj +++ b/vs/SmartOS.vcxproj @@ -107,6 +107,7 @@ + diff --git a/vs/SmartOS.vcxproj.filters b/vs/SmartOS.vcxproj.filters index cf3b64df..1aceecad 100644 --- a/vs/SmartOS.vcxproj.filters +++ b/vs/SmartOS.vcxproj.filters @@ -590,5 +590,8 @@ Link + + Link + \ No newline at end of file