stream_engine.hpp 5.54 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_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 34 35

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

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

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

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

55 56
        stream_engine_t (fd_t fd_, const options_t &options_, 
                         const std::string &endpoint);
57
        ~stream_engine_t ();
58

59
        //  i_engine interface implementation.
60 61
        void plug (zmq::io_thread_t *io_thread_,
           zmq::session_base_t *session_);
62
        void terminate ();
63 64
        void restart_input ();
        void restart_output ();
65
        void zap_msg_available ();
66 67 68 69

        //  i_poll_events interface implementation.
        void in_event ();
        void out_event ();
70 71 72

    private:

73 74 75
        //  Unplug the engine from the session.
        void unplug ();

76 77 78
        //  Function to handle network disconnections.
        void error ();

Martin Hurton's avatar
Martin Hurton committed
79 80 81 82 83 84
        //  Receives the greeting message from the peer.
        int receive_greeting ();

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

85 86 87 88 89
        //  Writes data to the socket. Returns the number of bytes actually
        //  written (even zero is to be considered to be a success). In case
        //  of error or orderly shutdown by the other peer -1 is returned.
        int write (const void *data_, size_t size_);

90 91 92
        //  Reads data from the socket (up to 'size' bytes).
        //  Returns the number of bytes actually read or -1 on error.
        //  Zero indicates the peer has closed the connection.
93 94
        int read (void *data_, size_t size_);

95 96
        int read_identity (msg_t *msg_);
        int write_identity (msg_t *msg_);
97

98 99
        int next_handshake_command (msg_t *msg);
        int process_handshake_command (msg_t *msg);
100

101 102 103
        int pull_msg_from_session (msg_t *msg_);
        int push_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 118
        //  Underlying socket.
        fd_t s;

119 120 121
        //  True iff this is server's engine.
        bool as_server;

122
        msg_t tx_msg;
Martin Hurton's avatar
Martin Hurton committed
123

124 125
        handle_t handle;

126
        unsigned char *inpos;
127
        size_t insize;
128
        i_decoder *decoder;
129

130
        unsigned char *outpos;
131
        size_t outsize;
132
        i_encoder *encoder;
133

Martin Hurton's avatar
Martin Hurton committed
134 135 136 137 138
        //  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;

139 140 141 142 143 144 145 146 147 148
        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
149

Pieter Hintjens's avatar
Pieter Hintjens committed
150
        //  Greeting received from, and sent to peer
151 152
        unsigned char greeting_recv [v3_greeting_size];
        unsigned char greeting_send [v3_greeting_size];
Martin Hurton's avatar
Martin Hurton committed
153

Pieter Hintjens's avatar
Pieter Hintjens committed
154 155
        //  Size of greeting received so far
        unsigned int greeting_bytes_read;
Martin Hurton's avatar
Martin Hurton committed
156

157
        //  The session this engine is attached to.
158
        zmq::session_base_t *session;
159

160 161
        options_t options;

162
        // String representation of endpoint
163
        std::string endpoint;
164

165 166
        bool plugged;

167 168 169 170
        int (stream_engine_t::*read_msg) (msg_t *msg_);

        int (stream_engine_t::*write_msg) (msg_t *msg_);

171 172
        bool io_error;

173 174
        //  Indicates whether the engine is to inject a phantom
        //  subscription message into the incoming stream.
175 176 177
        //  Needed to support old peers.
        bool subscription_required;

178 179
        mechanism_t *mechanism;

180
        //  True iff the engine couldn't consume the last decoded message.
181
        bool input_stopped;
182 183

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

186 187 188
        // Socket
        zmq::socket_base_t *socket;

189 190
        std::string peer_address;

191 192
        stream_engine_t (const stream_engine_t&);
        const stream_engine_t &operator = (const stream_engine_t&);
193 194 195 196 197
    };

}

#endif