stream_engine.hpp 6.85 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_STREAM_ENGINE_HPP_INCLUDED__
#define __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
32

33 34
#include <stddef.h>

35
#include "fd.hpp"
36
#include "i_engine.hpp"
37
#include "io_object.hpp"
38 39
#include "i_encoder.hpp"
#include "i_decoder.hpp"
40
#include "options.hpp"
41
#include "socket_base.hpp"
42
#include "metadata.hpp"
43
#include "msg.hpp"
44 45 46

namespace zmq
{
47 48 49 50 51 52
//  Protocol revisions
enum
{
    ZMTP_1_0 = 0,
    ZMTP_2_0 = 1
};
53

54 55 56
class io_thread_t;
class session_base_t;
class mechanism_t;
57

58 59
//  This engine handles any socket with SOCK_STREAM semantics,
//  e.g. TCP socket or an UNIX domain socket.
60

61 62 63 64
class stream_engine_t : public io_object_t, public i_engine
{
  public:
    enum error_reason_t
65
    {
66 67 68 69
        protocol_error,
        connection_error,
        timeout_error
    };
70

71 72
    stream_engine_t (fd_t fd_,
                     const options_t &options_,
73
                     const std::string &endpoint_);
74
    ~stream_engine_t ();
75

76 77 78 79 80 81 82
    //  i_engine interface implementation.
    void plug (zmq::io_thread_t *io_thread_, zmq::session_base_t *session_);
    void terminate ();
    void restart_input ();
    void restart_output ();
    void zap_msg_available ();
    const char *get_endpoint () const;
83

84 85 86 87
    //  i_poll_events interface implementation.
    void in_event ();
    void out_event ();
    void timer_event (int id_);
88

89 90 91
  private:
    //  Unplug the engine from the session.
    void unplug ();
92

93
    //  Function to handle network disconnections.
94
    void error (error_reason_t reason_);
Martin Hurton's avatar
Martin Hurton committed
95

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

99 100 101 102 103 104 105 106 107 108 109 110 111
    //  Receive the greeting from the peer.
    int receive_greeting ();
    void receive_greeting_versioned ();

    typedef bool (stream_engine_t::*handshake_fun_t) ();
    static handshake_fun_t select_handshake_fun (bool unversioned,
                                                 unsigned char revision);

    bool handshake_v1_0_unversioned ();
    bool handshake_v1_0 ();
    bool handshake_v2_0 ();
    bool handshake_v3_0 ();

112 113
    int routing_id_msg (msg_t *msg_);
    int process_routing_id_msg (msg_t *msg_);
114

115 116
    int next_handshake_command (msg_t *msg_);
    int process_handshake_command (msg_t *msg_);
117

118
    int pull_msg_from_session (msg_t *msg_);
119
    int push_msg_to_session (msg_t *msg_);
120

121
    int push_raw_msg_to_session (msg_t *msg_);
122

123 124 125 126
    int write_credential (msg_t *msg_);
    int pull_and_encode (msg_t *msg_);
    int decode_and_push (msg_t *msg_);
    int push_one_then_decode_and_push (msg_t *msg_);
127

128
    void mechanism_ready ();
129

130 131 132 133
    size_t add_property (unsigned char *ptr_,
                         const char *name_,
                         const void *value_,
                         size_t value_len_);
134

135
    void set_handshake_timer ();
Thomas Rodgers's avatar
Thomas Rodgers committed
136

137
    typedef metadata_t::dict_t properties_t;
138
    bool init_properties (properties_t &properties_);
Jonathan Reams's avatar
Jonathan Reams committed
139

140
    int process_command_message (msg_t *msg_);
141 142 143
    int produce_ping_message (msg_t *msg_);
    int process_heartbeat_message (msg_t *msg_);
    int produce_pong_message (msg_t *msg_);
144

145
    //  Underlying socket.
146
    fd_t _s;
147

148
    //  True iff this is server's engine.
149
    bool _as_server;
Martin Hurton's avatar
Martin Hurton committed
150

151
    msg_t _tx_msg;
152
    //  Need to store PING payload for PONG
153
    msg_t _pong_msg;
154

155
    handle_t _handle;
156

157 158 159
    unsigned char *_inpos;
    size_t _insize;
    i_decoder *_decoder;
160

161 162 163
    unsigned char *_outpos;
    size_t _outsize;
    i_encoder *_encoder;
164

165
    //  Metadata to be attached to received messages. May be NULL.
166
    metadata_t *_metadata;
Martin Hurton's avatar
Martin Hurton committed
167

168 169 170
    //  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.
171
    bool _handshaking;
172

173
    static const size_t signature_size = 10;
174

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

178 179
    //  Size of ZMTP/3.0 greeting message
    static const size_t v3_greeting_size = 64;
Martin Hurton's avatar
Martin Hurton committed
180

181
    //  Expected greeting size.
182
    size_t _greeting_size;
Martin Hurton's avatar
Martin Hurton committed
183

184
    //  Greeting received from, and sent to peer
185 186
    unsigned char _greeting_recv[v3_greeting_size];
    unsigned char _greeting_send[v3_greeting_size];
Martin Hurton's avatar
Martin Hurton committed
187

188
    //  Size of greeting received so far
189
    unsigned int _greeting_bytes_read;
190

191
    //  The session this engine is attached to.
192
    zmq::session_base_t *_session;
193

194
    const options_t _options;
195

196
    // String representation of endpoint
197
    std::string _endpoint;
198

199
    bool _plugged;
200

201
    int (stream_engine_t::*_next_msg) (msg_t *msg_);
202

203
    int (stream_engine_t::*_process_msg) (msg_t *msg_);
204

205
    bool _io_error;
206

207 208 209
    //  Indicates whether the engine is to inject a phantom
    //  subscription message into the incoming stream.
    //  Needed to support old peers.
210
    bool _subscription_required;
211

212
    mechanism_t *_mechanism;
213

214
    //  True iff the engine couldn't consume the last decoded message.
215
    bool _input_stopped;
216

217
    //  True iff the engine doesn't have any message to encode.
218
    bool _output_stopped;
219

220 221 222 223 224
    //  ID of the handshake timer
    enum
    {
        handshake_timer_id = 0x40
    };
225

226
    //  True is linger timer is running.
227
    bool _has_handshake_timer;
Jonathan Reams's avatar
Jonathan Reams committed
228

229 230 231 232 233 234 235
    //  Heartbeat stuff
    enum
    {
        heartbeat_ivl_timer_id = 0x80,
        heartbeat_timeout_timer_id = 0x81,
        heartbeat_ttl_timer_id = 0x82
    };
236 237 238 239
    bool _has_ttl_timer;
    bool _has_timeout_timer;
    bool _has_heartbeat_timer;
    int _heartbeat_timeout;
240

241
    // Socket
242
    zmq::socket_base_t *_socket;
243

244
    std::string _peer_address;
245

246 247 248
    stream_engine_t (const stream_engine_t &);
    const stream_engine_t &operator= (const stream_engine_t &);
};
249 250 251
}

#endif