select.hpp 4.59 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 39 40
#include "platform.hpp"

#include <stddef.h>
#include <vector>
41
#include <map>
Martin Sustrik's avatar
Martin Sustrik committed
42

43 44
#if defined ZMQ_HAVE_WINDOWS
#include "windows.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
45
#elif defined ZMQ_HAVE_OPENVMS
Martin Sustrik's avatar
Martin Sustrik committed
46 47 48 49 50 51
#include <sys/types.h>
#include <sys/time.h>
#else
#include <sys/select.h>
#endif

52
#include "ctx.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
53 54
#include "fd.hpp"
#include "thread.hpp"
55
#include "poller_base.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
56

Martin Sustrik's avatar
Martin Sustrik committed
57
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
58 59
{

60 61
    struct i_poll_events;

Martin Sustrik's avatar
Martin Sustrik committed
62 63 64
    //  Implements socket polling mechanism using POSIX.1-2001 select()
    //  function.

65
    class select_t : public poller_base_t
Martin Sustrik's avatar
Martin Sustrik committed
66 67 68
    {
    public:

69 70
        typedef fd_t handle_t;

71
        select_t (const ctx_t &ctx_);
72
        ~select_t ();
Martin Sustrik's avatar
Martin Sustrik committed
73

74
        //  "poller" concept.
75
        handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
Martin Sustrik's avatar
Martin Sustrik committed
76 77 78 79 80 81 82 83
        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
84
        static int max_fds ();
85

Martin Sustrik's avatar
Martin Sustrik committed
86 87 88 89 90 91 92 93
    private:

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

        //  Main event loop.
        void loop ();

94
        //  Reference to ZMQ context.
95 96
        const ctx_t &ctx;

97 98 99 100 101 102 103 104 105 106 107 108 109 110
        //  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
111 112 113
        struct fd_entry_t
        {
            fd_t fd;
114
            zmq::i_poll_events* events;
Martin Sustrik's avatar
Martin Sustrik committed
115
        };
116
        typedef std::vector<fd_entry_t> fd_entries_t;
Martin Sustrik's avatar
Martin Sustrik committed
117

118 119 120 121
#if defined ZMQ_HAVE_WINDOWS
        struct family_entry_t
        {
            family_entry_t ();
122

123 124 125 126 127
            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
128

129 130 131 132
        struct wsa_events_t
        {
            wsa_events_t ();
            ~wsa_events_t ();
Martin Sustrik's avatar
Martin Sustrik committed
133

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

139 140 141 142 143 144 145
#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
146 147
        fd_t maxfd;
        bool retired;
148 149 150 151 152 153 154 155
#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
156 157 158 159 160 161 162 163

        //  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&);
164
        const select_t &operator = (const select_t&);
Martin Sustrik's avatar
Martin Sustrik committed
165 166
    };

167 168
    typedef select_t poller_t;

Martin Sustrik's avatar
Martin Sustrik committed
169 170 171 172
}

#endif

173
#endif