⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queue.h

📁 用c++编写的网络通信框架
💻 H
字号:
/*-----------------------------------------------------------------------------
 * FILE: queue.h
 * AUTH: xuwannian@gmail.com
 * TIME: 2008-04-29
 *---------------------------------------------------------------------------*/
#ifndef __QUEUE_QUEUE_HPP__
#define __QUEUE_QUEUE_HPP__

#include "mutex.h"
#include <list>
#include <map>
#include <string>
#include <time.h>

namespace xuwn 
{
	//////////////////////////////////////////////////////////////////////////
	class times
	{
	public:
				 times	() { restart();							};
		virtual ~times	() {									};
		double	 elapsed() { return (double)(time(NULL) - __tx);};
		void	 restart() { __tx = time(NULL);					};
		
		static char* now(const char* format, char* now__)
		{
			time_t		__tx;
			struct tm*	__ts = NULL;

			__tx = time((time_t*)NULL);
			__ts = (struct tm*)localtime(&__tx);

			char sztemp[16];
			::memset(sztemp, 0, sizeof(sztemp));

			int pos = 0;

			while ('\0' != format[pos])
			{
				::memset(sztemp, 0, sizeof(sztemp));

				if (0 == ::memcmp(&format[pos], "YYYY", 4) || \
					0 == ::memcmp(&format[pos], "yyyy", 4))
				{
					::sprintf(sztemp, "%04d", (1900 + __ts->tm_year));
					pos += 4;
				}
				else if (0 == ::memcmp(&format[pos], "YY", 2) || \
						 0 == ::memcmp(&format[pos], "yy", 2))
				{
					::sprintf(sztemp, "%04d", (__ts->tm_year - 100));
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "MM", 2) || \
						 0 == ::memcmp(&format[pos], "mm", 2))
				{
					::sprintf(sztemp, "%02d", __ts->tm_mon + 1);
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "M", 1) || \
						 0 == ::memcmp(&format[pos], "m", 1))
				{
					::sprintf(sztemp, "%d", __ts->tm_mon + 1);
					pos += 1;
				}
				else if (0 == ::memcmp(&format[pos], "DD", 2)	||\
						 0 == ::memcmp(&format[pos], "dd", 2))
				{
					memset(sztemp, 0, sizeof(sztemp));
					::sprintf(sztemp, "%02d", __ts->tm_mday);
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "D", 1)	||\
	    				 0 == ::memcmp(&format[pos], "d", 1))
				{
					memset(sztemp, 0, sizeof(sztemp));
					::sprintf(sztemp, "%d", __ts->tm_mday);
					pos += 1;
				}
				else if (0 == ::memcmp(&format[pos], "HH", 2) || \
						 0 == ::memcmp(&format[pos], "hh", 2))
				{
					::sprintf(sztemp, "%02d",  __ts->tm_hour);
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "H", 1) || \
						 0 == ::memcmp(&format[pos], "h", 1))
				{
					::sprintf(sztemp, "%d", __ts->tm_hour);
					pos += 1;
				}
				else if (0 == ::memcmp(&format[pos], "NN", 2)	||\
						 0 == ::memcmp(&format[pos], "nn", 2))
				{
					::sprintf(sztemp, "%02d", __ts->tm_min);
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "N", 1)	||\
						 0 == ::memcmp(&format[pos], "n", 1))
				{
					::sprintf(sztemp, "%d", __ts->tm_min);
					pos += 1;
				}
				else if (0 == ::memcmp(&format[pos], "SS", 2)	||\
						 0 == ::memcmp(&format[pos], "ss", 2))
				{
					::sprintf(sztemp, "%02d", __ts->tm_sec);
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "S", 1)	||\
						 0 == ::memcmp(&format[pos], "s", 1))
				{
					::sprintf(sztemp, "%d", __ts->tm_sec);
					pos += 1;
				}
				else if (0 == ::memcmp(&format[pos], "年", 2))
				{
					::sprintf(sztemp, "%s",  "年");
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "月", 2))
				{
					::sprintf(sztemp, "%s",  "月");
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "日", 2))
				{
					::sprintf(sztemp, "%s",  "日");
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "时", 2))
				{
					::sprintf(sztemp, "%s",  "时");
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "分", 2))
				{
					::sprintf(sztemp, "%s",  "分");

					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "秒", 2))
				{
					::sprintf(sztemp, "%s",  "秒");
					pos += 2;
				}
				else if (0 == ::memcmp(&format[pos], "-", 1))
				{
					::sprintf(sztemp, "%s",  "-");
					pos += 1;
				}
				else if (0 == ::memcmp(&format[pos], ":", 1))
				{
					::sprintf(sztemp, "%s",  ":");
					pos += 1;
				}
				else if (0 == ::memcmp(&format[pos], " ", 1))
				{
					::sprintf(sztemp, "%s",  " ");
					pos += 1;
				}
				else
				{
					pos += 1;
				}

				::strcat(now__, sztemp);
			}

			return now__;
		}
	private:
		time_t	__tx;
	};

	/*
	int main()
	{
	times t;
	Sleep(10000);

	int d = t.elapsed();
	printf("elapsed = %d\n", d);

	getchar();
	return 0;
	}

	打印结果为:elapsed = 10
	*/

	// 定义消息队列结构体
	template<typename T>
	struct __BODY
	{
	public:
		__BODY(T t) : t(t) {};

		bool IsTimeOut(int timeout=1200)
		{
			return ((timeout - ts.elapsed()) >= 0) ? false : true;
		}
	public:
		T t;
	private:
		times ts;
	};
	//////////////////////////////////////////////////////////////////////////
	// CList应用std::list编写的消息队列
	// 2008-03-04
	template<typename T>
	class CList
	{
	public:
					CList() {	clear();	};
		virtual	   ~CList() {	clear();	};	
	public:
		int			put(T t);
		int			get(T& t, bool bRemove=true);
		int			size();
		void		cleartimeout();
	private:
		void		clear();
	private:
		std::list<__BODY<T> >	__q;
		mutex					__m;
	}; // END CLASS CLQUEUE

	//-------------------------实现-------------------------//
	template<typename T>
	int CList<T>::put(T t)
	{
		lock l(__m);

		__BODY<T> body(t);
		__q.push_back(body);

		return 0;
	}

	template<typename T>
	int CList<T>::get(T& t, bool bRemove/* =true */)
	{
		lock l(__m);

		std::list<__BODY<T> >::iterator it = __q.begin();

		if (it == __q.end())
			return -1;

		__BODY<T> a = (__BODY<T>)*it;

		t = (T)a.t;

		if (bRemove)
			__q.erase(it);

		return 0;
	}

	template<typename T>
	int CList<T>::size()
	{
		lock l(__m);
		return __q.size();
	}

	template<typename T>
	void CList<T>::clear()
	{
		lock l(__m);
		__q.clear();
	}

	template<typename T>
	void CList<T>::cleartimeout()
	{
		lock l(__m);
		std::list<__BODY<T> >::iterator it;

		for (it = __q.begin(); it != __q.end(); )
		{
			__BODY<T> a = (__BODY<T>)*it;

			if (a.IsTimeOut())
			{
				__q.erase(it);

				it = __q.begin();
			}
			else
			{
				it++;
			}
		}
	}
	//////////////////////////////////////////////////////////////////////////
	// CMap 应用std::map 编写的消息队列
	// 2008-03-04
	template<typename T>
	class CMap
	{
	public:
		CMap() {	clear();	};
		virtual	   ~CMap() {	clear();	};	
	public:
		int			put(std::string key, T  t, bool bRemove=true);
		int			get(std::string key, T& t, bool bRemove=true);
		int			erase(std::string key);
		int			has(std::string key);
		int			gets(std::list<T>& ls, bool bRemove=false);
		int			size();
		void		cleartimeout();
	private:
		void		clear();
	private:
		std::map<std::string, __BODY<T> >	__q;
		mutex								__m;
	};	// END CLASS CMQUEUE
	//-------------------------实现-------------------------//
	template<typename T>
	int CMap<T>::put(std::string key, T t, bool bRemove/* =true */)
	{
		lock l(__m);

		std::map<std::string, __BODY<T> >::iterator it = __q.find(key);

		if (it == __q.end())
		{	
			__q.insert(std::map<std::string, __BODY<T> >::value_type(key, t));
			return 0;
		}

		if (bRemove)
		{
			__q.erase(it);
			__q.insert(std::map<std::string, __BODY<T> >::value_type(key, t));

			return 0;
		}
		else
		{
			return -2;
		}
	}

	template<typename T>
	int CMap<T>::get(std::string key, T& t, bool bRemove/* =true */)
	{
		lock l(__m);

		std::map<std::string, __BODY<T> >::iterator it = __q.find(key);

		if (it == __q.end())
		{	
			return -1;
		}

		__BODY<T> a = (__BODY<T>)it->second;

		t = (T)a.t;

		if (bRemove)
		{
			__q.erase(it);
		}

		return 0;
	}

	template<typename T>
	int CMap<T>::size()
	{
		lock l(__m);
		return __q.size();
	}

	template<typename T>
	void CMap<T>::clear()
	{
		lock l(__m);
		__q.clear();
	}

	template<typename T>
	void CMap<T>::cleartimeout()
	{
		lock l(__m);
		std::map<std::string, __BODY<T> >::iterator it = __q.begin();

		for (; it != __q.end(); )
		{
			__BODY<T> a = (__BODY<T>)it->second;

			if (a.IsTimeOut())
			{
				__q.erase(it);

				it = __q.begin();
			}
			else
				it++;
		}
	}

	template<typename T>
	int CMap<T>::erase(std::string key)
	{
		lock l(__m);
		std::map<std::string, __BODY<T> >::iterator it = __q.find(key);

		if (it != __q.end())
		{
			__q.erase(it);

			return 0;
		}

		// 没有元素可以删除
		return -3;
	}

	template<typename T>
	int CMap<T>::has(std::string key)
	{
		lock l(__m);
		std::map<std::string, __BODY<T> >::iterator it = __q.find(key);

		if (it != __q.end())
		{
			return 0;
		}

		// 没有元素可以删除
		return -3;
	}

	template<typename T>
	int CMap<T>::gets(std::list<T>& ls, bool bRemove)
	{
		lock l(__m);
		std::map<std::string, __BODY<T> >::iterator it = __q.begin();

		for (; it != __q.end(); it++)
		{
			__BODY<T> a = (__BODY<T>)it->second;

			ls.push_back(a.t);
		}

		if (bRemove)
			__q.clear();

		return ls.size();
	}
};	// END xuwn

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -