encoder.hpp 6.18 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2015 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
            bufsize (bufsize_),
            in_progress (NULL)
Martin Sustrik's avatar
Martin Sustrik committed
62
        {
63
            buf = (unsigned char*) malloc (bufsize_);
64
            alloc_assert (buf);
Martin Sustrik's avatar
Martin Sustrik committed
65 66
        }

67
        //  The destructor doesn't have to be virtual. It is made virtual
68
        //  just to keep ICC and code checking tools from complaining.
69
        inline virtual ~encoder_base_t ()
70 71 72
        {
            free (buf);
        }
73
        
Martin Sustrik's avatar
Martin Sustrik committed
74 75 76
        //  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.
77
        inline size_t encode (unsigned char **data_, size_t size_)
Martin Sustrik's avatar
Martin Sustrik committed
78
        {
Martin Sustrik's avatar
Martin Sustrik committed
79
            unsigned char *buffer = !*data_ ? buf : *data_;
80
            size_t buffersize = !*data_ ? bufsize : size_;
Martin Sustrik's avatar
Martin Sustrik committed
81

82 83
            if (in_progress == NULL)
                return 0;
84

Martin Hurton's avatar
Martin Hurton committed
85 86
            size_t pos = 0;
            while (pos < buffersize) {
87 88 89 90 91

                //  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) {
92 93 94 95 96 97
                    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
98
                        break;
99 100
                    }
                    (static_cast <T*> (this)->*next) ();
101
                }
102

103 104 105 106 107 108 109
                //  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.
110 111 112
                //  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
113
                if (!pos && !*data_ && to_write >= buffersize) {
114
                    *data_ = write_pos;
115
                    pos = to_write;
116
                    write_pos = NULL;
117
                    to_write = 0;
118
                    return pos;
119 120
                }

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

            *data_ = buffer;
130 131 132 133 134 135 136 137
            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
138
        }
139

Martin Sustrik's avatar
Martin Sustrik committed
140 141 142
    protected:

        //  Prototype of state machine action.
143
        typedef void (T::*step_t) ();
Martin Sustrik's avatar
Martin Sustrik committed
144 145

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

    private:

158
        //  Where to get the data to write from.
Martin Sustrik's avatar
Martin Sustrik committed
159
        unsigned char *write_pos;
160 161

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

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

168
        bool new_msg_flag;
Martin Sustrik's avatar
Martin Sustrik committed
169

170
        //  The buffer for encoded data.
171 172 173
        size_t bufsize;
        unsigned char *buf;

174 175
        encoder_base_t (const encoder_base_t&);
        void operator = (const encoder_base_t&);
176 177 178 179 180

    protected:

        msg_t *in_progress;

181
    };
Martin Sustrik's avatar
Martin Sustrik committed
182 183 184
}

#endif
185