decoder.cpp 4.85 KB
Newer Older
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2007-2009 iMatix Corporation
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
10 11 12 13 14 15
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ 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
16
    GNU Lesser General Public License for more details.
17

18
    You should have received a copy of the GNU Lesser General Public License
19 20 21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

22 23
#include <stdlib.h>
#include <string.h>
24
#include <limits>
25

26 27 28 29 30
#include "platform.hpp"
#if defined ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#endif

31
#include "decoder.hpp"
32
#include "i_msg_sink.hpp"
33
#include "likely.hpp"
34
#include "wire.hpp"
35
#include "err.hpp"
36

37
zmq::decoder_t::decoder_t (size_t bufsize_, int64_t maxmsgsize_) :
38
    decoder_base_t <decoder_t> (bufsize_),
39
    msg_sink (NULL),
40
    maxmsgsize (maxmsgsize_)
41
{
42 43
    int rc = in_progress.init ();
    errno_assert (rc == 0);
44 45

    //  At the beginning, read one byte and go to one_byte_size_ready state.
46
    next_step (tmpbuf, 1, &decoder_t::one_byte_size_ready);
47 48
}

49
zmq::decoder_t::~decoder_t ()
50
{
51 52
    int rc = in_progress.close ();
    errno_assert (rc == 0);
53 54
}

55
void zmq::decoder_t::set_msg_sink (i_msg_sink *msg_sink_)
56
{
57
    msg_sink = msg_sink_;
58 59
}

60
bool zmq::decoder_t::one_byte_size_ready ()
61 62 63 64 65
{
    //  First byte of size is read. If it is 0xff read 8-byte size.
    //  Otherwise allocate the buffer for message data and read the
    //  message data into it.
    if (*tmpbuf == 0xff)
66
        next_step (tmpbuf, 8, &decoder_t::eight_byte_size_ready);
67
    else {
68

69
        //  There has to be at least one byte (the flags) in the message).
70 71 72 73
        if (!*tmpbuf) {
            decoding_error ();
            return false;
        }
74

75 76 77
        //  in_progress is initialised at this point so in theory we should
        //  close it before calling zmq_msg_init_size, however, it's a 0-byte
        //  message and thus we can treat it as uninitialised...
78 79 80 81 82 83
        int rc;
        if (maxmsgsize >= 0 && (int64_t) (*tmpbuf - 1) > maxmsgsize) {
            rc = -1;
            errno = ENOMEM;
        }
        else
84
            rc = in_progress.init_size (*tmpbuf - 1);
85
        if (rc != 0 && errno == ENOMEM) {
86
            rc = in_progress.init ();
87
            errno_assert (rc == 0);
88 89 90
            decoding_error ();
            return false;
        }
91
        errno_assert (rc == 0);
92

93
        next_step (tmpbuf, 1, &decoder_t::flags_ready);
94 95 96 97
    }
    return true;
}

98
bool zmq::decoder_t::eight_byte_size_ready ()
99
{
100 101 102
    //  8-byte payload length is read. Allocate the buffer
    //  for message body and read the message data into it.
    const uint64_t payload_length = get_uint64 (tmpbuf);
103

104
    //  There has to be at least one byte (the flags) in the message).
105
    if (payload_length == 0) {
106 107 108
        decoding_error ();
        return false;
    }
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123
    //  Message size must not exceed the maximum allowed size.
    if (maxmsgsize >= 0 && payload_length - 1 > (uint64_t) maxmsgsize) {
        decoding_error ();
        return false;
    }

    //  Message size must fit within range of size_t data type.
    if (payload_length - 1 > std::numeric_limits <size_t>::max ()) {
        decoding_error ();
        return false;
    }

    const size_t msg_size = static_cast <size_t> (payload_length - 1);

124
    //  in_progress is initialised at this point so in theory we should
125
    //  close it before calling init_size, however, it's a 0-byte
126
    //  message and thus we can treat it as uninitialised...
127 128 129
    int rc = in_progress.init_size (msg_size);
    if (rc != 0) {
        errno_assert (errno == ENOMEM);
130
        rc = in_progress.init ();
131
        errno_assert (rc == 0);
132 133 134
        decoding_error ();
        return false;
    }
135

136
    next_step (tmpbuf, 1, &decoder_t::flags_ready);
137 138 139
    return true;
}

140
bool zmq::decoder_t::flags_ready ()
141
{
142
    //  Store the flags from the wire into the message structure.
143
    in_progress.set_flags (tmpbuf [0] & msg_t::more);
144

145
    next_step (in_progress.data (), in_progress.size (),
146
        &decoder_t::message_ready);
147

148 149 150
    return true;
}

151
bool zmq::decoder_t::message_ready ()
152 153
{
    //  Message is completely read. Push it further and start reading
154
    //  new message. (in_progress is a 0-byte message after this point.)
155
    if (unlikely (!msg_sink))
156
        return false;
157
    int rc = msg_sink->push_msg (&in_progress);
158 159 160 161 162
    if (unlikely (rc != 0)) {
        if (errno != EAGAIN)
            decoding_error ();
        return false;
    }
163

164
    next_step (tmpbuf, 1, &decoder_t::one_byte_size_ready);
165 166
    return true;
}