cocos2dx C++自定义线程安全消息管理
一、背景
由于cocos2dx本身的NotificationCenter是没有进行线程安全处理的,所以当我们在cocos2dx里面使用多线程进行消息同步的时候,
会出现问题。那么为了解决这个问题,我们只需要进行线程安全锁定即可。
为了不修改源码,我模仿NotificationCenter,自定义了一个消息管理。
二、思路
三、代码
头文件:
/************************************************************** * Copyright (c) 2014-11-12 by real.Xm * Blog Address: http://blog.csdn.net/q229827701 * Email: [email protected] **************************************************************/ #ifndef __XMESSAGE__H #define __XMESSAGE__H #include "cocos2d.h" class XMessage; class CC_DLL MessageMsg : public cocos2d::Ref { public: MessageMsg(); virtual ~MessageMsg(); static MessageMsg* getInstance(); bool addObserver(const std::string &msgName,cocos2d::Ref* target, cocos2d::SEL_CallFuncO selector,cocos2d::Ref* msgContent = nullptr); bool postMessage(const std::string &msgName,cocos2d::Ref* msgContent); bool removeObserverByName(const std::string &msgName,cocos2d::Ref* target = nullptr); bool removeAllObservers(cocos2d::Ref* target); protected: XMessage* getMessageByName(const std::string &msgName) const; private: //message container cocos2d::Vector<XMessage*> _msgContainer; }; class CC_DLL XMessage: public cocos2d::Ref { public: XMessage(const std::string &msgName,cocos2d::Ref* target, cocos2d::SEL_CallFuncO selector,cocos2d::Ref* msgContent); ~XMessage(); void handerMessage(cocos2d::Ref* msgContent); CC_SYNTHESIZE_READONLY(std::string,_msgName,MessageName);//message name CC_SYNTHESIZE_READONLY(cocos2d::Ref*,_target,Target); //target CC_SYNTHESIZE_READONLY(cocos2d::SEL_CallFuncO,_selector,Selector);//function CC_SYNTHESIZE_READONLY(cocos2d::Ref*,_msgContent,MessageConent);//function args }; #endif // !_XMESSAGE_H
实现文件
#include "XMessageManger.h" std::mutex _ContainerMutex; static MessageMsg* _Manager = nullptr; MessageMsg::MessageMsg() { } MessageMsg::~MessageMsg() { _msgContainer.clear(); } MessageMsg* MessageMsg::getInstance() { if (!_Manager) { _Manager = new MessageMsg; } return _Manager; } bool MessageMsg::addObserver( const std::string &msgName,cocos2d::Ref* target,cocos2d::SEL_CallFuncO selector,cocos2d::Ref* msgContent /*= nullptr*/ ) { if (!getMessageByName(msgName)) { auto msg = new XMessage(msgName,target,selector,msgContent); IF_NULL_RETURN_FALSE(msg); msg->autorelease(); std::lock_guard<std::mutex> ul(_ContainerMutex); _msgContainer.pushBack(msg); return true; } return false; } bool MessageMsg::postMessage( const std::string &msgName,cocos2d::Ref* msgContent ) { auto msg = getMessageByName(msgName); if (msg) { msg->handerMessage(msgContent); return true; } return false; } XMessage* MessageMsg::getMessageByName( const std::string &msgName ) const { std::lock_guard<std::mutex> ul(_ContainerMutex); for (auto &msg : _msgContainer) { if (msgName == msg->getMessageName()) { return msg; } } return nullptr; } bool MessageMsg::removeObserverByName( const std::string &msgName,cocos2d::Ref* target /*= nullptr*/ ) { std::lock_guard<std::mutex> ul(_ContainerMutex); for (auto &msg : _msgContainer) { if (msgName == msg->getMessageName() &&(target == msg->getTarget()||!target)) { _msgContainer.eraseObject(msg,true); return true; } } return false; } bool MessageMsg::removeAllObservers( cocos2d::Ref* target ) { std::lock_guard<std::mutex> ul(_ContainerMutex); for (auto &msg : _msgContainer) { if (target == msg->getTarget()) { _msgContainer.eraseObject(msg,true); return true; } } return false; } ////////////////////////////////////////////////////////////////////////// /// XMessage Class ///////////////////////////////////////////////////////////////////////// XMessage::XMessage( const std::string &msgName,cocos2d::Ref* target, cocos2d::SEL_CallFuncO selector,cocos2d::Ref* msgContent ) :_msgName(msgName),_target(target),_selector(selector),_msgContent(msgContent) { } XMessage::~XMessage() { } void XMessage::handerMessage( cocos2d::Ref* msgContent ) { if (_target) { if (msgContent) { (_target->*_selector)(msgContent); } else { (_target->*_selector)(msgContent); } } }
四、申明
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。