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

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
8 9 10 11 12 13
    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
14
    GNU Lesser General Public License for more details.
15

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

20 21
#ifndef __ZMQ_SESSION_BASE_HPP_INCLUDED__
#define __ZMQ_SESSION_BASE_HPP_INCLUDED__
22

23
#include <string>
24
#include <stdarg.h>
25

26
#include "own.hpp"
27
#include "io_object.hpp"
28
#include "pipe.hpp"
29
#include "socket_base.hpp"
30 31 32 33

namespace zmq
{

34 35 36 37
    class pipe_t;
    class io_thread_t;
    class socket_base_t;
    struct i_engine;
38
    struct address_t;
39

40
    class session_base_t :
41
        public own_t,
42
        public io_object_t,
43
        public i_pipe_events
44 45 46
    {
    public:

47
        //  Create a session of the particular type.
48
        static session_base_t *create (zmq::io_thread_t *io_thread_,
Martin Hurton's avatar
Martin Hurton committed
49
            bool active_, zmq::socket_base_t *socket_,
50
            const options_t &options_, const address_t *addr_);
51

52
        //  To be used once only, when creating the session.
53
        void attach_pipe (zmq::pipe_t *pipe_);
54

55
        //  Following functions are the interface exposed towards the engine.
Martin Hurton's avatar
Martin Hurton committed
56
        virtual void reset ();
57
        void flush ();
Martin Hurton's avatar
Martin Hurton committed
58
        void engine_error ();
59

60
        //  i_pipe_events interface implementation.
61 62 63
        void read_activated (zmq::pipe_t *pipe_);
        void write_activated (zmq::pipe_t *pipe_);
        void hiccuped (zmq::pipe_t *pipe_);
64
        void pipe_terminated (zmq::pipe_t *pipe_);
Martin Sustrik's avatar
Martin Sustrik committed
65

66 67 68 69
        //  Delivers a message. Returns 0 if successful; -1 otherwise.
        //  The function takes ownership of the message.
        int push_msg (msg_t *msg_);

70 71
        int zap_connect ();

72 73 74 75 76
        //  Fetches a message. Returns 0 if successful; -1 otherwise.
        //  The caller is responsible for freeing the message when no
        //  longer used.
        int pull_msg (msg_t *msg_);

77 78 79 80 81 82 83 84 85 86
        //  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_);

87
        socket_base_t *get_socket ();
88

89
    protected:
90

Martin Hurton's avatar
Martin Hurton committed
91
        session_base_t (zmq::io_thread_t *io_thread_, bool active_,
92
            zmq::socket_base_t *socket_, const options_t &options_,
93 94
            const address_t *addr_);
        virtual ~session_base_t ();
95 96

    private:
97

98
        void start_connecting (bool wait_);
99

Martin Hurton's avatar
Martin Hurton committed
100
        void reconnect ();
101

102 103
        //  Handlers for incoming commands.
        void process_plug ();
104
        void process_attach (zmq::i_engine *engine_);
105 106 107 108
        void process_term (int linger_);

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

110 111 112 113
        //  Remove any half processed messages. Flush unflushed messages.
        //  Call this function when engine disconnect to get rid of leftovers.
        void clean_pipes ();

114 115
        //  Call this function to move on with the delayed process_term.
        void proceed_with_term ();
116

117 118
        //  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
119
        bool active;
120

121
        //  Pipe connecting the session to its socket.
122
        zmq::pipe_t *pipe;
Martin Hurton's avatar
Martin Hurton committed
123

124 125 126
        //  Pipe used to exchange messages with ZAP socket.
        zmq::pipe_t *zap_pipe;

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

130 131 132 133
        //  This flag is true if the remainder of the message being processed
        //  is still in the in pipe.
        bool incomplete_in;

134 135 136
        //  True if termination have been suspended to push the pending
        //  messages to the network.
        bool pending;
137

138
        //  The protocol I/O engine connected to the session.
139
        zmq::i_engine *engine;
140

141
        //  The socket the session belongs to.
142
        zmq::socket_base_t *socket;
143 144 145

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

148 149 150 151 152 153
        //  ID of the linger timer
        enum {linger_timer_id = 0x20};

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

154
        //  Protocol and address to use when connecting.
155
        const address_t *addr;
156

157 158
        session_base_t (const session_base_t&);
        const session_base_t &operator = (const session_base_t&);
159 160 161 162 163
    };

}

#endif