Problem: extra abstraction layer type poller_t is in zmq.hpp

Solution: move to zmq_addon.hpp, rename to active_poller_t, and rename base_poller_t to poller_t
This commit is contained in:
Simon Giesecke 2018-05-11 10:56:41 +02:00
parent dc996cd2ec
commit 882f5e844c
4 changed files with 218 additions and 216 deletions

View File

@ -25,7 +25,7 @@ add_executable(
message.cpp
context.cpp
socket.cpp
poller.cpp
active_poller.cpp
multipart.cpp
)

View File

@ -1,36 +1,36 @@
#include <gtest/gtest.h>
#include <zmq.hpp>
#include <zmq_addon.hpp>
#if defined(ZMQ_CPP11) && defined(ZMQ_BUILD_DRAFT_API)
#include <array>
#include <memory>
TEST(poller, create_destroy)
TEST(active_poller, create_destroy)
{
zmq::poller_t poller;
ASSERT_TRUE(poller.empty ());
zmq::active_poller_t active_poller;
ASSERT_TRUE(active_poller.empty ());
}
static_assert(!std::is_copy_constructible<zmq::poller_t>::value, "poller_t should not be copy-constructible");
static_assert(!std::is_copy_assignable<zmq::poller_t>::value, "poller_t should not be copy-assignable");
static_assert(!std::is_copy_constructible<zmq::active_poller_t>::value, "active_active_poller_t should not be copy-constructible");
static_assert(!std::is_copy_assignable<zmq::active_poller_t>::value, "active_active_poller_t should not be copy-assignable");
TEST(poller, move_construct_empty)
TEST(active_poller, move_construct_empty)
{
std::unique_ptr<zmq::poller_t> a {new zmq::poller_t};
std::unique_ptr<zmq::active_poller_t> a {new zmq::active_poller_t};
ASSERT_TRUE(a->empty ());
zmq::poller_t b = std::move (*a);
zmq::active_poller_t b = std::move (*a);
ASSERT_TRUE(b.empty ());
ASSERT_EQ(0u, a->size ());
ASSERT_EQ(0u, b.size ());
a.reset ();
}
TEST(poller, move_assign_empty)
TEST(active_poller, move_assign_empty)
{
std::unique_ptr<zmq::poller_t> a{new zmq::poller_t};
std::unique_ptr<zmq::active_poller_t> a{new zmq::active_poller_t};
ASSERT_TRUE(a->empty());
zmq::poller_t b;
zmq::active_poller_t b;
ASSERT_TRUE(b.empty());
b = std::move(*a);
ASSERT_EQ(0u, a->size ());
@ -40,16 +40,16 @@ TEST(poller, move_assign_empty)
a.reset ();
}
TEST(poller, move_construct_non_empty)
TEST(active_poller, move_construct_non_empty)
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
std::unique_ptr<zmq::poller_t> a{new zmq::poller_t};
std::unique_ptr<zmq::active_poller_t> a{new zmq::active_poller_t};
a->add(socket, ZMQ_POLLIN, [](short) {});
ASSERT_FALSE(a->empty ());
ASSERT_EQ(1u, a->size ());
zmq::poller_t b = std::move (*a);
zmq::active_poller_t b = std::move (*a);
ASSERT_TRUE(a->empty ());
ASSERT_EQ(0u, a->size ());
ASSERT_FALSE(b.empty ());
@ -57,16 +57,16 @@ TEST(poller, move_construct_non_empty)
a.reset ();
}
TEST(poller, move_assign_non_empty)
TEST(active_poller, move_assign_non_empty)
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
std::unique_ptr<zmq::poller_t> a{new zmq::poller_t};
std::unique_ptr<zmq::active_poller_t> a{new zmq::active_poller_t};
a->add(socket, ZMQ_POLLIN, [](short) {});
ASSERT_FALSE(a->empty());
ASSERT_EQ(1u, a->size ());
zmq::poller_t b;
zmq::active_poller_t b;
b = std::move(*a);
ASSERT_TRUE(a->empty ());
ASSERT_EQ(0u, a->size ());
@ -75,70 +75,70 @@ TEST(poller, move_assign_non_empty)
a.reset ();
}
TEST(poller, add_handler)
TEST(active_poller, add_handler)
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t poller;
zmq::poller_t::handler_t handler;
ASSERT_NO_THROW(poller.add(socket, ZMQ_POLLIN, handler));
zmq::active_poller_t active_poller;
zmq::active_poller_t::handler_t handler;
ASSERT_NO_THROW(active_poller.add(socket, ZMQ_POLLIN, handler));
}
TEST(poller, add_handler_invalid_events_type)
TEST(active_poller, add_handler_invalid_events_type)
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t poller;
zmq::poller_t::handler_t handler;
zmq::active_poller_t active_poller;
zmq::active_poller_t::handler_t handler;
short invalid_events_type = 2 << 10;
ASSERT_NO_THROW(poller.add(socket, invalid_events_type, handler));
ASSERT_FALSE(poller.empty ());
ASSERT_EQ(1u, poller.size ());
ASSERT_NO_THROW(active_poller.add(socket, invalid_events_type, handler));
ASSERT_FALSE(active_poller.empty ());
ASSERT_EQ(1u, active_poller.size ());
}
TEST(poller, add_handler_twice_throws)
TEST(active_poller, add_handler_twice_throws)
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t poller;
zmq::poller_t::handler_t handler;
poller.add(socket, ZMQ_POLLIN, handler);
zmq::active_poller_t active_poller;
zmq::active_poller_t::handler_t handler;
active_poller.add(socket, ZMQ_POLLIN, handler);
/// \todo the actual error code should be checked
ASSERT_THROW(poller.add(socket, ZMQ_POLLIN, handler), zmq::error_t);
ASSERT_THROW(active_poller.add(socket, ZMQ_POLLIN, handler), zmq::error_t);
}
TEST(poller, wait_with_no_handlers_throws)
TEST(active_poller, wait_with_no_handlers_throws)
{
zmq::poller_t poller;
zmq::active_poller_t active_poller;
/// \todo the actual error code should be checked
ASSERT_THROW(poller.wait(std::chrono::milliseconds{10}), zmq::error_t);
ASSERT_THROW(active_poller.wait(std::chrono::milliseconds{10}), zmq::error_t);
}
TEST(poller, remove_unregistered_throws)
TEST(active_poller, remove_unregistered_throws)
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t poller;
zmq::active_poller_t active_poller;
/// \todo the actual error code should be checked
ASSERT_THROW(poller.remove(socket), zmq::error_t);
ASSERT_THROW(active_poller.remove(socket), zmq::error_t);
}
TEST(poller, remove_registered_empty)
TEST(active_poller, remove_registered_empty)
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t poller;
poller.add(socket, ZMQ_POLLIN, zmq::poller_t::handler_t{});
ASSERT_NO_THROW(poller.remove(socket));
zmq::active_poller_t active_poller;
active_poller.add(socket, ZMQ_POLLIN, zmq::active_poller_t::handler_t{});
ASSERT_NO_THROW(active_poller.remove(socket));
}
TEST(poller, remove_registered_non_empty)
TEST(active_poller, remove_registered_non_empty)
{
zmq::context_t context;
zmq::socket_t socket{context, zmq::socket_type::router};
zmq::poller_t poller;
poller.add(socket, ZMQ_POLLIN, [](short) {});
ASSERT_NO_THROW(poller.remove(socket));
zmq::active_poller_t active_poller;
active_poller.add(socket, ZMQ_POLLIN, [](short) {});
ASSERT_NO_THROW(active_poller.remove(socket));
}
namespace {
@ -178,7 +178,7 @@ struct server_client_setup
ASSERT_NO_THROW (client.connect (endpoint));
}
zmq::poller_t::handler_t handler = [&](short e) {
zmq::active_poller_t::handler_t handler = [&](short e) {
events = e;
};
@ -191,35 +191,35 @@ struct server_client_setup
} //namespace
TEST(poller, poll_basic)
TEST(active_poller, poll_basic)
{
server_client_setup s;
ASSERT_NO_THROW(s.client.send("Hi"));
zmq::poller_t poller;
zmq::active_poller_t active_poller;
bool message_received = false;
zmq::poller_t::handler_t handler = [&message_received](short events) {
zmq::active_poller_t::handler_t handler = [&message_received](short events) {
ASSERT_TRUE(0 != (events & ZMQ_POLLIN));
message_received = true;
};
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, handler));
ASSERT_EQ(1, poller.wait(std::chrono::milliseconds{-1}));
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN, handler));
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{-1}));
ASSERT_TRUE(message_received);
}
/// \todo this contains multiple test cases that should be split up
TEST(poller, client_server)
TEST(active_poller, client_server)
{
const std::string send_msg = "Hi";
// Setup server and client
server_client_setup s;
// Setup poller
zmq::poller_t poller;
// Setup active_poller
zmq::active_poller_t active_poller;
short events;
zmq::poller_t::handler_t handler = [&](short e) {
zmq::active_poller_t::handler_t handler = [&](short e) {
if (0 != (e & ZMQ_POLLIN)) {
zmq::message_t zmq_msg;
ASSERT_NO_THROW(s.server.recv(&zmq_msg)); // get message
@ -232,123 +232,123 @@ TEST(poller, client_server)
events = e;
};
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, handler));
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN, handler));
// client sends message
ASSERT_NO_THROW(s.client.send(send_msg));
ASSERT_EQ(1, poller.wait(std::chrono::milliseconds{-1}));
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{-1}));
ASSERT_EQ(events, ZMQ_POLLIN);
// Re-add server socket with pollout flag
ASSERT_NO_THROW(poller.remove(s.server));
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN | ZMQ_POLLOUT, handler));
ASSERT_EQ(1, poller.wait(std::chrono::milliseconds{-1}));
ASSERT_NO_THROW(active_poller.remove(s.server));
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN | ZMQ_POLLOUT, handler));
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{-1}));
ASSERT_EQ(events, ZMQ_POLLOUT);
}
TEST(poller, add_invalid_socket_throws)
TEST(active_poller, add_invalid_socket_throws)
{
zmq::context_t context;
zmq::poller_t poller;
zmq::active_poller_t active_poller;
zmq::socket_t a {context, zmq::socket_type::router};
zmq::socket_t b {std::move (a)};
ASSERT_THROW (poller.add (a, ZMQ_POLLIN, zmq::poller_t::handler_t {}),
ASSERT_THROW (active_poller.add (a, ZMQ_POLLIN, zmq::active_poller_t::handler_t {}),
zmq::error_t);
}
TEST(poller, remove_invalid_socket_throws)
TEST(active_poller, remove_invalid_socket_throws)
{
zmq::context_t context;
zmq::socket_t socket {context, zmq::socket_type::router};
zmq::poller_t poller;
ASSERT_NO_THROW (poller.add (socket, ZMQ_POLLIN, zmq::poller_t::handler_t {}));
ASSERT_EQ (1u, poller.size ());
zmq::active_poller_t active_poller;
ASSERT_NO_THROW (active_poller.add (socket, ZMQ_POLLIN, zmq::active_poller_t::handler_t {}));
ASSERT_EQ (1u, active_poller.size ());
std::vector<zmq::socket_t> sockets;
sockets.emplace_back (std::move (socket));
ASSERT_THROW (poller.remove (socket), zmq::error_t);
ASSERT_EQ (1u, poller.size ());
ASSERT_THROW (active_poller.remove (socket), zmq::error_t);
ASSERT_EQ (1u, active_poller.size ());
}
TEST(poller, wait_on_added_empty_handler)
TEST(active_poller, wait_on_added_empty_handler)
{
server_client_setup s;
ASSERT_NO_THROW (s.client.send ("Hi"));
zmq::poller_t poller;
zmq::poller_t::handler_t handler;
ASSERT_NO_THROW (poller.add (s.server, ZMQ_POLLIN, handler));
ASSERT_NO_THROW (poller.wait (std::chrono::milliseconds {-1}));
zmq::active_poller_t active_poller;
zmq::active_poller_t::handler_t handler;
ASSERT_NO_THROW (active_poller.add (s.server, ZMQ_POLLIN, handler));
ASSERT_NO_THROW (active_poller.wait (std::chrono::milliseconds {-1}));
}
TEST(poller, modify_empty_throws)
TEST(active_poller, modify_empty_throws)
{
zmq::context_t context;
zmq::socket_t socket {context, zmq::socket_type::push};
zmq::poller_t poller;
ASSERT_THROW (poller.modify (socket, ZMQ_POLLIN), zmq::error_t);
zmq::active_poller_t active_poller;
ASSERT_THROW (active_poller.modify (socket, ZMQ_POLLIN), zmq::error_t);
}
TEST(poller, modify_invalid_socket_throws)
TEST(active_poller, modify_invalid_socket_throws)
{
zmq::context_t context;
zmq::socket_t a {context, zmq::socket_type::push};
zmq::socket_t b {std::move (a)};
zmq::poller_t poller;
ASSERT_THROW (poller.modify (a, ZMQ_POLLIN), zmq::error_t);
zmq::active_poller_t active_poller;
ASSERT_THROW (active_poller.modify (a, ZMQ_POLLIN), zmq::error_t);
}
TEST(poller, modify_not_added_throws)
TEST(active_poller, modify_not_added_throws)
{
zmq::context_t context;
zmq::socket_t a {context, zmq::socket_type::push};
zmq::socket_t b {context, zmq::socket_type::push};
zmq::poller_t poller;
ASSERT_NO_THROW (poller.add (a, ZMQ_POLLIN, zmq::poller_t::handler_t {}));
ASSERT_THROW (poller.modify (b, ZMQ_POLLIN), zmq::error_t);
zmq::active_poller_t active_poller;
ASSERT_NO_THROW (active_poller.add (a, ZMQ_POLLIN, zmq::active_poller_t::handler_t {}));
ASSERT_THROW (active_poller.modify (b, ZMQ_POLLIN), zmq::error_t);
}
TEST(poller, modify_simple)
TEST(active_poller, modify_simple)
{
zmq::context_t context;
zmq::socket_t a {context, zmq::socket_type::push};
zmq::poller_t poller;
ASSERT_NO_THROW (poller.add (a, ZMQ_POLLIN, zmq::poller_t::handler_t {}));
ASSERT_NO_THROW (poller.modify (a, ZMQ_POLLIN|ZMQ_POLLOUT));
zmq::active_poller_t active_poller;
ASSERT_NO_THROW (active_poller.add (a, ZMQ_POLLIN, zmq::active_poller_t::handler_t {}));
ASSERT_NO_THROW (active_poller.modify (a, ZMQ_POLLIN|ZMQ_POLLOUT));
}
TEST(poller, poll_client_server)
TEST(active_poller, poll_client_server)
{
// Setup server and client
server_client_setup s;
// Setup poller
zmq::poller_t poller;
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, s.handler));
// Setup active_poller
zmq::active_poller_t active_poller;
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN, s.handler));
// client sends message
ASSERT_NO_THROW(s.client.send("Hi"));
// wait for message and verify events
ASSERT_NO_THROW(poller.wait(std::chrono::milliseconds{500}));
ASSERT_NO_THROW(active_poller.wait(std::chrono::milliseconds{500}));
ASSERT_TRUE(s.events == ZMQ_POLLIN);
ASSERT_EQ(s.events, ZMQ_POLLIN);
// Modify server socket with pollout flag
ASSERT_NO_THROW(poller.modify(s.server, ZMQ_POLLIN | ZMQ_POLLOUT));
ASSERT_EQ(1, poller.wait(std::chrono::milliseconds{500}));
ASSERT_NO_THROW(active_poller.modify(s.server, ZMQ_POLLIN | ZMQ_POLLOUT));
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{500}));
ASSERT_EQ(s.events, ZMQ_POLLIN | ZMQ_POLLOUT);
}
TEST(poller, wait_one_return)
TEST(active_poller, wait_one_return)
{
// Setup server and client
server_client_setup s;
int count = 0;
// Setup poller
zmq::poller_t poller;
ASSERT_NO_THROW(poller.add(s.server, ZMQ_POLLIN, [&count](short) {
// Setup active_poller
zmq::active_poller_t active_poller;
ASSERT_NO_THROW(active_poller.add(s.server, ZMQ_POLLIN, [&count](short) {
++count;
}));
@ -356,32 +356,32 @@ TEST(poller, wait_one_return)
ASSERT_NO_THROW(s.client.send("Hi"));
// wait for message and verify events
ASSERT_EQ(1, poller.wait(std::chrono::milliseconds{500}));
ASSERT_EQ(1, active_poller.wait(std::chrono::milliseconds{500}));
ASSERT_EQ(1u, count);
}
TEST(poller, wait_on_move_constructed_poller)
TEST(active_poller, wait_on_move_constructed_active_poller)
{
server_client_setup s;
ASSERT_NO_THROW (s.client.send ("Hi"));
zmq::poller_t a;
zmq::poller_t::handler_t handler;
zmq::active_poller_t a;
zmq::active_poller_t::handler_t handler;
ASSERT_NO_THROW (a.add (s.server, ZMQ_POLLIN, handler));
zmq::poller_t b {std::move (a)};
zmq::active_poller_t b {std::move (a)};
ASSERT_EQ(1u, b.size ());
/// \todo the actual error code should be checked
ASSERT_THROW(a.wait(std::chrono::milliseconds{10}), zmq::error_t);
ASSERT_TRUE (b.wait (std::chrono::milliseconds {-1}));
}
TEST(poller, wait_on_move_assigned_poller)
TEST(active_poller, wait_on_move_assigned_active_poller)
{
server_client_setup s;
ASSERT_NO_THROW (s.client.send ("Hi"));
zmq::poller_t a;
zmq::poller_t::handler_t handler;
zmq::active_poller_t a;
zmq::active_poller_t::handler_t handler;
ASSERT_NO_THROW (a.add (s.server, ZMQ_POLLIN, handler));
zmq::poller_t b;
zmq::active_poller_t b;
b = {std::move (a)};
ASSERT_EQ(1u, b.size ());
/// \todo the actual error code should be checked
@ -389,13 +389,13 @@ TEST(poller, wait_on_move_assigned_poller)
ASSERT_TRUE (b.wait (std::chrono::milliseconds {-1}));
}
TEST(poller, received_on_move_constructed_poller)
TEST(active_poller, received_on_move_constructed_active_poller)
{
// Setup server and client
server_client_setup s;
int count = 0;
// Setup poller a
zmq::poller_t a;
// Setup active_poller a
zmq::active_poller_t a;
ASSERT_NO_THROW(a.add(s.server, ZMQ_POLLIN, [&count](short) {
++count;
}));
@ -404,8 +404,8 @@ TEST(poller, received_on_move_constructed_poller)
// wait for message and verify it is received
ASSERT_EQ(1, a.wait(std::chrono::milliseconds{500}));
ASSERT_EQ(1u, count);
// Move construct poller b
zmq::poller_t b{std::move(a)};
// Move construct active_poller b
zmq::active_poller_t b{std::move(a)};
// client sends message again
ASSERT_NO_THROW(s.client.send("Hi"));
// wait for message and verify it is received
@ -414,7 +414,7 @@ TEST(poller, received_on_move_constructed_poller)
}
TEST(poller, remove_from_handler)
TEST(active_poller, remove_from_handler)
{
constexpr auto ITER_NO = 10;
@ -423,18 +423,18 @@ TEST(poller, remove_from_handler)
for (auto i = 0; i < ITER_NO; ++i)
setup_list.emplace_back (server_client_setup{});
// Setup poller
zmq::poller_t poller;
// Setup active_poller
zmq::active_poller_t active_poller;
int count = 0;
for (auto i = 0; i < ITER_NO; ++i) {
ASSERT_NO_THROW(poller.add(setup_list[i].server, ZMQ_POLLIN, [&,i](short events) {
ASSERT_NO_THROW(active_poller.add(setup_list[i].server, ZMQ_POLLIN, [&,i](short events) {
ASSERT_EQ(events, ZMQ_POLLIN);
poller.remove(setup_list[ITER_NO-i-1].server);
ASSERT_EQ(ITER_NO-i-1, poller.size());
active_poller.remove(setup_list[ITER_NO-i-1].server);
ASSERT_EQ(ITER_NO-i-1, active_poller.size());
}));
++count;
}
ASSERT_EQ(ITER_NO, poller.size());
ASSERT_EQ(ITER_NO, active_poller.size());
// Clients send messages
for (auto & s : setup_list) {
ASSERT_NO_THROW(s.client.send("Hi"));
@ -447,7 +447,7 @@ TEST(poller, remove_from_handler)
}
// Fire all handlers in one wait
ASSERT_EQ(ITER_NO, poller.wait (std::chrono::milliseconds{-1}));
ASSERT_EQ(ITER_NO, active_poller.wait (std::chrono::milliseconds{-1}));
ASSERT_EQ(ITER_NO, count);
}

93
zmq.hpp
View File

@ -1019,7 +1019,7 @@ namespace zmq
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
template <typename T = void>
class base_poller_t
class poller_t
{
public:
void add (zmq::socket_t &socket, short events, T *user_data)
@ -1078,99 +1078,8 @@ namespace zmq
}
};
};
class poller_t
{
public:
poller_t () = default;
~poller_t () = default;
poller_t(const poller_t&) = delete;
poller_t &operator=(const poller_t&) = delete;
poller_t(poller_t&& src) = default;
poller_t &operator=(poller_t&& src) = default;
using handler_t = std::function<void(short)>;
void add (zmq::socket_t &socket, short events, handler_t handler)
{
auto it = decltype (handlers)::iterator {};
auto inserted = bool {};
std::tie(it, inserted) = handlers.emplace (static_cast<void*>(socket), std::make_shared<handler_t> (std::move (handler)));
try
{
base_poller.add (socket, events, inserted && *(it->second) ? it->second.get() : nullptr);
need_rebuild |= inserted;
}
catch (const zmq::error_t&)
{
// rollback
if (inserted)
{
handlers.erase (static_cast<void*>(socket));
}
throw;
}
}
void remove (zmq::socket_t &socket)
{
base_poller.remove (socket);
handlers.erase (static_cast<void*>(socket));
need_rebuild = true;
}
void modify (zmq::socket_t &socket, short events)
{
base_poller.modify (socket, events);
}
int wait (std::chrono::milliseconds timeout)
{
if (need_rebuild) {
poller_events.clear ();
poller_handlers.clear ();
poller_events.reserve (handlers.size ());
poller_handlers.reserve (handlers.size ());
for (const auto &handler : handlers) {
poller_events.emplace_back (zmq_poller_event_t {});
poller_handlers.push_back (handler.second);
}
need_rebuild = false;
}
const int count = base_poller.wait_all (poller_events, timeout);
if (count != 0) {
std::for_each (poller_events.begin (), poller_events.begin () + count,
[](zmq_poller_event_t& event) {
if (event.user_data != NULL)
(*reinterpret_cast<handler_t*> (event.user_data)) (event.events);
});
}
return count;
}
bool empty () const
{
return handlers.empty ();
}
size_t size () const
{
return handlers.size ();
}
private:
bool need_rebuild {false};
base_poller_t<handler_t> base_poller {};
std::unordered_map<void*, std::shared_ptr<handler_t>> handlers {};
std::vector<zmq_poller_event_t> poller_events {};
std::vector<std::shared_ptr<handler_t>> poller_handlers {};
}; // class poller_t
#endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
inline std::ostream& operator<<(std::ostream& os, const message_t& msg)
{
return os << msg.str();

View File

@ -426,6 +426,99 @@ inline std::ostream& operator<<(std::ostream& os, const multipart_t& msg)
return os << msg.str();
}
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
class active_poller_t
{
public:
active_poller_t () = default;
~active_poller_t () = default;
active_poller_t(const active_poller_t&) = delete;
active_poller_t &operator=(const active_poller_t&) = delete;
active_poller_t(active_poller_t&& src) = default;
active_poller_t &operator=(active_poller_t&& src) = default;
using handler_t = std::function<void(short)>;
void add (zmq::socket_t &socket, short events, handler_t handler)
{
auto it = decltype (handlers)::iterator {};
auto inserted = bool {};
std::tie(it, inserted) = handlers.emplace (static_cast<void*>(socket), std::make_shared<handler_t> (std::move (handler)));
try
{
base_poller.add (socket, events, inserted && *(it->second) ? it->second.get() : nullptr);
need_rebuild |= inserted;
}
catch (const zmq::error_t&)
{
// rollback
if (inserted)
{
handlers.erase (static_cast<void*>(socket));
}
throw;
}
}
void remove (zmq::socket_t &socket)
{
base_poller.remove (socket);
handlers.erase (static_cast<void*>(socket));
need_rebuild = true;
}
void modify (zmq::socket_t &socket, short events)
{
base_poller.modify (socket, events);
}
int wait (std::chrono::milliseconds timeout)
{
if (need_rebuild) {
poller_events.clear ();
poller_handlers.clear ();
poller_events.reserve (handlers.size ());
poller_handlers.reserve (handlers.size ());
for (const auto &handler : handlers) {
poller_events.emplace_back (zmq_poller_event_t {});
poller_handlers.push_back (handler.second);
}
need_rebuild = false;
}
const int count = base_poller.wait_all (poller_events, timeout);
if (count != 0) {
std::for_each (poller_events.begin (), poller_events.begin () + count,
[](zmq_poller_event_t& event) {
if (event.user_data != NULL)
(*reinterpret_cast<handler_t*> (event.user_data)) (event.events);
});
}
return count;
}
bool empty () const
{
return handlers.empty ();
}
size_t size () const
{
return handlers.size ();
}
private:
bool need_rebuild {false};
poller_t<handler_t> base_poller {};
std::unordered_map<void*, std::shared_ptr<handler_t>> handlers {};
std::vector<zmq_poller_event_t> poller_events {};
std::vector<std::shared_ptr<handler_t>> poller_handlers {};
}; // class active_poller_t
#endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
} // namespace zmq
#endif // __ZMQ_ADDON_HPP_INCLUDED__