You need to sign in or sign up before continuing.
encoder.hpp 5.86 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
    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
    ~encoder_base_t () ZMQ_OVERRIDE { free (_buf); }
70 71 72 73

    //  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.
74
    size_t encode (unsigned char **data_, size_t size_) ZMQ_FINAL
75
    {
76
        unsigned char *buffer = !*data_ ? _buf : *data_;
77
        const size_t buffersize = !*data_ ? _buf_size : size_;
78

79
        if (in_progress () == NULL)
80 81 82 83 84 85 86
            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.
87 88
            if (!_to_write) {
                if (_new_msg_flag) {
89
                    int rc = _in_progress->close ();
90
                    errno_assert (rc == 0);
91
                    rc = _in_progress->init ();
92
                    errno_assert (rc == 0);
93
                    _in_progress = NULL;
94
                    break;
95
                }
96
                (static_cast<T *> (this)->*_next) ();
Martin Sustrik's avatar
Martin Sustrik committed
97
            }
Martin Hurton's avatar
Martin Hurton committed
98

99 100 101 102 103 104 105 106 107 108
            //  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.
109 110 111 112 113
            if (!pos && !*data_ && _to_write >= buffersize) {
                *data_ = _write_pos;
                pos = _to_write;
                _write_pos = NULL;
                _to_write = 0;
114 115
                return pos;
            }
Martin Sustrik's avatar
Martin Sustrik committed
116

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

125 126 127
        *data_ = buffer;
        return pos;
    }
128

129
    void load_msg (msg_t *msg_) ZMQ_FINAL
130
    {
131 132
        zmq_assert (in_progress () == NULL);
        _in_progress = msg_;
133
        (static_cast<T *> (this)->*_next) ();
134 135 136 137 138 139 140 141
    }

  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.
142 143 144 145
    void next_step (void *write_pos_,
                    size_t to_write_,
                    step_t next_,
                    bool new_msg_flag_)
146
    {
147 148 149 150
        _write_pos = static_cast<unsigned char *> (write_pos_);
        _to_write = to_write_;
        _next = next_;
        _new_msg_flag = new_msg_flag_;
151
    }
152

153 154
    msg_t *in_progress () { return _in_progress; }

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

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

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

166
    bool _new_msg_flag;
167

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

172
    msg_t *_in_progress;
173 174

    ZMQ_NON_COPYABLE_NOR_MOVABLE (encoder_base_t)
175
};
Martin Sustrik's avatar
Martin Sustrik committed
176 177 178
}

#endif