向上位机发送完整请求包通过

This commit is contained in:
大石头 2017-08-22 21:51:15 +08:00
parent ffd61bf17d
commit 51a8cddf16
3 changed files with 33 additions and 8 deletions

View File

@ -126,20 +126,26 @@ bool LinkClient::Send(const LinkMessage& msg) {
bool LinkClient::Invoke(const String& action, const String& args) {
// 消息缓冲区,跳过头部
char cs[512];
String str(&cs[sizeof(LinkMessage)], sizeof(cs) - sizeof(LinkMessage), false);
str.SetLength(0);
/*String str(&cs[sizeof(LinkMessage)], sizeof(cs) - sizeof(LinkMessage), false);
str.SetLength(0);*/
// 构造内容
str += "{\"action\":\"";
/*str += "{\"action\":\"";
str += action;
str += "\",\"args\":{";
str += "\",\"args\":";
str += args;
str += "\"}";
str += "}";*/
// 格式化消息
auto& msg = *(LinkMessage*)cs;
msg.Init();
auto js = msg.Create(sizeof(cs));
js.Add("action", action);
js.Add("args", args);
auto str = js.ToString();
// 长度
msg.Length = str.Length();
msg.Code = 1;
@ -149,6 +155,12 @@ bool LinkClient::Invoke(const String& action, const String& args) {
msg.Seq = _g_seq++;
if (_g_seq == 0)_g_seq++;
#if DEBUG
debug_printf("LinkClient::Send seq=%d [%d] => ", msg.Seq, msg.Length);
str.Show(true);
#endif
// 发送
return Send(msg);
}
@ -158,8 +170,7 @@ void LinkClient::Login()
{
TS("LinkClient::Login");
String args;
Json json(args);
Json json;
json.Add("User", User);
@ -175,7 +186,7 @@ void LinkClient::Login()
json.Add("Password", pass);
Invoke("Login", args);
Invoke("Login", json.ToString());
}
bool LinkClient::OnLogin(LinkMessage& msg)

View File

@ -17,6 +17,16 @@ void LinkMessage::Init() {
Length = 0;
}
const Json LinkMessage::Create() const { return Json((cstring)Data()); }
// 在数据区上建立Json对象
Json LinkMessage::Create(int len) {
len -= sizeof(this[0]);
if (len <= 0) return Json();
return Json((char*)Data(), len);
}
void LinkMessage::Show(bool newline) const {
String str((cstring)&this[1], Length);
str.Show(newline);

View File

@ -22,6 +22,10 @@ public:
void Init();
// 在数据区上建立Json对象
const Json Create() const;
Json Create(int len);
void Show(bool newline = false) const;
private: