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