encoder.hpp 6.04 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_ENCODER_HPP_INCLUDED__
#define __ZMQ_ENCODER_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
32

33 34 35 36 37 38
#if defined(_MSC_VER)
#ifndef NOMINMAX
#define NOMINMAX
#endif
#endif

Martin Sustrik's avatar
Martin Sustrik committed
39 40
#include <stddef.h>
#include <string.h>
41
#include <stdlib.h>
Martin Sustrik's avatar
Martin Sustrik committed
42 43
#include <algorithm>

44
#include "err.hpp"
45
#include "i_encoder.hpp"
46
#include "msg.hpp"
47

Martin Sustrik's avatar
Martin Sustrik committed
48
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
49
{
50 51 52
//  Helper base class for encoders. It implements the state machine that
//  fills the outgoing buffer. Derived classes should implement individual
//  state machine actions.
Martin Sustrik's avatar
Martin Sustrik committed
53

54 55 56
template <typename T> class encoder_base_t : public i_encoder
{
  public:
57
    inline explicit encoder_base_t (size_t bufsize_) :
58 59 60 61 62 63
        _write_pos (0),
        _to_write (0),
        _next (NULL),
        _new_msg_flag (false),
        _buf_size (bufsize_),
        _buf (static_cast<unsigned char *> (malloc (bufsize_))),
64
        _in_progress (NULL)
Martin Sustrik's avatar
Martin Sustrik committed
65
    {
66
        alloc_assert (_buf);
67 68 69 70
    }

    //  The destructor doesn't have to be virtual. It is made virtual
    //  just to keep ICC and code checking tools from complaining.
71
    inline virtual ~encoder_base_t () { free (_buf); }
72 73 74 75 76 77

    //  The function returns a batch of binary data. The data
    //  are filled to a supplied buffer. If no buffer is supplied (data_
    //  points to NULL) decoder object will provide buffer of its own.
    inline size_t encode (unsigned char **data_, size_t size_)
    {
78 79
        unsigned char *buffer = !*data_ ? _buf : *data_;
        size_t buffersize = !*data_ ? _buf_size : size_;
80

81
        if (in_progress () == NULL)
82 83 84 85 86 87 88
            return 0;

        size_t pos = 0;
        while (pos < buffersize) {
            //  If there are no more data to return, run the state machine.
            //  If there are still no data, return what we already have
            //  in the buffer.
89 90
            if (!_to_write) {
                if (_new_msg_flag) {
91
                    int rc = _in_progress->close ();
92
                    errno_assert (rc == 0);
93
                    rc = _in_progress->init ();
94
                    errno_assert (rc == 0);
95
                    _in_progress = NULL;
96
                    break;
97
                }
98
                (static_cast<T *> (this)->*_next) ();
Martin Sustrik's avatar
Martin Sustrik committed
99
            }
Martin Hurton's avatar
Martin Hurton committed
100

101 102 103 104 105 106 107 108 109 110
            //  If there are no data in the buffer yet and we are able to
            //  fill whole buffer in a single go, let's use zero-copy.
            //  There's no disadvantage to it as we cannot stuck multiple
            //  messages into the buffer anyway. Note that subsequent
            //  write(s) are non-blocking, thus each single write writes
            //  at most SO_SNDBUF bytes at once not depending on how large
            //  is the chunk returned from here.
            //  As a consequence, large messages being sent won't block
            //  other engines running in the same I/O thread for excessive
            //  amounts of time.
111 112 113 114 115
            if (!pos && !*data_ && _to_write >= buffersize) {
                *data_ = _write_pos;
                pos = _to_write;
                _write_pos = NULL;
                _to_write = 0;
116 117
                return pos;
            }
Martin Sustrik's avatar
Martin Sustrik committed
118

119
            //  Copy data to the buffer. If the buffer is full, return.
120 121
            size_t to_copy = std::min (_to_write, buffersize - pos);
            memcpy (buffer + pos, _write_pos, to_copy);
122
            pos += to_copy;
123 124
            _write_pos += to_copy;
            _to_write -= to_copy;
Martin Sustrik's avatar
Martin Sustrik committed
125 126
        }

127 128 129
        *data_ = buffer;
        return pos;
    }
130

131 132
    void load_msg (msg_t *msg_)
    {
133 134
        zmq_assert (in_progress () == NULL);
        _in_progress = msg_;
135
        (static_cast<T *> (this)->*_next) ();
136 137 138 139 140 141 142 143 144 145 146 147 148
    }

  protected:
    //  Prototype of state machine action.
    typedef void (T::*step_t) ();

    //  This function should be called from derived class to write the data
    //  to the buffer and schedule next state machine action.
    inline void next_step (void *write_pos_,
                           size_t to_write_,
                           step_t next_,
                           bool new_msg_flag_)
    {
149 150 151 152
        _write_pos = static_cast<unsigned char *> (write_pos_);
        _to_write = to_write_;
        _next = next_;
        _new_msg_flag = new_msg_flag_;
153
    }
154

155 156
    msg_t *in_progress () { return _in_progress; }

157 158
  private:
    //  Where to get the data to write from.
159
    unsigned char *_write_pos;
160

161
    //  How much data to write before next step should be executed.
162
    size_t _to_write;
Martin Sustrik's avatar
Martin Sustrik committed
163

164 165
    //  Next step. If set to NULL, it means that associated data stream
    //  is dead.
166
    step_t _next;
167

168
    bool _new_msg_flag;
169

170
    //  The buffer for encoded data.
171 172
    const size_t _buf_size;
    unsigned char *const _buf;
173

174 175
    encoder_base_t (const encoder_base_t &);
    void operator= (const encoder_base_t &);
176

177
    msg_t *_in_progress;
178
};
Martin Sustrik's avatar
Martin Sustrik committed
179 180 181
}

#endif