session_base.hpp 5.68 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq 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 GNU Lesser General Public
    License for more details.
25

26
    You should have received a copy of the GNU Lesser General Public License
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30 31
#ifndef __ZMQ_SESSION_BASE_HPP_INCLUDED__
#define __ZMQ_SESSION_BASE_HPP_INCLUDED__
32

33
#include <string>
34
#include <stdarg.h>
35

36
#include "own.hpp"
37
#include "io_object.hpp"
38
#include "pipe.hpp"
39
#include "socket_base.hpp"
40
#include "stream_engine.hpp"
41 42 43 44

namespace zmq
{

45 46 47 48
    class pipe_t;
    class io_thread_t;
    class socket_base_t;
    struct i_engine;
49
    struct address_t;
50

51
    class session_base_t :
52
        public own_t,
53
        public io_object_t,
54
        public i_pipe_events
55 56 57
    {
    public:

58
        //  Create a session of the particular type.
59
        static session_base_t *create (zmq::io_thread_t *io_thread_,
Martin Hurton's avatar
Martin Hurton committed
60
            bool active_, zmq::socket_base_t *socket_,
61
            const options_t &options_, address_t *addr_);
62

63
        //  To be used once only, when creating the session.
64
        void attach_pipe (zmq::pipe_t *pipe_);
65

66
        //  Following functions are the interface exposed towards the engine.
Martin Hurton's avatar
Martin Hurton committed
67
        virtual void reset ();
68
        void flush ();
69
        void engine_error (zmq::stream_engine_t::error_reason_t reason);
70

71
        //  i_pipe_events interface implementation.
72 73 74
        void read_activated (zmq::pipe_t *pipe_);
        void write_activated (zmq::pipe_t *pipe_);
        void hiccuped (zmq::pipe_t *pipe_);
75
        void pipe_terminated (zmq::pipe_t *pipe_);
Martin Sustrik's avatar
Martin Sustrik committed
76

77 78
        //  Delivers a message. Returns 0 if successful; -1 otherwise.
        //  The function takes ownership of the message.
79
        virtual int push_msg (msg_t *msg_);
80

81
        int zap_connect ();
Min RK's avatar
Min RK committed
82
        bool zap_enabled ();
83

84 85 86
        //  Fetches a message. Returns 0 if successful; -1 otherwise.
        //  The caller is responsible for freeing the message when no
        //  longer used.
87
        virtual int pull_msg (msg_t *msg_);
88

89 90 91 92 93 94 95 96 97 98
        //  Receives message from ZAP socket.
        //  Returns 0 on success; -1 otherwise.
        //  The caller is responsible for freeing the message.
        int read_zap_msg (msg_t *msg_);

        //  Sends message to ZAP socket.
        //  Returns 0 on success; -1 otherwise.
        //  The function takes ownership of the message.
        int write_zap_msg (msg_t *msg_);

99
        socket_base_t *get_socket ();
100
        const char *get_endpoint () const;
101

102
    protected:
103

Martin Hurton's avatar
Martin Hurton committed
104
        session_base_t (zmq::io_thread_t *io_thread_, bool active_,
105
            zmq::socket_base_t *socket_, const options_t &options_,
106
            address_t *addr_);
107
        virtual ~session_base_t ();
108 109

    private:
110

111
        void start_connecting (bool wait_);
112

Martin Hurton's avatar
Martin Hurton committed
113
        void reconnect ();
114

115 116
        //  Handlers for incoming commands.
        void process_plug ();
117
        void process_attach (zmq::i_engine *engine_);
118 119 120 121
        void process_term (int linger_);

        //  i_poll_events handlers.
        void timer_event (int id_);
122

123 124 125 126
        //  Remove any half processed messages. Flush unflushed messages.
        //  Call this function when engine disconnect to get rid of leftovers.
        void clean_pipes ();

127 128
        //  If true, this session (re)connects to the peer. Otherwise, it's
        //  a transient session created by the listener.
Martin Hurton's avatar
Martin Hurton committed
129
        const bool active;
130

131
        //  Pipe connecting the session to its socket.
132
        zmq::pipe_t *pipe;
Martin Hurton's avatar
Martin Hurton committed
133

134 135 136
        //  Pipe used to exchange messages with ZAP socket.
        zmq::pipe_t *zap_pipe;

137
        //  This set is added to with pipes we are disconnecting, but haven't yet completed
Martin Hurton's avatar
Martin Hurton committed
138
        std::set <pipe_t *> terminating_pipes;
139

140 141 142 143
        //  This flag is true if the remainder of the message being processed
        //  is still in the in pipe.
        bool incomplete_in;

144 145 146
        //  True if termination have been suspended to push the pending
        //  messages to the network.
        bool pending;
147

148
        //  The protocol I/O engine connected to the session.
149
        zmq::i_engine *engine;
150

151
        //  The socket the session belongs to.
152
        zmq::socket_base_t *socket;
153 154 155

        //  I/O thread the session is living in. It will be used to plug in
        //  the engines into the same thread.
156
        zmq::io_thread_t *io_thread;
157

158 159 160 161 162 163
        //  ID of the linger timer
        enum {linger_timer_id = 0x20};

        //  True is linger timer is running.
        bool has_linger_timer;

164
        //  Protocol and address to use when connecting.
165
        address_t *addr;
166

167 168
        session_base_t (const session_base_t&);
        const session_base_t &operator = (const session_base_t&);
169 170 171 172 173
    };

}

#endif