encoder.hpp 6.28 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 "msg.hpp"
46
#include "i_encoder.hpp"
47

Martin Sustrik's avatar
Martin Sustrik committed
48
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
49 50 51 52 53 54
{

    //  Helper base class for encoders. It implements the state machine that
    //  fills the outgoing buffer. Derived classes should implement individual
    //  state machine actions.

55
    template <typename T> class encoder_base_t : public i_encoder
Martin Sustrik's avatar
Martin Sustrik committed
56 57 58
    {
    public:

59
        inline encoder_base_t (size_t bufsize_) :
60 61
            write_pos(0),
            to_write(0),
62
            next(NULL),
63
            new_msg_flag(false),
64
            bufsize (bufsize_),
65
            in_progress (NULL)
Martin Sustrik's avatar
Martin Sustrik committed
66
        {
67
            buf = (unsigned char*) malloc (bufsize_);
68
            alloc_assert (buf);
Martin Sustrik's avatar
Martin Sustrik committed
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
        inline virtual ~encoder_base_t ()
74 75 76
        {
            free (buf);
        }
77

Martin Sustrik's avatar
Martin Sustrik committed
78 79 80
        //  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.
81
        inline size_t encode (unsigned char **data_, size_t size_)
Martin Sustrik's avatar
Martin Sustrik committed
82
        {
Martin Sustrik's avatar
Martin Sustrik committed
83
            unsigned char *buffer = !*data_ ? buf : *data_;
84
            size_t buffersize = !*data_ ? bufsize : size_;
Martin Sustrik's avatar
Martin Sustrik committed
85

86 87
            if (in_progress == NULL)
                return 0;
88

Martin Hurton's avatar
Martin Hurton committed
89 90
            size_t pos = 0;
            while (pos < buffersize) {
91 92 93 94 95

                //  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.
                if (!to_write) {
96 97 98 99 100 101
                    if (new_msg_flag) {
                        int rc = in_progress->close ();
                        errno_assert (rc == 0);
                        rc = in_progress->init ();
                        errno_assert (rc == 0);
                        in_progress = NULL;
Martin Hurton's avatar
Martin Hurton committed
102
                        break;
103 104
                    }
                    (static_cast <T*> (this)->*next) ();
105
                }
106

107 108 109 110 111 112 113
                //  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.
114 115 116
                //  As a consequence, large messages being sent won't block
                //  other engines running in the same I/O thread for excessive
                //  amounts of time.
Martin Sustrik's avatar
Martin Sustrik committed
117
                if (!pos && !*data_ && to_write >= buffersize) {
118
                    *data_ = write_pos;
119
                    pos = to_write;
120
                    write_pos = NULL;
121
                    to_write = 0;
122
                    return pos;
123 124
                }

125
                //  Copy data to the buffer. If the buffer is full, return.
Martin Sustrik's avatar
Martin Sustrik committed
126 127
                size_t to_copy = std::min (to_write, buffersize - pos);
                memcpy (buffer + pos, write_pos, to_copy);
128 129 130
                pos += to_copy;
                write_pos += to_copy;
                to_write -= to_copy;
Martin Sustrik's avatar
Martin Sustrik committed
131
            }
Martin Hurton's avatar
Martin Hurton committed
132 133

            *data_ = buffer;
134 135 136 137 138 139 140 141
            return pos;
        }

        void load_msg (msg_t *msg_)
        {
            zmq_assert (in_progress == NULL);
            in_progress = msg_;
            (static_cast <T*> (this)->*next) ();
Martin Sustrik's avatar
Martin Sustrik committed
142
        }
143

Martin Sustrik's avatar
Martin Sustrik committed
144 145 146
    protected:

        //  Prototype of state machine action.
147
        typedef void (T::*step_t) ();
Martin Sustrik's avatar
Martin Sustrik committed
148 149

        //  This function should be called from derived class to write the data
150
        //  to the buffer and schedule next state machine action.
Martin Sustrik's avatar
Martin Sustrik committed
151
        inline void next_step (void *write_pos_, size_t to_write_,
152
            step_t next_, bool new_msg_flag_)
Martin Sustrik's avatar
Martin Sustrik committed
153 154 155 156
        {
            write_pos = (unsigned char*) write_pos_;
            to_write = to_write_;
            next = next_;
157
            new_msg_flag = new_msg_flag_;
Martin Sustrik's avatar
Martin Sustrik committed
158 159 160 161
        }

    private:

162
        //  Where to get the data to write from.
Martin Sustrik's avatar
Martin Sustrik committed
163
        unsigned char *write_pos;
164 165

        //  How much data to write before next step should be executed.
Martin Sustrik's avatar
Martin Sustrik committed
166
        size_t to_write;
167 168 169

        //  Next step. If set to NULL, it means that associated data stream
        //  is dead.
Martin Sustrik's avatar
Martin Sustrik committed
170
        step_t next;
171

172
        bool new_msg_flag;
Martin Sustrik's avatar
Martin Sustrik committed
173

174
        //  The buffer for encoded data.
175 176 177
        size_t bufsize;
        unsigned char *buf;

178 179
        encoder_base_t (const encoder_base_t&);
        void operator = (const encoder_base_t&);
180 181 182 183 184

    protected:

        msg_t *in_progress;

185
    };
Martin Sustrik's avatar
Martin Sustrik committed
186 187 188
}

#endif
189