pipe.hpp 4.85 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    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/>.
*/

Martin Sustrik's avatar
Martin Sustrik committed
20 21
#ifndef __ZMQ_PIPE_HPP_INCLUDED__
#define __ZMQ_PIPE_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
22

23
#include "../include/zmq.h"
Martin Sustrik's avatar
Martin Sustrik committed
24

Martin Sustrik's avatar
Martin Sustrik committed
25 26
#include "stdint.hpp"
#include "i_endpoint.hpp"
27
#include "yarray_item.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
28
#include "ypipe.hpp"
Martin Hurton's avatar
Martin Hurton committed
29
#include "msg_store.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
30
#include "config.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
31
#include "object.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
32

Martin Sustrik's avatar
Martin Sustrik committed
33
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
34 35
{

36
    class reader_t : public object_t, public yarray_item_t
Martin Sustrik's avatar
Martin Sustrik committed
37 38 39
    {
    public:

Martin Hurton's avatar
Martin Hurton committed
40
        reader_t (class object_t *parent_, uint64_t lwm_);
Martin Sustrik's avatar
Martin Sustrik committed
41 42
        ~reader_t ();

unknown's avatar
unknown committed
43
        void set_pipe (class pipe_t *pipe_);
Martin Sustrik's avatar
Martin Sustrik committed
44 45
        void set_endpoint (i_endpoint *endpoint_);

46 47 48
        //  Returns true if there is at least one message to read in the pipe.
        bool check_read ();

Martin Sustrik's avatar
Martin Sustrik committed
49
        //  Reads a message to the underlying pipe.
50
        bool read (zmq_msg_t *msg_);
Martin Sustrik's avatar
Martin Sustrik committed
51

Martin Sustrik's avatar
Martin Sustrik committed
52 53 54
        //  Ask pipe to terminate.
        void term ();

Martin Sustrik's avatar
Martin Sustrik committed
55 56 57 58
    private:

        //  Command handlers.
        void process_revive ();
Martin Sustrik's avatar
Martin Sustrik committed
59
        void process_pipe_term_ack ();
Martin Sustrik's avatar
Martin Sustrik committed
60

61 62 63
        //  Returns true if the message is delimiter; false otherwise.
        static bool is_delimiter (zmq_msg_t &msg_);

Martin Sustrik's avatar
Martin Sustrik committed
64 65 66 67
        //  The underlying pipe.
        class pipe_t *pipe;

        //  Pipe writer associated with the other side of the pipe.
Martin Sustrik's avatar
Martin Sustrik committed
68
        class writer_t *peer;
Martin Sustrik's avatar
Martin Sustrik committed
69

Martin Hurton's avatar
Martin Hurton committed
70
        //  Low watermark for in-memory storage (in bytes).
Martin Sustrik's avatar
Martin Sustrik committed
71 72
        uint64_t lwm;

Martin Hurton's avatar
Martin Hurton committed
73 74
        //  Number of messages read so far.
        uint64_t msgs_read;
Martin Sustrik's avatar
Martin Sustrik committed
75 76 77 78 79 80 81 82

        //  Endpoint (either session or socket) the pipe is attached to.
        i_endpoint *endpoint;

        reader_t (const reader_t&);
        void operator = (const reader_t&);
    };

83
    class writer_t : public object_t, public yarray_item_t
Martin Sustrik's avatar
Martin Sustrik committed
84 85 86
    {
    public:

Martin Hurton's avatar
Martin Hurton committed
87
        writer_t (class object_t *parent_, uint64_t hwm_, int64_t swap_size_);
Martin Sustrik's avatar
Martin Sustrik committed
88 89
        ~writer_t ();

unknown's avatar
unknown committed
90
        void set_pipe (class pipe_t *pipe_);
Martin Sustrik's avatar
Martin Sustrik committed
91 92
        void set_endpoint (i_endpoint *endpoint_);

Martin Hurton's avatar
Martin Hurton committed
93 94
        //  Checks whether a message can be written to the pipe.
        //  If writing the message would cause high watermark to be
Martin Sustrik's avatar
Martin Sustrik committed
95
        //  exceeded, the function returns false.
Martin Hurton's avatar
Martin Hurton committed
96
        bool check_write ();
Martin Sustrik's avatar
Martin Sustrik committed
97 98 99

        //  Writes a message to the underlying pipe. Returns false if the
        //  message cannot be written because high watermark was reached.
100
        bool write (zmq_msg_t *msg_);
Martin Sustrik's avatar
Martin Sustrik committed
101

102
        //  Remove unfinished part of a message from the pipe.
103 104
        void rollback ();

Martin Sustrik's avatar
Martin Sustrik committed
105 106 107
        //  Flush the messages downsteam.
        void flush ();

Martin Sustrik's avatar
Martin Sustrik committed
108 109 110
        //  Ask pipe to terminate.
        void term ();

Martin Sustrik's avatar
Martin Sustrik committed
111 112
    private:

Martin Hurton's avatar
Martin Hurton committed
113 114
        void process_reader_info (uint64_t msgs_read_);

Martin Sustrik's avatar
Martin Sustrik committed
115 116 117
        //  Command handlers.
        void process_pipe_term ();

Martin Hurton's avatar
Martin Hurton committed
118 119 120
        //  Tests whether the pipe is already full.
        bool pipe_full ();

Martin Hurton's avatar
Martin Hurton committed
121 122 123 124
        //  Write special message to the pipe so that the reader
        //  can find out we are finished.
        void write_delimiter ();

Martin Sustrik's avatar
Martin Sustrik committed
125 126 127 128
        //  The underlying pipe.
        class pipe_t *pipe;

        //  Pipe reader associated with the other side of the pipe.
Martin Sustrik's avatar
Martin Sustrik committed
129
        class reader_t *peer;
Martin Sustrik's avatar
Martin Sustrik committed
130

Martin Hurton's avatar
Martin Hurton committed
131
        //  High watermark for in-memory storage (in bytes).
Martin Sustrik's avatar
Martin Sustrik committed
132 133
        uint64_t hwm;

Martin Hurton's avatar
Martin Hurton committed
134 135 136 137 138 139 140
        //  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;

Martin Hurton's avatar
Martin Hurton committed
141 142 143 144 145 146 147 148
        //  Pointer to backing store. If NULL, messages are always
        //  kept in main memory.
        msg_store_t *msg_store;

        bool extra_msg_flag;

        zmq_msg_t extra_msg;

Martin Hurton's avatar
Martin Hurton committed
149 150
        //  True iff the last attempt to write a message has failed.
        bool stalled;
Martin Sustrik's avatar
Martin Sustrik committed
151

Martin Hurton's avatar
Martin Hurton committed
152 153
        bool pending_close;

Martin Sustrik's avatar
Martin Sustrik committed
154 155 156
        //  Endpoint (either session or socket) the pipe is attached to.
        i_endpoint *endpoint;

Martin Sustrik's avatar
Martin Sustrik committed
157 158 159 160
        writer_t (const writer_t&);
        void operator = (const writer_t&);
    };

161
    //  Message pipe.
162
    class pipe_t : public ypipe_t <zmq_msg_t, message_pipe_granularity>
Martin Sustrik's avatar
Martin Sustrik committed
163
    {
Martin Sustrik's avatar
Martin Sustrik committed
164 165 166
    public:

        pipe_t (object_t *reader_parent_, object_t *writer_parent_,
Martin Hurton's avatar
Martin Hurton committed
167
            uint64_t hwm_, int64_t swap_size_);
Martin Sustrik's avatar
Martin Sustrik committed
168 169 170 171 172 173 174
        ~pipe_t ();

        reader_t reader;
        writer_t writer;

    private:

175 176
        uint64_t compute_lwm (uint64_t hwm_);

Martin Sustrik's avatar
Martin Sustrik committed
177 178
        pipe_t (const pipe_t&);
        void operator = (const pipe_t&);
179
    };
Martin Sustrik's avatar
Martin Sustrik committed
180 181 182 183

}

#endif