diff --git a/App/Button.cpp b/App/Button.cpp index 5b708b8a..a9a234cd 100644 --- a/App/Button.cpp +++ b/App/Button.cpp @@ -55,7 +55,7 @@ void Button::OnPress(Pin pin, bool down) SetValue(!_Value); //if(_Handler) _Handler(this, _Param); - Press(this); + Press(*this); } } diff --git a/App/Button.h b/App/Button.h index 77bdbbeb..3c7ce67b 100644 --- a/App/Button.h +++ b/App/Button.h @@ -25,7 +25,7 @@ public: OutputPort Led; // 指示灯 OutputPort Relay; // 继电器 - Delegate Press; // 按下事件 + Delegate Press; // 按下事件 public: // 构造函数。指示灯和继电器一般开漏输出,需要倒置 diff --git a/App/Button_GrayLevel.cpp b/App/Button_GrayLevel.cpp index ba20696d..282e8d9f 100644 --- a/App/Button_GrayLevel.cpp +++ b/App/Button_GrayLevel.cpp @@ -120,14 +120,14 @@ void Button_GrayLevel::OnKeyPress(InputPort* port, bool down) } SetValue(!_Value); //if (_Handler) _Handler(this, _Param); - Press(this); + Press(*this); break; case set: Stat = normal; Next = 0xff; SetValue(!_Value); //if (_Handler) _Handler(this, _Param); - Press(this); + Press(*this); break; } } @@ -231,8 +231,7 @@ bool Button_GrayLevel::SetACZeroPin(Pin aczero) return false; } -void Button_GrayLevel::Init(TIMER tim, byte count, Button_GrayLevel* btns, Action onpress - , const ButtonPin* pins, byte* level, const byte* state) +void Button_GrayLevel::Init(TIMER tim, byte count, Button_GrayLevel* btns, TAction onpress, const ButtonPin* pins, byte* level, const byte* state) { debug_printf("\r\n初始化开关按钮 \r\n"); diff --git a/App/Button_GrayLevel.h b/App/Button_GrayLevel.h index 1da45b98..bd784676 100644 --- a/App/Button_GrayLevel.h +++ b/App/Button_GrayLevel.h @@ -23,6 +23,9 @@ enum ButtonStat :byte execution = 3, // 执行设置 }; +class Button_GrayLevel; +using TAction = Delegate::Action; + // 面板按钮 // 这里必须使用_packed关键字,生成对齐的代码,否则_Value只占一个字节,导致后面的成员进行内存操作时错乱 //__packed class Button @@ -45,7 +48,7 @@ public: void * ExterSetParam = nullptr; public: - Delegate Press; + Delegate Press; // 构造函数。指示灯和继电器一般开漏输出,需要倒置 Button_GrayLevel(); @@ -82,7 +85,7 @@ public: static byte OnGrayLevel; // 开灯时 led 灰度 static byte OffGrayLevel; // 关灯时 led 灰度 - static void Init(TIMER tim, byte count, Button_GrayLevel* btns, Action onpress, const ButtonPin* pins, byte* level, const byte* state); + static void Init(TIMER tim, byte count, Button_GrayLevel* btns, TAction onpress, const ButtonPin* pins, byte* level, const byte* state); static void InitZero(Pin zero, int us = 2300); static bool UpdateLevel(byte* level, Button_GrayLevel* btns, byte count); diff --git a/App/Button_magnetic.cpp b/App/Button_magnetic.cpp index 8244fff9..b9489726 100644 --- a/App/Button_magnetic.cpp +++ b/App/Button_magnetic.cpp @@ -73,7 +73,7 @@ void Button_magnetic::OnPress(Pin pin, bool down) SetValue(!_Value); //if(_Handler) _Handler(this, _Param); - Press(); + Press(*this); } } diff --git a/App/Button_magnetic.h b/App/Button_magnetic.h index b8c28336..73466277 100644 --- a/App/Button_magnetic.h +++ b/App/Button_magnetic.h @@ -25,7 +25,7 @@ private: public: cstring Name; - Delegate Press; + Delegate Press; InputPort* Key; // 输入按键 OutputPort* Led; // 指示灯 diff --git a/App/Sensor.cpp b/App/Sensor.cpp index 2db620dc..46a35318 100644 --- a/App/Sensor.cpp +++ b/App/Sensor.cpp @@ -65,7 +65,7 @@ void Sensor::OnPress(Pin pin, bool down) SetValue(!_Value); //if(_Handler) _Handler(this, _Param); - Press(); + Press(*this); } } diff --git a/App/Sensor.h b/App/Sensor.h index 91990ea7..a312f429 100644 --- a/App/Sensor.h +++ b/App/Sensor.h @@ -21,7 +21,7 @@ public: //I2C* Ifrared //红外转发 - Delegate Press; + Delegate Press; // 构造函数。指示灯和继电器一般开漏输出,需要倒置 Sensor() { Init(); } diff --git a/App/Sound.cpp b/App/Sound.cpp index 6f8b76e3..f006c1b9 100644 --- a/App/Sound.cpp +++ b/App/Sound.cpp @@ -49,7 +49,7 @@ void Music::Sound() { _timer->SetFrequency(100000); //_timer->Register(TimerHander, this); - _timer->Register(Delegate(&Music::TimerHander, this)); + _timer->Register(Delegate(&Music::TimerHander, this)); _timer->Open(); Sounding = true; } @@ -83,7 +83,7 @@ bool Music::getStat() return Sounding; } -void Music::TimerHander(Timer* timer) +void Music::TimerHander(Timer& timer) { /*if(param == nullptr)return; Music * music = (Music * )param; diff --git a/App/Sound.h b/App/Sound.h index c7f616af..71495fea 100644 --- a/App/Sound.h +++ b/App/Sound.h @@ -37,7 +37,7 @@ private: volatile int sound_cnt; volatile int music_freq; volatile int music_beat; - void TimerHander(Timer* timer); + void TimerHander(Timer& timer); void phonate(); }; diff --git a/Board/AP0104.cpp b/Board/AP0104.cpp index 15733b22..bae8c305 100644 --- a/Board/AP0104.cpp +++ b/Board/AP0104.cpp @@ -147,10 +147,8 @@ static void On_DHCP_Ready(void* param) if (_DHCP_Ready) _DHCP_Ready(param); } -static void OnDhcpStop(void* sender, void* param) +static void OnDhcpStop(Dhcp& dhcp) { - auto& dhcp = *(Dhcp*)sender; - // DHCP成功,或者失败且超过最大错误次数,都要启动网关,让它以上一次配置工作 if (dhcp.Result || dhcp.Times >= dhcp.MaxTimes) { diff --git a/Board/AP0801.cpp b/Board/AP0801.cpp index 6b897737..0d06fad2 100644 --- a/Board/AP0801.cpp +++ b/Board/AP0801.cpp @@ -150,10 +150,8 @@ static void On_DHCP_Ready(void* param) if(_DHCP_Ready) _DHCP_Ready(param); } -static void OnDhcpStop(void* sender, void* param) +static void OnDhcpStop(Dhcp& dhcp) { - auto& dhcp = *(Dhcp*)sender; - // DHCP成功,或者失败且超过最大错误次数,都要启动网关,让它以上一次配置工作 if(dhcp.Result || dhcp.Times >= dhcp.MaxTimes) { diff --git a/Board/IOK027X.cpp b/Board/IOK027X.cpp index 9be8f6d0..0c102bdd 100644 --- a/Board/IOK027X.cpp +++ b/Board/IOK027X.cpp @@ -76,10 +76,8 @@ static void On_DHCP_Ready(void* param) if(_DHCP_Ready) _DHCP_Ready(param); } -static void OnDhcpStop(void* sender, void* param) +static void OnDhcpStop(Dhcp& dhcp) { - auto& dhcp = *(Dhcp*)sender; - // DHCP成功,或者失败且超过最大错误次数,都要启动网关,让它以上一次配置工作 if(dhcp.Result || dhcp.Times >= dhcp.MaxTimes) { diff --git a/Board/Pandora.cpp b/Board/Pandora.cpp index b1ec2e6f..e1d35202 100644 --- a/Board/Pandora.cpp +++ b/Board/Pandora.cpp @@ -107,10 +107,8 @@ static void On_DHCP_Ready(void* param) if(_DHCP_Ready) _DHCP_Ready(param); } -static void OnDhcpStop(void* sender, void* param) +static void OnDhcpStop(Dhcp& dhcp) { - auto& dhcp = *(Dhcp*)sender; - // DHCP成功,或者失败且超过最大错误次数,都要启动网关,让它以上一次配置工作 if(dhcp.Result || dhcp.Times >= dhcp.MaxTimes) { diff --git a/Core/Delegate.cpp b/Core/Delegate.cpp index 5b06599c..9e9e50c5 100644 --- a/Core/Delegate.cpp +++ b/Core/Delegate.cpp @@ -5,51 +5,13 @@ #include "Delegate.h" /************************************************ Delegate ************************************************/ -Delegate::Delegate() +/*Delegate::Delegate() { Method = nullptr; Target = nullptr; } -Delegate::Delegate(void* func) { Method = (void*)func; Target = nullptr; } -Delegate::Delegate(void* func, void* target){ Method = (void*)func; Target = target; } -Delegate::Delegate(Func func) { Method = (void*)func; Target = nullptr; } Delegate::Delegate(Action func) { Method = (void*)func; Target = nullptr; } -Delegate::Delegate(Action2 func){ Method = (void*)func; Target = nullptr; } -Delegate::Delegate(Action3 func){ Method = (void*)func; Target = nullptr; } -Delegate& Delegate::operator=(void* func) { Method = (void*)func; return *this; } -Delegate& Delegate::operator=(Func func) { Method = (void*)func; return *this; } Delegate& Delegate::operator=(Action func) { Method = (void*)func; return *this; } -Delegate& Delegate::operator=(Action2 func) { Method = (void*)func; return *this; } -Delegate& Delegate::operator=(Action3 func) { Method = (void*)func; return *this; } - -void Delegate::operator()() -{ - if(!Method) return; - - if(Target) - ((Action)Method)(Target); - else - ((Func)Method)(); -} - -void Delegate::operator()(void* arg) -{ - if(!Method) return; - - if(Target) - ((Action2)Method)(Target, arg); - else - ((Action)Method)(arg); -} - -void Delegate::operator()(void* arg, void* arg2) -{ - if(!Method) return; - - if(Target) - ((Action3)Method)(Target, arg, arg2); - else - ((Action2)Method)(arg, arg2); -} +*/ diff --git a/Core/Delegate.h b/Core/Delegate.h index ab89196b..9e84e240 100644 --- a/Core/Delegate.h +++ b/Core/Delegate.h @@ -12,74 +12,171 @@ typedef void (*EventHandler)(void* sender, void* param); // 传入数据缓冲区地址和长度,如有反馈,仍使用该缓冲区,返回数据长度 typedef uint (*DataHandler)(void* sender, byte* buf, uint size, void* param); -// 事件处理器 +// 委托。第一参数目标对象指针,第二泛型参数 +template class Delegate { public: + typedef void(*Action)(TArg); + void* Method; // 函数指针 void* Target; // 参数 - Delegate(); - Delegate(const Delegate& dlg) = delete; - Delegate(void* func); - Delegate(void* func, void* target); - Delegate(Func func); - Delegate(Action func); - Delegate(Action2 func); - Delegate(Action3 func); - - template - Delegate(void(T::*func)(), T* target) { Method = (void*)&func; Target = target; } - template - Delegate(void(T::*func)(TArg), T* target) { Method = (void*)&func; Target = target; } - template - Delegate(void(T::*func)(TArg, TArg2), T* target) { Method = (void*)&func; Target = target; } - - Delegate& operator=(void* func); - Delegate& operator=(Func func); - Delegate& operator=(Action func); - Delegate& operator=(Action2 func); - Delegate& operator=(Action3 func); - - void operator()(); - void operator()(void* arg); - void operator()(void* arg, void* arg2); - template - void operator()() + Delegate() { - if(Method) - { - auto obj = (T*)Target; - typedef void(T::*TAction)(); - auto act = *(TAction*)Method; - - (obj->*act)(); - } + Method = nullptr; + Target = nullptr; } - template + Delegate(const Delegate& dlg) = delete; + Delegate(Action func) // 全局函数或类静态函数 + { + Method = (void*)func; + Target = nullptr; + } + template + Delegate(void(*func)(T&, TArg), T* target) + { + Method = (void*)func; + Target = target; + } + + template + Delegate(void(T::*func)(TArg), T* target) { Method = (void*)&func; Target = target; } + + Delegate& operator=(Action func) // 全局函数或类静态函数 + { + Method = (void*)func; + Target = nullptr; + + return *this; + } + void operator()(TArg arg) { if(Method) { - auto obj = (T*)Target; - typedef void(T::*TAction)(TArg); - auto act = *(TAction*)Method; + if(Target) + { + typedef void(*TAction)(void*, TArg); + auto act = *(TAction*)Method; - (obj->*act)(arg); + (*act)(Target, arg); + } + else + { + //typedef void(*TAction)(TArg); + auto act = *(Action*)Method; + + (*act)(arg); + } } } - template - void operator()(TArg arg, TArg arg2) +}; + +//*************************************************************************** +// 函数模版接口 +template +class ifunction +{ +public: + // 函数参数的类型 + typedef TParameter parameter_type; + + // 将被重载的函数操作 + virtual void operator ()(TParameter) const = 0; +}; + +// 无参函数模版接口 +template <> +class ifunction +{ +public: + typedef void parameter_type; + + virtual void operator ()() const = 0; +}; + +// 对象函数模版 +template +class function : public ifunction +{ +public: + typedef TObject object_type; // 对象类型 + typedef TParameter parameter_type; // 函数参数的类型 + + function(TObject& object, void(TObject::* p_function)(TParameter)) + : p_object(&object), + p_function(p_function) { - if(Method) - { - auto obj = (T*)Target; - typedef void(T::*TAction)(TArg, TArg2); - auto act = *(TAction*)Method; - - (obj->*act)(arg, arg2); - } } + + virtual void operator ()(TParameter data) const + { + // 调用对象的成员函数 + (p_object->*p_function)(data); + } + +private: + TObject* p_object; // 对象指针 + void (TObject::* p_function)(TParameter); // 成员函数指针 +}; + +// 对象无参函数模版 +template +class function : public ifunction +{ +public: + function(TObject& object, void(TObject::* p_function)(void)) + : p_object(&object), + p_function(p_function) + { + } + + virtual void operator ()() const + { + (p_object->*p_function)(); + } + +private: + TObject* p_object; + void (TObject::* p_function)(); +}; + +// 全局函数模版 +template +class function : public ifunction +{ +public: + function(void(*p_function)(TParameter)) + : p_function(p_function) + { + } + + virtual void operator ()(TParameter data) const + { + (*p_function)(data); + } + +private: + void (*p_function)(TParameter); +}; + +template <> +class function : public ifunction +{ +public: + function(void(*p_function)(void)) + : p_function(p_function) + { + } + + virtual void operator ()() const + { + (*p_function)(); + } + +private: + void (*p_function)(); }; /* @@ -102,29 +199,4 @@ A* pa=&a; 要调用一个成员函数,仅仅有成员函数指针是不够的,还需要一个对象指针,所以要用一个类将两者绑到一起。 */ -//*************************************************************************** - -// 对象函数模版 -template -class function -{ -public: - void* Method; // 函数指针 - void* Target; // 参数 - - template - void bind(TObject& object, void(TObject::* func)(TArg, TArg2)) - { - Target = &object; - Method = (void*)&func; - } - - virtual void operator ()(TArg arg, TArg2 arg2) const - { - // 调用对象的成员函数 - typedef void(*TFunc)(void*, TArg, TArg2); - ((TFunc)Method)(Target, arg, arg2); - } -}; - #endif //_Delegate_H_ diff --git a/Device/Timer.cpp b/Device/Timer.cpp index b3962b51..a843147e 100644 --- a/Device/Timer.cpp +++ b/Device/Timer.cpp @@ -100,7 +100,7 @@ void Timer::Close() SetHandler(handler != nullptr); }*/ -void Timer::Register(const Delegate& dlg) +void Timer::Register(const Delegate& dlg) { OnTick = dlg; @@ -110,7 +110,7 @@ void Timer::Register(const Delegate& dlg) void Timer::OnInterrupt() { //if(_Handler) _Handler(this, _Param); - OnTick(this); + OnTick(*this); } /*================ PWM ================*/ diff --git a/Device/Timer.h b/Device/Timer.h index fc8bdb2d..a68134bf 100644 --- a/Device/Timer.h +++ b/Device/Timer.h @@ -10,7 +10,7 @@ class Timer { protected: byte _index; // 第几个定时器,从0开始 - Delegate OnTick; // 带this参数 + Delegate OnTick; // 带this参数 void SetHandler(bool set); public: @@ -33,7 +33,7 @@ public: void SetCounter(uint cnt); // 设置计数器值 //void Register(EventHandler handler, void* param = nullptr); - void Register(const Delegate& dlg); + void Register(const Delegate& dlg); static void ClockCmd(int idx, bool state); diff --git a/Drivers/Esp8266/Esp8266.cpp b/Drivers/Esp8266/Esp8266.cpp index 6657ceaf..5da0bc37 100644 --- a/Drivers/Esp8266/Esp8266.cpp +++ b/Drivers/Esp8266/Esp8266.cpp @@ -159,7 +159,7 @@ bool Esp8266::OnOpen() ShowConfig(); } - if(NetReady) NetReady(this); + if(NetReady) NetReady(*this); return true; } diff --git a/Drivers/W5500.cpp b/Drivers/W5500.cpp index 4de8f092..53c84b7d 100644 --- a/Drivers/W5500.cpp +++ b/Drivers/W5500.cpp @@ -745,9 +745,9 @@ IPAddress W5500::QueryDNS(const String& domain) { auto ip = IPAddress::Parse(domain); if(ip != IPAddress::Any()) return ip; - + if(_Dns) return _Dns(this, domain); - + return ip; } @@ -760,20 +760,24 @@ static IPAddress FullQueryDNS(ISocketHost* host, const String& domain) bool W5500::EnableDNS() { _Dns = FullQueryDNS; - + return true; } -static void OnDhcpStop(void* sender, void* param) +static void OnDhcpStopTask(void* param) { - auto& dhcp = *(Dhcp*)sender; + auto& net = *(W5500*)param; + if(net.NetReady) net.NetReady(net); +} +static void OnDhcpStop(W5500& net, Dhcp& dhcp) +{ // DHCP成功,或者失败且超过最大错误次数,都要启动网关,让它以上一次配置工作 if(dhcp.Result || dhcp.Times >= dhcp.MaxTimes) { - auto callback = (Action)param; + //auto callback = (Action)param; // 防止调用栈太深,另外开任务 - if(callback) Sys.AddTask(callback, &dhcp.Host, 0, -1, "网络就绪"); + if(net.NetReady) Sys.AddTask(OnDhcpStopTask, &net, 0, -1, "网络就绪"); } } @@ -781,14 +785,14 @@ static void OnDhcpStop(void* sender, void* param) bool W5500::EnableDHCP() { if(_Dhcp) return true; - + // 打开DHCP auto dhcp = new Dhcp(*this); - dhcp->OnStop = OnDhcpStop; + dhcp->OnStop = Delegate(OnDhcpStop, this); dhcp->Start(); - + _Dhcp = dhcp; - + return true; } diff --git a/Message/Controller.cpp b/Message/Controller.cpp index 8ff586a5..aab60ed6 100644 --- a/Message/Controller.cpp +++ b/Message/Controller.cpp @@ -15,8 +15,8 @@ Controller::Controller() MinSize = 0; Opened = false; - //Received = nullptr; - //Param = nullptr; + Received = nullptr; + Param = nullptr; } Controller::~Controller() @@ -134,7 +134,10 @@ bool Controller::OnReceive(Message& msg) TS("Controller::OnReceive"); // 外部公共消息事件 - Received(msg, *this); + if(Received) + { + if(!Received(this, msg, Param)) return true; + } return true; } diff --git a/Message/Controller.h b/Message/Controller.h index 4bcb65dc..e151beeb 100644 --- a/Message/Controller.h +++ b/Message/Controller.h @@ -40,7 +40,7 @@ public: virtual bool Reply(Message& msg); // 收到消息时触发 - function Received; + MessageHandler Received; void* Param; protected: diff --git a/Net/Dhcp.cpp b/Net/Dhcp.cpp index 1e4c66e5..e843907e 100644 --- a/Net/Dhcp.cpp +++ b/Net/Dhcp.cpp @@ -201,7 +201,7 @@ void Dhcp::Stop() } //if(OnStop) OnStop(this, nullptr); - OnStop(this); + OnStop(*this); } void Dhcp::Loop(void* param) diff --git a/Net/Dhcp.h b/Net/Dhcp.h index 78763842..69ea0f48 100644 --- a/Net/Dhcp.h +++ b/Net/Dhcp.h @@ -33,7 +33,7 @@ public: void Start(); // 开始 void Stop(); // 停止 - Delegate OnStop; // 带this参数 + Delegate OnStop; // 带this参数 private: static uint OnReceive(ITransport* port, Buffer& bs, void* param, void* param2); diff --git a/Net/Socket.h b/Net/Socket.h index 0e3460ea..e7732bc0 100644 --- a/Net/Socket.h +++ b/Net/Socket.h @@ -32,7 +32,7 @@ public: String* SSID; // 无线SSID String* Pass; // 无线密码 - typedef void (*NetReadyHandler)(ISocketHost* host); + typedef void (*NetReadyHandler)(ISocketHost& host); NetReadyHandler NetReady; // 网络准备就绪 ISocketHost(); diff --git a/Test/TimerTest.cpp b/Test/TimerTest.cpp index 69623a32..6c11ca03 100644 --- a/Test/TimerTest.cpp +++ b/Test/TimerTest.cpp @@ -4,15 +4,15 @@ //Timer* timer; -void TimerTask(OutputPort* leds, Timer* timer) +void TimerTask(OutputPort& led, Timer& timer) { - *leds = !*leds; + led = !led; } uint frequency = 0; int step = 1; -void TimerTask2(Timer* timer) +void TimerTask2(Timer& timer) { frequency += step; @@ -27,24 +27,24 @@ void TimerTask2(Timer* timer) step = -1; } - timer->SetFrequency(frequency); + timer.SetFrequency(frequency); } -void TestTimer(OutputPort& leds) +void TestTimer(OutputPort& led) { debug_printf("\r\n"); debug_printf("TestTimer Start......\r\n"); auto timer = new Timer(Timer2); timer->SetFrequency(50); - //timer->Register(TimerTask, &leds); - timer->Register(Delegate((void*)&TimerTask, &leds)); + //timer->Register(TimerTask, &led); + timer->Register(Delegate(TimerTask, &led)); timer->Open(); auto timer2 = Timer::Create(); timer2->SetFrequency(10); //timer2->Register(TimerTask2, nullptr); - timer2->Register(Delegate((void*)&TimerTask2)); + timer2->Register(TimerTask2); timer2->Open(); debug_printf("\r\n TestTimer Finish!\r\n"); diff --git a/TinyNet/Tiny.cpp b/TinyNet/Tiny.cpp index 0ebf085a..bcf16e45 100644 --- a/TinyNet/Tiny.cpp +++ b/TinyNet/Tiny.cpp @@ -234,10 +234,9 @@ bool CheckUserPress(InputPort* port, bool down, void* param) return false; } -void CheckUserPress3(void* sender) +void CheckUserPress3(Button_GrayLevel& btn) { - auto but = (Button_GrayLevel *)sender; - CheckUserPress(&but->Key, but->Key.Read(), nullptr); + CheckUserPress(&btn.Key, btn.Key.Read(), nullptr); } void InitButtonPress(Button_GrayLevel* btns, byte count) diff --git a/TokenNet/Token.cpp b/TokenNet/Token.cpp index d3bf8fab..81381543 100644 --- a/TokenNet/Token.cpp +++ b/TokenNet/Token.cpp @@ -32,10 +32,8 @@ static void StartGateway(void* param); -static void OnDhcpStop(void* sender, void* param) +static void OnDhcpStop(Dhcp& dhcp) { - auto& dhcp = *(Dhcp*)sender; - // DHCP成功,或者失败且超过最大错误次数,都要启动网关,让它以上一次配置工作 if(dhcp.Result || dhcp.Times >= dhcp.MaxTimes) { diff --git a/TokenNet/TokenClient.cpp b/TokenNet/TokenClient.cpp index 5fdfd982..bb432b66 100644 --- a/TokenNet/TokenClient.cpp +++ b/TokenNet/TokenClient.cpp @@ -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,7 +44,7 @@ void TokenClient::Open() TS("TokenClient::Open"); assert(Control, "令牌客户端还没设置控制器呢"); - Control->Received.bind(*this, &TokenClient::OnReceive); + Control->Received = OnTokenClientReceived; Control->Param = this; Control->Open(); @@ -54,7 +54,7 @@ void TokenClient::Open() // 向服务端握手时,汇报内网本地端口,用户端将会通过该端口连接 //ctrl = Local; - Local->Received = Control->Received; + Local->Received = OnTokenClientReceived; Local->Param = this; Local->Open(); } @@ -111,13 +111,10 @@ bool TokenClient::Reply(TokenMessage& msg, TokenController* ctrl) return ctrl->Reply(msg); } -void TokenClient::OnReceive(Message& msg_, Controller& ctrl_) +bool TokenClient::OnReceive(TokenMessage& msg, TokenController* ctrl) { TS("TokenClient::OnReceive"); - auto& msg = (TokenMessage&)msg_; - auto ctrl = (TokenController*)&ctrl_; - LastActive = Sys.Ms(); switch(msg.Code) @@ -145,7 +142,7 @@ void TokenClient::OnReceive(Message& msg_, Controller& ctrl_) break; } // todo 握手登录心跳消息不需要转发 - if(msg.Code < 0x03) return; + if(msg.Code < 0x03) return true; // 消息转发 if (Received) @@ -162,9 +159,11 @@ void TokenClient::OnReceive(Message& msg_, Controller& ctrl_) 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); @@ -172,7 +171,7 @@ void TokenClient::OnReceive(Message& msg_, Controller& ctrl_) assert_ptr(client); return client->OnReceive((TokenMessage&)msg, ctrl); -}*/ +} // 常用系统级消息 diff --git a/TokenNet/TokenClient.h b/TokenNet/TokenClient.h index 819f39b8..fced1383 100644 --- a/TokenNet/TokenClient.h +++ b/TokenNet/TokenClient.h @@ -38,7 +38,7 @@ public: // 发送消息 bool Send(TokenMessage& msg, TokenController* ctrl = nullptr); bool Reply(TokenMessage& msg, TokenController* ctrl = nullptr); - void OnReceive(Message& msg, Controller& ctrl); + bool OnReceive(TokenMessage& msg, TokenController* ctrl); // 收到功能消息时触发 MessageHandler Received;