Message Queuing Library--ZEROMQ

Source: Internet
Author: User
Tags epoll set socket

Message Queuing Library--ZEROMQ

ZeroMQ (ZMQ) is a multi-threaded network library based on Message Queuing that abstracts the underlying details of socket types, connection processing, frames, and even routing, providing sockets that span multiple transport protocols.

ZMQ is a new layer in network communication, between the application layer and the Transport layer (according to TCP/IP division), it is a scalable layer, can run in parallel, dispersed in the distributed system.

ZMQ is not a separate service, but an embedded library that encapsulates the functions of network communication, Message Queuing, thread scheduling, and provides a concise API to the upper layer, and applications can implement high-performance network communication by loading library files and invoking API functions.

Main thread and I/O thread:

I/O thread , ZMQ creates the corresponding number of I/O threads based on the parameters passed in when the user invokes the Zmq_init function. Each I/O thread has a poller,poller that is bound to it using the classic reactor pattern implementation.

Poller using different network I/O models (SELECT, poll, Epoll, Devpoll, Kequeue, etc.) based on different operating system platforms, all I/O operations are asynchronous and threads are not blocked.

The main thread communicates with the I/O thread through the mail box to deliver the message.

Server, creates zmq_listener in the main thread, binds it to the I/O thread in the form of a mail box message, and the I/O thread adds Zmq_listener to Poller to listen for read events.

Client, creates a zmq_connecter in the main thread, binds it to an I/O thread in the form of a message from the mail box, and the I/O thread adds zmq_connecter to the Poller for dictation events.

When the client communicates with the server for the first time, Zmq_init is created to send the identity for authentication. After the certification, the two sides will create a session for this connection, and then communicate through the session.

Each session is associated with the corresponding read/write pipeline, and the main thread is sending and receiving messages just reading/writing data from the pipeline separately. The session does not actually exchange I/O data with kernel, but instead exchanges I/O data with kernel by plugin to the engine in the session.

The ZMQ divides the message traffic into 4 different models:

    1. A-to-one pair model (Exclusive-pair), which can be considered a TCP Connection, but TCP server can only accept one connection. The data can flow in two directions, which differs from the subsequent request response model.
    2. The request response Model (request-reply), which is initiated by the client and responded by the server, differs from a one-to-many pair model in that there can be multiple clients.
    3. Publish subscription model (Publish-subscribe), Publish-side distribution of data, and do not care whether to send all the information to the Subscribe end. If the publish side begins to publish information, the subscribe end is not connected, and the information is discarded directly. Subscribe end can only receive, can not feedback, and at the subscribe end of consumption slower than the publish end of the case, will accumulate data on the subscribe side.
    4. Pipeline model (Push-pull), one-way push data flow from the push side to the pull end. If multiple pull ends are connected to the push side at the same time, the push side does a load balancer internally, using an evenly distributed algorithm that distributes all message balances to the pull end. Compared with the publish-subscribe model, the pipeline model will not be consumed without consumers, and can provide a multi-consumer parallel consumption solution in the case of insufficient consumer capability. This model is mainly used for multi-task parallelism.

These 4 models summarize the general network communication model, and in practice can combine 2 or more models to form their own solution according to the application needs.

ZMQ provides four communication protocols in-process (inproc://), interprocess (ipc://), Inter-machine (tcp://), broadcast (pgm://).

ZMQ API

All APIs provided by ZMQ start with Zmq_,

#include <zmq.h>-lzmq [libraries]

For example, returns the version information for the current ZMQ library

void zmq_version (intint int *patch);

Context

Before you can use any of the ZQM library functions, you must first create the ZMQ context (context), and you will need to destroy the context when the program terminates.


Create a context

void *zmq_ctx_new ();

ZMQ context is thread-safe and can be used in multithreaded environments without the programmer having to add/unlock it.

In a process, you can have multiple ZMQ context coexisting.

Set context Options

int zmq_ctx_set (voidintint  option_value); int zmq_ctx_get (voidint option_name);

Destroy context

int zmq_ctx_term (void *context);

Sockets

ZMQ Sockets is an abstraction that represents an asynchronous message queue , noting that the socket for the ZMQ socket and the POSIX socket are not the same thing, ZMQ encapsulates the underlying details of the physical connection and is opaque to the user.

The traditional POSIX sockets can only support 1 to 1 connections, while the ZMQ socket supports multiple client concurrent connections, even in the absence of any peer, ZMQ sockets can also be placed on the message;

ZMQ sockets is not thread-safe, so do not operate the same sockets in parallel across multiple threads.

Create ZMQ Sockets

void *zmq_socket (voidint type);

Note that the ZMQ socket is not available before bind.

type parameter meaning

Pattern

Type

Description

One-to-one pairing model

Zmq_pair

Request Response Model

Zmq_req

Client Side use

Zmq_rep

Server-side Use

Zmq_dealer

Distribute the message to all peers (peers) in a polling manner

Zmq_router

Publish subscription model

Zmq_pub

Publisher side Use

Zmq_xpub

Zmq_sub

Subscriber End Use

Zmq_xsub

Piping model

Zmq_push

Push End Use

Zmq_pull

Pull End Use

Native model

Zmq_stream

Set socket options

int zmq_getsockopt (voidintvoid *option_value, size_t *option_len); int zmq_setsockopt (voidintconstvoid *option_value, size_t Option_len) ;

Close socket

int zmq_close (void *socket);

Create a message Flow

int zmq_bind (voidconstChar *endpoint); int zmq_connect (voidconstchar *endpoint);

The BIND function binds the socket to the local endpoint (endpoint), and the Connect function connects to the specified peer endpoint.

Types supported by endpoint:

Transports

Description

URI Example

Zmp_tcp

Unicast Communication for TCP

tcp://*:8080

Zmp_ipc

Local inter-process communication

ipc://

Zmp_inproc

Local inter-thread communication

inproc://

Zmp_pgm

PGM Broadcast Communication

pgm://

Send and receive messages

int zmq_send (voidvoidint  flags); int zmq_recv (voidvoidint  flags); int zmq_send_const (voidvoidint flags);

The Len parameter of the ZMQ_RECV () function specifies the maximum length of the receive buf, the excess portion is truncated, the value returned by the function is the number of bytes received, and 1 indicates an error;

The Zmq_send () function writes a byte of the specified length of the specified buf to the queue, the function return value is the number of bytes sent, and a return of 1 indicates an error;

The Zmq_send_const () function indicates that the sent buf is a constant memory area (constant-memory), which does not need to be copied or freed.

Socket Event Monitoring

int zmq_socket_monitor (voidcharint events);

The Zmq_socket_monitor () function generates events for a pair of sockets,publishers-side releases sockets state changes through the INPROC://protocol;
The message contains 2 frames, the 1th frame contains the events ID and the associated value, and the 2nd frame represents the affected endpoint.

Events for monitoring support:

Zmq_event_connected: Establishing a connection
Zmq_event_connect_delayed: Connection Failed
Zmq_event_connect_retried: Asynchronous Connect/Reconnect
Zmq_event_listening:bind to Endpoint
Zmq_event_bind_failed:bind failure
Zmq_event_accepted: Receiving Requests
Zmq_event_accept_failed: Failed to receive request
zmq_event_closed: Close Connection
Zmq_event_close_failed: Failed to close connection
Zmq_event_disconnected: Session (TCP/IPC) interrupt

I/O multiplexing

int int long timeout);

I/O multiplexing of the sockets collection, using a horizontal trigger.

Similar to Epoll, the items parameter specifies a struct array (defined by the struct), Nitems specifies the number of elements of the array, and the timeout parameter is the time-out (in units: ms,0 means not waiting for immediate return, 1 for blocking wait).

struct {    void *socket;     int FD;      Short events;      Short revents;} zmq_pollitem_t;

For each zmq_pollitem_t element, ZMQ checks whether the specified events occur on its socket (ZMQ socket) and FD (native socket), and the ZMQ socket takes precedence.

Events specifies the event that the sockets needs to follow, Revents returns the events that have occurred for the sockets, and their values are:

    • Zmq_pollin, readable;
    • Zmq_pollout, can write;
    • Zmq_pollerr, error;

Messages

A ZMQ message is a unit of data that is used to transmit in a message queue (inside or across processes), the ZMQ message itself does not have a data structure, and therefore supports any type of data, which is entirely dependent on how the programmer defines the data structure of the message.

A ZMQ message can contain multiple message slices (multi-part messages), each of which is an independent zmq_msg_t structure.

The ZMQ guarantees that the message is delivered atomically, either all of the messages are sent successfully or not successfully.

Initializing messages

void (ZMQ_FREE_FN) (voidvoid *hint); int zmq_msg_init (zmq_msg_t *msg); int void void *hint); int zmq_msg_init_size (zmq_msg_t *msg, size_t size);

The Zmq_msg_init () function Initializes a message object zmq_msg_t and does not directly access the Zmq_msg_t object, which can be accessed through the zmq_msg_* function.
Zmq_msg_init (), Zmq_msg_init_data (), zmq_msg_init_size () Three functions are mutually exclusive and each one can be used.

Setting message Properties

int int Property ); int int int value);


Release message

int zmq_msg_close (zmq_msg_t *msg);


Send and receive messages

int void int flags); int void int flags);

Where the flags parameter is as follows:

Zmq_dontwait, non-blocking mode, if no messages are available, set errno to Eagain;
Zmq_sndmore, when sending multi-part messages, each message slice must use the zmq_sndmore tag bit, except for the last message slice.

Get message Content

void *zmq_msg_data (zmq_msg_t *msg); int zmq_msg_more (zmq_msg_t **msg);

Zmq_msg_data () Returns a pointer to the contents of the Message object;
Zmq_msg_size () returns the number of bytes of the message;
Zmq_msg_more () identifies whether the message slice is part of the whole message, and whether there are more messages to be received;


Control messages

int zmq_msg_copy (zmq_msg_t *dest, zmq_msg_t *src); int zmq_msg_move (zmq_msg_t *dest, zmq_msg_t *src);

The Zmq_msg_copy () function implements a shallow copy;
In the Zmq_msg_move () function, DST points to the SRC message, and then SRC is empty.

eg, the code sample that receives the message:

zmq_msg_t part; while(true) {    //Create an empty? MQ message to hold the message part    intrc = Zmq_msg_init (&Part ); ASSERT (RC==0); //Block until a message is available to being received from socketrc = ZMQ_MSG_RECV (socket, &part,0); ASSERT (RC!= -1); if(Zmq_msg_more (&Part )) fprintf (stderr,"more\n"); Else{fprintf (stderr,"end\n");  Break; } zmq_msg_close (&Part ); }

Agent

ZMQ provides proxy functionality that proxies can forward messages between the front-end socket and the back-end socket.

int zmq_proxy (constvoidconstvoidconst void *  Capture); int zmq_proxy_steerable (constvoidconstvoidconst void Const void *control);

Shared Queue, front end is zmq_router socket, backend is Zmq_dealer Socket,proxy will send clients request and distribute to services fairly;
Forwarding queue (forwarded), the front end is the zmq_xsub socket, the back end is the zmq_xpub socket, proxy will be received from the publishers messages forwarded to all subscribers;
Stream (streamer), the front end is the zmq_pull socket, and the back end is the zmq_push socket.

An example of proxy use:

//Create frontend and backend socketsvoid*frontend =zmq_socket (context, zmq_router); assert (backend);void*backend =zmq_socket (context, Zmq_dealer); assert (frontend);//Bind both sockets to TCP portsASSERT (Zmq_bind (frontend,"tcp://*:5555") ==0); assert (Zmq_bind (backend,"tcp://*:5556") ==0);//Start the queue proxy, which runs until Eterm Zmq_proxy frontend, backend, NULL);


Error handling

The ZMQ library uses POSIX handler function errors, which means that the call error occurs when a null pointer or negative number is returned.

int zmq_errno (void); Const Char *zmq_strerror (int errnum);

The Zmq_errno () function returns the value of the current thread's error code errno variable;

The Zmq_strerror () function maps an error to an error string.

Encrypted transfer

The ZQM can provide security for both IPC and TCP connections:

    • No encryption, Zmq_null
    • Authorization with username/password, zmq_plain
    • Oval encryption, Zmq_curve

These are configured via the zmq_setsockopt () function when the socket option is set.

Summarize:

1, just provide 24 API interface, style similar to BSD Socket.

2, the network exception is handled, including the connection abnormal interrupt, the reconnection and so on.

3, change the TCP based on the byte stream to send and receive data, processing the sticky packet, half-pack and other problems, with MSG units to send and receive data, combined with protocol buffers, the application layer can be completely shielded network communication layer.

4, the big data through the Sendmore/recvmore to provide sub-packet transceiver mechanism.

5, through the data flow between threads to ensure that at the same time any data will only be held by a thread, in order to achieve multi-threaded "de-lock."

6, through the high water level HWM to control the flow, swap swap to dump memory data, to compensate for HWM lost data defects.

7, server-side and client start-up no sequencing.

Message Queuing Library--ZEROMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.