v3_1_encoder.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "precompiled.hpp"
4 #include "v2_protocol.hpp"
5 #include "v3_1_encoder.hpp"
6 #include "msg.hpp"
7 #include "likely.hpp"
8 #include "wire.hpp"
9 
10 #include <limits.h>
11 
12 zmq::v3_1_encoder_t::v3_1_encoder_t (size_t bufsize_) :
13  encoder_base_t<v3_1_encoder_t> (bufsize_)
14 {
15  // Write 0 bytes to the batch and go to message_ready state.
16  next_step (NULL, 0, &v3_1_encoder_t::message_ready, true);
17 }
18 
19 zmq::v3_1_encoder_t::~v3_1_encoder_t ()
20 {
21 }
22 
23 void zmq::v3_1_encoder_t::message_ready ()
24 {
25  // Encode flags.
26  size_t size = in_progress ()->size ();
27  size_t header_size = 2; // flags byte + size byte
28  unsigned char &protocol_flags = _tmp_buf[0];
29  protocol_flags = 0;
30  if (in_progress ()->flags () & msg_t::more)
31  protocol_flags |= v2_protocol_t::more_flag;
32  if (in_progress ()->flags () & msg_t::command
33  || in_progress ()->is_subscribe () || in_progress ()->is_cancel ()) {
34  protocol_flags |= v2_protocol_t::command_flag;
35  if (in_progress ()->is_subscribe ())
37  else if (in_progress ()->is_cancel ())
39  }
40  // Calculate large_flag after command_flag. Subscribe or cancel commands
41  // increase the message size.
42  if (size > UCHAR_MAX)
43  protocol_flags |= v2_protocol_t::large_flag;
44 
45  // Encode the message length. For messages less then 256 bytes,
46  // the length is encoded as 8-bit unsigned integer. For larger
47  // messages, 64-bit unsigned integer in network byte order is used.
48  if (unlikely (size > UCHAR_MAX)) {
49  put_uint64 (_tmp_buf + 1, size);
50  header_size = 9; // flags byte + size 8 bytes
51  } else {
52  _tmp_buf[1] = static_cast<uint8_t> (size);
53  }
54 
55  // Encode the sub/cancel command string. This is done in the encoder as
56  // opposed to when the subscribe message is created to allow different
57  // protocol behaviour on the wire in the v3.1 and legacy encoders.
58  // It results in the work being done multiple times in case the sub
59  // is sending the subscription/cancel to multiple pubs, but it cannot
60  // be avoided. This processing can be moved to xsub once support for
61  // ZMTP < 3.1 is dropped.
62  if (in_progress ()->is_subscribe ()) {
63  memcpy (_tmp_buf + header_size, zmq::sub_cmd_name,
65  header_size += zmq::msg_t::sub_cmd_name_size;
66  } else if (in_progress ()->is_cancel ()) {
67  memcpy (_tmp_buf + header_size, zmq::cancel_cmd_name,
69  header_size += zmq::msg_t::cancel_cmd_name_size;
70  }
71 
72  next_step (_tmp_buf, header_size, &v3_1_encoder_t::size_ready, false);
73 }
74 
75 void zmq::v3_1_encoder_t::size_ready ()
76 {
77  // Write message body into the buffer.
78  next_step (in_progress ()->data (), in_progress ()->size (),
79  &v3_1_encoder_t::message_ready, true);
80 }
zmq::msg_t::command
@ command
Definition: msg.hpp:56
NULL
NULL
Definition: test_security_zap.cpp:405
zmq::msg_t::cancel_cmd_name_size
@ cancel_cmd_name_size
Definition: msg.hpp:160
precompiled.hpp
zmq::msg_t::sub_cmd_name_size
@ sub_cmd_name_size
Definition: msg.hpp:161
zmq::cancel_cmd_name
static const char cancel_cmd_name[]
Definition: msg.hpp:30
flags
GLbitfield flags
Definition: glcorearb.h:3585
wire.hpp
zmq::v2_protocol_t::command_flag
@ command_flag
Definition: v2_protocol.hpp:17
zmq::v2_protocol_t::large_flag
@ large_flag
Definition: v2_protocol.hpp:16
size
#define size
Definition: glcorearb.h:2944
msg.hpp
zmq::put_uint64
void put_uint64(unsigned char *buffer_, uint64_t value_)
Definition: wire.hpp:51
zmq::msg_t::more
@ more
Definition: msg.hpp:55
size
GLsizeiptr size
Definition: glcorearb.h:2943
likely.hpp
zmq::sub_cmd_name
static const char sub_cmd_name[]
Definition: msg.hpp:31
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
v3_1_encoder.hpp
v2_protocol.hpp
zmq::v2_protocol_t::more_flag
@ more_flag
Definition: v2_protocol.hpp:15
unlikely
#define unlikely(x)
Definition: likely.hpp:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:01