2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2011-03-02 16:30:40 +01:00
|
|
|
Copyright (c) 2007-2011 iMatix Corporation
|
|
|
|
Copyright (c) 2007-2011 Other 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_PIPE_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_PIPE_HPP_INCLUDED__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
#include "msg.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "ypipe.hpp"
|
|
|
|
#include "config.hpp"
|
2009-08-27 10:54:28 +02:00
|
|
|
#include "object.hpp"
|
2011-05-22 17:26:53 +02:00
|
|
|
#include "array.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
|
|
|
{
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Create a pipepair for bi-directional transfer of messages.
|
|
|
|
// First HWM is for messages passed from first pipe to the second pipe.
|
|
|
|
// Second HWM is for messages passed from second pipe to the first pipe.
|
|
|
|
// Delay specifies whether the pipe receives all the pending messages
|
|
|
|
// before terminating or whether it terminates straight away.
|
|
|
|
int pipepair (class object_t *parents_ [2], class pipe_t* pipes_ [2],
|
|
|
|
int hwms_ [2], bool delays_ [2]);
|
2010-08-06 17:49:37 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
struct i_pipe_events
|
2010-08-06 17:49:37 +02:00
|
|
|
{
|
2011-05-22 17:26:53 +02:00
|
|
|
virtual ~i_pipe_events () {}
|
2010-09-07 17:06:33 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
virtual void read_activated (class pipe_t *pipe_) = 0;
|
|
|
|
virtual void write_activated (class pipe_t *pipe_) = 0;
|
|
|
|
virtual void terminated (class pipe_t *pipe_) = 0;
|
2010-08-06 17:49:37 +02:00
|
|
|
};
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
class pipe_t :
|
|
|
|
public object_t,
|
|
|
|
public array_item_t
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
2011-05-22 17:26:53 +02:00
|
|
|
// This allows pipepair to create pipe objects.
|
|
|
|
friend int pipepair (class object_t *parents_ [2],
|
|
|
|
class pipe_t* pipes_ [2], int hwms_ [2], bool delays_ [2]);
|
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
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Specifies the object to send events to.
|
|
|
|
void set_event_sink (i_pipe_events *sink_);
|
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.
|
2011-04-21 22:27:48 +02:00
|
|
|
bool read (msg_t *msg_);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Checks whether messages can be written to the pipe. If writing
|
|
|
|
// the message would cause high watermark the function returns false.
|
2011-04-21 22:27:48 +02:00
|
|
|
bool check_write (msg_t *msg_);
|
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.
|
2011-04-21 22:27:48 +02:00
|
|
|
bool write (msg_t *msg_);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Remove unfinished parts of the outbound 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 ();
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Ask pipe to terminate. The termination will happen asynchronously
|
|
|
|
// and user will be notified about actual deallocation by 'terminated'
|
|
|
|
// event.
|
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:
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
// Command handlers.
|
2011-05-22 17:26:53 +02:00
|
|
|
void process_activate_read ();
|
|
|
|
void process_activate_write (uint64_t msgs_read_);
|
2009-08-28 16:51:46 +02:00
|
|
|
void process_pipe_term ();
|
2011-05-22 17:26:53 +02:00
|
|
|
void process_pipe_term_ack ();
|
|
|
|
|
|
|
|
// Type of the underlying lock-free pipe.
|
|
|
|
typedef ypipe_t <msg_t, message_pipe_granularity> upipe_t;
|
2009-08-28 16:51:46 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Constructor is private. Pipe can only be created using
|
|
|
|
// pipepair function.
|
|
|
|
pipe_t (object_t *parent_, upipe_t *inpipe_, upipe_t *outpipe_,
|
|
|
|
int inhwm_, int outhwm_, bool delay_);
|
2010-03-01 10:13:26 +01:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Pipepair uses this function to let us know about
|
|
|
|
// the peer pipe object.
|
|
|
|
void set_peer (pipe_t *pipe_);
|
2010-06-21 15:06:51 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Destructor is private. Pipe objects destroy themselves.
|
|
|
|
~pipe_t ();
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Underlying pipes for both directions.
|
|
|
|
upipe_t *inpipe;
|
|
|
|
upipe_t *outpipe;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Can the pipe be read from / written to?
|
|
|
|
bool in_active;
|
|
|
|
bool out_active;
|
|
|
|
|
|
|
|
// High watermark for the outbound pipe.
|
2011-03-24 15:43:03 +01:00
|
|
|
int hwm;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Low watermark for the inbound pipe.
|
|
|
|
int lwm;
|
2010-03-01 10:13:26 +01:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Number of messages read and written so far.
|
|
|
|
uint64_t msgs_read;
|
2010-03-01 10:13:26 +01:00
|
|
|
uint64_t msgs_written;
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Last received peer's msgs_read. The actual number in the peer
|
|
|
|
// can be higher at the moment.
|
|
|
|
uint64_t peers_msgs_read;
|
2010-06-21 15:06:51 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// The pipe object on the other side of the pipepair.
|
|
|
|
pipe_t *peer;
|
|
|
|
|
|
|
|
// Sink to send events to.
|
|
|
|
i_pipe_events *sink;
|
|
|
|
|
|
|
|
// True is 'terminate' method was called or termination request
|
|
|
|
// was received from the peer.
|
2010-08-06 17:49:37 +02:00
|
|
|
bool terminating;
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// True is we've already got pipe_term command from the peer.
|
|
|
|
bool term_recvd;
|
|
|
|
|
|
|
|
// True if delimiter was already received from the peer.
|
|
|
|
bool delimited;
|
|
|
|
|
|
|
|
// If true, we receive all the pending inbound messages before
|
|
|
|
// terminating. If false, we terminate immediately when the peer
|
|
|
|
// asks us to.
|
|
|
|
bool delay;
|
|
|
|
|
|
|
|
// Returns true if the message is delimiter; false otherwise.
|
|
|
|
static bool is_delimiter (msg_t &msg_);
|
|
|
|
|
|
|
|
// Computes appropriate low watermark from the given high watermark.
|
|
|
|
static int compute_lwm (int hwm_);
|
|
|
|
|
|
|
|
// Disable copying.
|
|
|
|
pipe_t (const pipe_t&);
|
|
|
|
const pipe_t &operator = (const pipe_t&);
|
2009-08-27 10:54:28 +02:00
|
|
|
};
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|