Esempio n. 1
0
//blocking call
Message *MessageEndpoint::PopMessage(Ticket &ticket, int timeout)
{
	Message *ret;

	MessageQueue *queue = m_queues.GetByOurSerial(ticket.m_ourSerialNum);
	if(queue == NULL)
	{
		//Tried to read from a queue, but it didn't exist. We must have closed it, and then tried reading.
		//	This is a protocol error
		return new ErrorMessage(ERROR_PROTOCOL_MISTAKE);
	}

	//TODO: This is not quite right. We should keep returning valid messages as long as
	//	we have them. We should ask the MessageQueue for a new message (maybe peek?)
	//	and only return a shutdown message if one doesn't exist there.

	//If we're shut down, then return a shutdown message
	{
		Lock shutdownLock(&m_isShutdownMutex);
		if(m_isShutDown)
		{
			return new ErrorMessage(ERROR_SOCKET_CLOSED);
		}
	}

	ret = queue->PopMessage(timeout);

	if(ret->m_messageType == ERROR_MESSAGE)
	{
		ErrorMessage *error = (ErrorMessage*)ret;
		if(error->m_errorType == ERROR_TIMEOUT)
		{
			m_consecutiveTimeouts++;
			//TODO: deal with timeouts
		}
	}

	return ret;
}