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"
|
|
|
|
#include "i_endpoint.hpp"
|
2009-09-21 14:39:59 +02:00
|
|
|
#include "yarray_item.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"
|
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
|
|
|
{
|
|
|
|
|
2009-09-21 14:39:59 +02:00
|
|
|
class reader_t : public object_t, public yarray_item_t
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-12-01 14:58:00 +01:00
|
|
|
reader_t (class object_t *parent_,
|
2009-08-27 10:54:28 +02:00
|
|
|
uint64_t hwm_, uint64_t lwm_);
|
|
|
|
~reader_t ();
|
|
|
|
|
2009-12-01 14:58:00 +01:00
|
|
|
void set_pipe (class pipe_t *pipe_);
|
2009-08-28 16:51:46 +02:00
|
|
|
void set_endpoint (i_endpoint *endpoint_);
|
|
|
|
|
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.
|
|
|
|
void term ();
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
// Command handlers.
|
|
|
|
void process_revive ();
|
2009-08-28 16:51:46 +02:00
|
|
|
void process_pipe_term_ack ();
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
// The underlying pipe.
|
|
|
|
class pipe_t *pipe;
|
|
|
|
|
|
|
|
// Pipe writer associated with the other side of the pipe.
|
2009-08-28 16:51:46 +02:00
|
|
|
class writer_t *peer;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
// High and low watermarks for in-memory storage (in bytes).
|
|
|
|
uint64_t hwm;
|
|
|
|
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
|
|
|
|
|
|
|
// Endpoint (either session or socket) the pipe is attached to.
|
|
|
|
i_endpoint *endpoint;
|
|
|
|
|
|
|
|
reader_t (const reader_t&);
|
|
|
|
void operator = (const reader_t&);
|
|
|
|
};
|
|
|
|
|
2009-09-21 14:39:59 +02:00
|
|
|
class writer_t : public object_t, public yarray_item_t
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-12-01 14:58:00 +01:00
|
|
|
writer_t (class object_t *parent_,
|
2009-08-27 10:54:28 +02:00
|
|
|
uint64_t hwm_, uint64_t lwm_);
|
|
|
|
~writer_t ();
|
|
|
|
|
2009-12-01 14:58:00 +01:00
|
|
|
void set_pipe (class pipe_t *pipe_);
|
2009-08-28 16:51:46 +02:00
|
|
|
void set_endpoint (i_endpoint *endpoint_);
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
// Checks whether a message can be written to the pipe.
|
|
|
|
// If writing the message would cause high watermark to be
|
2009-08-27 10:54:28 +02:00
|
|
|
// 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.
|
|
|
|
void term ();
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
private:
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
void process_reader_info (uint64_t msgs_read_);
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
// Command handlers.
|
|
|
|
void process_pipe_term ();
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
// Tests whether the pipe is already full.
|
|
|
|
bool pipe_full ();
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
// The underlying pipe.
|
|
|
|
class pipe_t *pipe;
|
|
|
|
|
|
|
|
// Pipe reader associated with the other side of the pipe.
|
2009-08-28 16:51:46 +02:00
|
|
|
class reader_t *peer;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
|
|
|
// High and low watermarks for in-memory storage (in bytes).
|
|
|
|
uint64_t hwm;
|
|
|
|
uint64_t lwm;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// True iff the last attempt to write a message has failed.
|
|
|
|
bool stalled;
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
// Endpoint (either session or socket) the pipe is attached to.
|
|
|
|
i_endpoint *endpoint;
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
writer_t (const writer_t&);
|
|
|
|
void operator = (const writer_t&);
|
|
|
|
};
|
|
|
|
|
2009-08-06 12:51:32 +02:00
|
|
|
// Message pipe.
|
2010-05-04 10:22:16 +02:00
|
|
|
class pipe_t : public ypipe_t <zmq_msg_t, message_pipe_granularity>
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-27 10:54:28 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
pipe_t (object_t *reader_parent_, object_t *writer_parent_,
|
2010-05-25 15:03:57 +02:00
|
|
|
uint64_t hwm_);
|
2009-08-27 10:54:28 +02:00
|
|
|
~pipe_t ();
|
|
|
|
|
|
|
|
reader_t reader;
|
|
|
|
writer_t writer;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2010-05-25 15:03:57 +02:00
|
|
|
uint64_t compute_lwm (uint64_t hwm_);
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
pipe_t (const pipe_t&);
|
|
|
|
void operator = (const pipe_t&);
|
2009-08-06 12:51:32 +02:00
|
|
|
};
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|