Commit 84728cd9 authored by Ian Barber's avatar Ian Barber

Merge pull request #421 from hurtonm/master

New message encoder/decoder + code simplification
parents a224c973 2a41c8d7
......@@ -133,6 +133,8 @@ libzmq_la_SOURCES = \
xpub.cpp \
router.cpp \
dealer.cpp \
v1_decoder.cpp \
v1_encoder.cpp \
xsub.cpp \
zmq.cpp \
zmq_utils.cpp
......
......@@ -29,6 +29,7 @@
#include "err.hpp"
#include "msg.hpp"
#include "i_decoder.hpp"
#include "stdint.hpp"
namespace zmq
......@@ -47,7 +48,7 @@ namespace zmq
// This class implements the state machine that parses the incoming buffer.
// Derived class should implement individual state machine actions.
template <typename T> class decoder_base_t
template <typename T> class decoder_base_t : public i_decoder
{
public:
......
......@@ -29,6 +29,7 @@
#include "err.hpp"
#include "msg.hpp"
#include "i_encoder.hpp"
namespace zmq
{
......@@ -39,7 +40,7 @@ namespace zmq
// fills the outgoing buffer. Derived classes should implement individual
// state machine actions.
template <typename T> class encoder_base_t
template <typename T> class encoder_base_t : public i_encoder
{
public:
......
/*
Copyright (c) 2007-2012 iMatix Corporation
Copyright (c) 2007-2012 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
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
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ZMQ_I_DECODER_HPP_INCLUDED__
#define __ZMQ_I_DECODER_HPP_INCLUDED__
#include <stdint.h>
namespace zmq
{
class i_msg_sink;
// Interface to be implemented by message decoder.
struct i_decoder
{
virtual ~i_decoder () {}
virtual void set_msg_sink (i_msg_sink *msg_sink_) = 0;
virtual void get_buffer (unsigned char **data_, size_t *size_) = 0;
virtual size_t process_buffer (unsigned char *data_, size_t size_) = 0;
virtual bool stalled () const = 0;
};
}
#endif
/*
Copyright (c) 2007-2012 iMatix Corporation
Copyright (c) 2007-2012 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
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
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ZMQ_I_ENCODER_HPP_INCLUDED__
#define __ZMQ_I_ENCODER_HPP_INCLUDED__
#include <stdint.h>
namespace zmq
{
// Forward declaration
class i_msg_source;
// Interface to be implemented by message encoder.
struct i_encoder
{
virtual ~i_encoder () {}
// Set message producer.
virtual void set_msg_source (i_msg_source *msg_source_) = 0;
// The function returns a batch of binary data. The data
// are filled to a supplied buffer. If no buffer is supplied (data_
// is NULL) encoder will provide buffer of its own.
// If offset is not NULL, it is filled by offset of the first message
// in the batch.If there's no beginning of a message in the batch,
// offset is set to -1.
virtual void get_data (unsigned char **data_, size_t *size_,
int *offset_ = NULL) = 0;
};
}
#endif
......@@ -38,6 +38,10 @@
#include "stream_engine.hpp"
#include "io_thread.hpp"
#include "session_base.hpp"
#include "encoder.hpp"
#include "decoder.hpp"
#include "v1_encoder.hpp"
#include "v1_decoder.hpp"
#include "config.hpp"
#include "err.hpp"
#include "ip.hpp"
......@@ -48,14 +52,13 @@ zmq::stream_engine_t::stream_engine_t (fd_t fd_, const options_t &options_, cons
s (fd_),
inpos (NULL),
insize (0),
decoder (in_batch_size, options_.maxmsgsize),
decoder (NULL),
input_error (false),
outpos (NULL),
outsize (0),
encoder (out_batch_size),
encoder (NULL),
handshaking (true),
greeting_bytes_read (0),
greeting_size (0),
session (NULL),
options (options_),
endpoint (endpoint_),
......@@ -106,6 +109,11 @@ zmq::stream_engine_t::~stream_engine_t ()
#endif
s = retired_fd;
}
if (encoder != NULL)
delete encoder;
if (decoder != NULL)
delete decoder;
}
void zmq::stream_engine_t::plug (io_thread_t *io_thread_,
......@@ -123,13 +131,6 @@ void zmq::stream_engine_t::plug (io_thread_t *io_thread_,
io_object_t::plug (io_thread_);
handle = add_fd (s);
// We need to detect whether our peer is using the versioned
// protocol. The detection is done in two steps. First, we read
// first two bytes and check if the long format of length is in use.
// If so, we receive and check the 'flags' field. If the rightmost bit
// is 1, the peer is using versioned protocol.
greeting_size = 2;
// Send the 'length' and 'flags' fields of the identity message.
// The 'length' field is encoded in the long format.
outpos = greeting_output_buffer;
......@@ -156,8 +157,10 @@ void zmq::stream_engine_t::unplug ()
io_object_t::unplug ();
// Disconnect from session object.
encoder.set_msg_source (NULL);
decoder.set_msg_sink (NULL);
if (encoder)
encoder->set_msg_source (NULL);
if (decoder)
decoder->set_msg_sink (NULL);
session = NULL;
}
......@@ -174,6 +177,7 @@ void zmq::stream_engine_t::in_event ()
if (!handshake ())
return;
zmq_assert (decoder);
bool disconnection = false;
// If there's no data to process in the buffer...
......@@ -183,7 +187,7 @@ void zmq::stream_engine_t::in_event ()
// Note that buffer can be arbitrarily large. However, we assume
// the underlying TCP layer has fixed buffer size and thus the
// number of bytes read will be always limited.
decoder.get_buffer (&inpos, &insize);
decoder->get_buffer (&inpos, &insize);
insize = read (inpos, insize);
// Check whether the peer has closed the connection.
......@@ -194,7 +198,7 @@ void zmq::stream_engine_t::in_event ()
}
// Push the data to the decoder.
size_t processed = decoder.process_buffer (inpos, insize);
size_t processed = decoder->process_buffer (inpos, insize);
if (unlikely (processed == (size_t) -1)) {
disconnection = true;
......@@ -220,7 +224,7 @@ void zmq::stream_engine_t::in_event ()
// until after the session has accepted the message.
if (disconnection) {
input_error = true;
if (decoder.stalled ())
if (decoder->stalled ())
reset_pollin (handle);
else
error ();
......@@ -233,7 +237,8 @@ void zmq::stream_engine_t::out_event ()
if (!outsize) {
outpos = NULL;
encoder.get_data (&outpos, &outsize);
zmq_assert (encoder);
encoder->get_data (&outpos, &outsize);
// If there is no data to send, stop polling for output.
if (outsize == 0) {
......@@ -284,8 +289,9 @@ void zmq::stream_engine_t::activate_in ()
// There was an input error but the engine could not
// be terminated (due to the stalled decoder).
// Flush the pending message and terminate the engine now.
decoder.process_buffer (inpos, 0);
zmq_assert (!decoder.stalled ());
zmq_assert (decoder);
decoder->process_buffer (inpos, 0);
zmq_assert (!decoder->stalled ());
session->flush ();
error ();
return;
......@@ -297,97 +303,66 @@ void zmq::stream_engine_t::activate_in ()
in_event ();
}
int zmq::stream_engine_t::receive_greeting ()
bool zmq::stream_engine_t::handshake ()
{
zmq_assert (handshaking);
zmq_assert (greeting_bytes_read < greeting_size);
// Receive the greeting.
while (greeting_bytes_read < greeting_size) {
const int n = read (greeting + greeting_bytes_read,
greeting_size - greeting_bytes_read);
if (n == -1)
return -1;
if (n == -1) {
error ();
return false;
}
if (n == 0)
return 0;
return false;
greeting_bytes_read += n;
if (greeting_bytes_read < greeting_size)
continue;
// We have received at least one byte from the peer.
// If the first byte is not 0xff, we know that the
// peer is using unversioned protocol.
if (greeting [0] != 0xff)
break;
if (greeting_size == 2) {
// We have received the first two bytes from the peer.
// If the first byte is not 0xff, we know that the
// peer is using unversioned protocol.
if (greeting [0] != 0xff)
break;
// This may still be a long identity message (either
// 254 or 255 bytes long). We need to receive 8 more
// bytes so we can inspect the potential 'flags' field.
greeting_size = 10;
}
else
if (greeting_size == 10) {
// Inspect the rightmost bit of the 10th byte (which coincides
// with the 'flags' field if a regular message was sent).
// Zero indicates this is a header of identity message
// (i.e. the peer is using the unversioned protocol).
if (!(greeting [9] & 0x01))
break;
if (greeting_bytes_read < 10)
continue;
// This is truly a handshake and we can now send the rest of
// the greeting message out.
// Inspect the right-most bit of the 10th byte (which coincides
// with the 'flags' field if a regular message was sent).
// Zero indicates this is a header of identity message
// (i.e. the peer is using the unversioned protocol).
if (!(greeting [9] & 0x01))
break;
// The peer is using versioned protocol.
// Send the rest of the greeting, if necessary.
if (outpos + outsize != greeting_output_buffer + greeting_size) {
if (outsize == 0)
set_pollout (handle);
zmq_assert (outpos != NULL);
outpos [outsize++] = 1; // Protocol version
outpos [outsize++] = 1; // Remaining length (1 byte for v1)
outpos [outsize++] = 1; // Protocol version
outpos [outsize++] = options.type; // Socket type
// Read the 'version' and 'remaining_length' fields.
greeting_size = 12;
}
else
if (greeting_size == 12) {
// We have received the greeting message up to
// the 'remaining_length' field. Receive the remaining
// bytes of the greeting.
greeting_size += greeting [11];
}
}
return 0;
}
bool zmq::stream_engine_t::handshake ()
{
zmq_assert (handshaking);
zmq_assert (greeting_bytes_read < greeting_size);
int rc = receive_greeting ();
if (rc == -1) {
error ();
return false;
}
if (greeting_bytes_read < greeting_size)
return false;
// We have received either a header of identity message
// or the whole greeting.
encoder.set_msg_source (session);
decoder.set_msg_sink (session);
zmq_assert (greeting [0] != 0xff || greeting_bytes_read >= 10);
// Position of the version field in the greeting.
const size_t version_pos = 10;
// Is the peer using the unversioned protocol?
// If so, we send and receive rests of identity
// messages.
if (greeting [0] != 0xff || !(greeting [9] & 0x01)) {
encoder = new (std::nothrow) encoder_t (out_batch_size);
alloc_assert (encoder);
encoder->set_msg_source (session);
decoder = new (std::nothrow) decoder_t (in_batch_size, options.maxmsgsize);
alloc_assert (decoder);
decoder->set_msg_sink (session);
// We have already sent the message header.
// Since there is no way to tell the encoder to
// skip the message header, we simply throw that
......@@ -395,7 +370,7 @@ bool zmq::stream_engine_t::handshake ()
const size_t header_size = options.identity_size + 1 >= 255 ? 10 : 2;
unsigned char tmp [10], *bufferp = tmp;
size_t buffer_size = header_size;
encoder.get_data (&bufferp, &buffer_size);
encoder->get_data (&bufferp, &buffer_size);
zmq_assert (buffer_size == header_size);
// Make sure the decoder sees the data we have already received.
......@@ -408,7 +383,27 @@ bool zmq::stream_engine_t::handshake ()
// message right after the identity message, we temporarily
// divert the message stream from session to ourselves.
if (options.type == ZMQ_PUB || options.type == ZMQ_XPUB)
decoder.set_msg_sink (this);
decoder->set_msg_sink (this);
}
else
if (greeting [version_pos] == 0) {
// ZMTP/1.0 framing.
encoder = new (std::nothrow) encoder_t (out_batch_size);
alloc_assert (encoder);
encoder->set_msg_source (session);
decoder = new (std::nothrow) decoder_t (in_batch_size, options.maxmsgsize);
alloc_assert (decoder);
decoder->set_msg_sink (session);
}
else {
// v1 framing protocol.
encoder = new (std::nothrow) v1_encoder_t (out_batch_size, session);
alloc_assert (encoder);
decoder = new (std::nothrow)
v1_decoder_t (in_batch_size, options.maxmsgsize, session);
alloc_assert (decoder);
}
// Start polling for output if necessary.
......@@ -441,7 +436,8 @@ int zmq::stream_engine_t::push_msg (msg_t *msg_)
// Once we have injected the subscription message, we can
// Divert the message flow back to the session.
decoder.set_msg_sink (session);
zmq_assert (decoder);
decoder->set_msg_sink (session);
return rc;
}
......
......@@ -28,8 +28,8 @@
#include "i_engine.hpp"
#include "i_msg_sink.hpp"
#include "io_object.hpp"
#include "encoder.hpp"
#include "decoder.hpp"
#include "i_encoder.hpp"
#include "i_decoder.hpp"
#include "options.hpp"
#include "../include/zmq.h"
......@@ -91,26 +91,20 @@ namespace zmq
// Underlying socket.
fd_t s;
// Maximum size of a greeting message:
// preamble (10 bytes) + version (1 byte) + remaining_length (1 byte) +
// up to 255 remaining bytes.
const static size_t maximum_greeting_size = 10 + 1 + 1 + 255;
// Size of v1 greeting message:
// preamble (10 bytes) + version (1 byte) + remaining_length (1 byte) +
// socket_type (1)
const static size_t v1_greeting_size = 10 + 1 + 1 + 1;
// Size of the greeting message:
// Preamble (10 bytes) + version (1 byte) + socket type (1 byte).
const static size_t greeting_size = 12;
handle_t handle;
unsigned char *inpos;
size_t insize;
decoder_t decoder;
i_decoder *decoder;
bool input_error;
unsigned char *outpos;
size_t outsize;
encoder_t encoder;
i_encoder *encoder;
// When true, we are still trying to determine whether
// the peer is using versioned protocol, and if so, which
......@@ -119,18 +113,15 @@ namespace zmq
// The receive buffer holding the greeting message
// that we are receiving from the peer.
unsigned char greeting [maximum_greeting_size];
unsigned char greeting [greeting_size];
// The number of bytes of the greeting message that
// we have already received.
unsigned int greeting_bytes_read;
// The size of the greeting message.
unsigned int greeting_size;
// The send buffer holding the greeting message
// that we are sending to the peer.
unsigned char greeting_output_buffer [v1_greeting_size];
unsigned char greeting_output_buffer [greeting_size];
// The session this engine is attached to.
zmq::session_base_t *session;
......
/*
Copyright (c) 2009-2011 250bpm s.r.o.
Copyright (c) 2007-2009 iMatix Corporation
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
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
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "platform.hpp"
#ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#endif
#include "v1_protocol.hpp"
#include "v1_decoder.hpp"
#include "likely.hpp"
#include "wire.hpp"
#include "err.hpp"
zmq::v1_decoder_t::v1_decoder_t (size_t bufsize_,
int64_t maxmsgsize_, i_msg_sink *msg_sink_) :
decoder_base_t <v1_decoder_t> (bufsize_),
msg_sink (msg_sink_),
msg_flags (0),
maxmsgsize (maxmsgsize_)
{
int rc = in_progress.init ();
errno_assert (rc == 0);
// At the beginning, read one byte and go to flags_ready state.
next_step (tmpbuf, 1, &v1_decoder_t::flags_ready);
}
zmq::v1_decoder_t::~v1_decoder_t ()
{
int rc = in_progress.close ();
errno_assert (rc == 0);
}
void zmq::v1_decoder_t::set_msg_sink (i_msg_sink *msg_sink_)
{
msg_sink = msg_sink_;
}
bool zmq::v1_decoder_t::stalled () const
{
return next == &v1_decoder_t::message_ready;
}
bool zmq::v1_decoder_t::flags_ready ()
{
msg_flags = 0;
if (tmpbuf [0] & v1_protocol_t::more_flag)
msg_flags |= msg_t::more;
// The payload length is either one or eight bytes,
// depending on whether the 'large' bit is set.
if (tmpbuf [0] & v1_protocol_t::large_flag)
next_step (tmpbuf, 8, &v1_decoder_t::eight_byte_size_ready);
else
next_step (tmpbuf, 1, &v1_decoder_t::one_byte_size_ready);
return true;
}
bool zmq::v1_decoder_t::one_byte_size_ready ()
{
int rc = 0;
// Message size must not exceed the maximum allowed size.
if (maxmsgsize >= 0)
if (unlikely (tmpbuf [0] > static_cast <uint64_t> (maxmsgsize)))
goto error;
// 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...
rc = in_progress.init_size (tmpbuf [0]);
if (unlikely (rc)) {
errno_assert (errno == ENOMEM);
int rc = in_progress.init ();
errno_assert (rc == 0);
goto error;
}
in_progress.set_flags (msg_flags);
next_step (in_progress.data (), in_progress.size (),
&v1_decoder_t::message_ready);
return true;
error:
decoding_error ();
return false;
}
bool zmq::v1_decoder_t::eight_byte_size_ready ()
{
int rc = 0;
// The payload size is encoded as 64-bit unsigned integer.
// The most significant byte comes first.
const uint64_t msg_size = get_uint64 (tmpbuf);
// Message size must not exceed the maximum allowed size.
if (maxmsgsize >= 0)
if (unlikely (msg_size > static_cast <uint64_t> (maxmsgsize)))
goto error;
// Message size must fit into size_t data type.
if (unlikely (msg_size != static_cast <size_t> (msg_size)))
goto error;
// in_progress is initialised at this point so in theory we should
// close it before calling init_size, however, it's a 0-byte
// message and thus we can treat it as uninitialised.
rc = in_progress.init_size (static_cast <size_t> (msg_size));
if (unlikely (rc)) {
errno_assert (errno == ENOMEM);
int rc = in_progress.init ();
errno_assert (rc == 0);
goto error;
}
in_progress.set_flags (msg_flags);
next_step (in_progress.data (), in_progress.size (),
&v1_decoder_t::message_ready);
return true;
error:
decoding_error ();
return false;
}
bool zmq::v1_decoder_t::message_ready ()
{
// Message is completely read. Push it further and start reading
// new message. (in_progress is a 0-byte message after this point.)
if (unlikely (!msg_sink))
return false;
int rc = msg_sink->push_msg (&in_progress);
if (unlikely (rc != 0)) {
if (errno != EAGAIN)
decoding_error ();
return false;
}
next_step (tmpbuf, 1, &v1_decoder_t::flags_ready);
return true;
}
/*
Copyright (c) 2009-2011 250bpm s.r.o.
Copyright (c) 2007-2012 iMatix Corporation
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
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
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ZMQ_V1_DECODER_HPP_INCLUDED__
#define __ZMQ_V1_DECODER_HPP_INCLUDED__
#include "err.hpp"
#include "msg.hpp"
#include "decoder.hpp"
#include "i_msg_sink.hpp"
#include "stdint.hpp"
namespace zmq
{
// Decoder for 0MQ v1 framing protocol. Converts data stream into messages.
class v1_decoder_t : public decoder_base_t <v1_decoder_t>
{
public:
v1_decoder_t (size_t bufsize_,
int64_t maxmsgsize_, i_msg_sink *msg_sink_);
virtual ~v1_decoder_t ();
// i_decoder interface.
virtual void set_msg_sink (i_msg_sink *msg_sink_);
virtual bool stalled () const;
private:
bool flags_ready ();
bool one_byte_size_ready ();
bool eight_byte_size_ready ();
bool message_ready ();
i_msg_sink *msg_sink;
unsigned char tmpbuf [8];
unsigned char msg_flags;
msg_t in_progress;
const int64_t maxmsgsize;
v1_decoder_t (const v1_decoder_t&);
void operator = (const v1_decoder_t&);
};
}
#endif
/*
Copyright (c) 2007-2012 iMatix Corporation
Copyright (c) 2009-2011 250bpm s.r.o.
Copyright (c) 2011 VMware, Inc.
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
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
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "v1_protocol.hpp"
#include "v1_encoder.hpp"
#include "likely.hpp"
#include "wire.hpp"
zmq::v1_encoder_t::v1_encoder_t (size_t bufsize_, i_msg_source *msg_source_) :
encoder_base_t <v1_encoder_t> (bufsize_),
msg_source (msg_source_)
{
int rc = in_progress.init ();
errno_assert (rc == 0);
// Write 0 bytes to the batch and go to message_ready state.
next_step (NULL, 0, &v1_encoder_t::message_ready, true);
}
zmq::v1_encoder_t::~v1_encoder_t ()
{
int rc = in_progress.close ();
errno_assert (rc == 0);
}
void zmq::v1_encoder_t::set_msg_source (i_msg_source *msg_source_)
{
msg_source = msg_source_;
}
bool zmq::v1_encoder_t::message_ready ()
{
// Release the content of the old message.
int rc = in_progress.close ();
errno_assert (rc == 0);
// Read new message. If there is none, return false.
// Note that new state is set only if write is successful. That way
// unsuccessful write will cause retry on the next state machine
// invocation.
if (unlikely (!msg_source)) {
rc = in_progress.init ();
errno_assert (rc == 0);
return false;
}
rc = msg_source->pull_msg (&in_progress);
if (unlikely (rc)) {
errno_assert (errno == EAGAIN);
rc = in_progress.init ();
errno_assert (rc == 0);
return false;
}
// Encode flags.
unsigned char &protocol_flags = tmpbuf [0];
protocol_flags = 0;
if (in_progress.flags () & msg_t::more)
protocol_flags |= v1_protocol_t::more_flag;
if (in_progress.size () > 255)
protocol_flags |= v1_protocol_t::large_flag;
// Encode the message length. For messages less then 256 bytes,
// the length is encoded as 8-bit unsigned integer. For larger
// messages, 64-bit unsigned integer in network byte order is used.
const size_t size = in_progress.size ();
if (unlikely (size > 255)) {
put_uint64 (tmpbuf + 1, size);
next_step (tmpbuf, 9, &v1_encoder_t::size_ready, false);
}
else {
tmpbuf [1] = static_cast <uint8_t> (size);
next_step (tmpbuf, 2, &v1_encoder_t::size_ready, false);
}
return true;
}
bool zmq::v1_encoder_t::size_ready ()
{
// Write message body into the buffer.
next_step (in_progress.data (), in_progress.size (),
&v1_encoder_t::message_ready, !(in_progress.flags () & msg_t::more));
return true;
}
/*
Copyright (c) 2009-2011 250bpm s.r.o.
Copyright (c) 2007-2012 iMatix Corporation
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
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
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ZMQ_V1_ENCODER_HPP_INCLUDED__
#define __ZMQ_V1_ENCODER_HPP_INCLUDED__
#include "msg.hpp"
#include "i_msg_source.hpp"
#include "encoder.hpp"
namespace zmq
{
class i_msg_source;
// Encoder for 0MQ framing protocol. Converts messages into data stream.
class v1_encoder_t : public encoder_base_t <v1_encoder_t>
{
public:
v1_encoder_t (size_t bufsize_, i_msg_source *msg_source_);
virtual ~v1_encoder_t ();
virtual void set_msg_source (i_msg_source *msg_source_);
private:
bool size_ready ();
bool message_ready ();
i_msg_source *msg_source;
msg_t in_progress;
unsigned char tmpbuf [9];
v1_encoder_t (const v1_encoder_t&);
const v1_encoder_t &operator = (const v1_encoder_t&);
};
}
#endif
/*
Copyright (c) 2007-2012 iMatix Corporation
Copyright (c) 2007-2012 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
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
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ZMQ_V1_PROTOCOL_HPP_INCLUDED__
#define __ZMQ_V1_PROTOCOL_HPP_INCLUDED__
namespace zmq
{
// Definition of constans for v1 transport protocol.
class v1_protocol_t
{
public:
// Message flags.
enum
{
more_flag = 1,
large_flag = 2
};
};
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment