decoder.hpp 7.34 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
5

6 7 8
    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
Martin Sustrik's avatar
Martin Sustrik committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    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.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

Martin Sustrik's avatar
Martin Sustrik committed
30 31
#ifndef __ZMQ_DECODER_HPP_INCLUDED__
#define __ZMQ_DECODER_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
32 33

#include <algorithm>
34
#include <cstddef>
35
#include <cstring>
Martin Sustrik's avatar
Martin Sustrik committed
36

37
#include "decoder_allocators.hpp"
38
#include "err.hpp"
39
#include "i_decoder.hpp"
40
#include "msg.hpp"
41
#include "stdint.hpp"
42

Martin Sustrik's avatar
Martin Sustrik committed
43
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
44 45 46
{
    //  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
47
    //  of the protocol used. 0MQ framing protocol is based size-prefixed
Wouter Overmeire's avatar
Wouter Overmeire committed
48
    //  paradigm, which qualifies it to be parsed by this class.
49 50 51
    //  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.
Martin Sustrik's avatar
Martin Sustrik committed
52
    //
53
    //  This class implements the state machine that parses the incoming buffer.
Martin Sustrik's avatar
Martin Sustrik committed
54
    //  Derived class should implement individual state machine actions.
Jens Auer's avatar
Jens Auer committed
55
    //
56
    //  Buffer management is done by an allocator policy.
Jens Auer's avatar
Jens Auer committed
57 58
    template <typename T, typename A = c_single_allocator>
    class decoder_base_t : public i_decoder
Martin Sustrik's avatar
Martin Sustrik committed
59 60 61
    {
    public:

62 63 64 65 66
        explicit decoder_base_t (A *allocator_) :
            next (NULL),
            read_pos (NULL),
            to_read (0),
            allocator(allocator_)
67
        {
68
            buf = allocator->allocate ();
69 70
        }

71
        //  The destructor doesn't have to be virtual. It is made virtual
72
        //  just to keep ICC and code checking tools from complaining.
73
        virtual ~decoder_base_t ()
74
        {
75
            allocator->deallocate ();
76 77 78
        }

        //  Returns a buffer to be filled with binary data.
79
        void get_buffer (unsigned char **data_, std::size_t *size_)
Martin Sustrik's avatar
Martin Sustrik committed
80
        {
81
            buf = allocator->allocate ();
Jens Auer's avatar
Jens Auer committed
82

83 84 85 86 87 88 89 90
            //  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.
91
            if (to_read >= allocator->size ()) {
92 93 94 95 96 97
                *data_ = read_pos;
                *size_ = to_read;
                return;
            }

            *data_ = buf;
98
            *size_ = allocator->size ();
Martin Sustrik's avatar
Martin Sustrik committed
99 100
        }

101
        //  Processes the data in the buffer previously allocated using
102
        //  get_buffer function. size_ argument specifies number of bytes
103 104 105
        //  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.
106
        //  Number of bytes processed is returned in bytes_used_.
107 108
        int decode (const unsigned char *data_, std::size_t size_,
                    std::size_t &bytes_used_)
Martin Sustrik's avatar
Martin Sustrik committed
109
        {
110
            bytes_used_ = 0;
111

112 113 114 115
            //  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) {
116
                zmq_assert (size_ <= to_read);
117 118
                read_pos += size_;
                to_read -= size_;
119
                bytes_used_ = size_;
120

121
                while (!to_read) {
122
                    const int rc =
123
                      (static_cast <T *> (this)->*next) (data_ + bytes_used_);
124 125
                    if (rc != 0)
                        return rc;
126
                }
127
                return 0;
128 129
            }

130
            while (bytes_used_ < size_) {
131
                //  Copy the data from buffer to the message.
132 133 134
                const size_t to_copy = std::min (to_read, size_ - bytes_used_);
                // Only copy when destination address is different from the
                // current address in the buffer.
Jens Auer's avatar
Jens Auer committed
135
                if (read_pos != data_ + bytes_used_) {
136
                    memcpy (read_pos, data_ + bytes_used_, to_copy);
Jens Auer's avatar
Jens Auer committed
137 138
                }

139 140
                read_pos += to_copy;
                to_read -= to_copy;
141 142 143 144
                bytes_used_ += to_copy;
                //  Try to get more space in the message to fill in.
                //  If none is available, return.
                while (to_read == 0) {
Jens Auer's avatar
Jens Auer committed
145
                    // pass current address in the buffer
146 147
                    const int rc =
                      (static_cast <T *> (this)->*next) (data_ + bytes_used_);
148 149
                    if (rc != 0)
                        return rc;
Martin Hurton's avatar
Martin Hurton committed
150 151 152
                }
            }

153
            return 0;
154 155
        }

156
        virtual void resize_buffer (std::size_t new_size)
157
        {
158
            allocator->resize (new_size);
159 160
        }

Martin Sustrik's avatar
Martin Sustrik committed
161 162 163 164
    protected:

        //  Prototype of state machine action. Action should return false if
        //  it is unable to push the data to the system.
165
        typedef int (T:: *step_t) (unsigned char const *);
Martin Sustrik's avatar
Martin Sustrik committed
166 167 168

        //  This function should be called from derived class to read data
        //  from the buffer and schedule next state machine action.
169
        void next_step (void *read_pos_, std::size_t to_read_, step_t next_)
Martin Sustrik's avatar
Martin Sustrik committed
170
        {
171
            read_pos = static_cast <unsigned char*> (read_pos_);
Martin Sustrik's avatar
Martin Sustrik committed
172 173 174 175
            to_read = to_read_;
            next = next_;
        }

Martin Hurton's avatar
Martin Hurton committed
176 177
    private:

Martin Hurton's avatar
Martin Hurton committed
178 179 180 181 182
        //  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;

183
        //  Where to store the read data.
184
        unsigned char *read_pos;
185 186

        //  How much data to read before taking next step.
187
        std::size_t to_read;
188 189

        //  The duffer for data to decode.
190 191
        A *allocator;
        unsigned char *buf;
192

193 194
        decoder_base_t (const decoder_base_t &);
        const decoder_base_t &operator = (const decoder_base_t &);
195
    };
Martin Sustrik's avatar
Martin Sustrik committed
196 197 198
}

#endif