libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
v2_decoder.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 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 <stdlib.h>
32 #include <string.h>
33 #include <cmath>
34 
35 #include "platform.hpp"
36 #ifdef ZMQ_HAVE_WINDOWS
37 #include "windows.hpp"
38 #endif
39 
40 #include "v2_protocol.hpp"
41 #include "v2_decoder.hpp"
42 #include "likely.hpp"
43 #include "wire.hpp"
44 #include "err.hpp"
45 
46 
47 
48 zmq::v2_decoder_t::v2_decoder_t (size_t bufsize_, int64_t maxmsgsize_) :
51  msg_flags (0),
52  maxmsgsize (maxmsgsize_)
53 {
54  int rc = in_progress.init ();
55  errno_assert (rc == 0);
56 
57  // At the beginning, read one byte and go to flags_ready state.
59 }
60 
62 {
63  int rc = in_progress.close ();
64  errno_assert (rc == 0);
65 }
66 
67 int zmq::v2_decoder_t::flags_ready (unsigned char const*)
68 {
69  msg_flags = 0;
74 
75  // The payload length is either one or eight bytes,
76  // depending on whether the 'large' bit is set.
79  else
81 
82  return 0;
83 }
84 
85 int zmq::v2_decoder_t::one_byte_size_ready (unsigned char const* read_from)
86 {
87  return size_ready(tmpbuf[0], read_from);
88 }
89 
90 int zmq::v2_decoder_t::eight_byte_size_ready (unsigned char const* read_from) {
91  // The payload size is encoded as 64-bit unsigned integer.
92  // The most significant byte comes first.
93  const uint64_t msg_size = get_uint64(tmpbuf);
94 
95  return size_ready(msg_size, read_from);
96 }
97 
98 int zmq::v2_decoder_t::size_ready(uint64_t msg_size, unsigned char const* read_pos) {
99  // Message size must not exceed the maximum allowed size.
100  if (maxmsgsize >= 0)
101  if (unlikely (msg_size > static_cast <uint64_t> (maxmsgsize))) {
102  errno = EMSGSIZE;
103  return -1;
104  }
105 
106  // Message size must fit into size_t data type.
107  if (unlikely (msg_size != static_cast <size_t> (msg_size))) {
108  errno = EMSGSIZE;
109  return -1;
110  }
111 
112  int rc = in_progress.close();
113  assert(rc == 0);
114 
115  // the current message can exceed the current buffer. We have to copy the buffer
116  // data into a new message and complete it in the next receive.
117 
118  if (unlikely ((unsigned char*)read_pos + msg_size > (data() + size())))
119  {
120  // a new message has started, but the size would exceed the pre-allocated arena
121  // this happens every time when a message does not fit completely into the buffer
122  rc = in_progress.init_size (static_cast <size_t> (msg_size));
123  }
124  else
125  {
126  // construct message using n bytes from the buffer as storage
127  // increase buffer ref count
128  // if the message will be a large message, pass a valid refcnt memory location as well
129  rc = in_progress.init ((unsigned char *) read_pos, static_cast <size_t> (msg_size),
131  provide_content ());
132 
133  // For small messages, data has been copied and refcount does not have to be increased
134  if (in_progress.is_zcmsg())
135  {
136  advance_content();
137  inc_ref();
138  }
139  }
140 
141  if (unlikely (rc)) {
142  errno_assert (errno == ENOMEM);
143  rc = in_progress.init ();
144  errno_assert (rc == 0);
145  errno = ENOMEM;
146  return -1;
147  }
148 
150  // this sets read_pos to
151  // the message data address if the data needs to be copied
152  // for small message / messages exceeding the current buffer
153  // or
154  // to the current start address in the buffer because the message
155  // was constructed to use n bytes from the address passed as argument
158 
159  return 0;
160 }
161 
162 int zmq::v2_decoder_t::message_ready (unsigned char const*)
163 {
164  // Message is completely read. Signal this to the caller
165  // and prepare to decode next message.
167  return 1;
168 }
int message_ready(unsigned char const *)
Definition: v2_decoder.cpp:162
int one_byte_size_ready(unsigned char const *)
Definition: v2_decoder.cpp:85
int close()
Definition: msg.cpp:217
v2_decoder_t(size_t bufsize_, int64_t maxmsgsize_)
Definition: v2_decoder.cpp:48
unsigned char size
Definition: msg.hpp:188
int init_size(size_t size_)
Definition: msg.cpp:93
static void call_dec_ref(void *, void *buffer)
const int64_t maxmsgsize
Definition: v2_decoder.hpp:66
uint64_t get_uint64(const unsigned char *buffer_)
Definition: wire.hpp:93
#define unlikely(x)
Definition: likely.hpp:38
int init()
Definition: msg.cpp:82
#define EMSGSIZE
Definition: zmq.h:139
void set_flags(unsigned char flags_)
Definition: msg.cpp:384
bool is_zcmsg() const
Definition: msg.cpp:442
unsigned char msg_flags
Definition: v2_decoder.hpp:63
virtual ~v2_decoder_t()
Definition: v2_decoder.cpp:61
#define errno_assert(x)
Definition: err.hpp:129
int eight_byte_size_ready(unsigned char const *)
Definition: v2_decoder.cpp:90
unsigned char data[max_vsm_size]
Definition: msg.hpp:187
unsigned char tmpbuf[8]
Definition: v2_decoder.hpp:62
int size_ready(uint64_t size_, unsigned char const *)
Definition: v2_decoder.cpp:98
void next_step(void *read_pos_, std::size_t to_read_, step_t next_)
Definition: decoder.hpp:169
zmq::msg_t::content_t * provide_content()
int flags_ready(unsigned char const *)
Definition: v2_decoder.cpp:67