stream_engine.hpp 4.43 KB
Newer Older
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2007-2009 iMatix Corporation
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
5 6 7 8

    This file is part of 0MQ.

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

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

22 23
#ifndef __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
#define __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
24

25 26
#include <stddef.h>

27
#include "fd.hpp"
28
#include "i_engine.hpp"
29
#include "i_msg_sink.hpp"
30
#include "io_object.hpp"
31 32
#include "i_encoder.hpp"
#include "i_decoder.hpp"
33
#include "options.hpp"
34
#include "socket_base.hpp"
35
#include "../include/zmq.h"
36 37 38 39

namespace zmq
{

40 41 42
    class io_thread_t;
    class session_base_t;

43 44 45
    //  This engine handles any socket with SOCK_STREAM semantics,
    //  e.g. TCP socket or an UNIX domain socket.

46
    class stream_engine_t : public io_object_t, public i_engine, public i_msg_sink
47 48 49
    {
    public:

50
        stream_engine_t (fd_t fd_, const options_t &options_, const std::string &endpoint);
51
        ~stream_engine_t ();
52

53
        //  i_engine interface implementation.
54 55
        void plug (zmq::io_thread_t *io_thread_,
           zmq::session_base_t *session_);
56
        void terminate ();
57 58
        void activate_in ();
        void activate_out ();
59

60 61 62
        //  i_msg_sink interface implementation.
        virtual int push_msg (msg_t *msg_);

63 64 65
        //  i_poll_events interface implementation.
        void in_event ();
        void out_event ();
66 67 68

    private:

69 70 71
        //  Unplug the engine from the session.
        void unplug ();

72 73 74
        //  Function to handle network disconnections.
        void error ();

Martin Hurton's avatar
Martin Hurton committed
75 76 77 78 79 80
        //  Receives the greeting message from the peer.
        int receive_greeting ();

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

81 82 83 84 85 86 87 88 89 90 91 92 93 94
        //  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_);

        //  Reads data from the socket (up to 'size' bytes). Returns the number
        //  of bytes actually read (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 read (void *data_, size_t size_);

        //  Underlying socket.
        fd_t s;

95 96 97
        //  Size of the greeting message:
        //  Preamble (10 bytes) + version (1 byte) + socket type (1 byte).
        const static size_t greeting_size = 12;
Martin Hurton's avatar
Martin Hurton committed
98

Martin Hurton's avatar
Martin Hurton committed
99 100 101
        //  True iff we are registered with an I/O poller.
        bool io_enabled;

102 103
        handle_t handle;

104
        unsigned char *inpos;
105
        size_t insize;
106
        i_decoder *decoder;
107

108
        unsigned char *outpos;
109
        size_t outsize;
110
        i_encoder *encoder;
111

Martin Hurton's avatar
Martin Hurton committed
112 113 114 115 116 117 118
        //  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;

        //  The receive buffer holding the greeting message
        //  that we are receiving from the peer.
119
        unsigned char greeting [greeting_size];
Martin Hurton's avatar
Martin Hurton committed
120 121 122 123 124 125 126

        //  The number of bytes of the greeting message that
        //  we have already received.
        unsigned int greeting_bytes_read;

        //  The send buffer holding the greeting message
        //  that we are sending to the peer.
127
        unsigned char greeting_output_buffer [greeting_size];
Martin Hurton's avatar
Martin Hurton committed
128

129
        //  The session this engine is attached to.
130
        zmq::session_base_t *session;
131

132 133
        options_t options;

134
        // String representation of endpoint
135
        char *endpoint;
136

137 138
        bool plugged;

139 140 141
        // Socket
        zmq::socket_base_t *socket;

142 143
        stream_engine_t (const stream_engine_t&);
        const stream_engine_t &operator = (const stream_engine_t&);
144 145 146 147 148
    };

}

#endif