ZMQ Version3.2.6基础介绍
来源:互联网 发布:资产管理系统源码 编辑:程序博客网 时间:2024/06/07 03:47
### ZMQ Version3.2.6基础介绍
可参考ØMQ/3.2.6 API Reference
ZMQ是基于标准socket接口的扩展库,ZMQ套接字提供异步消息队列,多消息模式,消息过滤(订阅),对多个传输协议的无缝接口等的抽象。下面介绍各模块
Context 上下文
使用ZMQ必须先创建ZMQ上下文,就如同使用设备要获得设备上下文,当使用完后需销毁。
创建新的context
void *zmq_ctx_new (); //thread safety
Return value:如果成功返回指向ZMQ上下文的handle,不然返回NULL
context属性相关
int zmq_ctx_set (void *context, int option_name, int option_value); //set int zmq_ctx_get (void *context, int option_name); //get
其中option_name可为:
ZMQ_IO_THREADS: number of I/O threads
ZMQ_MAX_SOCKETS: maximum number of sockets
Return value: 如果成功返回0,失败返回-1
销毁ZMQ context
int zmq_ctx_destroy (void *context);
Return value: 如果成功返回0,失败返回-1
Context termination is performed in the following steps:
- Any blocking operations currently in progress on sockets open within context shall return immediately with an error code of ETERM. With the exception of zmq_close(), any further operations on sockets open within context shall fail with an error code of ETERM.
- After interrupting all blocking calls, zmq_ctx_destroy() shall block until the following conditions are satisfied: All sockets open within context have been closed with zmq_close().
- For each socket within context, all messages sent by the application with zmq_send() have either been physically transferred to a network peer, or the socket’s linger period set with the ZMQ_LINGER socket option has expired.
监视ZMQ context
int zmq_ctx_set_monitor (void *context, zmq_monitor_fn *monitor);
Message
message是在应用程序之间或者同一应用程序内的不同组件之间传递的数据单元。此篇主要介绍ZMQ的网络通讯部分,虽然message应该也可以用于网络,但还是暂不介绍此部分。
Sockets
ØMQ sockets present an abstraction of a asynchronous message queue, with the exact queueing semantics depending on the socket type in use.
即socket为异步消息队列的抽象,其队列形式由socket type决定。
创建socket
void *zmq_socket (void *context, int type);
根据ZMQ上下文和socket类型创建socket
Return value: 成功返回指向socket的指针,失败返回NULL。
socket type在之后详细介绍。
关闭socket
int zmq_close (void *socket);
The zmq_close() function shall destroy the socket referenced by the socket argument. Any outstanding messages physically received from the network but not yet received by the application with zmq_recv() shall be discarded. The behaviour for discarding messages sent by the application with zmq_send() but not yet physically transferred to the network depends on the value of the ZMQ_LINGER socket option for the specified socket.
关闭后仍未接收的消息会被丢弃,对于发送消息的处理则取决于the value of the ZMQ_LINGER socket option
Return value: 0 for success,-1 for error
socket属性相关
int zmq_getsockopt (void *socket, int option_name, void *option_value, size_t *option_len); \\getint zmq_setsockopt (void *socket, int option_name, const void *option_value, size_t option_len); \\set
Caution: All options, with the exception of ZMQ_SUBSCRIBE, ZMQ_UNSUBSCRIBE, ZMQ_LINGER, ZMQ_ROUTER_MANDATORY and ZMQ_XPUB_VERBOSE only take effect for subsequent socket bind/connects.
对于option_name在之后详细介绍。
建立消息流
int zmq_bind (void *socket, const char *endpoint); \\server,accept incoming connections on a socketint zmq_connect (void *socket, const char *endpoint); \\client,create outgoing connection from socket
zmq_bind绑定socket与本地端点。
zmq_connect连接socket与服务器端点。
endpoint格式为:transport://address,其中transport可为:
tcp
unicast transport using TCP
ipc
local inter-process communication transport
inproc
local in-process (inter-thread) communication transport
pgm, epgm
reliable multicast transport using PGM
cation
1. 对于大多数传输和套接字类型,连接并不是立即执行,因此成功调用zmq_connect()并不意味着连接已经或者实际上可以被建立。因此,对于大多数传输和套接字类型,服务器套接字绑定和客户端套接字连接到它的顺序无关紧要。
第一个例外是使用inproc:// transport:在调用zmq_connect()之前必须调用zmq_bind()。
第二个例外是ZMQ_PAIR套接字,它不会自动重新连接到端点。
2. 对于zmq_bind(),调用后如果没有connection则进入静默状态,若有则进入ready state;对于zmq_connect(),调用后进入ready state
发生和接受消息
int zmq_msg_send (zmq_msg_t *msg, void *socket, int flags);int zmq_msg_recv (zmq_msg_t *msg, void *socket, int flags);int zmq_send (void *socket, void *buf, size_t len, int flags);int zmq_recv (void *socket, void *buf, size_t len, int flags);
消息可通过两种方式传递,其一通过结构体zmq_msg_t,其二通过buf(字符流),
send flags,下述标志结合:
1. ZMQ_DONTWAIT 非阻塞模式,如果message不能加入消息队列,则失败
2. ZMQ_SNDMORE 消息分成多部分发生,此为其中一部分(不为结尾),最后一部分,不应包含ZMQ_SNDMORE
recv flag:
ZMQ_DONTWAIT 如果队列中没有消息则失败,errno set to EAGAIN.
Return value: 成功返回消息字节数,失败-1.
Error handling
ZMQ的error机制是建立在POSIX系统上的,当函数遇到错误返回NULL或者-1,而error code会记录在errno变量中。在非POSIX系统上需通过zmq_errno()间接获得errno的值,
int zmq_errno (void);const char *zmq_strerror (int errnum);
通过zmq_strerror()可把error code转换为字符串。
其它
Input/output multiplexing:
Proxies:
Miscellaneous:
…
…
- ZMQ Version3.2.6基础介绍
- ZMQ socket type介绍
- ZMQ 第一章 ZeroMQ基础
- ZMQ 第一章 ZeroMQ基础
- zmq中zmq_poll()函数介绍
- ZMQ
- ZMQ
- ZMQ
- 6.基于ZMQ的游戏网络层基础架构
- ZMQ源码分析(一)-- 基础数据结构的实现
- 使用zmq,probuf,缓冲池实现序列化和反序列化框架(二)-ZMQ介绍
- zmq封装
- ZMQ简介
- php-zmq
- Zmq封装
- ZMQ简介
- zmq学习
- ZMQ guider
- SSM搭建-Spring之IOC的XML注入(5)
- Mysql支持的数据类型(总结)
- 231. Power of Two (判断一个数是否是2的幂)
- AsyncTask源码阅读笔记
- 常见组件对象的搞法
- ZMQ Version3.2.6基础介绍
- 如何把andorid5.1的source中的framework的class提供给android工程师调试
- 最短路算法
- 232. Implement Queue using Stacks (用栈实现队列)
- 离散化操作
- android UEventObserver的用法
- synchronized 中 intern 用法
- poj 3040
- js验证表单输入框