|
52 | 52 | #ifdef ZMQ_CPP11
|
53 | 53 | #include <chrono>
|
54 | 54 | #include <tuple>
|
| 55 | +#include <functional> |
55 | 56 | #endif
|
56 | 57 |
|
57 | 58 | // Detect whether the compiler supports C++11 rvalue references.
|
@@ -490,6 +491,7 @@ namespace zmq
|
490 | 491 | class socket_t
|
491 | 492 | {
|
492 | 493 | friend class monitor_t;
|
| 494 | + friend class poller_t; |
493 | 495 | public:
|
494 | 496 | inline socket_t(context_t& context_, int type_)
|
495 | 497 | {
|
@@ -827,6 +829,62 @@ namespace zmq
|
827 | 829 | private:
|
828 | 830 | void* socketPtr;
|
829 | 831 | };
|
| 832 | + |
| 833 | +#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) |
| 834 | + class poller_t |
| 835 | + { |
| 836 | + public: |
| 837 | + poller_t () : poller_ptr (zmq_poller_new ()) |
| 838 | + { |
| 839 | + if (!poller_ptr) |
| 840 | + throw error_t (); |
| 841 | + } |
| 842 | + |
| 843 | + ~poller_t () |
| 844 | + { |
| 845 | + zmq_poller_destroy (&poller_ptr); |
| 846 | + } |
| 847 | + |
| 848 | + bool add (zmq::socket_t &socket, short events, std::function<void(void)> &handler) |
| 849 | + { |
| 850 | + if (0 == zmq_poller_add (poller_ptr, socket.ptr, &handler, events)) { |
| 851 | + poller_events.emplace_back (zmq_poller_event_t ()); |
| 852 | + return true; |
| 853 | + } |
| 854 | + return false; |
| 855 | + } |
| 856 | + |
| 857 | + bool remove (zmq::socket_t &socket) |
| 858 | + { |
| 859 | + if (0 == zmq_poller_remove (poller_ptr, socket.ptr)) { |
| 860 | + poller_events.pop_back (); |
| 861 | + return true; |
| 862 | + } |
| 863 | + return false; |
| 864 | + } |
| 865 | + |
| 866 | + bool wait (std::chrono::milliseconds timeout) |
| 867 | + { |
| 868 | + int rc = zmq_poller_wait_all (poller_ptr, poller_events.data (), poller_events.size (), static_cast<long>(timeout.count ())); |
| 869 | + if (rc >= 0) { |
| 870 | + std::for_each (poller_events.begin (), poller_events.begin () + rc, [](zmq_poller_event_t& event) { |
| 871 | + (*reinterpret_cast<std::function<void(void)>*> (event.user_data)) (); |
| 872 | + }); |
| 873 | + return true; |
| 874 | + } |
| 875 | + |
| 876 | + if (zmq_errno () == ETIMEDOUT) |
| 877 | + return false; |
| 878 | + |
| 879 | + throw error_t (); |
| 880 | + } |
| 881 | + |
| 882 | + private: |
| 883 | + void *poller_ptr; |
| 884 | + std::vector<zmq_poller_event_t> poller_events; |
| 885 | + }; |
| 886 | +#endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) |
| 887 | + |
830 | 888 | }
|
831 | 889 |
|
832 | 890 | #endif
|
0 commit comments