Invoke单元测试通过
This commit is contained in:
parent
4e8373e094
commit
bf95430002
|
@ -15,6 +15,9 @@
|
|||
|
||||
#include "App\FlushPort.h"
|
||||
|
||||
bool SetWiFi(const BinaryPair& args, BinaryPair& result);
|
||||
void SetWiFiTask(void* param);
|
||||
|
||||
static FlushPort* CreateFlushPort(OutputPort* led)
|
||||
{
|
||||
auto fp = new FlushPort();
|
||||
|
@ -122,6 +125,8 @@ ISocketHost* AP0801::Create8266(Action onNetReady)
|
|||
if(EthernetLed) net->Led = CreateFlushPort(EthernetLed);
|
||||
net->NetReady = onNetReady;
|
||||
|
||||
Sys.AddTask(SetWiFiTask, this, 0, -1, "SetWiFi");
|
||||
|
||||
net->OpenAsync();
|
||||
|
||||
Host = net;
|
||||
|
@ -202,7 +207,55 @@ TokenClient* AP0801::CreateClient()
|
|||
client->Local = token2;
|
||||
}
|
||||
|
||||
return client;
|
||||
return Client = client;
|
||||
}
|
||||
|
||||
bool SetWiFi(const BinaryPair& args, BinaryPair& result)
|
||||
{
|
||||
ByteArray rs;
|
||||
|
||||
String ssid;
|
||||
String pass;
|
||||
|
||||
if(!args.Get("ssid", ssid)) return false;
|
||||
if(!args.Get("pass", pass)) return false;
|
||||
|
||||
//todo 保存WiFi信息
|
||||
//auto esp = (ESP8266*)
|
||||
|
||||
result.Set("Result", (byte)1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SetWiFiTask(void* param)
|
||||
{
|
||||
auto ap = (AP0801*)param;
|
||||
auto client = ap->Client;
|
||||
|
||||
client->Register("SetWiFi", SetWiFi);
|
||||
|
||||
#if DEBUG
|
||||
MemoryStream ms1;
|
||||
MemoryStream ms2;
|
||||
|
||||
BinaryPair bp(ms1);
|
||||
bp.Set("ssid", "yws007");
|
||||
bp.Set("pass", "yws52718");
|
||||
|
||||
BinaryPair args(ms1);
|
||||
BinaryPair result(ms2);
|
||||
|
||||
client->OnInvoke("SetWiFi", args, result);
|
||||
|
||||
byte rt = 0;
|
||||
bool rs = result.Get("Result", rt);
|
||||
|
||||
assert(rs, "rs");
|
||||
assert(rt == 1, "rt");
|
||||
|
||||
debug_printf("Invoke测试通过");
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************** 2401 ********************************/
|
||||
|
|
|
@ -18,6 +18,8 @@ public:
|
|||
|
||||
ISocketHost* Host; // 网络主机
|
||||
|
||||
TokenClient* Client;
|
||||
|
||||
AP0801();
|
||||
|
||||
void Setup(ushort code, cstring name, COM message = COM1, int baudRate = 0);
|
||||
|
|
|
@ -750,42 +750,56 @@ void TokenClient::Invoke(const String& action, const Buffer& bs)
|
|||
void TokenClient::OnInvoke(const TokenMessage& msg, TokenController* ctrl)
|
||||
{
|
||||
auto rs = msg.CreateReply();
|
||||
auto ms = rs.ToStream();
|
||||
// 考虑到结果可能比较大,允许扩容
|
||||
ms.CanResize = true;
|
||||
//auto ms = rs.ToStream();
|
||||
//ms.CanResize = true;
|
||||
MemoryStream ms(rs.Data, rs.Length);
|
||||
|
||||
BinaryPair bp(msg.ToStream());
|
||||
BinaryPair args(msg.ToStream());
|
||||
|
||||
String action;
|
||||
if(!bp.Get("Action", action) || !action)
|
||||
if(!args.Get("Action", action) || !action)
|
||||
{
|
||||
rs.SetError(0x01, "请求错误");
|
||||
}
|
||||
else
|
||||
{
|
||||
void* handler = nullptr;
|
||||
if(!Routes.TryGetValue(action.GetBuffer(), handler) || !handler)
|
||||
BinaryPair bprs(ms);
|
||||
if(!OnInvoke(action, args, bprs))
|
||||
{
|
||||
rs.SetError(0x02, "操作注册有误");
|
||||
}
|
||||
else
|
||||
{
|
||||
auto ivh = (InvokeHandler)handler;
|
||||
// 执行成功
|
||||
|
||||
BinaryPair result(ms);
|
||||
if(!ivh(bp, result))
|
||||
{
|
||||
rs.SetError(0x03, "执行出错");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 执行成功
|
||||
// 数据流可能已经扩容
|
||||
rs.Data = ms.GetBuffer();
|
||||
rs.Length = ms.Position();
|
||||
}
|
||||
// 数据流可能已经扩容
|
||||
rs.Data = ms.GetBuffer();
|
||||
rs.Length = ms.Position();
|
||||
}
|
||||
}
|
||||
|
||||
ctrl->Reply(rs);
|
||||
}
|
||||
|
||||
bool TokenClient::OnInvoke(const String& action, const BinaryPair& args, BinaryPair& result)
|
||||
{
|
||||
void* handler = nullptr;
|
||||
if(!Routes.TryGetValue(action.GetBuffer(), handler) || !handler) return false;
|
||||
|
||||
auto rs = ((InvokeHandler)handler)(args, result);
|
||||
if(!rs) return false;
|
||||
|
||||
//BinaryPair bprs(ms);
|
||||
//bprs.Set("Result", bs);
|
||||
return true;
|
||||
}
|
||||
|
||||
void TokenClient::Register(const String& action, InvokeHandler handler)
|
||||
{
|
||||
auto act = action.GetBuffer();
|
||||
if(handler)
|
||||
Routes.Add(act, (void*)handler);
|
||||
else
|
||||
Routes.Remove(act);
|
||||
}
|
||||
|
|
|
@ -94,6 +94,9 @@ private:
|
|||
void OnWrite(const TokenMessage& msg, TokenController* ctrl);
|
||||
|
||||
void OnInvoke(const TokenMessage& msg, TokenController* ctrl);
|
||||
|
||||
public:
|
||||
bool OnInvoke(const String& action, const BinaryPair& args, BinaryPair& result);
|
||||
};
|
||||
|
||||
// 令牌会话
|
||||
|
|
Loading…
Reference in New Issue