增加全局Api接口,各子模块独立注册,物联协议调用
This commit is contained in:
parent
21a505b4cc
commit
61c8f8751b
|
@ -6,6 +6,7 @@
|
|||
#include "Net\ITransport.h"
|
||||
|
||||
#include "Message\Json.h"
|
||||
#include "Message\Api.h"
|
||||
|
||||
#include "LinkClient.h"
|
||||
|
||||
|
@ -185,6 +186,14 @@ void LinkClient::OnReceive(LinkMessage& msg)
|
|||
OnRead(msg);
|
||||
else if (act == "Write")
|
||||
OnWrite(msg);
|
||||
else if (!msg.Reply) {
|
||||
// 调用全局动作
|
||||
auto act2 = act;
|
||||
String rs;
|
||||
int code = Api.Invoke(act2.GetBuffer(), this, js["args"].AsString(), rs);
|
||||
|
||||
Reply(act, msg.Seq, code, rs);
|
||||
}
|
||||
|
||||
// 外部公共消息事件
|
||||
//Received(msg, *this);
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
#include "LinkConfig.h"
|
||||
|
||||
#include "Message\Json.h"
|
||||
#include "Message\Api.h"
|
||||
|
||||
LinkConfig* LinkConfig::Current = nullptr;
|
||||
|
||||
LinkConfig::LinkConfig() : ConfigBase()
|
||||
|
@ -57,7 +60,40 @@ LinkConfig* LinkConfig::Create(cstring server)
|
|||
}
|
||||
|
||||
tc.Show();
|
||||
|
||||
// 注册全局动作
|
||||
Api.Register("SetServer", &LinkConfig::SetServer, &tc);
|
||||
Api.Register("GetServer", &LinkConfig::GetServer, &tc);
|
||||
}
|
||||
|
||||
return &tc;
|
||||
}
|
||||
|
||||
// 设置服务器地址
|
||||
int LinkConfig::SetServer(const String& args, String& result)
|
||||
{
|
||||
if (!args) return -1;
|
||||
|
||||
Json js(args);
|
||||
|
||||
auto svr = js["server"].AsString();
|
||||
if (!svr) return -1;
|
||||
|
||||
Server() = svr;
|
||||
Save();
|
||||
Show();
|
||||
|
||||
// 3秒后重启
|
||||
Sys.Reboot(3000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 获取服务器地址
|
||||
int LinkConfig::GetServer(const String& args, String& result)
|
||||
{
|
||||
Json js(result);
|
||||
js.Add("server", Server());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -27,6 +27,12 @@ public:
|
|||
|
||||
static LinkConfig* Current;
|
||||
static LinkConfig* Create(cstring server);
|
||||
|
||||
// 设置服务器地址
|
||||
int SetServer(const String& args, String& result);
|
||||
// 获取服务器地址
|
||||
int GetServer(const String& args, String& result);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
#include "Api.h"
|
||||
|
||||
// 注册远程调用处理器
|
||||
void TApi::Register(cstring action, ApiHandler handler, void* param) {
|
||||
Routes[action] = handler;
|
||||
Params[action] = param;
|
||||
}
|
||||
|
||||
// 是否包含指定动作
|
||||
bool TApi::Contain(cstring action) {
|
||||
return Routes.ContainKey(action);
|
||||
}
|
||||
|
||||
// 执行接口
|
||||
int TApi::Invoke(cstring action, void* param, const String& args, String& result) {
|
||||
ApiHandler handler;
|
||||
if (!Routes.TryGetValue(action, handler)) return -1;
|
||||
|
||||
return handler(param, args, result);
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef __Api_H__
|
||||
#define __Api_H__
|
||||
|
||||
#include "Kernel\Sys.h"
|
||||
#include "Message.h"
|
||||
|
||||
#include "Net\IPEndPoint.h"
|
||||
|
||||
// 远程调用委托。传入参数名值对以及结果缓冲区引用,业务失败时返回false并把错误信息放在结果缓冲区
|
||||
typedef int(*ApiHandler)(void* param, const String& args, String& result);
|
||||
|
||||
// 接口
|
||||
class TApi
|
||||
{
|
||||
public:
|
||||
Dictionary<cstring, ApiHandler> Routes; // 路由集合
|
||||
Dictionary<cstring, void*> Params; // 参数集合
|
||||
|
||||
// 注册远程调用处理器
|
||||
void Register(cstring action, ApiHandler handler, void* param = nullptr);
|
||||
// 模版支持成员函数
|
||||
template<typename T>
|
||||
void Register(cstring action, int(T::*func)(const String&, String&), T* target)
|
||||
{
|
||||
Register(action, *(ApiHandler*)&func, target);
|
||||
}
|
||||
|
||||
// 是否包含指定动作
|
||||
bool Contain(cstring action);
|
||||
|
||||
// 执行接口
|
||||
int Invoke(cstring action, void* param, const String& args, String& result);
|
||||
};
|
||||
|
||||
// 全局对象
|
||||
extern TApi Api;
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue