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
6
This file is part of 0MQ.
8
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
the Free Software Foundation; either version 3 of the License, or
11
(at your option) any later version.
13
0MQ is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
25
#include "platform.hpp"
26
#ifdef ZMQ_HAVE_WINDOWS
27
#include "windows.hpp"
30
#include "v1_protocol.hpp"
31
#include "v1_decoder.hpp"
36
zmq::v1_decoder_t::v1_decoder_t (size_t bufsize_,
37
int64_t maxmsgsize_, i_msg_sink *msg_sink_) :
38
decoder_base_t <v1_decoder_t> (bufsize_),
41
maxmsgsize (maxmsgsize_)
43
int rc = in_progress.init ();
44
errno_assert (rc == 0);
46
// At the beginning, read one byte and go to flags_ready state.
47
next_step (tmpbuf, 1, &v1_decoder_t::flags_ready);
50
zmq::v1_decoder_t::~v1_decoder_t ()
52
int rc = in_progress.close ();
53
errno_assert (rc == 0);
56
void zmq::v1_decoder_t::set_msg_sink (i_msg_sink *msg_sink_)
61
bool zmq::v1_decoder_t::stalled () const
63
return next == &v1_decoder_t::message_ready;
66
bool zmq::v1_decoder_t::flags_ready ()
69
if (tmpbuf [0] & v1_protocol_t::more_flag)
70
msg_flags |= msg_t::more;
72
// The payload length is either one or eight bytes,
73
// depending on whether the 'large' bit is set.
74
if (tmpbuf [0] & v1_protocol_t::large_flag)
75
next_step (tmpbuf, 8, &v1_decoder_t::eight_byte_size_ready);
77
next_step (tmpbuf, 1, &v1_decoder_t::one_byte_size_ready);
82
bool zmq::v1_decoder_t::one_byte_size_ready ()
86
// Message size must not exceed the maximum allowed size.
88
if (unlikely (tmpbuf [0] > static_cast <uint64_t> (maxmsgsize)))
91
// in_progress is initialised at this point so in theory we should
92
// close it before calling zmq_msg_init_size, however, it's a 0-byte
93
// message and thus we can treat it as uninitialised...
94
rc = in_progress.init_size (tmpbuf [0]);
96
errno_assert (errno == ENOMEM);
97
int rc = in_progress.init ();
98
errno_assert (rc == 0);
102
in_progress.set_flags (msg_flags);
103
next_step (in_progress.data (), in_progress.size (),
104
&v1_decoder_t::message_ready);
113
bool zmq::v1_decoder_t::eight_byte_size_ready ()
117
// The payload size is encoded as 64-bit unsigned integer.
118
// The most significant byte comes first.
119
const uint64_t msg_size = get_uint64 (tmpbuf);
121
// Message size must not exceed the maximum allowed size.
123
if (unlikely (msg_size > static_cast <uint64_t> (maxmsgsize)))
126
// Message size must fit into size_t data type.
127
if (unlikely (msg_size != static_cast <size_t> (msg_size)))
130
// in_progress is initialised at this point so in theory we should
131
// close it before calling init_size, however, it's a 0-byte
132
// message and thus we can treat it as uninitialised.
133
rc = in_progress.init_size (static_cast <size_t> (msg_size));
135
errno_assert (errno == ENOMEM);
136
int rc = in_progress.init ();
137
errno_assert (rc == 0);
141
in_progress.set_flags (msg_flags);
142
next_step (in_progress.data (), in_progress.size (),
143
&v1_decoder_t::message_ready);
152
bool zmq::v1_decoder_t::message_ready ()
154
// Message is completely read. Push it further and start reading
155
// new message. (in_progress is a 0-byte message after this point.)
156
if (unlikely (!msg_sink))
158
int rc = msg_sink->push_msg (&in_progress);
159
if (unlikely (rc != 0)) {
165
next_step (tmpbuf, 1, &v1_decoder_t::flags_ready);