1. 程式人生 > >ZeroMQ使用匯總

ZeroMQ使用匯總

size conn gre ear init sdn tail https message

ZeroMQ,史上最快的消息隊列 —– ZMQ的學習和研究

ZeroMQ 的模式

[架構] ZeroMQ 深度探索(一)

消息隊列ZeroMQ

服務端使用流程:

  

void* m_Context;
void* m_sktMsgVideoFrame;

m_sktMsgVideoFrame = zmq_socket(m_Context,ZMQ_PUB);
    int ret = zmq_bind(m_sktMsgVideoFrame,bytesMsgVideoFrameAddress.data());

 zmq_msg_t msg;

   zmq_msg_init_size(&msg,frameBuffSize+MIN_MSG_LEN);
   memcpy(zmq_msg_data(&msg),MSG_VIDEO_FRAME,5);

   memcpy((char*)zmq_msg_data(&msg)+5, frameBuff, frameBuffSize); //

   int iRet = zmq_msg_send(&msg, (zmq_msg_t*)m_sktMsgVideoFrame, 0);


   zmq_msg_close(&msg);


zmq_close(m_sktMsgVideoFrame);
zmq_ctx_shutdown(m_Context);

  

客戶端流程:

QByteArray bytesMsgControlAddress = g_strMsgControlAddress.toUtf8();
    QByteArray bytesMsgReqCaptureRetAddress = g_strMsgReqCaptureRetAddress.toUtf8();
    QByteArray bytesMsgVideoFrameAddress = g_strMsgVideoFrameAddress.toUtf8();


    void* context = zmq_ctx_new();

    void* m_pSktMsgControl = zmq_socket(context, ZMQ_SUB);
    int ret = zmq_connect(m_pSktMsgControl,bytesMsgControlAddress.data());
    qDebug()<<"MSG_CONTROL PULL 連接地址:"<<g_strMsgControlAddress;
    qDebug()<<"MSG_CONTROL PULL 連接結果:"<<ret;
    ret = zmq_setsockopt(m_pSktMsgControl, ZMQ_SUBSCRIBE, "", 0);/// 必須添加該語句對消息濾波,否則接受不到消息


    void* m_pSktMsgReqCaptureRet = zmq_socket(context, ZMQ_SUB);
    ret = zmq_connect(m_pSktMsgReqCaptureRet, bytesMsgReqCaptureRetAddress.data());
    qDebug()<<"MSG_REQ_CAPUTRE_RET PULL 連接地址:"<<g_strMsgReqCaptureRetAddress;
    qDebug()<<"MSG_REQ_CAPUTRE_RET PULL 連接結果:"<<ret;

    ret = zmq_setsockopt(m_pSktMsgReqCaptureRet, ZMQ_SUBSCRIBE, "", 0);/// 必須添加該語句對消息濾波,否則接受不到消息

    void* m_pSktVideoFrame = zmq_socket(context, ZMQ_SUB);
    ret = zmq_connect(m_pSktVideoFrame, bytesMsgVideoFrameAddress.data());    

    qDebug()<<"MSG_VIDEO_FRAME PULL 連接地址:"<<g_strMsgVideoFrameAddress;
    qDebug()<<"MSG_VIDEO_FRAME PULL 連接結果:"<<ret;

    ret = zmq_setsockopt(m_pSktVideoFrame, ZMQ_SUBSCRIBE, "", 0);/// 必須添加該語句對消息濾波,否則接受不到消息

zmq_msg_t msg;
    int responseLen = zmq_msg_init(&msg);
    responseLen = zmq_msg_recv(&msg,receiver, ZMQ_DONTWAIT);

zmq_msg_close(&msg);

zmq_close(m_pSktMsgControl);
    zmq_close(m_pSktMsgReqCaptureRet);
    zmq_close(m_pSktVideoFrame);

    zmq_ctx_shutdown(context);

  

ZeroMQ使用匯總