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

📄 sesseltimeout.c

📁 linux下编写的一个时间定时触发器
💻 C
字号:
#include <stdio.h>
#include <assert.h>
#include "sesSelTimeOut.H"

static const int g_minD = 200000;
static const int g_mSec = 1000000;

SelTime_T operator+ (SelTime_T &t1, SelTime_T &t2)
{
	SelTime_T t;
	t.tv_usec = t1.tv_usec + t2.tv_usec;
	t.tv_sec = t1.tv_sec + t2.tv_sec + t.tv_usec/g_mSec;
	t.tv_usec %= g_mSec;
	return t;
}

SelTime_T operator- (SelTime_T &t1, SelTime_T &t2)
{
	//assert(t1.tv_sec >= t2.tv_sec);
	SelTime_T t;
	t.tv_sec = t1.tv_sec - t2.tv_sec;
	if (t1.tv_usec >= t2.tv_usec)
	{
		t.tv_usec = t1.tv_usec - t2.tv_usec;
	}
	else
	{
		--t.tv_sec;
		t.tv_usec = t1.tv_usec + g_mSec - t2.tv_usec;
	}
	return t;
}

bool operator> (SelTime_T &t1, SelTime_T &t2)
{
	return t1.tv_sec > t2.tv_sec || t1.tv_sec == t2.tv_sec && t1.tv_usec > t2.tv_usec;
}

bool operator< (SelTime_T &t1, SelTime_T &t2)
{
	return !(t1 > t2) && !(t1 == t2);
}

bool operator== (SelTime_T &t1, SelTime_T &t2)
{
	long d;
	if (t1.tv_usec > t2.tv_usec)
	{
		d = t1.tv_usec - t2.tv_usec;
	}
	else
	{
		d = t2.tv_usec - t1.tv_usec;
	}
	
	return t1.tv_sec == t2.tv_sec && d <= g_minD;
}


void sesSelTimeOut::AddTimeOut (SelTime_T &t, time_t rt, char* MsgSourceName)
{
	for(size_t i = 0; i < tosVct.size(); ++i)
	{
		if(strcmp(tosVct[i].MsgSourceName, MsgSourceName)==1 && tosVct[i].t==t && tosVct[i].rt==rt)
		{
			return;
		}
	}

//	SelTime_T cur;
//	gettimeofday(&cur, NULL);
//	cur = cur + t;
//	tosVct.push_back(TimeOutStruct_T(cur, MsgSourceName));
	tosVct.push_back(TimeOutStruct_T(t, rt, MsgSourceName));
}

void sesSelTimeOut::ClearAllTimeOut (void)
{
	tosVct.clear();
}

void sesSelTimeOut::DelTimeOut (char* MsgSourceName)
{
	TOSVct_T::iterator pos = tosVct.begin();
	for (; pos != tosVct.end(); ++pos)
	{
		if (strcmp((*pos).MsgSourceName, MsgSourceName)==0)
		{
			tosVct.erase(pos);
			break;
		}
	}
}

void sesSelTimeOut::DelTimeOut (SelTime_T &t, time_t rt, char* MsgSourceName)
{
	TOSVct_T::iterator pos = tosVct.begin();
	for (; pos != tosVct.end(); ++pos)
	{
		if (strcmp((*pos).MsgSourceName, MsgSourceName)==0 && (*pos).t == t && (*pos).rt== rt)
		{
			tosVct.erase(pos);
			break;
		}
	}
}

bool sesSelTimeOut::ExistTimeOut (SelTime_T &t, time_t rt, char* MsgSourceName)
{
	for (size_t i = 0; i < tosVct.size(); ++i)
	{
		if(strcmp(tosVct[i].MsgSourceName, MsgSourceName)==0 && tosVct[i].t == t && tosVct[i].rt ==rt)
		{
			return true;
		}
	}
	return false;
}

SelTime_T sesSelTimeOut::GetFirstTimeOut (void)
{
	SelTime_T cur;
	gettimeofday(&cur, NULL);
	
	SelTime_T fst = {0, 0};
	if (tosVct.size() == 0)
	{
		return fst;
	}
	
	size_t i;
	for (i = 0; i < tosVct.size(); ++i)
	{
		if (tosVct[i].t > cur)
		{
			break;
		}
	}
	if (tosVct.size() == i)
	{
		return fst;
	}
	for (fst = tosVct[i].t; i < tosVct.size(); ++i)
	{
		if (tosVct[i].t < cur)
		{
			continue;
		}
		if (tosVct[i].t < fst)
		{
			fst = tosVct[i].t;
		}
	}
	return fst;
}

void sesSelTimeOut::RaiseTimeOut (SelTime_T &t, time_t rt, char* MsgSourceName)
{
	if(!ExistTimeOut(t, rt, MsgSourceName))
	{
		AddTimeOut(t, rt, MsgSourceName);
		//printf("AddTimeOut(%ld, %ld, %s)\n", t.tv_sec, rt, MsgSourceName);
	}
}

void sesSelTimeOut::ShowInfo (void)
{
	for (size_t i = 0; i < tosVct.size(); ++i)
	{
		printf("tosVct[%d].t.tv_sec=%ld\n", i, tosVct[i].t.tv_sec);
		//printf("tosVct[%d].MsgSourceName=%s\n", i, tosVct[i].MsgSourceName);
	}
}

⌨️ 快捷键说明

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