v2_decoder.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "precompiled.hpp"
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <cmath>
  28. #include "v2_protocol.hpp"
  29. #include "v2_decoder.hpp"
  30. #include "likely.hpp"
  31. #include "wire.hpp"
  32. #include "err.hpp"
  33. zmq::v2_decoder_t::v2_decoder_t (size_t bufsize_,
  34. int64_t maxmsgsize_,
  35. bool zero_copy_) :
  36. decoder_base_t<v2_decoder_t, shared_message_memory_allocator> (bufsize_),
  37. _msg_flags (0),
  38. _zero_copy (zero_copy_),
  39. _max_msg_size (maxmsgsize_)
  40. {
  41. int rc = _in_progress.init ();
  42. errno_assert (rc == 0);
  43. // At the beginning, read one byte and go to flags_ready state.
  44. next_step (_tmpbuf, 1, &v2_decoder_t::flags_ready);
  45. }
  46. zmq::v2_decoder_t::~v2_decoder_t ()
  47. {
  48. const int rc = _in_progress.close ();
  49. errno_assert (rc == 0);
  50. }
  51. int zmq::v2_decoder_t::flags_ready (unsigned char const *)
  52. {
  53. _msg_flags = 0;
  54. if (_tmpbuf[0] & v2_protocol_t::more_flag)
  55. _msg_flags |= msg_t::more;
  56. if (_tmpbuf[0] & v2_protocol_t::command_flag)
  57. _msg_flags |= msg_t::command;
  58. // The payload length is either one or eight bytes,
  59. // depending on whether the 'large' bit is set.
  60. if (_tmpbuf[0] & v2_protocol_t::large_flag)
  61. next_step (_tmpbuf, 8, &v2_decoder_t::eight_byte_size_ready);
  62. else
  63. next_step (_tmpbuf, 1, &v2_decoder_t::one_byte_size_ready);
  64. return 0;
  65. }
  66. int zmq::v2_decoder_t::one_byte_size_ready (unsigned char const *read_from_)
  67. {
  68. return size_ready (_tmpbuf[0], read_from_);
  69. }
  70. int zmq::v2_decoder_t::eight_byte_size_ready (unsigned char const *read_from_)
  71. {
  72. // The payload size is encoded as 64-bit unsigned integer.
  73. // The most significant byte comes first.
  74. const uint64_t msg_size = get_uint64 (_tmpbuf);
  75. return size_ready (msg_size, read_from_);
  76. }
  77. int zmq::v2_decoder_t::size_ready (uint64_t msg_size_,
  78. unsigned char const *read_pos_)
  79. {
  80. // Message size must not exceed the maximum allowed size.
  81. if (_max_msg_size >= 0)
  82. if (unlikely (msg_size_ > static_cast<uint64_t> (_max_msg_size))) {
  83. errno = EMSGSIZE;
  84. return -1;
  85. }
  86. // Message size must fit into size_t data type.
  87. if (unlikely (msg_size_ != static_cast<size_t> (msg_size_))) {
  88. errno = EMSGSIZE;
  89. return -1;
  90. }
  91. int rc = _in_progress.close ();
  92. assert (rc == 0);
  93. // the current message can exceed the current buffer. We have to copy the buffer
  94. // data into a new message and complete it in the next receive.
  95. shared_message_memory_allocator &allocator = get_allocator ();
  96. if (unlikely (!_zero_copy
  97. || msg_size_ > static_cast<size_t> (
  98. allocator.data () + allocator.size () - read_pos_))) {
  99. // a new message has started, but the size would exceed the pre-allocated arena
  100. // this happens every time when a message does not fit completely into the buffer
  101. rc = _in_progress.init_size (static_cast<size_t> (msg_size_));
  102. } else {
  103. // construct message using n bytes from the buffer as storage
  104. // increase buffer ref count
  105. // if the message will be a large message, pass a valid refcnt memory location as well
  106. rc =
  107. _in_progress.init (const_cast<unsigned char *> (read_pos_),
  108. static_cast<size_t> (msg_size_),
  109. shared_message_memory_allocator::call_dec_ref,
  110. allocator.buffer (), allocator.provide_content ());
  111. // For small messages, data has been copied and refcount does not have to be increased
  112. if (_in_progress.is_zcmsg ()) {
  113. allocator.advance_content ();
  114. allocator.inc_ref ();
  115. }
  116. }
  117. if (unlikely (rc)) {
  118. errno_assert (errno == ENOMEM);
  119. rc = _in_progress.init ();
  120. errno_assert (rc == 0);
  121. errno = ENOMEM;
  122. return -1;
  123. }
  124. _in_progress.set_flags (_msg_flags);
  125. // this sets read_pos to
  126. // the message data address if the data needs to be copied
  127. // for small message / messages exceeding the current buffer
  128. // or
  129. // to the current start address in the buffer because the message
  130. // was constructed to use n bytes from the address passed as argument
  131. next_step (_in_progress.data (), _in_progress.size (),
  132. &v2_decoder_t::message_ready);
  133. return 0;
  134. }
  135. int zmq::v2_decoder_t::message_ready (unsigned char const *)
  136. {
  137. // Message is completely read. Signal this to the caller
  138. // and prepare to decode next message.
  139. next_step (_tmpbuf, 1, &v2_decoder_t::flags_ready);
  140. return 1;
  141. }