stream_engine.hpp 5.58 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2015 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_STREAM_ENGINE_HPP_INCLUDED__
#define __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
22

23 24
#include <stddef.h>

25
#include "fd.hpp"
26
#include "i_engine.hpp"
27
#include "io_object.hpp"
28 29
#include "i_encoder.hpp"
#include "i_decoder.hpp"
30
#include "options.hpp"
31
#include "socket_base.hpp"
32
#include "../include/zmq.h"
33
#include "metadata.hpp"
34 35 36

namespace zmq
{
37 38 39 40
    //  Protocol revisions
    enum
    {
        ZMTP_1_0 = 0,
41
        ZMTP_2_0 = 1
42
    };
43

44
    class io_thread_t;
45
    class msg_t;
46
    class session_base_t;
47
    class mechanism_t;
48

49 50 51
    //  This engine handles any socket with SOCK_STREAM semantics,
    //  e.g. TCP socket or an UNIX domain socket.

52
    class stream_engine_t : public io_object_t, public i_engine
53 54 55
    {
    public:

56 57 58 59 60 61
        enum error_reason_t {
            protocol_error,
            connection_error,
            timeout_error
        };

62
        stream_engine_t (fd_t fd_, const options_t &options_,
63
                         const std::string &endpoint);
64
        ~stream_engine_t ();
65

66
        //  i_engine interface implementation.
67 68
        void plug (zmq::io_thread_t *io_thread_,
           zmq::session_base_t *session_);
69
        void terminate ();
70 71
        void restart_input ();
        void restart_output ();
72
        void zap_msg_available ();
73 74 75 76

        //  i_poll_events interface implementation.
        void in_event ();
        void out_event ();
77
        void timer_event (int id_);
78 79 80

    private:

81 82 83
        //  Unplug the engine from the session.
        void unplug ();

84
        //  Function to handle network disconnections.
85
        void error (error_reason_t reason);
86

Martin Hurton's avatar
Martin Hurton committed
87 88 89 90 91 92
        //  Receives the greeting message from the peer.
        int receive_greeting ();

        //  Detects the protocol used by the peer.
        bool handshake ();

Martin Hurton's avatar
Martin Hurton committed
93 94
        int identity_msg (msg_t *msg_);
        int process_identity_msg (msg_t *msg_);
95

96 97
        int next_handshake_command (msg_t *msg);
        int process_handshake_command (msg_t *msg);
98

99 100 101
        int pull_msg_from_session (msg_t *msg_);
        int push_msg_to_session (msg_t *msg);

102 103
        int push_raw_msg_to_session (msg_t *msg);

104
        int write_credential (msg_t *msg_);
105 106 107 108
        int pull_and_encode (msg_t *msg_);
        int decode_and_push (msg_t *msg_);
        int push_one_then_decode_and_push (msg_t *msg_);

109
        void mechanism_ready ();
110

111
        int write_subscription_msg (msg_t *msg_);
112

113 114 115
        size_t add_property (unsigned char *ptr,
            const char *name, const void *value, size_t value_len);

116 117
        void set_handshake_timer();

118 119 120
        //  Underlying socket.
        fd_t s;

121 122 123
        //  True iff this is server's engine.
        bool as_server;

124
        msg_t tx_msg;
Martin Hurton's avatar
Martin Hurton committed
125

126 127
        handle_t handle;

128
        unsigned char *inpos;
129
        size_t insize;
130
        i_decoder *decoder;
131

132
        unsigned char *outpos;
133
        size_t outsize;
134
        i_encoder *encoder;
135

136 137 138
        //  Metadata to be attached to received messages. May be NULL.
        metadata_t *metadata;

Martin Hurton's avatar
Martin Hurton committed
139 140 141 142 143
        //  When true, we are still trying to determine whether
        //  the peer is using versioned protocol, and if so, which
        //  version.  When false, normal message flow has started.
        bool handshaking;

144 145 146 147 148 149 150 151 152 153
        static const size_t signature_size = 10;

        //  Size of ZMTP/1.0 and ZMTP/2.0 greeting message
        static const size_t v2_greeting_size = 12;

        //  Size of ZMTP/3.0 greeting message
        static const size_t v3_greeting_size = 64;

        //  Expected greeting size.
        size_t greeting_size;
Martin Hurton's avatar
Martin Hurton committed
154

Pieter Hintjens's avatar
Pieter Hintjens committed
155
        //  Greeting received from, and sent to peer
156 157
        unsigned char greeting_recv [v3_greeting_size];
        unsigned char greeting_send [v3_greeting_size];
Martin Hurton's avatar
Martin Hurton committed
158

Pieter Hintjens's avatar
Pieter Hintjens committed
159 160
        //  Size of greeting received so far
        unsigned int greeting_bytes_read;
Martin Hurton's avatar
Martin Hurton committed
161

162
        //  The session this engine is attached to.
163
        zmq::session_base_t *session;
164

165 166
        options_t options;

167
        // String representation of endpoint
168
        std::string endpoint;
169

170 171
        bool plugged;

Martin Hurton's avatar
Martin Hurton committed
172
        int (stream_engine_t::*next_msg) (msg_t *msg_);
173

Martin Hurton's avatar
Martin Hurton committed
174
        int (stream_engine_t::*process_msg) (msg_t *msg_);
175

176 177
        bool io_error;

178 179
        //  Indicates whether the engine is to inject a phantom
        //  subscription message into the incoming stream.
180 181 182
        //  Needed to support old peers.
        bool subscription_required;

183 184
        mechanism_t *mechanism;

185
        //  True iff the engine couldn't consume the last decoded message.
186
        bool input_stopped;
187 188

        //  True iff the engine doesn't have any message to encode.
189
        bool output_stopped;
190

191 192 193 194 195 196
        //  ID of the handshake timer
        enum {handshake_timer_id = 0x40};

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

197 198 199
        // Socket
        zmq::socket_base_t *socket;

200 201
        std::string peer_address;

202 203
        stream_engine_t (const stream_engine_t&);
        const stream_engine_t &operator = (const stream_engine_t&);
204 205 206 207 208
    };

}

#endif