2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2013-03-12 13:17:00 +01:00
|
|
|
Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
2010-10-30 15:08:28 +02:00
|
|
|
the terms of the GNU Lesser General Public License as published by
|
2009-07-29 12:07:54 +02:00
|
|
|
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
|
2010-10-30 15:08:28 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-07-29 12:07:54 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#ifndef __ZMQ_ENCODER_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_ENCODER_HPP_INCLUDED__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2012-09-26 14:09:55 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
2009-12-11 22:29:04 +01:00
|
|
|
#include <stdlib.h>
|
2009-07-29 12:07:54 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
#include "err.hpp"
|
2011-04-21 22:27:48 +02:00
|
|
|
#include "msg.hpp"
|
2012-09-04 19:44:20 +02:00
|
|
|
#include "i_encoder.hpp"
|
2010-08-31 07:01:40 +02:00
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
namespace zmq
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
|
2012-09-02 18:03:38 +02:00
|
|
|
class i_msg_source;
|
2011-11-09 15:22:20 +01:00
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
// Helper base class for encoders. It implements the state machine that
|
|
|
|
// fills the outgoing buffer. Derived classes should implement individual
|
|
|
|
// state machine actions.
|
|
|
|
|
2012-09-04 19:44:20 +02:00
|
|
|
template <typename T> class encoder_base_t : public i_encoder
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-08-31 07:01:40 +02:00
|
|
|
inline encoder_base_t (size_t bufsize_) :
|
2009-12-11 22:29:04 +01:00
|
|
|
bufsize (bufsize_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-12-11 22:29:04 +01:00
|
|
|
buf = (unsigned char*) malloc (bufsize_);
|
2011-02-22 16:23:36 +01:00
|
|
|
alloc_assert (buf);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2010-09-20 00:06:05 +02:00
|
|
|
// The destructor doesn't have to be virtual. It is made virtual
|
2010-06-10 07:21:05 +02:00
|
|
|
// just to keep ICC and code checking tools from complaining.
|
2010-08-31 07:01:40 +02:00
|
|
|
inline virtual ~encoder_base_t ()
|
2009-12-11 22:29:04 +01:00
|
|
|
{
|
|
|
|
free (buf);
|
|
|
|
}
|
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
// 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.
|
|
|
|
// 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.
|
|
|
|
inline void get_data (unsigned char **data_, size_t *size_,
|
2009-07-29 12:07:54 +02:00
|
|
|
int *offset_ = NULL)
|
|
|
|
{
|
2009-12-13 09:11:08 +01:00
|
|
|
unsigned char *buffer = !*data_ ? buf : *data_;
|
|
|
|
size_t buffersize = !*data_ ? bufsize : *size_;
|
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
if (offset_)
|
|
|
|
*offset_ = -1;
|
|
|
|
|
2012-05-08 13:30:13 +02:00
|
|
|
size_t pos = 0;
|
|
|
|
while (pos < buffersize) {
|
2009-12-11 22:29:04 +01:00
|
|
|
|
|
|
|
// 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) {
|
2012-05-08 09:28:17 +02:00
|
|
|
// If we are to encode the beginning of a new message,
|
|
|
|
// adjust the message offset.
|
|
|
|
if (beginning)
|
|
|
|
if (offset_ && *offset_ == -1)
|
|
|
|
*offset_ = static_cast <int> (pos);
|
|
|
|
|
2012-05-08 13:30:13 +02:00
|
|
|
if (!(static_cast <T*> (this)->*next) ())
|
|
|
|
break;
|
2009-12-11 22:29:04 +01:00
|
|
|
}
|
2009-12-08 15:41:50 +01:00
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
// 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.
|
2009-12-10 08:25:21 +01:00
|
|
|
// As a consequence, large messages being sent won't block
|
|
|
|
// other engines running in the same I/O thread for excessive
|
|
|
|
// amounts of time.
|
2009-12-13 09:11:08 +01:00
|
|
|
if (!pos && !*data_ && to_write >= buffersize) {
|
2009-12-08 15:41:50 +01:00
|
|
|
*data_ = write_pos;
|
2009-12-11 22:29:04 +01:00
|
|
|
*size_ = to_write;
|
|
|
|
write_pos = NULL;
|
2009-12-10 08:25:21 +01:00
|
|
|
to_write = 0;
|
2009-12-11 22:29:04 +01:00
|
|
|
return;
|
2009-12-08 15:41:50 +01:00
|
|
|
}
|
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
// Copy data to the buffer. If the buffer is full, return.
|
2009-12-13 09:11:08 +01:00
|
|
|
size_t to_copy = std::min (to_write, buffersize - pos);
|
|
|
|
memcpy (buffer + pos, write_pos, to_copy);
|
2009-12-11 22:29:04 +01:00
|
|
|
pos += to_copy;
|
|
|
|
write_pos += to_copy;
|
|
|
|
to_write -= to_copy;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
2012-05-08 13:30:13 +02:00
|
|
|
|
|
|
|
*data_ = buffer;
|
|
|
|
*size_ = pos;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
2009-12-11 22:29:04 +01:00
|
|
|
|
2013-02-01 09:32:28 +01:00
|
|
|
inline bool has_data ()
|
|
|
|
{
|
|
|
|
return to_write > 0;
|
|
|
|
}
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
protected:
|
|
|
|
|
|
|
|
// Prototype of state machine action.
|
|
|
|
typedef bool (T::*step_t) ();
|
|
|
|
|
|
|
|
// This function should be called from derived class to write the data
|
|
|
|
// to the buffer and schedule next state machine action. Set beginning
|
|
|
|
// to true when you are writing first byte of a message.
|
|
|
|
inline void next_step (void *write_pos_, size_t to_write_,
|
|
|
|
step_t next_, bool beginning_)
|
|
|
|
{
|
|
|
|
write_pos = (unsigned char*) write_pos_;
|
|
|
|
to_write = to_write_;
|
|
|
|
next = next_;
|
|
|
|
beginning = beginning_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-09-16 09:29:43 +02:00
|
|
|
// Where to get the data to write from.
|
2009-07-29 12:07:54 +02:00
|
|
|
unsigned char *write_pos;
|
2011-09-16 09:29:43 +02:00
|
|
|
|
|
|
|
// How much data to write before next step should be executed.
|
2009-07-29 12:07:54 +02:00
|
|
|
size_t to_write;
|
2011-09-16 09:29:43 +02:00
|
|
|
|
|
|
|
// Next step. If set to NULL, it means that associated data stream
|
|
|
|
// is dead.
|
2009-07-29 12:07:54 +02:00
|
|
|
step_t next;
|
2011-09-16 09:29:43 +02:00
|
|
|
|
|
|
|
// If true, first byte of the message is being written.
|
2009-07-29 12:07:54 +02:00
|
|
|
bool beginning;
|
|
|
|
|
2011-09-16 09:29:43 +02:00
|
|
|
// The buffer for encoded data.
|
2009-12-11 22:29:04 +01:00
|
|
|
size_t bufsize;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2010-08-31 07:01:40 +02:00
|
|
|
encoder_base_t (const encoder_base_t&);
|
|
|
|
void operator = (const encoder_base_t&);
|
|
|
|
};
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2010-08-31 07:01:40 +02:00
|
|
|
|