ZeroMQ API (1) general sequence

sequence

  zeromq is a lightweight messaging library. It extends the standard socket interface, and its characteristics are different from traditional message middleware. Zeromq provides abstraction of asynchronous message queue, multiple message delivery mode, message filtering (subscription), seamless access to multiple transport protocols, etc.

1. Context

  Before using any of the ZMQ library functions, the ZMQ context must be created, and, when you exit the program, the context must also be destroyed. There are these functions related to the context:

  Create context: zmq_ctx_new(3)
  Get/set context attributes: zmq_ctx_set(3) zmq_ctx_get(3)
  Destroy context: zmq_ctx_shutdown(3) zmq_ctx_term(3)

1.1 Thread Safety

  ØMQ contexts are thread-safe and can be shared among as many application threads as needed without requiring additional locking by the caller.

  A single ØMQ socket is not thread-safe, except in cases where a full memory barrier is issued when migrating a socket from one thread to another.

  In practice, this means that an application can use zmq_socket() to create a socket in one thread and then pass it to the newly created thread as part of thread initialization, e.g. via a structure passed as an argument to pthread_create().

1.2 Multiple contexts

  Multiple contexts may coexist in an application. 

  Therefore, applications can use ØMQ directly, and at the same time use any number of additional libraries or components, which themselves can use ØMQ as long as the above guidelines regarding thread safety are adhered to.

  A ØMQ message is an independent unit of data passed between applications or components of the same application.

  ØMQ messages have no internal structure, and from the perspective of ØMQ themselves, they are considered opaque binary data.

  The following functions are provided to process messages:

  Initialize message: zmq_msg_init(3) zmq_msg_init_size(3) zmq_msg_init_data(3)
  Send and receive message: zmq_msg_send(3) zmq_msg_recv(3)
  Post message: zmq_msg_close(3)
  Access message content: zmq_msg_data(3) zmq_msg_size(3) zmq_msg_more(3) )
  using message attributes: zmq_msg_gets(3) zmq_msg_get(3) zmq_msg_set(3)
  message operations: zmq_msg_copy(3) zmq_msg_move(3)

3. Socket

  ØMQ sockets provide an abstraction for asynchronous message queues, depending on the type of socket being used. See zmq_socket(3) for the provided socket types.

  The following functions are provided to handle sockets:

  Create a socket: zmq_socket(3)
  Close a socket: zmq_close(3)
  Operate socket options: zmq_getsockopt(3) zmq_setsockopt(3)
  Establish message flow: zmq_bind(3) zmq_connect(3)
  Send and receive messages: zmq_msg_send (3) zmq_msg_recv(3) zmq_send(3) zmq_recv(3) zmq_send_const(3)
  Monitor socket events: zmq_socket_monitor(3)

3.1. Input/output multiplexing

  ØMQ provides a mechanism for applications to multiplex input/output events into a collection containing ØMQ sockets and standard sockets. This mechanism mirrors the standard poll() system call and is detailed in zmq_poll(3).

4. Message transmission

  ØMQ sockets can use a number of different underlying transport mechanisms. Each transport mechanism is suitable for a specific purpose and has its own advantages and disadvantages.

  The following transport mechanisms are provided:

  Unicast transport using TCP: zmq_tcp(7)
  Reliable multicast transport using PGM: zmq_pgm(7)
  Local inter-process communication Transport: zmq_ipc(7)
  Local intra-process (inter-thread) communication Transport: zmq_inproc(7)

5. Agent

  ØMQ provides brokers to create fan-out and fan-in topologies. The broker connects the front-end socket to the back-end socket and opaquely switches all messages between the two sockets. The proxy can optionally capture all traffic to a third socket. To start a proxy in an application thread, use zmq_proxy(3).

6. Safety

  ØMQ sockets can choose a security mechanism. Both peers must use the same security mechanism.

  Provides the following security mechanisms for IPC and TCP connections:

  No security: zmq_null(7)
  Plain text authentication with username and password: zmq_plain(7)
  Elliptic curve authentication and encryption: zmq_curve(7)
  CURVE key pair generation in armored text format: zmq_curve_keypair(3)
  The armored key Convert to 32-byte binary key: zmq_z85_decode(3)
  Convert 32-byte binary CURVE key to armored text string: zmq_z85_encode(3)

7. Error handling

  The ØMQ library functions use standard conventions on POSIX systems to handle errors. 

  Typically, this means that upon failure, a ØMQ library function will return a NULL value (if returning a pointer) or a negative value (if returning an integer), and the actual error code should be stored in the errno variable.

  The zmq_errno() function is provided to assist in these cases; see zmq_errno(3) for details.

  The zmq_strerror() function is used to convert ØMQ-specific error codes into error message strings; see zmq_strerror(3) for details.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324726768&siteId=291194637