Example #1
0
ImageIOManager::ImageIOManager()
{
	AddIO(new BMPImageIO());
}
Example #2
0
bool EventLoopL::Prepare()
{
	_DBG("EventLoopL::Prepare");

	if(!EventConfig_)
	{
		EventConfig_ = event_config_new();
	}

	if(!EventConfig_)
	{
		throwSystemError("event_config_new allocate fail!");
	}

	//event_config_avoid_method(EventConfig_, "select");//avoid select
	//event_config_avoid_method(EventConfig_, "poll");//avoid poll
	if(0 != event_config_require_features(EventConfig_, EV_FEATURE_O1))//EV_FEATURE_ET ?
	{
		_ERR("set event config feature fail!");
		return false;
	}

	if(0 != event_config_set_flag(EventConfig_, EVENT_BASE_FLAG_NOLOCK))
	{
		_ERR("set event config flag fail!");
		return false;
	}

	if(!EventBase_)
	{
		EventBase_ = event_base_new_with_config(EventConfig_);
	}

	if(!EventBase_)
	{
		throwSystemError("event_base_new_with_config allocate fail!");
		return false;
	}

	_DBG("Current used method : %s", event_base_get_method(EventBase_));

	for(auto &data : Datas_)
	{
		if(data)
		{
			std::shared_ptr<IO> io(data->io.lock());
			if(!io)
			{
				_WRN("This io %d is not exist!", io->Index());
				continue;
			}

			if(io->Index() < 0)
			{
				_ERR("This IO has been removed!");
				continue;
			}
			
			if(data->ev)
			{
				event_del(data->ev);
				event_free(data->ev);
				data->ev = NULL;
			}

			event* ev = event_new(EventBase_, io->Fd(), io->Condition(), CEventCallBack, (void*)&(data->fn));	
			_DBG("Add io %p(index : %d  fd : %d  cond : %d)", ev, io->Index(), io->Fd(), io->Condition());

			if(ev == NULL)
			{
				_ERR("event_new fail, index %d", io->Index());
				continue;
			}
			data->ev = ev;

			if(event_add(ev, NULL))
			{
				_ERR("event_add fail, index %d!", io->Index());
				continue;
			}
		}
	}

	int evtfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
	if (evtfd < 0)
	{
		_ERR("Get eventfd fail!");
		return false;
	}
	Wakeup_.reset(new IO(evtfd, EV_READ | EV_PERSIST));
	Wakeup_->SetCallback(std::bind(&EventLoopL::quitAsync, this, std::placeholders::_1));
	AddIO(Wakeup_);

	return true;
}