My Project
|
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 |
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>
|
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.
sQueueName | Name of the queue. |
usage | The usage. The most common usage is perhaps IPQU_open_or_create |
|
inline |
clear all messages.
This function is usually called by the owner of the queue to empty the queue.
|
inline |
if this is valid.
|
inline |
blocking call to force receive a message.
|
inline |
remove all messages.
|
inline |
non-blocking call
|
inline |
only send if queue is not full.
non-blocking. Internally it check available queue size and send via the blocking send() method.