select.hpp 4.54 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
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
Martin Sustrik's avatar
Martin Sustrik committed
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.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

Martin Sustrik's avatar
Martin Sustrik committed
30 31
#ifndef __ZMQ_SELECT_HPP_INCLUDED__
#define __ZMQ_SELECT_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
32

33 34 35 36
//  poller.hpp decides which polling mechanism to use.
#include "poller.hpp"
#if defined ZMQ_USE_SELECT

Martin Sustrik's avatar
Martin Sustrik committed
37 38
#include <stddef.h>
#include <vector>
39
#include <map>
Martin Sustrik's avatar
Martin Sustrik committed
40

41
#if defined ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
42
#elif defined ZMQ_HAVE_OPENVMS
Martin Sustrik's avatar
Martin Sustrik committed
43 44 45 46 47 48
#include <sys/types.h>
#include <sys/time.h>
#else
#include <sys/select.h>
#endif

49
#include "ctx.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
50 51
#include "fd.hpp"
#include "thread.hpp"
52
#include "poller_base.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
53

Martin Sustrik's avatar
Martin Sustrik committed
54
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
55 56
{

57 58
    struct i_poll_events;

Martin Sustrik's avatar
Martin Sustrik committed
59 60 61
    //  Implements socket polling mechanism using POSIX.1-2001 select()
    //  function.

62
    class select_t : public poller_base_t
Martin Sustrik's avatar
Martin Sustrik committed
63 64 65
    {
    public:

66 67
        typedef fd_t handle_t;

68
        select_t (const ctx_t &ctx_);
69
        ~select_t ();
Martin Sustrik's avatar
Martin Sustrik committed
70

71
        //  "poller" concept.
72
        handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
Martin Sustrik's avatar
Martin Sustrik committed
73 74 75 76 77 78 79 80
        void rm_fd (handle_t handle_);
        void set_pollin (handle_t handle_);
        void reset_pollin (handle_t handle_);
        void set_pollout (handle_t handle_);
        void reset_pollout (handle_t handle_);
        void start ();
        void stop ();

Richard Newton's avatar
Richard Newton committed
81
        static int max_fds ();
82

Martin Sustrik's avatar
Martin Sustrik committed
83 84 85 86 87 88 89 90
    private:

        //  Main worker thread routine.
        static void worker_routine (void *arg_);

        //  Main event loop.
        void loop ();

91
        //  Reference to ZMQ context.
92 93
        const ctx_t &ctx;

94 95 96 97 98 99 100 101 102 103 104 105 106 107
        //  Internal state.
        struct fds_set_t
        {
            fds_set_t ();
            fds_set_t (const fds_set_t& other_);
            fds_set_t& operator=(const fds_set_t& other_);
            //  Convinient method to descriptor from all sets.
            void remove_fd (const fd_t& fd_);

            fd_set read;
            fd_set write;
            fd_set error;
        };

Martin Sustrik's avatar
Martin Sustrik committed
108 109 110
        struct fd_entry_t
        {
            fd_t fd;
111
            zmq::i_poll_events* events;
Martin Sustrik's avatar
Martin Sustrik committed
112
        };
113
        typedef std::vector<fd_entry_t> fd_entries_t;
Martin Sustrik's avatar
Martin Sustrik committed
114

115 116 117 118
#if defined ZMQ_HAVE_WINDOWS
        struct family_entry_t
        {
            family_entry_t ();
119

120 121 122 123 124
            fd_entries_t fd_entries;
            fds_set_t fds_set;
            bool retired;
        };
        typedef std::map<u_short, family_entry_t> family_entries_t;
Martin Sustrik's avatar
Martin Sustrik committed
125

126 127 128 129
        struct wsa_events_t
        {
            wsa_events_t ();
            ~wsa_events_t ();
Martin Sustrik's avatar
Martin Sustrik committed
130

131 132 133 134
            //  read, write, error and readwrite
            WSAEVENT events [4];
        };
#endif
Martin Sustrik's avatar
Martin Sustrik committed
135

136 137 138 139 140 141 142
#if defined ZMQ_HAVE_WINDOWS
        family_entries_t family_entries;
        // See loop for details.
        family_entries_t::iterator current_family_entry_it;
#else
        fd_entries_t fd_entries;
        fds_set_t fds_set;
Martin Sustrik's avatar
Martin Sustrik committed
143 144
        fd_t maxfd;
        bool retired;
145 146 147 148 149 150 151 152
#endif

#if defined ZMQ_HAVE_WINDOWS
        //  Socket's family or AF_UNSPEC on error.
        static u_short get_fd_family (fd_t fd_);
#endif
        //  Checks if an fd_entry_t is retired.
        static bool is_retired_fd (const fd_entry_t &entry);
Martin Sustrik's avatar
Martin Sustrik committed
153 154 155 156 157 158 159 160

        //  If true, thread is shutting down.
        bool stopping;

        //  Handle of the physical thread doing the I/O work.
        thread_t worker;

        select_t (const select_t&);
161
        const select_t &operator = (const select_t&);
Martin Sustrik's avatar
Martin Sustrik committed
162 163
    };

164 165
    typedef select_t poller_t;

Martin Sustrik's avatar
Martin Sustrik committed
166 167 168 169
}

#endif

170
#endif