📄 message_queue.cpp
字号:
/******************************************************************************
*******************************************************************************
** **
** Copyright (c) 2006 Videon Central, Inc. **
** All rights reserved. **
** **
** The computer program contained herein contains proprietary information **
** which is the property of Videon Central, Inc. The program may be used **
** and/or copied only with the written permission of Videon Central, Inc. **
** or in accordance with the terms and conditions stipulated in the **
** agreement/contract under which the programs have been supplied. **
** **
*******************************************************************************
******************************************************************************/
/**
* @file message_queue.cpp
*
* $Revision: 1.5 $
*
* Implements a Message Queue class
*
*/
#include "message_queue.h"
/**
*******************************************************************************
* MessageQueue::MessageQueue initializes object to valid state
*
*******************************************************************************/
MessageQueue::MessageQueue ( void )
{
m_messageQueueHandle = 0;
m_queueSize = 0;
m_messageSize = 0;
}
/**
*******************************************************************************
* MessageQueue::Construct allocates required resources, prepares object for use
*
* This method is private and will be automatically called from
* ReferenceCounter::AddReference method and will be inside a critical section
*
* @return VDVD_SUCCESS if the function is successful
*******************************************************************************/
VDVD_ERROR MessageQueue::Construct ( void )
{
if (( m_queueSize == 0) || (m_messageSize == 0))
{
return ( VDVD_ERROR_OBJECT_HAS_INVALID_STATE );
}
m_messageQueueHandle = OS_MsgQCreate(m_queueSize, m_messageSize, 0);
VDVD_ASSERT_ERROR( (m_messageQueueHandle==0), VDVD_ERROR_PLATFORM_FAILED );
return ( VDVD_SUCCESS );
}
/**
*******************************************************************************
* MessageQueue::Destruct frees allocated resources, prepares object for non-use
*
* This method is private and will be automatically called from
* ReferenceCounter::DeleteReference method when the object is no longer being
* referenced(used). This code will execute inside a critical section.
*
* @return VDVD_SUCCESS if the function is successful
*******************************************************************************/
VDVD_ERROR MessageQueue::Destruct ( void )
{
OS_STATUS osStatus;
m_queueSize = 0;
m_messageSize = 0;
VDVD_ASSERT_ERROR( (m_messageQueueHandle==0), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );
osStatus = OS_MsgQDelete( m_messageQueueHandle );
VDVD_ASSERT_ERROR( (osStatus!=OS_OK), VDVD_ERROR_PLATFORM_FAILED );
return ( VDVD_SUCCESS );
}
/**
*******************************************************************************
* MessageQueue::Create Creates message queue
*
* Creates message queue via Construct method automatically called from method
* ReferenceCounter::AddReference.
*
* @param queueSize max number of messages queue can hold
* @param messageSize size(in bytes) of message. (all messages must be same size)
*
* @return VDVD_SUCCESS if the function is successful
* VDVD_ERROR_OBJECT_HAS_INVALID_STATE if the queue has already been referenced(created)
*******************************************************************************/
VDVD_ERROR MessageQueue::Create( uint32 queueSize, uint32 messageSize )
{
VDVD_ERROR error = VDVD_SUCCESS;
VDVD_ASSERT_ERROR( (queueSize==0), VDVD_ERROR_INVALID_PARAMETER );
VDVD_ASSERT_ERROR( (messageSize==0), VDVD_ERROR_INVALID_PARAMETER );
m_mutex.Lock();
if (( m_queueSize == 0 ) && ( m_messageSize == 0 ))
{
m_queueSize = queueSize;
m_messageSize = messageSize;
error = AddReference();
}
else
{
error = VDVD_ERROR_OBJECT_HAS_INVALID_STATE;
}
m_mutex.Unlock();
return( error );
}
/**
*******************************************************************************
* MessageQueue::Destroy Destroys message queue
*
* Deletes reference. If there are no more references(no one is using the object)
* the Destruct method will be called automatically from the method
* ReferenceCounter::DeleteReference.
*
* @return VDVD_SUCCESS if the function is successful
* VDVD_ERROR_OBJECT_HAS_INVALID_STATE if the queue has already been referenced(created)
*******************************************************************************/
VDVD_ERROR MessageQueue::Destroy( void )
{
return ( DeleteReference() );
}
/**
*******************************************************************************
* MessageQueue::Send Adds a message to the message queue
*
* @param message pointer to a buffer containing the message
*
* @return VDVD_SUCCESS if the function is successful
*******************************************************************************/
VDVD_ERROR MessageQueue::Send( void* message )
{
VDVD_ASSERT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );
VDVD_ASSERT_ERROR( (message==NULL), VDVD_ERROR_INVALID_PARAMETER );
VDVD_RAISE_ERROR( (VDVD_ERROR)OS_MsgQSend( m_messageQueueHandle, (char*)message, m_messageSize, OS_WAIT_FOREVER, 0 ) );
return ( VDVD_SUCCESS );
}
/**
*******************************************************************************
* MessageQueue::Receive Reads a message from the message queue
*
* @param message pointer to a buffer containing the message
*
* @return VDVD_SUCCESS if the function is successful
*******************************************************************************/
VDVD_ERROR MessageQueue::Receive( void* message, int timeoutMilliseconds )
{
VDVD_ASSERT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );
VDVD_ASSERT_ERROR( (message==NULL), VDVD_ERROR_INVALID_PARAMETER );
VDVD_RAISE_ERROR( (VDVD_ERROR)OS_MsgQReceive( m_messageQueueHandle, (char*)message, m_messageSize, timeoutMilliseconds ));
return ( VDVD_SUCCESS );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -