ZMQ socket type介绍

socket types

ZMQ定义有多种套接字类型,可以根据不同消息模型对其进行分类。
可参考
ZeroMQ总结
http://api.zeromq.org/3-2:zmq-socket

Request-reply pattern 请求应答模式

The request-reply pattern is used for sending requests from a ZMQ_REQ client to one or more ZMQ_REP services, and receiving subsequent replies to each request sent.

典型通信模式,客户端发送请求给服务端,并接受来着服务端的应答。

ZMQ_REQ

socket type设为此模式表明此socket用于客户端,zmq_send()和zmq_recv()需被交替调用,发送一次消息,就需接收一次应答。

Each request sent is round-robined among all services, and each reply received is matched with the last issued request.

请求在所有服务端中循环发送,接收的应答为最后请求的应答。
socket会阻塞,但不会丢弃消息

ZMQ_REP

与上面的ZMQ_REP对应,用于服务端接收请求和发送应答,需zmq_recv和zmq_send交替调用。

Each request received is fair-queued from among all clients, and each reply sent is routed to the client that issued the last request. If the original requester doesn’t exist any more the reply is silently discarded.

从客户端接收请求消息使用了公平队列,回应客户端时,所有的reply都会被路由到最后下达请求的客户端。
丢弃且不阻塞

ZMQ_DEALER

此类型是对request/reply sockets的扩展。

Each message sent is round-robined among all connected peers, and each message received is fair-queued from all connected peers.
消息采用循环轮询方式发送给所以连接端,以公平队列方式接收消息.
阻塞且不丢弃
When a ZMQ_DEALER socket is connected to a ZMQ_REP socket each message sent must consist of an empty message part, the delimiter, followed by one or more body parts.

ZMQ_ROUTER

此类型是对request/reply sockets的扩展。

When receiving messages a ZMQ_ROUTER socket shall prepend a message part containing the identity of the originating peer to the message before passing it to the application. Messages received are fair-queued from among all connected peers. When sending messages a ZMQ_ROUTER socket shall remove the first part of the message and use it to determine the identity of the peer the message shall be routed to. If the peer does not exist anymore the message shall be silently discarded by default, unless ZMQ_ROUTER_BEHAVIOR socket option is set to 1.

当接收消息时,会自动在消息顶部加入来源地址标识符,公平队列;当发生时去掉这个标识符,并根据这个标识符路由到相应端点,若此端点不存在则丢弃消息,除非ZMQ_ROUTER_BEHAVIOR socket option is set to 1
当为静默状态时丢弃后续发送到此socket的信息,同样若对端也为静默状态,丢弃发生信息。

When a ZMQ_REQ socket is connected to a ZMQ_ROUTER socket, in addition to the identity of the originating peer each message received shall contain an empty delimiter message part. Hence, the entire structure of each received message as seen by the application becomes: one or more identity parts, delimiter part, one or more body parts. When sending replies to a ZMQ_REQ socket the application must include the delimiter part.

Publish-subscribe pattern 订阅模式

The publish-subscribe pattern is used for one-to-many distribution of data from a single publisher to multiple subscribers in a fan out fashion.

即一个发布者多个订阅者,发布者发布一条消息,订阅者都会收到。

ZMQ_PUB

此类型表明socket为发布者,不可使用zmq_rev(),当静默时,待发送消息都会丢弃。

ZMQ_SUB

订阅者,初始的ZMQ_SUB Socket没有订阅任何消息,可以通过设置ZMQ_SUBSRIBE选项来指定需要订阅的消息。
ZMQ_SUB Socket没有实现zmq_send函数,所以不能对其调用zmq_send函数!

ZMQ_XPUB

与ZMQ_PUB相比,此类型可接收来自来自订阅者的订阅信息

Subscription message is a byte 1 (for subscriptions) or byte 0 (for unsubscriptions) followed by the subscription body.

ZMQ_XSUB

与ZMQ_SUB相比,通过发送订阅信息给发布者来订阅。

Pipeline pattern 流水线模式

The pipeline pattern is used for distributing data to nodes arranged in a pipeline. Data always flows down the pipeline, and each stage of the pipeline is connected to at least one node. When a pipeline stage is connected to multiple nodes data is round-robined among all connected nodes.

流水线模式用于向布置在流水线中的节点分发数据。数据总是沿着流水线向下流动,并且流水线的每个级连接到至少一个节点。当流水线级连接到多个节点时,在所有连接的节点中对数据进行轮询

ZMQ_PUSH

发送数据给下游节点,采用轮询方式发送,没有zmq_recv().
阻塞不丢弃。

ZMQ_PULL

从上游节点接收消息,消息以公平队列方式从上游上游连接节点接收。
没有实现zmq_send().

Exclusive pair pattern 独占对模式

one to one ,用于线程间通信

ZMQ_PAIR

只能连接到一个端点。

ZMQ_PAIR sockets are designed for inter-thread communication across the zmq_inproc(7) transport and do not implement functionality such as auto-reconnection. ZMQ_PAIR sockets are considered experimental and may have other missing or broken aspects.

你可能感兴趣的:(网络通信)