OnInvoke使用数据流返回数据
This commit is contained in:
parent
e1b8c0bd5e
commit
251d63b82e
|
@ -217,27 +217,26 @@ TokenClient* AP0801::CreateClient()
|
||||||
return Client = client;
|
return Client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SetWiFi(const Dictionary& args, Buffer& result)
|
bool SetWiFi(void* param, const BinaryPair& args, Stream& result)
|
||||||
{
|
{
|
||||||
ByteArray rs;
|
ByteArray rs;
|
||||||
|
|
||||||
/*String ssid;
|
String ssid;
|
||||||
String pass;
|
String pass;
|
||||||
|
|
||||||
if(!args.Get("ssid", ssid)) return false;
|
if(!args.Get("ssid", ssid)) return false;
|
||||||
if(!args.Get("pass", pass)) return false;*/
|
if(!args.Get("pass", pass)) return false;
|
||||||
|
|
||||||
auto ssid = args.GetString("ssid");
|
/*auto ssid = args.GetString("ssid");
|
||||||
auto pass = args.GetString("pass");
|
auto pass = args.GetString("pass");
|
||||||
|
|
||||||
if(!ssid || !pass) return false;
|
if(!ssid || !pass) return false;*/
|
||||||
|
|
||||||
//todo 保存WiFi信息
|
//todo 保存WiFi信息
|
||||||
//auto esp = (ESP8266*)
|
//auto esp = (ESP8266*)
|
||||||
|
|
||||||
//result.Set("Result", (byte)1);
|
//result.Set("Result", (byte)1);
|
||||||
result.SetLength(1);
|
result.Write((byte)1);
|
||||||
result[0] = true;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -260,8 +259,9 @@ void SetWiFiTask(void* param)
|
||||||
BinaryPair args(ms1);
|
BinaryPair args(ms1);
|
||||||
|
|
||||||
ByteArray result;
|
ByteArray result;
|
||||||
client->OnInvoke("SetWiFi", args.GetAll(), result);
|
bool rs = client->OnInvoke("SetWiFi", args, result);
|
||||||
|
|
||||||
|
assert(rs, "OnInvoke");
|
||||||
assert(result, "result");
|
assert(result, "result");
|
||||||
assert(result[0] == 1, "rt");
|
assert(result[0] == 1, "rt");
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,20 @@
|
||||||
#include "Sys.h"
|
#include "Sys.h"
|
||||||
#include "TokenNet/TokenClient.h"
|
#include "TokenNet/TokenClient.h"
|
||||||
|
|
||||||
bool InvokeFun(const BinaryPair& args, BinaryPair& result)
|
bool InvokeFun(void* param, const BinaryPair& args, Buffer& result)
|
||||||
{
|
{
|
||||||
byte rt;
|
byte rt;
|
||||||
bool rs;
|
bool rs;
|
||||||
|
|
||||||
rs = args.Get("Hello", rt);
|
rs = args.Get("Hello", rt);
|
||||||
if (rt == 1 && rs)
|
if(!rs || rt != 1) return false;
|
||||||
result.Set("Hello", (byte)0);
|
|
||||||
else
|
result.SetAt(0, 0);
|
||||||
return false;
|
|
||||||
|
|
||||||
rs = args.Get("Rehello", rt);
|
rs = args.Get("Rehello", rt);
|
||||||
if (rt == 0 && rs)
|
if(!rs || rt != 0) return false;
|
||||||
result.Set("Rehello", (byte)1);
|
|
||||||
else
|
result.SetAt(0, 1);
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -36,10 +34,11 @@ void InvokeTest(TokenClient * client)
|
||||||
// 封装成所需数据格式
|
// 封装成所需数据格式
|
||||||
BinaryPair args(ms1);
|
BinaryPair args(ms1);
|
||||||
// 准备返回数据的容器
|
// 准备返回数据的容器
|
||||||
MemoryStream ms2;
|
ByteArray bs;
|
||||||
|
Stream ms2(bs);
|
||||||
BinaryPair result(ms2);
|
BinaryPair result(ms2);
|
||||||
// 调用
|
// 调用
|
||||||
client->OnInvoke("Test", args, result);
|
client->OnInvoke("Test", args, bs);
|
||||||
|
|
||||||
bool isOk = true;
|
bool isOk = true;
|
||||||
|
|
||||||
|
|
|
@ -764,17 +764,22 @@ void TokenClient::OnInvoke(const TokenMessage& msg, TokenController* ctrl)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto args = bp.GetAll();
|
// 传入参数名值对以及结果缓冲区引用,业务失败时返回false并把错误信息放在结果缓冲区
|
||||||
ByteArray result;
|
MemoryStream result;
|
||||||
if(!OnInvoke(action, args, result))
|
if(!OnInvoke(action, bp, result))
|
||||||
{
|
{
|
||||||
rs.SetError(0x02, "操作注册有误");
|
if(result.Position() > 0)
|
||||||
|
rs.SetError(0x03, (cstring)result.GetBuffer());
|
||||||
|
else
|
||||||
|
rs.SetError(0x02, "执行出错");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 执行成功
|
// 执行成功
|
||||||
|
result.SetPosition(0);
|
||||||
|
|
||||||
BinaryPair bprs(ms);
|
BinaryPair bprs(ms);
|
||||||
bprs.Set("Result", result);
|
bprs.Set("Result", Buffer(result.GetBuffer(), result.Length));
|
||||||
|
|
||||||
// 数据流可能已经扩容
|
// 数据流可能已经扩容
|
||||||
rs.Data = ms.GetBuffer();
|
rs.Data = ms.GetBuffer();
|
||||||
|
@ -785,19 +790,36 @@ void TokenClient::OnInvoke(const TokenMessage& msg, TokenController* ctrl)
|
||||||
ctrl->Reply(rs);
|
ctrl->Reply(rs);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TokenClient::OnInvoke(const String& action, const Dictionary& args, Buffer& result)
|
bool TokenClient::OnInvoke(const String& action, const BinaryPair& args, Stream& result)
|
||||||
{
|
{
|
||||||
void* handler = nullptr;
|
void* ps = nullptr;
|
||||||
if(!Routes.TryGetValue(action.GetBuffer(), handler) || !handler) return false;
|
if(!Routes.TryGetValue(action.GetBuffer(), ps) || !ps) return false;
|
||||||
|
|
||||||
return ((InvokeHandler)handler)(args, result);
|
auto ps2 = (int*)ps;
|
||||||
|
auto inv = (InvokeHandler)ps2[0];
|
||||||
|
auto param = (void*)ps2[1];
|
||||||
|
|
||||||
|
return inv(param, args, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TokenClient::Register(const String& action, InvokeHandler handler)
|
void TokenClient::Register(const String& action, InvokeHandler handler, void* param)
|
||||||
{
|
{
|
||||||
auto act = action.GetBuffer();
|
auto act = action.GetBuffer();
|
||||||
if(handler)
|
if(handler)
|
||||||
Routes.Add(act, (void*)handler);
|
{
|
||||||
|
int* ps = new int[2];
|
||||||
|
ps[0] = (int)handler;
|
||||||
|
ps[1] = (int)param;
|
||||||
|
Routes.Add(act, (void*)ps);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
void* ps = nullptr;
|
||||||
|
if(Routes.TryGetValue(act, ps))
|
||||||
|
{
|
||||||
|
delete (int*)ps;
|
||||||
|
|
||||||
Routes.Remove(act);
|
Routes.Remove(act);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -68,9 +68,16 @@ public:
|
||||||
// 远程调用
|
// 远程调用
|
||||||
void Invoke(const String& action, const Buffer& bs);
|
void Invoke(const String& action, const Buffer& bs);
|
||||||
|
|
||||||
|
// 远程调用委托。传入参数名值对以及结果缓冲区引用,业务失败时返回false并把错误信息放在结果缓冲区
|
||||||
|
typedef bool (*InvokeHandler)(void* param, const BinaryPair& args, Stream& result);
|
||||||
// 注册远程调用处理器
|
// 注册远程调用处理器
|
||||||
typedef bool (*InvokeHandler)(const Dictionary& args, Buffer& result);
|
void Register(const String& action, InvokeHandler handler, void* param = nullptr);
|
||||||
void Register(const String& action, InvokeHandler handler);
|
// 模版支持成员函数
|
||||||
|
template<typename T>
|
||||||
|
void Register(const String& action, void(T::*func)(const BinaryPair&, Stream&), T* target)
|
||||||
|
{
|
||||||
|
Register(action, *(InvokeHandler*)&func, target);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool OnHello(TokenMessage& msg, TokenController* ctrl);
|
bool OnHello(TokenMessage& msg, TokenController* ctrl);
|
||||||
|
@ -94,7 +101,7 @@ private:
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
public:
|
public:
|
||||||
#endif
|
#endif
|
||||||
bool OnInvoke(const String& action, const Dictionary& args, Buffer& result);
|
bool OnInvoke(const String& action, const BinaryPair& args, Stream& result);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint _task;
|
uint _task;
|
||||||
|
|
Loading…
Reference in New Issue