Exemple #1
0
void OutputMessagePool::send(OutputMessage_ptr msg)
{
	OTSYS_THREAD_LOCK(m_outputPoolLock, "");
	OutputMessage::OutputMessageState state = msg->getState();

	OTSYS_THREAD_UNLOCK(m_outputPoolLock, "");
	if(state == OutputMessage::STATE_ALLOCATED_NO_AUTOSEND)
	{
		#ifdef __DEBUG_NET_DETAIL__
		std::cout << "Sending message - SINGLE" << std::endl;
		#endif
		if(msg->getConnection())
		{
			if(!msg->getConnection()->send(msg) && msg->getProtocol())
				msg->getProtocol()->onSendMessage(msg);
		}
		#ifdef __DEBUG_NET__
		else
			std::cout << "Error: [OutputMessagePool::send] NULL connection." << std::endl;
		#endif
	}
	#ifdef __DEBUG_NET__
	else
		std::cout << "Warning: [OutputMessagePool::send] State != STATE_ALLOCATED_NO_AUTOSEND" << std::endl;
	#endif
}
Exemple #2
0
void Dispatcher::stop()
{
	OTSYS_THREAD_LOCK(m_taskLock, "");
	flush();
	m_shutdown = true;
	OTSYS_THREAD_UNLOCK(m_taskLock, "");
}
Exemple #3
0
void Dispatcher::shutdown()
{
	OTSYS_THREAD_LOCK(m_taskLock, "");
	m_threadState = Dispatcher::STATE_TERMINATED;
	flush();
	OTSYS_THREAD_UNLOCK(m_taskLock, "");
}
Exemple #4
0
OTSYS_THREAD_RETURN Dispatcher::dispatcherThread(void* p)
{
	#if defined __EXCEPTION_TRACER__
	ExceptionHandler dispatcherExceptionHandler;
	dispatcherExceptionHandler.InstallHandler();
	#endif
	srand((uint32_t)OTSYS_TIME());

	OutputMessagePool* outputPool = NULL;
	while(Dispatcher::m_threadState != Dispatcher::STATE_TERMINATED)
	{
		Task* task = NULL;
		// check if there are tasks waiting
		OTSYS_THREAD_LOCK(getDispatcher().m_taskLock, "");
		if(getDispatcher().m_taskList.empty()) //if the list is empty wait for signal
			OTSYS_THREAD_WAITSIGNAL(getDispatcher().m_taskSignal, getDispatcher().m_taskLock);

		if(!getDispatcher().m_taskList.empty() && Dispatcher::m_threadState != Dispatcher::STATE_TERMINATED)
		{
			// take the first task
			task = getDispatcher().m_taskList.front();
			getDispatcher().m_taskList.pop_front();
		}

		OTSYS_THREAD_UNLOCK(getDispatcher().m_taskLock, "");
		// finally execute the task...
		if(!task)
			continue;

		if(!task->hasExpired())
		{
			if((outputPool = OutputMessagePool::getInstance()))
				outputPool->startExecutionFrame();

			(*task)();
			if(outputPool)
				outputPool->sendAll();

			g_game.clearSpectatorCache();
		}

		delete task;
	}

	#if defined __EXCEPTION_TRACER__
	dispatcherExceptionHandler.RemoveHandler();
	#endif
	#if not defined(WIN32)
	return NULL;
	#endif
}
Exemple #5
0
void OutputMessagePool::internalReleaseMessage(OutputMessage* msg)
{
	if(msg->getProtocol())
		msg->getProtocol()->unRef();
	else
		std::cout << "[Warning - OutputMessagePool::internalReleaseMessage] protocol not found." << std::endl;

	if(msg->getConnection())
		msg->getConnection()->unRef();
	else
		std::cout << "[Warning - OutputMessagePool::internalReleaseMessage] connection not found." << std::endl;

	msg->freeMessage();
#ifdef __TRACK_NETWORK__
	msg->clearTrack();
#endif

	OTSYS_THREAD_LOCK(m_outputPoolLock, "");
	m_outputMessages.push_back(msg);
	OTSYS_THREAD_UNLOCK(m_outputPoolLock, "");
}
Exemple #6
0
void Dispatcher::addTask(Task* task)
{
	OTSYS_THREAD_LOCK(m_taskLock, "");

	bool do_signal = false;
	if(!Dispatcher::m_shutdown){
		do_signal = m_taskList.empty();
		m_taskList.push_back(task);
	}
#ifdef _DEBUG
	else{
		std::cout << "Error: [Dispatcher::addTask] Dispatcher thread is terminated." << std::endl;
	}
#endif

	OTSYS_THREAD_UNLOCK(m_taskLock, "");

	// send a signal if the list was empty
	if(do_signal){
		OTSYS_THREAD_SIGNAL_SEND(m_taskSignal);
	}
}
Exemple #7
0
void Dispatcher::addTask(Task* task, bool front/* = false*/)
{
	bool signal = false;
	if(Dispatcher::m_threadState == Dispatcher::STATE_RUNNING)
	{
		OTSYS_THREAD_LOCK(m_taskLock, "");
		signal = m_taskList.empty();

		if(front)
			m_taskList.push_front(task);
		else
			m_taskList.push_back(task);

		OTSYS_THREAD_UNLOCK(m_taskLock, "");
	}
	#ifdef __DEBUG_SCHEDULER__
	else
		std::cout << "[Error - Dispatcher::addTask] Dispatcher thread is terminated." << std::endl;
	#endif

	// send a signal if the list was empty
	if(signal)
		OTSYS_THREAD_SIGNAL_SEND(m_taskSignal);
}
Exemple #8
0
void Dispatcher::stop()
{
	OTSYS_THREAD_LOCK(m_taskLock, "");
	m_threadState = Dispatcher::STATE_CLOSING;
	OTSYS_THREAD_UNLOCK(m_taskLock, "");
}