My Project
Public Types | Public Member Functions | Protected Attributes | List of all members
ParaEngine::CInterprocessQueueT< MAX_QUEUE_SIZE, MAX_PACKET_SIZE, MessageQueueType > Class Template Reference

It is for sending and receiving InterProcessMessage. More...

#include <InterprocessQueue.hpp>

Public Types

typedef boost::shared_ptr< typename MessageQueueType > message_queue_t
 
typedef boost::array< char, MAX_PACKET_SIZE > Buffer_Type
 

Public Member Functions

 CInterprocessQueueT (const char *sQueueName, IPQueueUsageEnum usage=IPQU_open_or_create)
 Since the message queue is a global object, it is not removed even the queue object is deleted. More...
 
bool IsValid ()
 if this is valid. More...
 
void Cleanup ()
 
bool Remove ()
 remove all messages. More...
 
void Clear ()
 clear all messages. More...
 
IPQueueReturnCodeEnum send (const InterProcessMessage &msg, unsigned int nPriority=0)
 send a message and block if queue is full until message is sent out.
 
IPQueueReturnCodeEnum try_send (const InterProcessMessage &msg, unsigned int nPriority=0)
 only send if queue is not full. More...
 
IPQueueReturnCodeEnum receive (InterProcessMessage &msg, unsigned int &nPriority)
 blocking call to force receive a message. More...
 
IPQueueReturnCodeEnum try_receive (InterProcessMessage &msg, unsigned int &nPriority)
 non-blocking call More...
 
const std::string & GetName ()
 

Protected Attributes

std::string m_sQueueName
 
int m_queue_size
 
int m_max_packet_size
 
IPQueueUsageEnum m_usage
 
message_queue_t m_msg_queue
 
CInterProcessMessageOut_gen m_out_gen
 
InterProcessMessageIn m_input_msg
 
CInterProcessMessageIn_parser m_parser
 
Buffer_Type m_buffer
 Buffer for incoming data.
 
ParaEngine::Mutex m_mutex
 

Detailed Description

template<int MAX_QUEUE_SIZE = 2000, int MAX_PACKET_SIZE = 256, typename MessageQueueType = boost::interprocess::message_queue>
class ParaEngine::CInterprocessQueueT< MAX_QUEUE_SIZE, MAX_PACKET_SIZE, MessageQueueType >

It is for sending and receiving InterProcessMessage.

internally we use shared memory to create the message queue. Each message queue have a globally unique string name and a fixed max size Two processes can create CInterprocessQueue using the same name and send and receive messages via it. Usually one process is producer and the other is consumer. To achieve bi-directional communications, we can use two queues.

—++ Queue Life Time Since the message queue is a global object, it is not removed even the queue object is deleted. One must call Remove() method explicitly to remove a queue. Therefore to create a new empty queue, one usually needs to call Clear() to ensure that queue is emptied.

—++ Example Usage Message queue can be used between process or between threads of the same process, etc.

<verbatim> { process 1: writer CInterprocessQueue ipQueueWriter("InstanceName", IPQU_open_or_create); since we are the owner, clear all messages. ipQueueWriter.Clear(); InterProcessMessage msg_out;

for (int i=0;i<10;++i) { msg_out.m_method = "NPLv1"; msg_out.m_nMsgType = 100; msg_out.m_nParam1 = 200; msg_out.m_nParam2 = 300; msg_out.m_filename = "test.lua"; msg_out.m_from = "FromName"; msg_out.m_code = "msg={param=10}"; if(ipQueueWriter.try_send(msg_out, 1) == IPRC_OK){ OutputDebugString("Test Case i: msg sent\n"); } } }

{ process 2: reader CInterprocessQueue ipQueueReader("InstanceName", IPQU_open_or_create); InterProcessMessage msg_in; unsigned int nPriority = 0; while(ipQueueReader.try_receive(msg_in, nPriority) == IPRC_OK) { assert(msg_in.m_method == "NPLv1"); assert(msg_in.m_nMsgType == 100); assert(msg_in.m_nParam1 == 200); assert(msg_in.m_nParam2 == 300); assert(msg_in.m_from == "FromName"); assert(msg_in.m_filename == "test.lua"); assert(msg_in.m_code == "msg={param=10}"); assert(nPriority == 1); OutputDebugString("Test Case i passed\n"); } }

</verbatim>

Constructor & Destructor Documentation

§ CInterprocessQueueT()

template<int MAX_QUEUE_SIZE = 2000, int MAX_PACKET_SIZE = 256, typename MessageQueueType = boost::interprocess::message_queue>
ParaEngine::CInterprocessQueueT< MAX_QUEUE_SIZE, MAX_PACKET_SIZE, MessageQueueType >::CInterprocessQueueT ( const char *  sQueueName,
IPQueueUsageEnum  usage = IPQU_open_or_create 
)
inline

Since the message queue is a global object, it is not removed even the queue object is deleted.

One must call Remove() method explicitly to remove a queue. Therefore to create a new empty queue, one usually needs to call Clear() to ensure that queue is emptied.

Parameters
sQueueNameName of the queue.
usageThe usage. The most common usage is perhaps IPQU_open_or_create

Member Function Documentation

§ Clear()

template<int MAX_QUEUE_SIZE = 2000, int MAX_PACKET_SIZE = 256, typename MessageQueueType = boost::interprocess::message_queue>
void ParaEngine::CInterprocessQueueT< MAX_QUEUE_SIZE, MAX_PACKET_SIZE, MessageQueueType >::Clear ( void  )
inline

clear all messages.

This function is usually called by the owner of the queue to empty the queue.

§ IsValid()

template<int MAX_QUEUE_SIZE = 2000, int MAX_PACKET_SIZE = 256, typename MessageQueueType = boost::interprocess::message_queue>
bool ParaEngine::CInterprocessQueueT< MAX_QUEUE_SIZE, MAX_PACKET_SIZE, MessageQueueType >::IsValid ( )
inline

if this is valid.

§ receive()

template<int MAX_QUEUE_SIZE = 2000, int MAX_PACKET_SIZE = 256, typename MessageQueueType = boost::interprocess::message_queue>
IPQueueReturnCodeEnum ParaEngine::CInterprocessQueueT< MAX_QUEUE_SIZE, MAX_PACKET_SIZE, MessageQueueType >::receive ( InterProcessMessage msg,
unsigned int &  nPriority 
)
inline

blocking call to force receive a message.

Returns
IPRC_OK if a message is received. or IPRC_FAILED

§ Remove()

template<int MAX_QUEUE_SIZE = 2000, int MAX_PACKET_SIZE = 256, typename MessageQueueType = boost::interprocess::message_queue>
bool ParaEngine::CInterprocessQueueT< MAX_QUEUE_SIZE, MAX_PACKET_SIZE, MessageQueueType >::Remove ( )
inline

remove all messages.

§ try_receive()

template<int MAX_QUEUE_SIZE = 2000, int MAX_PACKET_SIZE = 256, typename MessageQueueType = boost::interprocess::message_queue>
IPQueueReturnCodeEnum ParaEngine::CInterprocessQueueT< MAX_QUEUE_SIZE, MAX_PACKET_SIZE, MessageQueueType >::try_receive ( InterProcessMessage msg,
unsigned int &  nPriority 
)
inline

non-blocking call

Returns
IPRC_OK if a message is received. or IPRC_QUEUE_IS_EMPTY is no complete message is read. or IPRC_FAILED

§ try_send()

template<int MAX_QUEUE_SIZE = 2000, int MAX_PACKET_SIZE = 256, typename MessageQueueType = boost::interprocess::message_queue>
IPQueueReturnCodeEnum ParaEngine::CInterprocessQueueT< MAX_QUEUE_SIZE, MAX_PACKET_SIZE, MessageQueueType >::try_send ( const InterProcessMessage msg,
unsigned int  nPriority = 0 
)
inline

only send if queue is not full.

non-blocking. Internally it check available queue size and send via the blocking send() method.


The documentation for this class was generated from the following file: