CMsgMng
/**
* @file CMsgMng.h
* @brief
* @author jxd
* @date 2019-04-02
*/
#ifndef CMSGMNG_H
#define CMSGMNG_H
#pragma once
#include "boost/thread.hpp"
#include <boost/noncopyable.hpp>
#include <boost/thread/shared_mutex.hpp>
#include "CMsgComm.h"
#include "string"
namespace kbd_msg {
typedef std::vector<CMsgComm * > MsgCommPtrVec;
typedef std::map<int,MsgCommPtrVec > ChanMsgCommMap;
class CMsgMng :public boost::noncopyable
{
public:
CMsgMng();
~CMsgMng();
void addSub(int nChannelId,CMsgComm *cMsgComm);
void removeSub(int nChannelId ,CMsgComm *cMsgComm);
bool send(CMsgPtr msgSend);
bool send(std::map<int, std::list<CMsgPtr> > &addMsgMap);
private:
typedef boost::shared_lock<boost::shared_mutex> read_lock;
typedef boost::unique_lock<boost::shared_mutex> write_lock;
boost::shared_mutex read_write_mutex;
ChanMsgCommMap m_chanMsgCommMap;
};
}//kbd_msg
#endif // CMSGMNG_H
/**
* @file CMsgMng.cpp
* @brief
* @author jxd
* @date 2019-04-02
*/
#include "msg/CMsgMng.h"
kbd_msg::CMsgMng::CMsgMng()
{
}
kbd_msg::CMsgMng::~CMsgMng()
{
m_chanMsgCommMap.clear();
}
void kbd_msg::CMsgMng::addSub(int nChannelId, kbd_msg::CMsgComm *cMsgComm)
{
write_lock rlock(read_write_mutex);
ChanMsgCommMap::iterator it= m_chanMsgCommMap.find(nChannelId);
if(it == m_chanMsgCommMap.end())
{
std::vector<CMsgComm *> msgCommVec ;
msgCommVec.push_back(cMsgComm);
m_chanMsgCommMap.insert(std::make_pair(nChannelId,msgCommVec));
}else
{
MsgCommPtrVec &msgCommVec =it->second;
for(MsgCommPtrVec::iterator pos =msgCommVec.begin();pos !=msgCommVec.end();pos++)
{
if(cMsgComm == *pos)
{
return ;
}
}
msgCommVec.push_back(cMsgComm);
}
}
void kbd_msg::CMsgMng::removeSub(int nChannelId, kbd_msg::CMsgComm *cMsgComm)
{
write_lock rlock(read_write_mutex);
ChanMsgCommMap::iterator it= m_chanMsgCommMap.find(nChannelId);
if(it == m_chanMsgCommMap.end())
{
return ;
}else
{
MsgCommPtrVec &msgCommVec =it->second;
for(MsgCommPtrVec::iterator pos =msgCommVec.begin();pos !=msgCommVec.end();pos++)
{
if(cMsgComm == *pos)
{
//清除接收消息队列
cMsgComm->m_msgList.clear();
//删除订阅
msgCommVec.erase(pos);
return ;
}
}
}
}
bool kbd_msg::CMsgMng::send(kbd_msg::CMsgPtr msgSend)
{
int nChanNo = msgSend->getChannelID();
int type = msgSend->getType();
if (nChanNo == -1 || type == -1)
{
LOGERROR("发送消息失败,通道:%d,消息类型:%d", nChanNo, type);
return false;
}
read_lock rlock(read_write_mutex);
ChanMsgCommMap::iterator it= m_chanMsgCommMap.find(nChanNo);
if(it != m_chanMsgCommMap.end())
{
MsgCommPtrVec &msgCommVec =it->second;
for(MsgCommPtrVec::iterator pos =msgCommVec.begin();pos !=msgCommVec.end();pos++)
{
(*pos)->insertMsg(msgSend);
}
}
return true;
}
bool CMsgMng::send(std::map<int, std::list<CMsgPtr> > &addMsgMap)
{
if (addMsgMap.size() != 0)
{
std::map<int, std::list<CMsgPtr> >::iterator it = addMsgMap.begin();
read_lock rlock(read_write_mutex);
for(;it!=addMsgMap.end();++it)
{
int nChanNo = it->first;
ChanMsgCommMap::iterator pos= m_chanMsgCommMap.find(nChanNo);
if(pos != m_chanMsgCommMap.end())
{
MsgCommPtrVec &msgCommVec =pos->second;
for(MsgCommPtrVec::iterator itpos =msgCommVec.begin();itpos !=msgCommVec.end();itpos++)
{
(*itpos)->insertVecMsg(it->second);
}
}
}
addMsgMap.clear();
}
return true;
}
浙公网安备 33010602011771号