C++消息分发器-简单实现

#include "Dispatcher.h"

int MessageQueue::PushMessage(Message* message)
{
	if (nullptr == message)
	{
		return -1;
	}
	lock_guard<mutex> lock(m_queueMutex);
	Message* copyMessage = new Message();
	copyMessage->cmdId = message->cmdId;
	copyMessage->jsonParam = message->jsonParam;
	m_messageQueue.push(copyMessage);
	return 0;
}

Message* MessageQueue::PopMessage()
{
	if (m_messageQueue.empty())
	{
		return nullptr;
	}
	lock_guard<mutex> lock(m_queueMutex);
	Message* message = m_messageQueue.front();
	m_messageQueue.pop();
	return message;
	// TODO: 在此处插入 return 语句
}

Dispatcher* Dispatcher::m_dispatcher = nullptr;
mutex Dispatcher::m_dispatcherMutex;
Dispatcher * Dispatcher::CreateDispatcher()
{
	if (nullptr == m_dispatcher)
	{
		lock_guard<mutex> lock(m_dispatcherMutex);
		if (nullptr == m_dispatcher)
		{
			m_dispatcher = new Dispatcher();
		}
	}
	return m_dispatcher;
}

int Dispatcher::RegisterHandler(int cmdId, pMessageHandler pHandler)
{
	m_messageHandlerMap.insert(pair<int, pMessageHandler>(cmdId, pHandler));
	return 0;
}

int Dispatcher::SendMessage(Message * message)
{
	if (nullptr == m_messageQueue)
	{
		return -1;
	}
	m_messageQueue->PushMessage(message);
	return 0;
}

int Dispatcher::DispatchLoop()
{
	while (m_bRunDispatch)
	{
		cout << "Dispatcher Thread is Runing.." << endl;
		this_thread::sleep_for(chrono::seconds(3));
		Message* message = m_messageQueue->PopMessage();
		if (nullptr == message)
		{
			continue;
		}
		auto handler = m_messageHandlerMap.find(message->cmdId);
		if (handler != m_messageHandlerMap.end())
		{
			handler->second(message->cmdId, message->jsonParam);
		}
		
	}
	return 0;
}

Dispatcher::Dispatcher()
{
	m_bRunDispatch = true;
	if (nullptr == m_messageQueue)
	{
		m_messageQueue = new MessageQueue();
	}
	if (nullptr == m_pThread)
	{
		m_pThread = new thread(&Dispatcher::DispatchLoop, this);
	}
}

Dispatcher::~Dispatcher()
{
}

#pragma once
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef int(*pMessageHandler)(int, string);

typedef struct _tagMessage
{
	int cmdId;
	string jsonParam;
}Message;

class MessageQueue
{
public:
	int PushMessage(Message* Message);
	Message* PopMessage();
private:
	mutex m_queueMutex;
	queue< Message*> m_messageQueue;
};


class Dispatcher
{
public:
	static Dispatcher* CreateDispatcher();
	int RegisterHandler(int cmdId, pMessageHandler pHandler);
	int SendMessage(Message* message);
private:
	Dispatcher();
	~Dispatcher();
	int DispatchLoop();
private:
	static Dispatcher* m_dispatcher;
	map<int, pMessageHandler> m_messageHandlerMap;
	static mutex m_dispatcherMutex;
	thread* m_pThread;
	bool m_bRunDispatch;
	MessageQueue* m_messageQueue;
};
// MessageDispatcher.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include 
#include "Dispatcher.h"

int TestFunction(int cmdId, string jsonParam)
{
	cout << "TestFunction Has been Deal A Message. cmdId:" << cmdId << ", param:" << jsonParam << endl;
}

int TestFunction1(int cmdId, string jsonParam)
{
	cout << "TestFunction1 Has been Deal A Message. cmdId:" << cmdId << ", param:" << jsonParam << endl;
}
int main()
{
	Message* message = new Message();
	message->cmdId = 100;
	message->jsonParam = "HelloMessage";
	Dispatcher* dispatcher = Dispatcher::CreateDispatcher();
	dispatcher->RegisterHandler(100, TestFunction);
	dispatcher->RegisterHandler(101, TestFunction1);
	while (true)
	{
		dispatcher->SendMessage(message);
		message->cmdId = message->cmdId == 100 ? 101 : 100;
        std::cout << "Hello World!\n";
		this_thread::sleep_for(chrono::seconds(5));
	}
	delete message;
}

你可能感兴趣的:(windows编程,c++)