decoder.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #ifndef __ZMQ_DECODER_HPP_INCLUDED__
  25. #define __ZMQ_DECODER_HPP_INCLUDED__
  26. #include <algorithm>
  27. #include <cstddef>
  28. #include <cstring>
  29. #include "decoder_allocators.hpp"
  30. #include "err.hpp"
  31. #include "i_decoder.hpp"
  32. #include "stdint.hpp"
  33. namespace zmq
  34. {
  35. // Helper base class for decoders that know the amount of data to read
  36. // in advance at any moment. Knowing the amount in advance is a property
  37. // of the protocol used. 0MQ framing protocol is based size-prefixed
  38. // paradigm, which qualifies it to be parsed by this class.
  39. // On the other hand, XML-based transports (like XMPP or SOAP) don't allow
  40. // for knowing the size of data to read in advance and should use different
  41. // decoding algorithms.
  42. //
  43. // This class implements the state machine that parses the incoming buffer.
  44. // Derived class should implement individual state machine actions.
  45. //
  46. // Buffer management is done by an allocator policy.
  47. template <typename T, typename A = c_single_allocator>
  48. class decoder_base_t : public i_decoder
  49. {
  50. public:
  51. explicit decoder_base_t (const size_t buf_size_) :
  52. _next (NULL), _read_pos (NULL), _to_read (0), _allocator (buf_size_)
  53. {
  54. _buf = _allocator.allocate ();
  55. }
  56. ~decoder_base_t () ZMQ_OVERRIDE { _allocator.deallocate (); }
  57. // Returns a buffer to be filled with binary data.
  58. void get_buffer (unsigned char **data_, std::size_t *size_) ZMQ_FINAL
  59. {
  60. _buf = _allocator.allocate ();
  61. // If we are expected to read large message, we'll opt for zero-
  62. // copy, i.e. we'll ask caller to fill the data directly to the
  63. // message. Note that subsequent read(s) are non-blocking, thus
  64. // each single read reads at most SO_RCVBUF bytes at once not
  65. // depending on how large is the chunk returned from here.
  66. // As a consequence, large messages being received won't block
  67. // other engines running in the same I/O thread for excessive
  68. // amounts of time.
  69. if (_to_read >= _allocator.size ()) {
  70. *data_ = _read_pos;
  71. *size_ = _to_read;
  72. return;
  73. }
  74. *data_ = _buf;
  75. *size_ = _allocator.size ();
  76. }
  77. // Processes the data in the buffer previously allocated using
  78. // get_buffer function. size_ argument specifies number of bytes
  79. // actually filled into the buffer. Function returns 1 when the
  80. // whole message was decoded or 0 when more data is required.
  81. // On error, -1 is returned and errno set accordingly.
  82. // Number of bytes processed is returned in bytes_used_.
  83. int decode (const unsigned char *data_,
  84. std::size_t size_,
  85. std::size_t &bytes_used_) ZMQ_FINAL
  86. {
  87. bytes_used_ = 0;
  88. // In case of zero-copy simply adjust the pointers, no copying
  89. // is required. Also, run the state machine in case all the data
  90. // were processed.
  91. if (data_ == _read_pos) {
  92. zmq_assert (size_ <= _to_read);
  93. _read_pos += size_;
  94. _to_read -= size_;
  95. bytes_used_ = size_;
  96. while (!_to_read) {
  97. const int rc =
  98. (static_cast<T *> (this)->*_next) (data_ + bytes_used_);
  99. if (rc != 0)
  100. return rc;
  101. }
  102. return 0;
  103. }
  104. while (bytes_used_ < size_) {
  105. // Copy the data from buffer to the message.
  106. const size_t to_copy = std::min (_to_read, size_ - bytes_used_);
  107. // Only copy when destination address is different from the
  108. // current address in the buffer.
  109. if (_read_pos != data_ + bytes_used_) {
  110. memcpy (_read_pos, data_ + bytes_used_, to_copy);
  111. }
  112. _read_pos += to_copy;
  113. _to_read -= to_copy;
  114. bytes_used_ += to_copy;
  115. // Try to get more space in the message to fill in.
  116. // If none is available, return.
  117. while (_to_read == 0) {
  118. // pass current address in the buffer
  119. const int rc =
  120. (static_cast<T *> (this)->*_next) (data_ + bytes_used_);
  121. if (rc != 0)
  122. return rc;
  123. }
  124. }
  125. return 0;
  126. }
  127. void resize_buffer (std::size_t new_size_) ZMQ_FINAL
  128. {
  129. _allocator.resize (new_size_);
  130. }
  131. protected:
  132. // Prototype of state machine action. Action should return false if
  133. // it is unable to push the data to the system.
  134. typedef int (T::*step_t) (unsigned char const *);
  135. // This function should be called from derived class to read data
  136. // from the buffer and schedule next state machine action.
  137. void next_step (void *read_pos_, std::size_t to_read_, step_t next_)
  138. {
  139. _read_pos = static_cast<unsigned char *> (read_pos_);
  140. _to_read = to_read_;
  141. _next = next_;
  142. }
  143. A &get_allocator () { return _allocator; }
  144. private:
  145. // Next step. If set to NULL, it means that associated data stream
  146. // is dead. Note that there can be still data in the process in such
  147. // case.
  148. step_t _next;
  149. // Where to store the read data.
  150. unsigned char *_read_pos;
  151. // How much data to read before taking next step.
  152. std::size_t _to_read;
  153. // The duffer for data to decode.
  154. A _allocator;
  155. unsigned char *_buf;
  156. ZMQ_NON_COPYABLE_NOR_MOVABLE (decoder_base_t)
  157. };
  158. }
  159. #endif