/src/libzmq/src/v2_decoder.cpp
Line | Count | Source |
1 | | /* SPDX-License-Identifier: MPL-2.0 */ |
2 | | |
3 | | #include "precompiled.hpp" |
4 | | #include <stdlib.h> |
5 | | #include <string.h> |
6 | | #include <cmath> |
7 | | |
8 | | #include "v2_protocol.hpp" |
9 | | #include "v2_decoder.hpp" |
10 | | #include "likely.hpp" |
11 | | #include "wire.hpp" |
12 | | #include "err.hpp" |
13 | | |
14 | | zmq::v2_decoder_t::v2_decoder_t (size_t bufsize_, |
15 | | int64_t maxmsgsize_, |
16 | | bool zero_copy_) : |
17 | 0 | decoder_base_t<v2_decoder_t, shared_message_memory_allocator> (bufsize_), |
18 | 0 | _msg_flags (0), |
19 | 0 | _zero_copy (zero_copy_), |
20 | 0 | _max_msg_size (maxmsgsize_) |
21 | 0 | { |
22 | 0 | int rc = _in_progress.init (); |
23 | 0 | errno_assert (rc == 0); |
24 | | |
25 | | // At the beginning, read one byte and go to flags_ready state. |
26 | 0 | next_step (_tmpbuf, 1, &v2_decoder_t::flags_ready); |
27 | 0 | } |
28 | | |
29 | | zmq::v2_decoder_t::~v2_decoder_t () |
30 | 0 | { |
31 | 0 | const int rc = _in_progress.close (); |
32 | 0 | errno_assert (rc == 0); |
33 | 0 | } |
34 | | |
35 | | int zmq::v2_decoder_t::flags_ready (unsigned char const *) |
36 | 0 | { |
37 | 0 | _msg_flags = 0; |
38 | 0 | if (_tmpbuf[0] & v2_protocol_t::more_flag) |
39 | 0 | _msg_flags |= msg_t::more; |
40 | 0 | if (_tmpbuf[0] & v2_protocol_t::command_flag) |
41 | 0 | _msg_flags |= msg_t::command; |
42 | | |
43 | | // The payload length is either one or eight bytes, |
44 | | // depending on whether the 'large' bit is set. |
45 | 0 | if (_tmpbuf[0] & v2_protocol_t::large_flag) |
46 | 0 | next_step (_tmpbuf, 8, &v2_decoder_t::eight_byte_size_ready); |
47 | 0 | else |
48 | 0 | next_step (_tmpbuf, 1, &v2_decoder_t::one_byte_size_ready); |
49 | |
|
50 | 0 | return 0; |
51 | 0 | } |
52 | | |
53 | | int zmq::v2_decoder_t::one_byte_size_ready (unsigned char const *read_from_) |
54 | 0 | { |
55 | 0 | return size_ready (_tmpbuf[0], read_from_); |
56 | 0 | } |
57 | | |
58 | | int zmq::v2_decoder_t::eight_byte_size_ready (unsigned char const *read_from_) |
59 | 0 | { |
60 | | // The payload size is encoded as 64-bit unsigned integer. |
61 | | // The most significant byte comes first. |
62 | 0 | const uint64_t msg_size = get_uint64 (_tmpbuf); |
63 | |
|
64 | 0 | return size_ready (msg_size, read_from_); |
65 | 0 | } |
66 | | |
67 | | int zmq::v2_decoder_t::size_ready (uint64_t msg_size_, |
68 | | unsigned char const *read_pos_) |
69 | 0 | { |
70 | | // Message size must not exceed the maximum allowed size. |
71 | 0 | if (_max_msg_size >= 0) |
72 | 0 | if (unlikely (msg_size_ > static_cast<uint64_t> (_max_msg_size))) { |
73 | 0 | errno = EMSGSIZE; |
74 | 0 | return -1; |
75 | 0 | } |
76 | | |
77 | | // Message size must fit into size_t data type. |
78 | 0 | if (unlikely (msg_size_ != static_cast<size_t> (msg_size_))) { |
79 | 0 | errno = EMSGSIZE; |
80 | 0 | return -1; |
81 | 0 | } |
82 | | |
83 | 0 | int rc = _in_progress.close (); |
84 | 0 | assert (rc == 0); |
85 | | |
86 | | // the current message can exceed the current buffer. We have to copy the buffer |
87 | | // data into a new message and complete it in the next receive. |
88 | | |
89 | 0 | shared_message_memory_allocator &allocator = get_allocator (); |
90 | 0 | if (unlikely (!_zero_copy |
91 | 0 | || msg_size_ > static_cast<size_t> ( |
92 | 0 | allocator.data () + allocator.size () - read_pos_))) { |
93 | | // a new message has started, but the size would exceed the pre-allocated arena |
94 | | // this happens every time when a message does not fit completely into the buffer |
95 | 0 | rc = _in_progress.init_size (static_cast<size_t> (msg_size_)); |
96 | 0 | } else { |
97 | | // construct message using n bytes from the buffer as storage |
98 | | // increase buffer ref count |
99 | | // if the message will be a large message, pass a valid refcnt memory location as well |
100 | 0 | rc = |
101 | 0 | _in_progress.init (const_cast<unsigned char *> (read_pos_), |
102 | 0 | static_cast<size_t> (msg_size_), |
103 | 0 | shared_message_memory_allocator::call_dec_ref, |
104 | 0 | allocator.buffer (), allocator.provide_content ()); |
105 | | |
106 | | // For small messages, data has been copied and refcount does not have to be increased |
107 | 0 | if (_in_progress.is_zcmsg ()) { |
108 | 0 | allocator.advance_content (); |
109 | 0 | allocator.inc_ref (); |
110 | 0 | } |
111 | 0 | } |
112 | |
|
113 | 0 | if (unlikely (rc)) { |
114 | 0 | errno_assert (errno == ENOMEM); |
115 | 0 | rc = _in_progress.init (); |
116 | 0 | errno_assert (rc == 0); |
117 | 0 | errno = ENOMEM; |
118 | 0 | return -1; |
119 | 0 | } |
120 | | |
121 | 0 | _in_progress.set_flags (_msg_flags); |
122 | | // this sets read_pos to |
123 | | // the message data address if the data needs to be copied |
124 | | // for small message / messages exceeding the current buffer |
125 | | // or |
126 | | // to the current start address in the buffer because the message |
127 | | // was constructed to use n bytes from the address passed as argument |
128 | 0 | next_step (_in_progress.data (), _in_progress.size (), |
129 | 0 | &v2_decoder_t::message_ready); |
130 | |
|
131 | 0 | return 0; |
132 | 0 | } |
133 | | |
134 | | int zmq::v2_decoder_t::message_ready (unsigned char const *) |
135 | 0 | { |
136 | | // Message is completely read. Signal this to the caller |
137 | | // and prepare to decode next message. |
138 | 0 | next_step (_tmpbuf, 1, &v2_decoder_t::flags_ready); |
139 | 0 | return 1; |
140 | 0 | } |