1 /*
2     Copyright (c) 2020 Contributors as noted in the AUTHORS file
3 
4     This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6     libzmq is free software; you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10 
11     As a special exception, the Contributors give you permission to link
12     this library with independent modules to produce an executable,
13     regardless of the license terms of these independent modules, and to
14     copy and distribute the resulting executable under terms of your choice,
15     provided that you also meet, for each linked independent module, the
16     terms and conditions of the license of that module. An independent
17     module is a module which is not derived from or based on this library.
18     If you modify this library, you must extend this exception to your
19     version of the library.
20 
21     libzmq is distributed in the hope that it will be useful, but WITHOUT
22     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23     FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24     License for more details.
25 
26     You should have received a copy of the GNU Lesser General Public License
27     along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "precompiled.hpp"
31 #include "v2_protocol.hpp"
32 #include "v3_1_encoder.hpp"
33 #include "msg.hpp"
34 #include "likely.hpp"
35 #include "wire.hpp"
36 
37 #include <limits.h>
38 
v3_1_encoder_t(size_t bufsize_)39 zmq::v3_1_encoder_t::v3_1_encoder_t (size_t bufsize_) :
40     encoder_base_t<v3_1_encoder_t> (bufsize_)
41 {
42     //  Write 0 bytes to the batch and go to message_ready state.
43     next_step (NULL, 0, &v3_1_encoder_t::message_ready, true);
44 }
45 
~v3_1_encoder_t()46 zmq::v3_1_encoder_t::~v3_1_encoder_t ()
47 {
48 }
49 
message_ready()50 void zmq::v3_1_encoder_t::message_ready ()
51 {
52     //  Encode flags.
53     size_t size = in_progress ()->size ();
54     size_t header_size = 2; // flags byte + size byte
55     unsigned char &protocol_flags = _tmp_buf[0];
56     protocol_flags = 0;
57     if (in_progress ()->flags () & msg_t::more)
58         protocol_flags |= v2_protocol_t::more_flag;
59     if (in_progress ()->size () > UCHAR_MAX)
60         protocol_flags |= v2_protocol_t::large_flag;
61     if (in_progress ()->flags () & msg_t::command
62         || in_progress ()->is_subscribe () || in_progress ()->is_cancel ()) {
63         protocol_flags |= v2_protocol_t::command_flag;
64         if (in_progress ()->is_subscribe ())
65             size += zmq::msg_t::sub_cmd_name_size;
66         else if (in_progress ()->is_cancel ())
67             size += zmq::msg_t::cancel_cmd_name_size;
68     }
69 
70     //  Encode the message length. For messages less then 256 bytes,
71     //  the length is encoded as 8-bit unsigned integer. For larger
72     //  messages, 64-bit unsigned integer in network byte order is used.
73     if (unlikely (size > UCHAR_MAX)) {
74         put_uint64 (_tmp_buf + 1, size);
75         header_size = 9; // flags byte + size 8 bytes
76     } else {
77         _tmp_buf[1] = static_cast<uint8_t> (size);
78     }
79 
80     //  Encode the sub/cancel command string. This is done in the encoder as
81     //  opposed to when the subscribe message is created to allow different
82     //  protocol behaviour on the wire in the v3.1 and legacy encoders.
83     //  It results in the work being done multiple times in case the sub
84     //  is sending the subscription/cancel to multiple pubs, but it cannot
85     //  be avoided. This processing can be moved to xsub once support for
86     //  ZMTP < 3.1 is dropped.
87     if (in_progress ()->is_subscribe ()) {
88         memcpy (_tmp_buf + header_size, zmq::sub_cmd_name,
89                 zmq::msg_t::sub_cmd_name_size);
90         header_size += zmq::msg_t::sub_cmd_name_size;
91     } else if (in_progress ()->is_cancel ()) {
92         memcpy (_tmp_buf + header_size, zmq::cancel_cmd_name,
93                 zmq::msg_t::cancel_cmd_name_size);
94         header_size += zmq::msg_t::cancel_cmd_name_size;
95     }
96 
97     next_step (_tmp_buf, header_size, &v3_1_encoder_t::size_ready, false);
98 }
99 
size_ready()100 void zmq::v3_1_encoder_t::size_ready ()
101 {
102     //  Write message body into the buffer.
103     next_step (in_progress ()->data (), in_progress ()->size (),
104                &v3_1_encoder_t::message_ready, true);
105 }
106