2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
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
|
|
|
|
the terms of the Lesser GNU 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
|
|
|
|
Lesser GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the Lesser GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#ifndef __ZMQ_PIPE_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_PIPE_HPP_INCLUDED__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-03-11 20:33:27 +01:00
|
|
|
#include "../include/zmq.h"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
#include "stdint.hpp"
|
2010-08-31 21:03:34 +02:00
|
|
|
#include "array.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "ypipe.hpp"
|
2010-08-28 13:14:45 +02:00
|
|
|
#include "swap.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "config.hpp"
|
2009-08-27 10:54:28 +02:00
|
|
|
#include "object.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
namespace zmq
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
|
2010-08-24 15:58:48 +02:00
|
|
|
// Creates a pipe. Returns pointer to reader and writer objects.
|
|
|
|
void create_pipe (object_t *reader_parent_, object_t *writer_parent_,
|
|
|
|
uint64_t hwm_, int64_t swap_size_, class reader_t **reader_,
|
|
|
|
class writer_t **writer_);
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// The shutdown mechanism for pipe works as follows: Either endpoint
|
|
|
|
// (or even both of them) can ask pipe to terminate by calling 'terminate'
|
|
|
|
// method. Pipe then terminates in asynchronous manner. When the part of
|
|
|
|
// the shutdown tied to the endpoint is done it triggers 'terminated'
|
|
|
|
// event. When endpoint processes the event and returns, associated
|
|
|
|
// reader/writer object is deallocated.
|
|
|
|
|
|
|
|
typedef ypipe_t <zmq_msg_t, message_pipe_granularity> pipe_t;
|
|
|
|
|
|
|
|
struct i_reader_events
|
|
|
|
{
|
2010-10-16 01:22:18 +02:00
|
|
|
virtual ~i_reader_events () {}
|
2010-09-07 17:06:33 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
virtual void terminated (class reader_t *pipe_) = 0;
|
|
|
|
virtual void activated (class reader_t *pipe_) = 0;
|
2010-10-08 17:23:21 +02:00
|
|
|
virtual void delimited (class reader_t *pipe_) = 0;
|
2010-08-06 17:49:37 +02:00
|
|
|
};
|
|
|
|
|
2010-08-31 21:03:34 +02:00
|
|
|
class reader_t : public object_t, public array_item_t
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
2010-10-15 10:38:09 +02:00
|
|
|
friend void create_pipe (object_t*, object_t*, uint64_t,
|
2010-08-06 17:49:37 +02:00
|
|
|
int64_t, reader_t**, writer_t**);
|
|
|
|
friend class writer_t;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
public:
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Specifies the object to get events from the reader.
|
|
|
|
void set_event_sink (i_reader_events *endpoint_);
|
2009-08-28 16:51:46 +02:00
|
|
|
|
2009-09-30 10:08:35 +02:00
|
|
|
// Returns true if there is at least one message to read in the pipe.
|
|
|
|
bool check_read ();
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
// Reads a message to the underlying pipe.
|
2009-09-23 10:22:54 +02:00
|
|
|
bool read (zmq_msg_t *msg_);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
// Ask pipe to terminate.
|
2010-08-06 17:49:37 +02:00
|
|
|
void terminate ();
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
private:
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
reader_t (class object_t *parent_, pipe_t *pipe_, uint64_t lwm_);
|
|
|
|
~reader_t ();
|
|
|
|
|
|
|
|
// To be called only by writer itself!
|
|
|
|
void set_writer (class writer_t *writer_);
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
// Command handlers.
|
2010-08-28 10:15:03 +02:00
|
|
|
void process_activate_reader ();
|
2009-08-28 16:51:46 +02:00
|
|
|
void process_pipe_term_ack ();
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-07-14 18:31:17 +02:00
|
|
|
// Returns true if the message is delimiter; false otherwise.
|
|
|
|
static bool is_delimiter (zmq_msg_t &msg_);
|
|
|
|
|
2010-08-28 13:06:58 +02:00
|
|
|
// True, if pipe can be read from.
|
|
|
|
bool active;
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
// The underlying pipe.
|
2010-08-06 17:49:37 +02:00
|
|
|
pipe_t *pipe;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
// Pipe writer associated with the other side of the pipe.
|
2010-08-06 17:49:37 +02:00
|
|
|
class writer_t *writer;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-06-21 15:06:51 +02:00
|
|
|
// Low watermark for in-memory storage (in bytes).
|
2009-08-27 10:54:28 +02:00
|
|
|
uint64_t lwm;
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
// Number of messages read so far.
|
|
|
|
uint64_t msgs_read;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Sink for the events (either the socket of the session).
|
|
|
|
i_reader_events *sink;
|
|
|
|
|
|
|
|
// True is 'terminate' method was called or delimiter
|
|
|
|
// was read from the pipe.
|
|
|
|
bool terminating;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
reader_t (const reader_t&);
|
|
|
|
void operator = (const reader_t&);
|
|
|
|
};
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
struct i_writer_events
|
|
|
|
{
|
2010-10-16 01:22:18 +02:00
|
|
|
virtual ~i_writer_events () {}
|
2010-09-07 17:06:33 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
virtual void terminated (class writer_t *pipe_) = 0;
|
|
|
|
virtual void activated (class writer_t *pipe_) = 0;
|
|
|
|
};
|
|
|
|
|
2010-08-31 21:03:34 +02:00
|
|
|
class writer_t : public object_t, public array_item_t
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
2010-10-15 10:38:09 +02:00
|
|
|
friend void create_pipe (object_t*, object_t*, uint64_t,
|
2010-08-06 17:49:37 +02:00
|
|
|
int64_t, reader_t**, writer_t**);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
public:
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Specifies the object to get events from the writer.
|
|
|
|
void set_event_sink (i_writer_events *endpoint_);
|
2009-08-28 16:51:46 +02:00
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
// Checks whether a message can be written to the pipe.
|
2010-08-28 13:06:58 +02:00
|
|
|
// If writing the message would cause high watermark and (optionally)
|
|
|
|
// swap to be exceeded, the function returns false.
|
2010-03-01 10:13:26 +01:00
|
|
|
bool check_write ();
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
// Writes a message to the underlying pipe. Returns false if the
|
|
|
|
// message cannot be written because high watermark was reached.
|
2009-09-23 10:22:54 +02:00
|
|
|
bool write (zmq_msg_t *msg_);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-03-13 12:34:55 +01:00
|
|
|
// Remove unfinished part of a message from the pipe.
|
2010-03-09 08:43:20 +01:00
|
|
|
void rollback ();
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
// Flush the messages downsteam.
|
|
|
|
void flush ();
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
// Ask pipe to terminate.
|
2010-08-06 17:49:37 +02:00
|
|
|
void terminate ();
|
2009-08-28 16:51:46 +02:00
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
private:
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
writer_t (class object_t *parent_, pipe_t *pipe_, reader_t *reader_,
|
|
|
|
uint64_t hwm_, int64_t swap_size_);
|
|
|
|
~writer_t ();
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
// Command handlers.
|
2010-08-28 13:06:58 +02:00
|
|
|
void process_activate_writer (uint64_t msgs_read_);
|
2009-08-28 16:51:46 +02:00
|
|
|
void process_pipe_term ();
|
|
|
|
|
2010-08-28 13:06:58 +02:00
|
|
|
// Tests whether underlying pipe is already full. The swap is not
|
|
|
|
// taken into account.
|
2010-03-01 10:13:26 +01:00
|
|
|
bool pipe_full ();
|
|
|
|
|
2010-08-28 13:06:58 +02:00
|
|
|
// True, if this object can be written to. Undelying ypipe may be full
|
|
|
|
// but as long as there's swap space available, this flag is true.
|
|
|
|
bool active;
|
2010-06-21 15:06:51 +02:00
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
// The underlying pipe.
|
2010-08-06 17:49:37 +02:00
|
|
|
pipe_t *pipe;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
// Pipe reader associated with the other side of the pipe.
|
2010-08-06 17:49:37 +02:00
|
|
|
reader_t *reader;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-06-21 15:06:51 +02:00
|
|
|
// High watermark for in-memory storage (in bytes).
|
2009-08-27 10:54:28 +02:00
|
|
|
uint64_t hwm;
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
// Last confirmed number of messages read from the pipe.
|
|
|
|
// The actual number can be higher.
|
|
|
|
uint64_t msgs_read;
|
|
|
|
|
|
|
|
// Number of messages we have written so far.
|
|
|
|
uint64_t msgs_written;
|
|
|
|
|
2010-08-28 13:06:58 +02:00
|
|
|
// Pointer to the message swap. If NULL, messages are always
|
2010-06-21 15:06:51 +02:00
|
|
|
// kept in main memory.
|
2010-08-28 13:14:45 +02:00
|
|
|
swap_t *swap;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// Sink for the events (either the socket or the session).
|
|
|
|
i_writer_events *sink;
|
2010-06-21 15:06:51 +02:00
|
|
|
|
2010-08-28 13:06:58 +02:00
|
|
|
// If true, swap is active. New messages are to be written to the swap.
|
|
|
|
bool swapping;
|
|
|
|
|
|
|
|
// If true, there's a delimiter to be written to the pipe after the
|
|
|
|
// swap is empied.
|
|
|
|
bool pending_delimiter;
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
// True is 'terminate' method was called of 'pipe_term' command
|
|
|
|
// arrived from the reader.
|
|
|
|
bool terminating;
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
writer_t (const writer_t&);
|
|
|
|
void operator = (const writer_t&);
|
|
|
|
};
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|