2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2015-01-22 10:32:06 +01:00
|
|
|
Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2009-07-29 12:07:54 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-07-29 12:07:54 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#ifndef __ZMQ_DECODER_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_DECODER_HPP_INCLUDED__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
2015-07-06 00:01:52 +01:00
|
|
|
#include <cstddef>
|
2015-07-06 00:20:46 +01:00
|
|
|
#include <cstring>
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-07-06 00:01:52 +01:00
|
|
|
#include "decoder_allocators.hpp"
|
2009-12-11 22:29:04 +01:00
|
|
|
#include "err.hpp"
|
2012-09-04 19:44:20 +02:00
|
|
|
#include "i_decoder.hpp"
|
2015-07-06 00:01:52 +01:00
|
|
|
#include "msg.hpp"
|
2011-03-02 09:00:36 +01:00
|
|
|
#include "stdint.hpp"
|
2009-12-11 22:29:04 +01:00
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
namespace zmq
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
// Helper base class for decoders that know the amount of data to read
|
|
|
|
// in advance at any moment. Knowing the amount in advance is a property
|
2010-08-31 07:01:40 +02:00
|
|
|
// of the protocol used. 0MQ framing protocol is based size-prefixed
|
2013-12-24 15:00:43 +01:00
|
|
|
// paradigm, which qualifies it to be parsed by this class.
|
2010-08-31 07:01:40 +02:00
|
|
|
// On the other hand, XML-based transports (like XMPP or SOAP) don't allow
|
|
|
|
// for knowing the size of data to read in advance and should use different
|
|
|
|
// decoding algorithms.
|
2009-07-29 12:07:54 +02:00
|
|
|
//
|
2010-08-31 07:01:40 +02:00
|
|
|
// This class implements the state machine that parses the incoming buffer.
|
2009-07-29 12:07:54 +02:00
|
|
|
// Derived class should implement individual state machine actions.
|
2015-05-29 23:54:43 +02:00
|
|
|
//
|
2015-09-06 18:46:32 +02:00
|
|
|
// Buffer management is done by an allocator policy.
|
2015-05-29 23:54:43 +02:00
|
|
|
template <typename T, typename A = c_single_allocator>
|
|
|
|
class decoder_base_t : public i_decoder
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-07-06 00:01:52 +01:00
|
|
|
explicit decoder_base_t (A *allocator_) :
|
|
|
|
next (NULL),
|
|
|
|
read_pos (NULL),
|
|
|
|
to_read (0),
|
|
|
|
allocator(allocator_)
|
2009-12-11 22:29:04 +01:00
|
|
|
{
|
2015-07-06 00:01:52 +01:00
|
|
|
buf = allocator->allocate ();
|
2009-12-11 22:29:04 +01:00
|
|
|
}
|
|
|
|
|
2015-07-06 00:01:52 +01:00
|
|
|
// The destructor doesn't have to be virtual. It is made virtual
|
2010-06-10 07:21:05 +02:00
|
|
|
// just to keep ICC and code checking tools from complaining.
|
2015-07-06 00:01:52 +01:00
|
|
|
virtual ~decoder_base_t ()
|
2009-12-11 22:29:04 +01:00
|
|
|
{
|
2015-07-06 00:01:52 +01:00
|
|
|
allocator->deallocate ();
|
2009-12-11 22:29:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a buffer to be filled with binary data.
|
2015-07-06 00:01:52 +01:00
|
|
|
void get_buffer (unsigned char **data_, std::size_t *size_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2015-07-06 00:01:52 +01:00
|
|
|
buf = allocator->allocate ();
|
2015-05-29 23:54:43 +02:00
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
// If we are expected to read large message, we'll opt for zero-
|
|
|
|
// copy, i.e. we'll ask caller to fill the data directly to the
|
|
|
|
// message. Note that subsequent read(s) are non-blocking, thus
|
|
|
|
// each single read reads at most SO_RCVBUF bytes at once not
|
|
|
|
// depending on how large is the chunk returned from here.
|
|
|
|
// As a consequence, large messages being received won't block
|
|
|
|
// other engines running in the same I/O thread for excessive
|
|
|
|
// amounts of time.
|
2015-07-06 00:01:52 +01:00
|
|
|
if (to_read >= allocator->size ()) {
|
2009-12-11 22:29:04 +01:00
|
|
|
*data_ = read_pos;
|
|
|
|
*size_ = to_read;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data_ = buf;
|
2015-07-06 00:01:52 +01:00
|
|
|
*size_ = allocator->size ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
// Processes the data in the buffer previously allocated using
|
2015-09-06 18:46:32 +02:00
|
|
|
// get_buffer function. size_ argument specifies number of bytes
|
2013-03-18 02:00:00 +01:00
|
|
|
// actually filled into the buffer. Function returns 1 when the
|
|
|
|
// whole message was decoded or 0 when more data is required.
|
|
|
|
// On error, -1 is returned and errno set accordingly.
|
2015-09-06 18:46:32 +02:00
|
|
|
// Number of bytes processed is returned in bytes_used_.
|
2015-07-06 00:01:52 +01:00
|
|
|
int decode (const unsigned char *data_, std::size_t size_,
|
|
|
|
std::size_t &bytes_used_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2013-03-18 02:00:00 +01:00
|
|
|
bytes_used_ = 0;
|
2011-01-20 07:52:40 +01:00
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
// In case of zero-copy simply adjust the pointers, no copying
|
|
|
|
// is required. Also, run the state machine in case all the data
|
|
|
|
// were processed.
|
|
|
|
if (data_ == read_pos) {
|
2013-03-18 02:00:00 +01:00
|
|
|
zmq_assert (size_ <= to_read);
|
2009-12-11 22:29:04 +01:00
|
|
|
read_pos += size_;
|
|
|
|
to_read -= size_;
|
2013-03-18 02:00:00 +01:00
|
|
|
bytes_used_ = size_;
|
2009-12-11 22:29:04 +01:00
|
|
|
|
2010-10-23 20:59:54 +02:00
|
|
|
while (!to_read) {
|
2015-07-06 00:01:52 +01:00
|
|
|
const int rc = (static_cast <T *> (this)->*next) (data_ + bytes_used_);
|
2013-03-18 02:00:00 +01:00
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
2010-10-23 20:59:54 +02:00
|
|
|
}
|
2013-03-18 02:00:00 +01:00
|
|
|
return 0;
|
2009-12-11 22:29:04 +01:00
|
|
|
}
|
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
while (bytes_used_ < size_) {
|
2009-12-11 22:29:04 +01:00
|
|
|
// Copy the data from buffer to the message.
|
2015-07-06 00:01:52 +01:00
|
|
|
const std::size_t to_copy = std::min (to_read, size_ - bytes_used_);
|
2015-05-29 23:54:43 +02:00
|
|
|
// only copy when the destination address is different from the
|
|
|
|
// current address in the buffer
|
|
|
|
if (read_pos != data_ + bytes_used_) {
|
2015-07-06 00:01:52 +01:00
|
|
|
std::memcpy (read_pos, data_ + bytes_used_, to_copy);
|
2015-05-29 23:54:43 +02:00
|
|
|
}
|
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
read_pos += to_copy;
|
|
|
|
to_read -= to_copy;
|
2013-03-18 02:00:00 +01:00
|
|
|
bytes_used_ += to_copy;
|
|
|
|
// Try to get more space in the message to fill in.
|
|
|
|
// If none is available, return.
|
|
|
|
while (to_read == 0) {
|
2015-05-29 23:54:43 +02:00
|
|
|
// pass current address in the buffer
|
2015-07-06 00:01:52 +01:00
|
|
|
const int rc = (static_cast <T *> (this)->*next) (data_ + bytes_used_);
|
2013-03-18 02:00:00 +01:00
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
2012-11-10 23:05:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
return 0;
|
2012-10-29 00:03:36 -07:00
|
|
|
}
|
|
|
|
|
2015-07-06 00:01:52 +01:00
|
|
|
virtual void resize_buffer (std::size_t new_size)
|
2015-06-10 22:09:55 +02:00
|
|
|
{
|
2015-07-06 00:01:52 +01:00
|
|
|
allocator->resize (new_size);
|
2015-06-10 22:09:55 +02:00
|
|
|
}
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
protected:
|
|
|
|
|
|
|
|
// Prototype of state machine action. Action should return false if
|
|
|
|
// it is unable to push the data to the system.
|
2015-07-06 00:01:52 +01:00
|
|
|
typedef int (T:: *step_t) (unsigned char const *);
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
// This function should be called from derived class to read data
|
|
|
|
// from the buffer and schedule next state machine action.
|
2015-07-06 00:01:52 +01:00
|
|
|
void next_step (void *read_pos_, std::size_t to_read_, step_t next_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2015-07-06 00:01:52 +01:00
|
|
|
read_pos = static_cast <unsigned char*> (read_pos_);
|
2009-07-29 12:07:54 +02:00
|
|
|
to_read = to_read_;
|
|
|
|
next = next_;
|
|
|
|
}
|
|
|
|
|
2012-11-10 23:05:10 +01:00
|
|
|
private:
|
|
|
|
|
2012-04-29 17:13:18 +02:00
|
|
|
// Next step. If set to NULL, it means that associated data stream
|
|
|
|
// is dead. Note that there can be still data in the process in such
|
|
|
|
// case.
|
|
|
|
step_t next;
|
|
|
|
|
2011-09-16 09:29:43 +02:00
|
|
|
// Where to store the read data.
|
2009-12-11 22:29:04 +01:00
|
|
|
unsigned char *read_pos;
|
2011-09-16 09:29:43 +02:00
|
|
|
|
|
|
|
// How much data to read before taking next step.
|
2015-07-06 00:01:52 +01:00
|
|
|
std::size_t to_read;
|
2011-09-16 09:29:43 +02:00
|
|
|
|
|
|
|
// The duffer for data to decode.
|
2015-07-06 00:01:52 +01:00
|
|
|
A *allocator;
|
|
|
|
unsigned char *buf;
|
2009-12-11 22:29:04 +01:00
|
|
|
|
2015-07-06 00:01:52 +01:00
|
|
|
decoder_base_t (const decoder_base_t &);
|
|
|
|
const decoder_base_t &operator = (const decoder_base_t &);
|
2010-08-31 07:01:40 +02:00
|
|
|
};
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|