MessageQueue.h
#ifndef MESSAGEQUEUE__H__
#define MESSAGEQUEUE__H__
#include
#include
//Message pair:
//
// |-->REP
//REQ -|-->REP
// |-->REP
//
// |-->SUB
//PUB -|-->SUB
// |-->SUB
//
// |-->PULL -- PUSH -->|
//PUSH -|-->PULL -- PUSH -->|-->PULL
// |-->PULL -- PUSH -->|
//
//PUSH -->|
//PUSH -->|-->PULL
//PUSH -->|
//
//REQ -->| |-->REP
//REQ -->|-->ROUTER -- DEALER -->|-->REP
//REQ -->| |-->REP
//
//PAIR -->PAIR -- PAIR -->PAIR
//
// |-->DEALTER
//ROUTER -->|-->DEALTER
// |-->DEALTER
//
// |-->DEALER
//DEALER -->|-->DEALER
// |-->DEALER
//
//PUB and SUB
//REQ and REP
//REQ and ROUTER
//DEALER and REP
//DEALER and ROUTER
//DEALER and DEALER
//ROUTER and ROUTER
//PUSH and PULL
//PAIR and PAIR
//
class CMessageQueue
{
public:
CMessageQueue(int nType, int nSafe = 1); //inproc : 0
~CMessageQueue();
//Bind Type:
//inproc://XX for thread
//tcp://*:port
//ipc://XXX
//pgm://192.168.1.1;239.192.1.1:6666 (only used with SUB and PUB)
//epgm://eth0;219.192.1.1:6666 (only used with SUB and PUB)
void BindSocket(const char* szAddr);
void Connect(const char* szAddr);
//Flags:
// ZMQ_NOBLOCK
// ZMQ_SNDMORE
int SendMsg(const char* szMsg, uint32_t nLen, int nFlags = 0);
//Flags:
// ZMQ_NOBLOCK
int RecvMsg(char* szMsg, uint32_t &nLen, int nFlags = 0);
//Type:
void SetSockOpt(int nType, const void* pOptVal, size_t nOptValLen);
protected:
zmq::context_t m_Context;
zmq::socket_t m_Socket;
};
#endif
MessageQueue.cpp
#include
#include "MessageQueue.h"
using namespace zmq;
CMessageQueue::CMessageQueue(int nType, int nSafe)
:m_Context(nSafe), m_Socket(m_Context, nType)
{
}
CMessageQueue::~CMessageQueue()
{
}
void CMessageQueue::BindSocket(const char* szAddr)
{
m_Socket.bind(szAddr);
}
void CMessageQueue::Connect(const char* szAddr)
{
zmq_connect(m_Socket, szAddr);
}
int CMessageQueue::SendMsg(const char* szMsg, uint32_t nLen, int nFlags)
{
message_t msg(nLen);
memcpy((void*)msg.data(), szMsg, nLen);
return m_Socket.send(msg, nFlags);
}