跟我一起学习ZeroMQ(10):Exclusive pair模式——ZMQ_PAIR
Exclusive pair模式简介
The exclusive pair pattern is used to connect a peer to precisely one other peer. This pattern is used for inter-thread communication across the inproc transport.
The exclusive pair pattern is formally defined by http://rfc.zeromq.org/spec:31.
ZMQ_PAIR
A socket of type ZMQ_PAIR can only be connected to a single peer at any one time. No message routing or filtering is performed on messages sent over a ZMQ_PAIR socket.
When a ZMQ_PAIR socket enters the mute state due to having reached the high water mark for the connected peer, or if no peer is connected, then any zmq_send(3) operations on the socket shall block until the peer becomes available for sending; messages are not discarded.
While ZMQ_PAIR sockets can be used over transports other than zmq_inproc(7), their inability to auto-reconnect coupled with the fact new incoming connections will be terminated while any previous connections (including ones in a closing state) exist makes them unsuitable for TCP in most cases.
ZMQ_PAIR sockets are designed for inter-thread communication across the zmq_inproc(7) transport and do not implement functionality such as auto-reconnection.
item | value |
---|---|
Compatible peer sockets | ZMQ_PAIR |
Direction | Bidirectional |
Send/receive pattern | Unrestricted |
Incoming routing strategy | N/A |
Outgoing routing strategy | N/A |
Action in mute state | Block |
示例代码
server.cpp
/*** @file server.cpp* @brief zmq_pair demo* @author shlian* @version 1.0* @date 2020-11-17*/#include <chrono>
#include <gflags/gflags.h>#include <zmqpp/context.hpp>
#include <zmqpp/context_options.hpp>
#include <zmqpp/loop.hpp>
#include <zmqpp/message.hpp>
#include <zmqpp/socket.hpp>
#include <zmqpp/socket_types.hpp>
#include <zmqpp/zmqpp.hpp>#include "../include/common.h"DEFINE_string(end_point,"tcp://*:12345","the end_point binded by the server");
DEFINE_int32(io_thread_count,2,"the io thread count of the zeromq context");
DEFINE_int32(send_interval,1000,"the interval of sending message to client");bool send_message(zmqpp::socket &socket);
bool handle_message(zmqpp::socket &socket);int main(int argc, char *argv[])
{zmqpp::context context;context.set(zmqpp::context_option::io_threads,FLAGS_io_thread_count);zmqpp::socket pair(context,zmqpp::socket_type::pair);pair.bind(FLAGS_end_point);zmqpp::loop looper;looper.add(std::chrono::milliseconds(FLAGS_send_interval),0,std::bind(send_message,std::ref(pair)));looper.add(pair,std::bind(handle_message,std::ref(pair)),zmqpp::poller::poll_in|zmqpp::poller::poll_error);looper.start();return 0;
}bool send_message(zmqpp::socket &socket)
{zmqpp::message msg;std::string tp=common::format_time();msg.add(tp);auto res=socket.send(msg);if(res){LOG_INFO("send:["<<tp<<"]");}return res;
}bool handle_message(zmqpp::socket &socket)
{zmqpp::message msg;auto res=socket.receive(msg);if(res){LOG_INFO("recv:["<<msg.get(0)<<"]");}return res;
}
client.cpp
/*** @file client.cpp* @brief zmq_pair demo* @author shlian* @version 1.0* @date 2020-11-17*/
#include <chrono>
#include <gflags/gflags.h>#include <zmqpp/context.hpp>
#include <zmqpp/context_options.hpp>
#include <zmqpp/loop.hpp>
#include <zmqpp/message.hpp>
#include <zmqpp/socket.hpp>
#include <zmqpp/socket_types.hpp>
#include <zmqpp/zmqpp.hpp>#include "../include/common.h"DEFINE_string(end_point,"tcp://127.0.0.1:12345","the end_point connected by client");
DEFINE_int32(io_thread_count,2,"the io thread count of the zeromq context");
DEFINE_int32(send_interval,1000,"the interval of sending message to client");bool send_message(zmqpp::socket &socket);
bool handle_message(zmqpp::socket &socket);int main(int argc, char *argv[])
{zmqpp::context context;context.set(zmqpp::context_option::io_threads,FLAGS_io_thread_count);zmqpp::socket pair(context,zmqpp::socket_type::pair);pair.connect(FLAGS_end_point);zmqpp::loop looper;looper.add(std::chrono::milliseconds(FLAGS_send_interval),0,std::bind(send_message,std::ref(pair)));looper.add(pair,std::bind(handle_message,std::ref(pair)),zmqpp::poller::poll_in|zmqpp::poller::poll_error);looper.start();return 0;
}bool send_message(zmqpp::socket &socket)
{zmqpp::message msg;std::string tp=common::format_time();msg.add(tp);auto res=socket.send(msg);if(res){LOG_INFO("send:["<<tp<<"]");}return res;
}bool handle_message(zmqpp::socket &socket)
{zmqpp::message msg;auto res=socket.receive(msg);if(res){LOG_INFO("recv:["<<msg.get(0)<<"]");}return res;
}
CMakeLists.txt
cmake_minimum_required( VERSION 3.8 FATAL_ERROR)
project(client LANGUAGES CXX)#set dirs
set(PROJECT_ROOT ${CMAKE_CURRENT_LIST_DIR})
message("project dir:${PROJECT_ROOT}")SET(BIN_DESTINATION ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${BIN_DESTINATION})
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${BIN_DESTINATION})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BIN_DESTINATION})#include cmake files
include(${PROJECT_ROOT}/../version.cmake)#set compile flags
#add_definitions(-std=c++11 -g -rdynamic)
set(CMAKE_CXX_FLAGS "-g3 -rdynamic -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG "-g3 -O0 ")#-fsanitize=address -fno-omit-frame-pointer -fsanitize=leak")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")#include dirs
include_directories(./ ../include/)#link dirs
link_directories(${BIN_DESTINATION})#execute
SET(SRC_MAIN client.cpp ../include/common.cpp)
add_executable( ${PROJECT_NAME} ${SRC_MAIN})
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
target_link_libraries(${PROJECT_NAME} pthread zmq zmqpp gflags)add_executable(server server.cpp ../include/common.cpp)
set_target_properties(server PROPERTIES VERSION ${PROJECT_VERSION})
target_link_libraries(server pthread zmq zmqpp gflags)
跟我一起学习ZeroMQ(10):Exclusive pair模式——ZMQ_PAIR相关推荐
- ADPRL - 近似动态规划和强化学习 - Note 10 - 蒙特卡洛法和时序差分学习及其实例 (Monte Carlo and Temporal Difference)
Note 10 蒙特卡洛法和时序差分学习 Monte Carlo and Temporal Difference 蒙特卡洛法和时序差分学习 Note 10 蒙特卡洛法和时序差分学习 Monte Car ...
- 2018 年,关于深度学习的 10 个预测
我有一种预感:2018年,所有的事情都会发生巨变.我们在2017年看到的深度学习取得的惊人突破将会以一种强大的方式延续到2018年.2017年在深度学习领域的研究成果将会应用于日常的软件应用中. 下面 ...
- thinkphp学习笔记10—看不懂的路由规则
原文:thinkphp学习笔记10-看不懂的路由规则 路由这部分貌似在实际工作中没有怎么设计过,只是在用默认的设置,在手册里面看到部分,艰涩难懂. 1.路由定义 要使用路由功能需要支持PATH_INF ...
- 分享Silverlight/WPF/Windows Phone一周学习导读(10月1日-10月15日)
分享Silverlight/WPF/Windows Phone一周学习导读(10月1日-10月15日) 本周Silverlight学习资源更新: [Silverlight入门系列]ListboxIte ...
- 分享Silverlight/WPF/Windows Phone一周学习导读(10月30日-11月6日)
分享Silverlight/WPF/Windows Phone一周学习导读(10月30日-11月6日) 本周Silverlight学习资源更新 Silverlight 定位 niejunhua [学习 ...
- 分享Silverlight/WPF/Windows Phone一周学习导读(10月16日-10月22日)
分享Silverlight/WPF/Windows Phone一周学习导读(10月16日-10月22日) 本周Silverlight学习资源更新 Silverlight:分包下载及SEO优化方案 菩提 ...
- SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传
SpringMVC:学习笔记(10)--整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...
- 最值得阅读学习的 10 个 C 语言开源项目代码
本文转载于: 最值得阅读学习的 10 个 C 语言开源项目代码 从扩展思路的角度来说,一个程序员应该好好读过这样一些代码: 一个操作系统内核 一个编译器(如:gcc,lua) 一个解释器(如:pyth ...
- springmvc学习笔记(10)-springmvc注解开发之商品改动功能
springmvc学习笔记(10)-springmvc注解开发之商品改动功能 springmvc学习笔记(10)-springmvc注解开发之商品改动功能 标签: springmvc springmv ...
最新文章
- JESD204B与LVDS接口并行 管线式ADC延迟问题分析及解答
- 使用malloc创建头结点的坑
- 201612-5 卡牌游戏
- 太吾绘卷第一世攻略_耽美推文-BL-仿佛在攻略一只河豚
- vim简单命令教程-firstblood
- 鸿蒙服务卡片-哔哩哔哩弹幕姬
- Netty 高性能架构设计
- TCP/IP:SCTP报文格式
- innobackupex做MySQL增量备份及恢复
- 【BZOJ 3505】 [Cqoi2014]数三角形 容斥原理+排列组合+GCD
- Notification的使用,以及他的监听方法
- Linux内存管理 (3)内核内存的布局图
- 火星人学习第二周——虚幻引擎蓝图应用与开发
- 海思3559万能平台搭建:YUV422的踩坑记录
- Prince和学生们2
- Dragonfly 三维可视化数据分析处理软件-切片分析工具使用教程
- php调用itunes,使用cURL和PHP检索iTunes App Store XML
- Mac电脑进入恢复模式详细教程
- Linux内核源码分析-scsi子系统-让磁盘转起来-sd_spinup_disk
- 织梦php开发tags功能开发,织梦全网最新联动筛选功能的实现(单选和多选)可显示分类的文章...