/src/libzmq/src/v1_encoder.cpp
Line | Count | Source |
1 | | /* SPDX-License-Identifier: MPL-2.0 */ |
2 | | |
3 | | #include "precompiled.hpp" |
4 | | #include "encoder.hpp" |
5 | | #include "v1_encoder.hpp" |
6 | | #include "msg.hpp" |
7 | | #include "wire.hpp" |
8 | | |
9 | | #include <limits.h> |
10 | | |
11 | | zmq::v1_encoder_t::v1_encoder_t (size_t bufsize_) : |
12 | 0 | encoder_base_t<v1_encoder_t> (bufsize_) |
13 | 0 | { |
14 | | // Write 0 bytes to the batch and go to message_ready state. |
15 | 0 | next_step (NULL, 0, &v1_encoder_t::message_ready, true); |
16 | 0 | } |
17 | | |
18 | | zmq::v1_encoder_t::~v1_encoder_t () |
19 | 0 | { |
20 | 0 | } |
21 | | |
22 | | void zmq::v1_encoder_t::size_ready () |
23 | 0 | { |
24 | | // Write message body into the buffer. |
25 | 0 | next_step (in_progress ()->data (), in_progress ()->size (), |
26 | 0 | &v1_encoder_t::message_ready, true); |
27 | 0 | } |
28 | | |
29 | | void zmq::v1_encoder_t::message_ready () |
30 | 0 | { |
31 | 0 | size_t header_size = 2; // flags byte + size byte |
32 | | // Get the message size. |
33 | 0 | size_t size = in_progress ()->size (); |
34 | | |
35 | | // Account for the 'flags' byte. |
36 | 0 | size++; |
37 | | |
38 | | // Account for the subscribe/cancel byte. |
39 | 0 | if (in_progress ()->is_subscribe () || in_progress ()->is_cancel ()) |
40 | 0 | size++; |
41 | | |
42 | | // For messages less than 255 bytes long, write one byte of message size. |
43 | | // For longer messages write 0xff escape character followed by 8-byte |
44 | | // message size. In both cases 'flags' field follows. |
45 | 0 | if (size < UCHAR_MAX) { |
46 | 0 | _tmpbuf[0] = static_cast<unsigned char> (size); |
47 | 0 | _tmpbuf[1] = (in_progress ()->flags () & msg_t::more); |
48 | 0 | } else { |
49 | 0 | _tmpbuf[0] = UCHAR_MAX; |
50 | 0 | put_uint64 (_tmpbuf + 1, size); |
51 | 0 | _tmpbuf[9] = (in_progress ()->flags () & msg_t::more); |
52 | 0 | header_size = 10; |
53 | 0 | } |
54 | | |
55 | | // Encode the subscribe/cancel byte. 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 | 0 | if (in_progress ()->is_subscribe ()) |
63 | 0 | _tmpbuf[header_size++] = 1; |
64 | 0 | else if (in_progress ()->is_cancel ()) |
65 | 0 | _tmpbuf[header_size++] = 0; |
66 | |
|
67 | 0 | next_step (_tmpbuf, header_size, &v1_encoder_t::size_ready, false); |
68 | 0 | } |