pgm_receiver.hpp 3.42 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 24 25
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __ZMQ_PGM_RECEIVER_HPP_INCLUDED__
#define __ZMQ_PGM_RECEIVER_HPP_INCLUDED__

#include "platform.hpp"

malosek's avatar
malosek committed
26
#if defined ZMQ_HAVE_OPENPGM
27

28 29 30 31 32
#ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#endif

#include <map>
Martin Sustrik's avatar
Martin Sustrik committed
33
#include <algorithm>
34

35 36 37
#include "io_object.hpp"
#include "i_engine.hpp"
#include "options.hpp"
38
#include "decoder.hpp"
39 40 41 42 43 44 45 46 47 48
#include "pgm_socket.hpp"

namespace zmq
{

    class pgm_receiver_t : public io_object_t, public i_engine
    {
    
    public:

49
        pgm_receiver_t (class io_thread_t *parent_, const options_t &options_);
50 51
        ~pgm_receiver_t ();

52
        int init (bool udp_encapsulation_, const char *network_);
53 54

        //  i_engine interface implementation.
55
        void plug (class io_thread_t *io_thread_, struct i_inout *inout_);
56
        void unplug ();
57 58 59
        void terminate ();
        void activate_in ();
        void activate_out ();
60 61 62

        //  i_poll_events interface implementation.
        void in_event ();
63
        void timer_event (int token);
64 65

    private:
malosek's avatar
malosek committed
66

67 68 69 70 71 72
        //  RX timeout timer ID.
        enum {rx_timer_id = 0xa1};

        //  RX timer is running.
        bool has_rx_timer;

Martin Sustrik's avatar
Martin Sustrik committed
73 74 75
        //  If joined is true we are already getting messages from the peer.
        //  It it's false, we are getting data but still we haven't seen
        //  beginning of a message.
76 77
        struct peer_info_t
        {
malosek's avatar
malosek committed
78
            bool joined;
79
            decoder_t *decoder;
malosek's avatar
malosek committed
80 81
        };

82 83
        struct tsi_comp
        {
84
            bool operator () (const pgm_tsi_t &ltsi,
85
                const pgm_tsi_t &rtsi) const
malosek's avatar
malosek committed
86
            {
87 88 89 90
                uint32_t ll[2], rl[2];
                memcpy (ll, &ltsi, sizeof (ll));
                memcpy (rl, &rtsi, sizeof (rl));
                return (ll[0] < rl[0]) || (ll[0] == rl[0] && ll[1] < rl[1]);
malosek's avatar
malosek committed
91 92 93
            }
        };

Martin Sustrik's avatar
Martin Sustrik committed
94 95
        typedef std::map <pgm_tsi_t, peer_info_t, tsi_comp> peers_t;
        peers_t peers;
malosek's avatar
malosek committed
96

97 98 99 100 101 102 103 104 105
        //  PGM socket.
        pgm_socket_t pgm_socket;

        //  Socket options.
        options_t options;

        //  Parent session.
        i_inout *inout;

106
        //  Most recently used decoder.
107
        decoder_t *mru_decoder;
108 109 110 111 112 113 114

        //  Number of bytes not consumed by the decoder due to pipe overflow.
        size_t pending_bytes;

        //  Pointer to data still waiting to be processed by the decoder.
        unsigned char *pending_ptr;

115 116 117 118 119 120 121
        //  Poll handle associated with PGM socket.
        handle_t socket_handle;

        //  Poll handle associated with engine PGM waiting pipe.
        handle_t pipe_handle;

        pgm_receiver_t (const pgm_receiver_t&);
122
        const pgm_receiver_t &operator = (const pgm_receiver_t&);
123 124 125 126 127 128 129
    };

}

#endif

#endif