muduo定时器实例

借用c11版的muduo库,git地址:https://github.com/AlexStocks/muduo.git

TimerManager.h

#pragma once

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "Timer.h"

class TimerManager
{
	typedef std::shared_ptr Timer_ptr_type;
public:

	TimerManager(){}
	~TimerManager(){}
	void SetTimerLoop(muduo::net::EventLoop* loop)
	{
		for(auto it : m_Timers)
		{
			it.second->SetLoop(loop);
		}
	}

	void Init()
	{
		for(int i=0;i<4;i++)
		{
			Timer_ptr_type Timer_ptr(new Timer(i));
			m_Timers.emplace(i, Timer_ptr);
		}
	}
	void AddCb(Timer::E_TIMER_TYPE id, const string& funcName, muduo::net::TimerCallback&& cb)
	{
		auto it = m_Timers.find(id);
		if (it == m_Timers.end())
		{
			return;
		}
		it->second->AddCb(funcName, cb);
	}
	void DelCb(Timer::E_TIMER_TYPE id, const std::string& funcName)
	{
		auto it = m_Timers.find(id);
		if (it == m_Timers.end())
		{
			return;
		}
		it->second->DelCb(funcName);
	}
	void Start()
	{
		for(auto it : m_Timers)
		{
			it.second->Start(time(0));//need to fix start time
		}
	}
	void Start(Timer::E_TIMER_TYPE id)
	{
		auto it = m_Timers.find(id);
		if (it != m_Timers.end())
		{
			it->second->Start(time(0));
		}
	}
	void Stop()
	{
		for(auto it : m_Timers)
		{
			it.second->Stop();
		}
	}

private:
	std::map m_Timers;
};


extern std::unique_ptr g_TimerManager;

Timer.h

#pragma once

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;


class Timer
{
	typedef std::map timercb_map_type;
public:
	enum E_TIMER_TYPE
	{
		E_TIME_SECOND = 1,
		E_TIME_MINUTE,
		E_TIME_HOUR,
		E_TIME_DAY,
	};

	Timer(int id):m_id(id){}
	~Timer(){
		Stop();
		m_TimerCb.clear();
	}
	void OnTimer()
	{
		for(auto it : m_TimerCb)
		{
			muduo::net::TimerCallback f = it.second;
			f();
			cout<< "execute timer logic(" << it.first <<")"<< endl;
		}
		NextTimer();
	}
	void NextTimer()
	{
		switch(m_id)
		{
			case E_TIME_SECOND:
				m_time += 1;
			break;
			case E_TIME_MINUTE:
				m_time += 60;
			break;
			case E_TIME_HOUR:
				m_time += 3600;
			break;
			case E_TIME_DAY:
			 	m_time += 24*3600;
			break;
		}
		Stop();
		m_TimerId = m_Loop->runAt(muduo::Timestamp::fromUnixTime(m_time), std::bind(&Timer::OnTimer, this));
	}
	void Start(time_t t)
	{
		m_time = t;
		m_TimerId = m_Loop->runAt(muduo::Timestamp::fromUnixTime(m_time), std::bind(&Timer::OnTimer, this));
	}
	void Stop()
	{
		m_Loop->cancel(m_TimerId);
	}
	void AddCb(const string& funcName, muduo::net::TimerCallback& cb)
	{
		auto it = m_TimerCb.find(funcName);
		if(it == m_TimerCb.end())
		{
			m_TimerCb.emplace(funcName, cb);
		}
	}
	void DelCb(const string& funcName)
	{
		auto it = m_TimerCb.find(funcName);
		if (it != m_TimerCb.end())
		{
			m_TimerCb.erase(it);
		}
	}

	void SetLoop(muduo::net::EventLoop* loop)
	{
		m_Loop = loop;
	}
private:
	time_t m_time;
	int m_id;
	int m_TimerType;
	muduo::net::EventLoop* m_Loop;
	timercb_map_type m_TimerCb;
	muduo::net::EventLoop*  m_TimerLoop;
	muduo::net::TimerId m_TimerId;
};

编译:

g++ --std=c++11 Test.cpp Timer.h TimerManager.h -I/home/share/testcpp/timer ../lib/libmuduo.a ../lib/libmuduo_base.a -lpthread 

你可能感兴趣的:(c/c++)