v2_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 "v2_encoder.hpp"
6 #include "msg.hpp"
7 #include "likely.hpp"
8 #include "wire.hpp"
9 
10 #include <limits.h>
11 
12 zmq::v2_encoder_t::v2_encoder_t (size_t bufsize_) :
13  encoder_base_t<v2_encoder_t> (bufsize_)
14 {
15  // Write 0 bytes to the batch and go to message_ready state.
16  next_step (NULL, 0, &v2_encoder_t::message_ready, true);
17 }
18 
19 zmq::v2_encoder_t::~v2_encoder_t ()
20 {
21 }
22 
23 void zmq::v2_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 ()->size () > UCHAR_MAX)
33  protocol_flags |= v2_protocol_t::large_flag;
34  if (in_progress ()->flags () & msg_t::command)
35  protocol_flags |= v2_protocol_t::command_flag;
36  if (in_progress ()->is_subscribe () || in_progress ()->is_cancel ())
37  ++size;
38 
39  // Encode the message length. For messages less then 256 bytes,
40  // the length is encoded as 8-bit unsigned integer. For larger
41  // messages, 64-bit unsigned integer in network byte order is used.
42  if (unlikely (size > UCHAR_MAX)) {
43  put_uint64 (_tmp_buf + 1, size);
44  header_size = 9; // flags byte + size 8 bytes
45  } else {
46  _tmp_buf[1] = static_cast<uint8_t> (size);
47  }
48 
49  // Encode the subscribe/cancel byte. This is done in the encoder as
50  // opposed to when the subscribe message is created to allow different
51  // protocol behaviour on the wire in the v3.1 and legacy encoders.
52  // It results in the work being done multiple times in case the sub
53  // is sending the subscription/cancel to multiple pubs, but it cannot
54  // be avoided. This processing can be moved to xsub once support for
55  // ZMTP < 3.1 is dropped.
56  if (in_progress ()->is_subscribe ())
57  _tmp_buf[header_size++] = 1;
58  else if (in_progress ()->is_cancel ())
59  _tmp_buf[header_size++] = 0;
60 
61  next_step (_tmp_buf, header_size, &v2_encoder_t::size_ready, false);
62 }
63 
64 void zmq::v2_encoder_t::size_ready ()
65 {
66  // Write message body into the buffer.
67  next_step (in_progress ()->data (), in_progress ()->size (),
68  &v2_encoder_t::message_ready, true);
69 }
zmq::msg_t::command
@ command
Definition: msg.hpp:56
NULL
NULL
Definition: test_security_zap.cpp:405
precompiled.hpp
flags
GLbitfield flags
Definition: glcorearb.h:3585
wire.hpp
v2_encoder.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
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
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