コード例 #1
0
ActorTimer_::timer_handle ActorTimer_::timeout(unsigned long long us, const actor_handle& host)
{
	if (!_strand)
	{
		_strand = _weakStrand.lock();
	}
	assert(_strand->running_in_this_thread());
	assert(us < 0x80000000LL * 1000);
	unsigned long long et = (get_tick_us() + us) & -256;
	timer_handle timerHandle;
	timerHandle._null = false;
	if (et >= _extMaxTick)
	{
		_extMaxTick = et;
		timerHandle._tableNode = _handlerTable.insert(_handlerTable.end(), make_pair(et, host));
	}
	else
	{
		timerHandle._tableNode = _handlerTable.insert(make_pair(et, host));
	}
	
	if (!_looping)
	{//定时器已经退出循环,重新启动定时器
		_looping = true;
		assert(_handlerTable.size() == 1);
		_extFinishTime = et;
		timer_loop(us);
	}
	else if (et < _extFinishTime)
	{//定时期限前于当前定时器期限,取消后重新计时
		boost::system::error_code ec;
		((timer_type*)_timer)->cancel(ec);
		_timerCount++;
		_extFinishTime = et;
		timer_loop(us);
	}
	return timerHandle;
}
コード例 #2
0
void ActorTimer_::timer_loop(unsigned long long us)
{
	int tc = ++_timerCount;
	boost::system::error_code ec;
	((timer_type*)_timer)->expires_from_now(boost::chrono::microseconds(us), ec);
	((timer_type*)_timer)->async_wait(_strand->wrap_asio([this, tc](const boost::system::error_code&)
	{
		assert(_strand->running_in_this_thread());
		if (tc == _timerCount)
		{
			_extFinishTime = 0;
			unsigned long long nt = get_tick_us();
			while (!_handlerTable.empty())
			{
				auto iter = _handlerTable.begin();
				if (iter->first > nt + 500)
				{
					_extFinishTime = iter->first;
					timer_loop(iter->first - nt);
					return;
				}
				else
				{
					iter->second->timeout_handler();
					_handlerTable.erase(iter);
				}
			}
			_looping = false;
			_strand.reset();
		}
		else if (tc == _timerCount - 1)
		{
			_strand.reset();
		}
	}));
}
コード例 #3
0
ファイル: test_echo_server.cpp プロジェクト: siwuxian/kcpuv
static uint64_t get_tick_ms() {
	return get_tick_us() / 1000;
}