Api测试通过

This commit is contained in:
大石头 2017-09-05 00:52:12 +08:00
parent 61c8f8751b
commit 1fbcd7dc20
9 changed files with 35 additions and 23 deletions

View File

@ -190,7 +190,7 @@ void LinkClient::OnReceive(LinkMessage& msg)
// 调用全局动作
auto act2 = act;
String rs;
int code = Api.Invoke(act2.GetBuffer(), this, js["args"].AsString(), rs);
int code = Api.Invoke(act2.GetBuffer(), js["args"].AsString(), rs);
Reply(act, msg.Seq, code, rs);
}
@ -236,7 +236,7 @@ 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) {
bool LinkClient::Reply(const String& action, int seq, int code, const String& result) {
// 消息缓冲区,跳过头部
char cs[512];
@ -458,7 +458,7 @@ void LinkClient::OnRead(LinkMessage& msg)
rs.Add("size", bs.Length());
rs.Add("data", bs.ToHex());
Reply(js["action"].AsString(), msg.Seq, 0, rs);
Reply(js["action"].AsString(), msg.Seq, 0, rs.ToString());
}
void LinkClient::OnWrite(LinkMessage& msg)
@ -480,7 +480,7 @@ void LinkClient::OnWrite(LinkMessage& msg)
rs.Add("size", bs.Length());
rs.Add("data", bs.ToHex());
Reply(js["action"].AsString(), msg.Seq, 0, rs);
Reply(js["action"].AsString(), msg.Seq, 0, rs.ToString());
}
void LinkClient::Write(int start, const Buffer& bs)

View File

@ -39,7 +39,7 @@ public:
// 发送消息
bool Invoke(const String& action, const Json& args);
bool Reply(const String& action, int seq, int code, const Json& result);
bool Reply(const String& action, int seq, int code, const String& result);
void Read(int start, int size);
void Write(int start, const Buffer& bs);

View File

@ -74,9 +74,10 @@ int LinkConfig::SetServer(const String& args, String& result)
{
if (!args) return -1;
Json js(args);
//Json js(args);
auto svr = js["server"].AsString();
//auto svr = js["server"].AsString();
auto& svr = args;
if (!svr) return -1;
Server() = svr;
@ -92,8 +93,9 @@ int LinkConfig::SetServer(const String& args, String& result)
// 获取服务器地址
int LinkConfig::GetServer(const String& args, String& result)
{
Json js(result);
js.Add("server", Server());
//Json js(result);
//js.Add("server", Server());
result = Server();
return 0;
}

View File

@ -1,9 +1,16 @@
#include "Api.h"
// 全局对象
TApi Api;
TApi::TApi() :Routes(String::Compare), Params(String::Compare) {
}
// 注册远程调用处理器
void TApi::Register(cstring action, ApiHandler handler, void* param) {
Routes[action] = handler;
Params[action] = param;
Routes.Add(action, handler);
Params.Add(action, param);
}
// 是否包含指定动作
@ -12,9 +19,11 @@ bool TApi::Contain(cstring action) {
}
// 执行接口
int TApi::Invoke(cstring action, void* param, const String& args, String& result) {
int TApi::Invoke(cstring action, const String& args, String& result) {
ApiHandler handler;
if (!Routes.TryGetValue(action, handler)) return -1;
return handler(param, args, result);
void* p = Params[action];
return handler(p, args, result);
}

View File

@ -16,6 +16,8 @@ public:
Dictionary<cstring, ApiHandler> Routes; // 路由集合
Dictionary<cstring, void*> Params; // 参数集合
TApi();
// 注册远程调用处理器
void Register(cstring action, ApiHandler handler, void* param = nullptr);
// 模版支持成员函数
@ -29,7 +31,7 @@ public:
bool Contain(cstring action);
// 执行接口
int Invoke(cstring action, void* param, const String& args, String& result);
int Invoke(cstring action, const String& args, String& result);
};
// 全局对象

View File

@ -59,7 +59,7 @@ bool Json::IsNull() const { return !_str; }
String Json::AsString() const {
if (!_str) return nullptr;
if (_str[0] != '"') return nullptr;
//if (_str[0] != '"') return nullptr;
// 去掉前后双引号
auto p = _str.GetBuffer();
@ -253,7 +253,8 @@ Json::Json(float value) : _str(value) { }
Json::Json(double value) : _str(value) { }*/
// 设置输出缓冲区
Json::Json(char* buf, int len) :_str(buf, len, false) { _str.SetLength(0); }
//Json::Json(String& value) : _str((char*)value.GetBuffer(), value.Length(), false) { }
Json::Json(char* buf, int len) : _str(buf, len, false) { _str.SetLength(0); }
// 添加对象成员
Json& Json::Add(cstring key, const Json& value) {

View File

@ -43,8 +43,6 @@ public:
// 读取成员。找到指定成员,并用它的值构造一个新的对象
const Json operator[](cstring key) const;
// 设置成员。找到指定成员,或添加成员,并返回对象
//Json& operator[](cstring key);
// 特殊支持数组
int Length() const;
@ -52,12 +50,8 @@ public:
//Json& operator[](int index);
Json();
/*Json(String& value);
Json(bool value);
Json(int value);
Json(float value);
Json(double value);*/
// 设置输出缓冲区
//Json(String& value);
Json(char* buf, int len);
// 添加成员

View File

@ -112,6 +112,7 @@
<ClCompile Include="..\Link\LinkClient.cpp" />
<ClCompile Include="..\Link\LinkConfig.cpp" />
<ClCompile Include="..\Link\LinkMessage.cpp" />
<ClCompile Include="..\Message\Api.cpp" />
<ClCompile Include="..\Message\BinaryPair.cpp" />
<ClCompile Include="..\Message\Controller.cpp" />
<ClCompile Include="..\Message\DataStore.cpp" />

View File

@ -599,5 +599,8 @@
<ClCompile Include="..\Board\W5500Module.cpp">
<Filter>Board</Filter>
</ClCompile>
<ClCompile Include="..\Message\Api.cpp">
<Filter>Message</Filter>
</ClCompile>
</ItemGroup>
</Project>