2015-10-18 21:07:23 +03:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2015-10-18 21:07:23 +03:00
|
|
|
|
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
|
|
|
|
|
|
|
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
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ZMQ_SOCKET_POLLER_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_SOCKET_POLLER_HPP_INCLUDED__
|
|
|
|
|
2015-10-22 11:12:04 +03:00
|
|
|
#include "poller.hpp"
|
|
|
|
|
2016-06-11 19:14:25 +02:00
|
|
|
#if defined ZMQ_POLL_BASED_ON_POLL && !defined ZMQ_HAVE_WINDOWS
|
2015-10-22 11:12:04 +03:00
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
#include "windows.hpp"
|
2018-03-10 03:03:02 -08:00
|
|
|
#elif defined ZMQ_HAVE_VXWORKS
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <strings.h>
|
2015-10-22 11:12:04 +03:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2015-10-18 21:07:23 +03:00
|
|
|
#include <vector>
|
|
|
|
|
2015-10-22 11:12:04 +03:00
|
|
|
#include "socket_base.hpp"
|
|
|
|
#include "signaler.hpp"
|
2018-06-01 16:58:53 +02:00
|
|
|
#include "polling_util.hpp"
|
2015-10-18 21:07:23 +03:00
|
|
|
|
|
|
|
namespace zmq
|
|
|
|
{
|
|
|
|
class socket_poller_t
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
|
|
|
public:
|
2015-10-18 21:07:23 +03:00
|
|
|
socket_poller_t ();
|
|
|
|
~socket_poller_t ();
|
|
|
|
|
2020-05-01 22:12:30 +00:00
|
|
|
typedef zmq_poller_event_t event_t;
|
2015-10-18 21:07:23 +03:00
|
|
|
|
2018-05-24 17:58:30 +02:00
|
|
|
int add (socket_base_t *socket_, void *user_data_, short events_);
|
2019-12-25 13:51:21 +01:00
|
|
|
int modify (const socket_base_t *socket_, short events_);
|
2018-05-24 17:58:30 +02:00
|
|
|
int remove (socket_base_t *socket_);
|
2015-10-22 11:12:04 +03:00
|
|
|
|
2018-05-24 17:58:30 +02:00
|
|
|
int add_fd (fd_t fd_, void *user_data_, short events_);
|
|
|
|
int modify_fd (fd_t fd_, short events_);
|
|
|
|
int remove_fd (fd_t fd_);
|
2019-04-27 20:36:24 -04:00
|
|
|
// Returns the signaler's fd if there is one, otherwise errors.
|
2019-12-25 15:51:26 +01:00
|
|
|
int signaler_fd (fd_t *fd_) const;
|
2015-10-18 21:07:23 +03:00
|
|
|
|
2019-12-08 14:31:29 +01:00
|
|
|
int wait (event_t *events_, int n_events_, long timeout_);
|
2015-10-18 21:07:23 +03:00
|
|
|
|
2020-02-04 11:57:58 +01:00
|
|
|
int size () const { return static_cast<int> (_items.size ()); };
|
2016-09-16 16:58:03 +02:00
|
|
|
|
2015-10-18 21:07:23 +03:00
|
|
|
// Return false if object is not a socket.
|
2019-12-25 15:51:26 +01:00
|
|
|
bool check_tag () const;
|
2015-10-18 21:07:23 +03:00
|
|
|
|
|
|
|
private:
|
2020-04-13 20:30:45 +00:00
|
|
|
typedef struct item_t
|
|
|
|
{
|
|
|
|
socket_base_t *socket;
|
|
|
|
fd_t fd;
|
|
|
|
void *user_data;
|
|
|
|
short events;
|
|
|
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
|
|
|
int pollfd_index;
|
|
|
|
#endif
|
|
|
|
} item_t;
|
|
|
|
|
2019-12-25 15:51:26 +01:00
|
|
|
static void zero_trail_events (zmq::socket_poller_t::event_t *events_,
|
|
|
|
int n_events_,
|
|
|
|
int found_);
|
2017-09-07 00:11:22 +02:00
|
|
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
|
|
|
int check_events (zmq::socket_poller_t::event_t *events_, int n_events_);
|
|
|
|
#elif defined ZMQ_POLL_BASED_ON_SELECT
|
|
|
|
int check_events (zmq::socket_poller_t::event_t *events_,
|
|
|
|
int n_events_,
|
2018-05-24 17:58:30 +02:00
|
|
|
fd_set &inset_,
|
|
|
|
fd_set &outset_,
|
|
|
|
fd_set &errset_);
|
2017-09-07 00:11:22 +02:00
|
|
|
#endif
|
2019-12-25 15:51:26 +01:00
|
|
|
static int adjust_timeout (zmq::clock_t &clock_,
|
|
|
|
long timeout_,
|
|
|
|
uint64_t &now_,
|
|
|
|
uint64_t &end_,
|
|
|
|
bool &first_pass_);
|
2020-04-13 20:30:45 +00:00
|
|
|
static bool is_socket (const item_t &item, const socket_base_t *socket_)
|
|
|
|
{
|
|
|
|
return item.socket == socket_;
|
|
|
|
}
|
|
|
|
static bool is_fd (const item_t &item, fd_t fd_)
|
|
|
|
{
|
|
|
|
return !item.socket && item.fd == fd_;
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:35:30 +01:00
|
|
|
int rebuild ();
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2015-10-18 21:07:23 +03:00
|
|
|
// Used to check whether the object is a socket_poller.
|
2018-05-27 11:10:39 +02:00
|
|
|
uint32_t _tag;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2015-10-22 11:12:04 +03:00
|
|
|
// Signaler used for thread safe sockets polling
|
2018-05-27 11:10:39 +02:00
|
|
|
signaler_t *_signaler;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2015-10-22 11:12:04 +03:00
|
|
|
// List of sockets
|
|
|
|
typedef std::vector<item_t> items_t;
|
2018-05-27 11:10:39 +02:00
|
|
|
items_t _items;
|
2015-10-18 21:07:23 +03:00
|
|
|
|
2015-10-22 11:12:04 +03:00
|
|
|
// Does the pollset needs rebuilding?
|
2018-05-27 11:10:39 +02:00
|
|
|
bool _need_rebuild;
|
2015-10-18 21:07:23 +03:00
|
|
|
|
2015-10-22 11:12:04 +03:00
|
|
|
// Should the signaler be used for the thread safe polling?
|
2018-05-27 11:10:39 +02:00
|
|
|
bool _use_signaler;
|
2016-04-25 12:18:46 +01:00
|
|
|
|
2015-10-18 21:07:23 +03:00
|
|
|
// Size of the pollset
|
2018-05-27 11:10:39 +02:00
|
|
|
int _pollset_size;
|
2016-04-25 12:18:46 +01:00
|
|
|
|
2015-10-22 11:12:04 +03:00
|
|
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
2018-05-27 11:10:39 +02:00
|
|
|
pollfd *_pollfds;
|
2015-10-22 11:12:04 +03:00
|
|
|
#elif defined ZMQ_POLL_BASED_ON_SELECT
|
2018-06-01 16:58:53 +02:00
|
|
|
resizable_optimized_fd_set_t _pollset_in;
|
|
|
|
resizable_optimized_fd_set_t _pollset_out;
|
|
|
|
resizable_optimized_fd_set_t _pollset_err;
|
2018-05-27 11:10:39 +02:00
|
|
|
zmq::fd_t _max_fd;
|
2015-10-22 11:12:04 +03:00
|
|
|
#endif
|
2016-04-25 12:18:46 +01:00
|
|
|
|
2019-12-08 19:22:04 +01:00
|
|
|
ZMQ_NON_COPYABLE_NOR_MOVABLE (socket_poller_t)
|
2015-10-18 21:07:23 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|