在控制器中使用Delegat2委托
This commit is contained in:
parent
2b0ed1f5c8
commit
46bbdb9557
|
@ -19,6 +19,8 @@ public:
|
|||
void* Method; // 函数指针
|
||||
void* Target; // 参数
|
||||
|
||||
IDelegate& operator=(const IDelegate& dlg) { Bind(dlg.Method, dlg.Target); return *this; }
|
||||
|
||||
protected:
|
||||
void Bind(void* method, void* target = nullptr)
|
||||
{
|
||||
|
@ -82,6 +84,8 @@ public:
|
|||
Delegate2& operator=(Action2 func) { Bind((void*)func); return *this; }
|
||||
Delegate2& operator=(TAction func) { Bind((void*)func); return *this; }
|
||||
|
||||
using IDelegate::operator=;
|
||||
|
||||
// 带目标的全局函数
|
||||
template<typename T>
|
||||
Delegate2(void(*func)(T&, TArg, TArg2), T* target) { Bind((void*)func, target); }
|
||||
|
|
|
@ -15,8 +15,8 @@ Controller::Controller()
|
|||
MinSize = 0;
|
||||
Opened = false;
|
||||
|
||||
Received = nullptr;
|
||||
Param = nullptr;
|
||||
//Received = nullptr;
|
||||
//Param = nullptr;
|
||||
}
|
||||
|
||||
Controller::~Controller()
|
||||
|
@ -134,10 +134,11 @@ bool Controller::OnReceive(Message& msg)
|
|||
TS("Controller::OnReceive");
|
||||
|
||||
// 外部公共消息事件
|
||||
if(Received)
|
||||
/*if(Received)
|
||||
{
|
||||
if(!Received(this, msg, Param)) return true;
|
||||
}
|
||||
}*/
|
||||
Received(msg, *this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -40,8 +40,8 @@ public:
|
|||
virtual bool Reply(Message& msg);
|
||||
|
||||
// 收到消息时触发
|
||||
MessageHandler Received;
|
||||
void* Param;
|
||||
Delegate2<Message&, Controller&> Received;
|
||||
//void* Param;
|
||||
|
||||
protected:
|
||||
bool SendInternal(const Message& msg);
|
||||
|
|
|
@ -11,7 +11,7 @@ TinyClient* TinyClient::Current = nullptr;
|
|||
|
||||
static void TinyClientTask(void* param);
|
||||
//static void TinyClientReset();
|
||||
static void GetDeviceKey(byte id, Buffer& key, void* param);
|
||||
//static void GetDeviceKey(byte id, Buffer& key, void* param);
|
||||
|
||||
/******************************** 初始化和开关 ********************************/
|
||||
|
||||
|
@ -20,7 +20,7 @@ TinyClient::TinyClient(TinyController* control)
|
|||
assert_ptr(control);
|
||||
|
||||
Control = control;
|
||||
Control->GetKey = GetDeviceKey;
|
||||
Control->GetKey = Delegate2<byte, Buffer&>(&TinyClient::GetDeviceKey, this);
|
||||
|
||||
Opened = false;
|
||||
Joining = false;
|
||||
|
@ -44,8 +44,9 @@ void TinyClient::Open()
|
|||
{
|
||||
if(Opened) return;
|
||||
|
||||
Control->Received = [](void* s, Message& msg, void* p){ return ((TinyClient*)p)->OnReceive((TinyMessage&)msg); };
|
||||
Control->Param = this;
|
||||
// 使用另一个强类型参数的委托,事件函数里面不再需要做类型
|
||||
Control->Received = Delegate2<TinyMessage&, TinyController&>(&TinyClient::OnReceive, this);
|
||||
//Control->Param = this;
|
||||
|
||||
TranID = (int)Sys.Ms();
|
||||
|
||||
|
@ -78,8 +79,8 @@ void TinyClient::Close()
|
|||
|
||||
Sys.RemoveTask(_TaskID);
|
||||
|
||||
Control->Received = nullptr;
|
||||
Control->Param = nullptr;
|
||||
//Control->Received = nullptr;
|
||||
//Control->Param = nullptr;
|
||||
|
||||
Control->Close();
|
||||
|
||||
|
@ -114,10 +115,10 @@ bool TinyClient::Reply(TinyMessage& msg)
|
|||
return Control->Reply(msg);
|
||||
}
|
||||
|
||||
bool TinyClient::OnReceive(TinyMessage& msg)
|
||||
void TinyClient::OnReceive(TinyMessage& msg, TinyController& ctrl)
|
||||
{
|
||||
// 不是组网消息。不是被组网网关消息,不受其它消息设备控制.
|
||||
if(msg.Code != 0x01 && Server != msg.Src) return true;
|
||||
if(msg.Code != 0x01 && Server != msg.Src) return;
|
||||
|
||||
if(msg.Src == Server) LastActive = Sys.Ms();
|
||||
|
||||
|
@ -143,9 +144,7 @@ bool TinyClient::OnReceive(TinyMessage& msg)
|
|||
}
|
||||
|
||||
// 消息转发
|
||||
if(Received) return Received(this, msg, Param);
|
||||
|
||||
return true;
|
||||
if(Received) Received(this, msg, Param);
|
||||
}
|
||||
|
||||
/******************************** 数据区 ********************************/
|
||||
|
@ -294,16 +293,14 @@ void TinyClientTask(void* param)
|
|||
if(client->Server != 0) client->Ping();
|
||||
}
|
||||
|
||||
void GetDeviceKey(byte id, Buffer& key, void* param)
|
||||
void TinyClient::GetDeviceKey(byte id, Buffer& key)
|
||||
{
|
||||
/*TS("TinyClient::GetDeviceKey");
|
||||
//debug_printf("微网客户端获取密钥");
|
||||
TS("TinyClient::GetDeviceKey");
|
||||
|
||||
auto client = (TinyClient*)param;
|
||||
if(Sys.Version < 0xFFFF) return;
|
||||
//if(Sys.Version < 0xFFFF) return;
|
||||
|
||||
//key = client->Password;
|
||||
key.Copy(client->Password, 8);*/
|
||||
//key = Password;
|
||||
//key.Copy(Password, 8);
|
||||
}
|
||||
|
||||
// 组网消息,告诉大家我在这
|
||||
|
|
|
@ -33,7 +33,7 @@ public:
|
|||
// 发送消息
|
||||
bool Send(TinyMessage& msg);
|
||||
bool Reply(TinyMessage& msg);
|
||||
bool OnReceive(TinyMessage& msg);
|
||||
void OnReceive(TinyMessage& msg, TinyController& ctrl);
|
||||
|
||||
// 收到功能消息时触发
|
||||
MessageHandler Received;
|
||||
|
@ -59,6 +59,8 @@ private:
|
|||
void OnWrite(const TinyMessage& msg);
|
||||
void OnRead(const TinyMessage& msg);
|
||||
|
||||
void GetDeviceKey(byte id, Buffer& key);
|
||||
|
||||
// 常用系统级消息
|
||||
public:
|
||||
// 组网
|
||||
|
|
|
@ -324,7 +324,7 @@ bool TinyController::Valid(const Message& _msg)
|
|||
if(msg.Dest == Address)
|
||||
{
|
||||
ByteArray key(0);
|
||||
GetKey(msg.Src, key, Param);
|
||||
GetKey(msg.Src, key);
|
||||
if(key.Length() > 0) Encrypt(msg, key);
|
||||
}
|
||||
|
||||
|
@ -418,7 +418,7 @@ bool TinyController::Send(Message& _msg)
|
|||
if(!msg.Reply) msg.Seq = ++_Sequence;
|
||||
|
||||
ByteArray key;
|
||||
GetKey(msg.Dest, key, Param);
|
||||
GetKey(msg.Dest, key);
|
||||
if(key.Length() > 0) Encrypt(msg, key);
|
||||
|
||||
#if MSG_DEBUG
|
||||
|
|
|
@ -90,7 +90,8 @@ public:
|
|||
void Loop();
|
||||
|
||||
// 获取密钥的回调
|
||||
void (*GetKey)(byte id, Buffer& key, void* param);
|
||||
//void (*GetKey)(byte id, Buffer& key, void* param);
|
||||
Delegate2<byte, Buffer&> GetKey;
|
||||
|
||||
public:
|
||||
// 统计。平均值=(LastCost + TotalCost)/(LastSend + TotalSend)。每一组完成以后,TotalXXX整体复制给LastXXX
|
||||
|
|
|
@ -13,8 +13,8 @@
|
|||
|
||||
/******************************** TinyServer ********************************/
|
||||
|
||||
static bool OnServerReceived(void* sender, Message& msg, void* param);
|
||||
static void GetDeviceKey(byte scr, Buffer& key,void* param);
|
||||
//static bool OnServerReceived(void* sender, Message& msg, void* param);
|
||||
//static void GetDeviceKey(byte scr, Buffer& key,void* param);
|
||||
|
||||
|
||||
TinyServer::TinyServer(TinyController* control)
|
||||
|
@ -23,9 +23,9 @@ TinyServer::TinyServer(TinyController* control)
|
|||
Cfg = nullptr;
|
||||
DeviceType = Sys.Code;
|
||||
|
||||
Control->Received = OnServerReceived;
|
||||
Control->GetKey = GetDeviceKey;
|
||||
Control->Param = this;
|
||||
Control->Received = Delegate2<TinyMessage&, TinyController&>(&TinyServer::OnReceive, this);
|
||||
Control->GetKey = Delegate2<byte, Buffer&>(&TinyServer::GetDeviceKey, this);
|
||||
//Control->Param = this;
|
||||
|
||||
Control->Mode = 2; // 服务端接收所有消息
|
||||
|
||||
|
@ -50,14 +50,14 @@ bool TinyServer::Send(Message& msg) const
|
|||
return Control->Send(msg);
|
||||
}
|
||||
|
||||
bool OnServerReceived(void* sender, Message& msg, void* param)
|
||||
/*bool OnServerReceived(void* sender, Message& msg, void* param)
|
||||
{
|
||||
auto server = (TinyServer*)param;
|
||||
assert_ptr(server);
|
||||
|
||||
// 消息转发
|
||||
return server->OnReceive((TinyMessage&)msg);
|
||||
}
|
||||
}*/
|
||||
|
||||
// 常用系统级消息
|
||||
|
||||
|
@ -103,7 +103,7 @@ void TinyServer::Start()
|
|||
}
|
||||
|
||||
// 收到本地无线网消息
|
||||
bool TinyServer::OnReceive(TinyMessage& msg)
|
||||
void TinyServer::OnReceive(TinyMessage& msg, TinyController& ctrl)
|
||||
{
|
||||
TS("TinyServer::OnReceive");
|
||||
|
||||
|
@ -112,23 +112,23 @@ bool TinyServer::OnReceive(TinyMessage& msg)
|
|||
auto dv = Current;
|
||||
if (!dv) dv = DevMgmt.FindDev(id);
|
||||
// 不响应不在设备列表设备的 非Join指令
|
||||
if(!dv && msg.Code > 2) return false;
|
||||
if(!dv && msg.Code > 2) return;
|
||||
|
||||
switch(msg.Code)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
if (!OnJoin(msg)) return false;
|
||||
if (!OnJoin(msg)) return;
|
||||
dv = Current;
|
||||
DevMgmt.DeviceRequest(DeviceAtions::Online, dv);
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
if (!OnDisjoin(msg))return false;
|
||||
if (!OnDisjoin(msg))return;
|
||||
DevMgmt.DeviceRequest(DeviceAtions::Delete, dv);
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
|
||||
case 3:
|
||||
|
@ -167,11 +167,9 @@ bool TinyServer::OnReceive(TinyMessage& msg)
|
|||
if(msg.Code == 0x05 || msg.Code == 0x06) msg.Code |= 0x10;
|
||||
|
||||
// 消息转发
|
||||
if(Received) return Received(this, msg, Param);
|
||||
if(Received) Received(this, msg, Param);
|
||||
|
||||
Current = nullptr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 分发外网过来的消息。返回值表示是否有响应
|
||||
|
@ -608,21 +606,22 @@ void TinyServer::SetChannel(byte channel)
|
|||
}
|
||||
}
|
||||
|
||||
void GetDeviceKey(byte scr, Buffer& key, void* param)
|
||||
void TinyServer::GetDeviceKey(byte id, Buffer& key)
|
||||
{
|
||||
/*TS("TinyServer::GetDeviceKey");
|
||||
|
||||
auto server = (TinyServer*)param;
|
||||
auto devMgmt = &(server->DevMgmt);
|
||||
|
||||
auto dv = devMgmt->FindDev(scr);
|
||||
auto dv = devMgmt->FindDev(id);
|
||||
if(!dv) return;
|
||||
|
||||
// 检查版本
|
||||
if(dv->Version < 0x00AA) return;
|
||||
|
||||
// debug_printf("%d 设备获取密匙\n",scr);
|
||||
key.Copy(dv->Pass, 8);*/
|
||||
//key.Copy(dv->Pass, 8);
|
||||
key = dv->Pass;*/
|
||||
}
|
||||
|
||||
void TinyServer::ClearDevices()
|
||||
|
|
|
@ -26,7 +26,7 @@ public:
|
|||
bool Send(Message& msg) const;
|
||||
//bool Reply(Message& msg) const;
|
||||
// 收到本地无线网消息
|
||||
bool OnReceive(TinyMessage& msg);
|
||||
void OnReceive(TinyMessage& msg, TinyController& ctrl);
|
||||
// 分发外网过来的消息。返回值表示是否有响应
|
||||
bool Dispatch(TinyMessage& msg);
|
||||
|
||||
|
@ -69,6 +69,9 @@ public:
|
|||
// 写入
|
||||
bool OnWrite(const Message& msg, Message& rs, Device& dv);
|
||||
bool OnWriteReply(const Message& msg, Device& dv);
|
||||
|
||||
private:
|
||||
void GetDeviceKey(byte id, Buffer& key);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
#include "Security\RC4.h"
|
||||
|
||||
static bool OnTokenClientReceived(void* sender, Message& msg, void* param);
|
||||
//static bool OnTokenClientReceived(void* sender, Message& msg, void* param);
|
||||
|
||||
static void LoopTask(void* param);
|
||||
static void BroadcastHelloTask(void* param);
|
||||
|
@ -44,8 +44,10 @@ void TokenClient::Open()
|
|||
TS("TokenClient::Open");
|
||||
assert(Control, "令牌客户端还没设置控制器呢");
|
||||
|
||||
Control->Received = OnTokenClientReceived;
|
||||
Control->Param = this;
|
||||
// 使用另一个强类型参数的委托,事件函数里面不再需要做类型
|
||||
Delegate2<TokenMessage&, TokenController&> dlg(&TokenClient::OnReceive, this);
|
||||
Control->Received = dlg;
|
||||
//Control->Param = this;
|
||||
Control->Open();
|
||||
|
||||
//auto ctrl = Control;
|
||||
|
@ -54,8 +56,8 @@ void TokenClient::Open()
|
|||
// 向服务端握手时,汇报内网本地端口,用户端将会通过该端口连接
|
||||
//ctrl = Local;
|
||||
|
||||
Local->Received = OnTokenClientReceived;
|
||||
Local->Param = this;
|
||||
Local->Received = Control->Received;
|
||||
//Local->Param = this;
|
||||
Local->Open();
|
||||
}
|
||||
|
||||
|
@ -111,7 +113,7 @@ bool TokenClient::Reply(TokenMessage& msg, TokenController* ctrl)
|
|||
return ctrl->Reply(msg);
|
||||
}
|
||||
|
||||
bool TokenClient::OnReceive(TokenMessage& msg, TokenController* ctrl)
|
||||
void TokenClient::OnReceive(TokenMessage& msg, TokenController& ctrl)
|
||||
{
|
||||
TS("TokenClient::OnReceive");
|
||||
|
||||
|
@ -121,49 +123,47 @@ bool TokenClient::OnReceive(TokenMessage& msg, TokenController* ctrl)
|
|||
{
|
||||
case 0x01:
|
||||
if(msg.Reply)
|
||||
OnHello(msg, ctrl);
|
||||
OnHello(msg, &ctrl);
|
||||
else
|
||||
OnLocalHello(msg, ctrl);
|
||||
OnLocalHello(msg, &ctrl);
|
||||
break;
|
||||
case 0x02:
|
||||
if(msg.Reply)
|
||||
OnLogin(msg, ctrl);
|
||||
OnLogin(msg, &ctrl);
|
||||
else
|
||||
OnLocalLogin(msg, ctrl);
|
||||
OnLocalLogin(msg, &ctrl);
|
||||
break;
|
||||
case 0x03:
|
||||
OnPing(msg, ctrl);
|
||||
OnPing(msg, &ctrl);
|
||||
break;
|
||||
case 0x07:
|
||||
OnRegister(msg, ctrl);
|
||||
OnRegister(msg, &ctrl);
|
||||
break;
|
||||
case 0x08:
|
||||
OnInvoke(msg, ctrl);
|
||||
OnInvoke(msg, &ctrl);
|
||||
break;
|
||||
}
|
||||
// todo 握手登录心跳消息不需要转发
|
||||
if(msg.Code < 0x03) return true;
|
||||
if(msg.Code < 0x03) return;
|
||||
|
||||
// 消息转发
|
||||
if (Received)
|
||||
{
|
||||
Received(ctrl, msg, Param);
|
||||
Received(&ctrl, msg, Param);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (msg.Code)
|
||||
{
|
||||
case 0x05: OnRead(msg, ctrl); break;
|
||||
case 0x06: OnWrite(msg, ctrl); break;
|
||||
case 0x05: OnRead(msg, &ctrl); break;
|
||||
case 0x06: OnWrite(msg, &ctrl); break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OnTokenClientReceived(void* sender, Message& msg, void* param)
|
||||
/*bool OnTokenClientReceived(void* sender, Message& msg, void* param)
|
||||
{
|
||||
auto ctrl = (TokenController*)sender;
|
||||
assert_ptr(ctrl);
|
||||
|
@ -171,7 +171,7 @@ bool OnTokenClientReceived(void* sender, Message& msg, void* param)
|
|||
assert_ptr(client);
|
||||
|
||||
return client->OnReceive((TokenMessage&)msg, ctrl);
|
||||
}
|
||||
}*/
|
||||
|
||||
// 常用系统级消息
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ public:
|
|||
// 发送消息
|
||||
bool Send(TokenMessage& msg, TokenController* ctrl = nullptr);
|
||||
bool Reply(TokenMessage& msg, TokenController* ctrl = nullptr);
|
||||
bool OnReceive(TokenMessage& msg, TokenController* ctrl);
|
||||
void OnReceive(TokenMessage& msg, TokenController& ctrl);
|
||||
|
||||
// 收到功能消息时触发
|
||||
MessageHandler Received;
|
||||
|
|
Loading…
Reference in New Issue