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

TimerManager.h

#pragma once

#include <iostream>
#include <vector>
#include <memory>
#include <string>
#include <muduo/net/TimerQueue.h>
#include <muduo/net/EventLoop.h>
#include <muduo/net/TimerId.h>

#include "Timer.h"

class TimerManager
{
	typedef std::shared_ptr<Timer> 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<int, Timer_ptr_type> m_Timers;
};


extern std::unique_ptr<TimerManager> g_TimerManager;

Timer.h

#pragma once

#include <iostream>
#include <map>
#include <string>
#include <muduo/net/TimerQueue.h>
#include <muduo/net/EventLoop.h>
#include <muduo/net/TimerId.h>

using namespace std;


class Timer
{
	typedef std::map<std::string, muduo::net::TimerCallback> 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 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐