session.hpp 3.68 KB
Newer Older
1
/*
2 3
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
4 5 6 7

    This file is part of 0MQ.

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

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

#ifndef __ZMQ_SESSION_HPP_INCLUDED__
#define __ZMQ_SESSION_HPP_INCLUDED__

24 25
#include <string>

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

namespace zmq
{

34
    class session_t :
35
        public own_t,
36
        public io_object_t,
37
        public i_pipe_events
38 39 40
    {
    public:

41 42 43
        session_t (class io_thread_t *io_thread_, bool connect_,
            class socket_base_t *socket_, const options_t &options_,
            const char *protocol_, const char *address_);
44

45 46 47
        //  To be used once only, when creating the session.
        void attach_pipe (class pipe_t *pipe_);

48
        //  Following functions are the interface exposed towards the engine.
49 50
        bool read (msg_t *msg_);
        bool write (msg_t *msg_);
51
        void flush ();
52
        void detach ();
53

54 55 56
        //  i_pipe_events interface implementation.
        void read_activated (class pipe_t *pipe_);
        void write_activated (class pipe_t *pipe_);
57
        void hiccuped (class pipe_t *pipe_);
58
        void terminated (class pipe_t *pipe_);
Martin Sustrik's avatar
Martin Sustrik committed
59

60
    private:
61 62 63

        ~session_t ();

64
        void start_connecting (bool wait_);
65

66 67
        void detached ();

68 69
        //  Handlers for incoming commands.
        void process_plug ();
70
        void process_attach (struct i_engine *engine_);
71 72 73 74
        void process_term (int linger_);

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

76 77 78 79
        //  Remove any half processed messages. Flush unflushed messages.
        //  Call this function when engine disconnect to get rid of leftovers.
        void clean_pipes ();

80 81
        //  Call this function to move on with the delayed process_term.
        void proceed_with_term ();
82

83 84 85 86
        //  If true, this session (re)connects to the peer. Otherwise, it's
        //  a transient session created by the listener.
        bool connect;

87 88
        //  Pipe connecting the session to its socket.
        class pipe_t *pipe;
Martin Sustrik's avatar
Martin Sustrik committed
89

90 91 92 93
        //  This flag is true if the remainder of the message being processed
        //  is still in the in pipe.
        bool incomplete_in;

94 95 96
        //  True if termination have been suspended to push the pending
        //  messages to the network.
        bool pending;
97

98
        //  The protocol I/O engine connected to the session.
99
        struct i_engine *engine;
100

101 102 103 104 105 106 107
        //  The socket the session belongs to.
        class socket_base_t *socket;

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

108 109 110 111 112 113
        //  ID of the linger timer
        enum {linger_timer_id = 0x20};

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

114 115 116 117
        //  Protocol and address to use when connecting.
        std::string protocol;
        std::string address;

118
        session_t (const session_t&);
119
        const session_t &operator = (const session_t&);
120 121 122 123 124
    };

}

#endif