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
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
2010-10-30 15:08:28 +02:00
|
|
|
the terms of the GNU Lesser General Public License as published by
|
2009-07-29 12:07:54 +02:00
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ 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
|
2010-10-30 15:08:28 +02:00
|
|
|
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 <stddef.h>
|
|
|
|
#include <string.h>
|
2009-12-11 22:29:04 +01:00
|
|
|
#include <stdlib.h>
|
2009-07-29 12:07:54 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
#include "err.hpp"
|
2011-04-21 22:27:48 +02:00
|
|
|
#include "msg.hpp"
|
2012-09-04 19:44:20 +02:00
|
|
|
#include "i_decoder.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.
|
|
|
|
|
2012-09-04 19:44:20 +02:00
|
|
|
template <typename T> class decoder_base_t : public i_decoder
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-08-31 07:01:40 +02:00
|
|
|
inline decoder_base_t (size_t bufsize_) :
|
2012-04-29 17:13:18 +02:00
|
|
|
next (NULL),
|
2009-12-11 22:29:04 +01:00
|
|
|
read_pos (NULL),
|
2009-07-29 12:07:54 +02:00
|
|
|
to_read (0),
|
2009-12-11 22:29:04 +01:00
|
|
|
bufsize (bufsize_)
|
|
|
|
{
|
|
|
|
buf = (unsigned char*) malloc (bufsize_);
|
2011-02-22 16:23:36 +01:00
|
|
|
alloc_assert (buf);
|
2009-12-11 22:29:04 +01:00
|
|
|
}
|
|
|
|
|
2010-06-10 07:21:05 +02:00
|
|
|
// The destructor doesn't have to be virtual. It is mad virtual
|
|
|
|
// just to keep ICC and code checking tools from complaining.
|
2010-08-31 07:01:40 +02:00
|
|
|
inline virtual ~decoder_base_t ()
|
2009-12-11 22:29:04 +01:00
|
|
|
{
|
|
|
|
free (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a buffer to be filled with binary data.
|
|
|
|
inline void get_buffer (unsigned char **data_, size_t *size_)
|
2009-07-29 12:07:54 +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.
|
|
|
|
if (to_read >= bufsize) {
|
|
|
|
*data_ = read_pos;
|
|
|
|
*size_ = to_read;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data_ = buf;
|
|
|
|
*size_ = bufsize;
|
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
|
|
|
|
// get_buffer function. size_ argument specifies nemuber 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.
|
|
|
|
// Number of bytes processed is returned in byts_used_.
|
|
|
|
inline int decode (const unsigned char *data_, size_t size_,
|
|
|
|
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) {
|
2013-03-18 02:00:00 +01:00
|
|
|
const int rc = (static_cast <T*> (this)->*next) ();
|
|
|
|
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.
|
2013-03-18 02:00:00 +01:00
|
|
|
const size_t to_copy = std::min (to_read, size_ - bytes_used_);
|
|
|
|
memcpy (read_pos, data_ + bytes_used_, to_copy);
|
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) {
|
|
|
|
const int rc = (static_cast <T*> (this)->*next) ();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
2013-03-18 02:00:00 +01:00
|
|
|
typedef int (T::*step_t) ();
|
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.
|
2013-03-18 02:00:00 +01:00
|
|
|
inline void next_step (void *read_pos_, size_t to_read_, step_t next_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-12-11 22:29:04 +01:00
|
|
|
read_pos = (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.
|
2009-07-29 12:07:54 +02:00
|
|
|
size_t to_read;
|
2011-09-16 09:29:43 +02:00
|
|
|
|
|
|
|
// The duffer for data to decode.
|
2009-12-11 22:29:04 +01:00
|
|
|
size_t bufsize;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2010-08-31 07:01:40 +02:00
|
|
|
decoder_base_t (const decoder_base_t&);
|
2011-01-13 11:44:23 +01:00
|
|
|
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
|
2010-08-31 07:01:40 +02:00
|
|
|
|