-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathpoller.cpp
More file actions
107 lines (91 loc) · 2.92 KB
/
poller.cpp
File metadata and controls
107 lines (91 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/protocol/zmq/poller.hpp>
#include <bitcoin/system.hpp>
#include <bitcoin/protocol/zmq/identifiers.hpp>
#include <bitcoin/protocol/zmq/socket.hpp>
#include <bitcoin/protocol/zmq/zeromq.hpp>
namespace libbitcoin {
namespace protocol {
namespace zmq {
using namespace bc::system;
poller::poller() NOEXCEPT
: expired_(false),
terminated_(false)
{
}
// Parameter fd is non-zmq socket (unused when socket is set).
void poller::add(socket& socket) NOEXCEPT
{
zmq_pollitem item;
item.socket = socket.self();
item.fd = 0;
item.events = ZMQ_POLLIN;
item.revents = 0;
pollers_.push_back(item);
}
void poller::clear() NOEXCEPT
{
return pollers_.clear();
}
identifiers poller::wait() NOEXCEPT
{
return wait(zmq_maximum_safe_wait_milliseconds);
}
// BUGBUG: zeromq 4.2 has an overflow bug in timer parameterization.
// The timeout is typed as 'long' by zeromq. This is 32 bit on windows and
// actually less (potentially 1000 or 1 second) on other platforms.
// On non-windows platforms negative doesn't actually produce infinity.
identifiers poller::wait(int32_t timeout_milliseconds) NOEXCEPT
{
const auto size = pollers_.size();
BC_ASSERT(size <= max_int32);
const auto count = possible_narrow_sign_cast<int32_t>(size);
const auto& items = pointer_cast<zmq_pollitem_t>(pollers_.data());
const auto signaled = zmq_poll(items, count, timeout_milliseconds);
// Either one of the sockets was terminated or a signal intervened.
if (is_negative(signaled))
{
terminated_ = true;
return {};
}
// No events have been signaled and no failure, so the timer expired.
if (is_zero(signaled))
{
expired_ = true;
return {};
}
// At least one event was signaled, but the poll-in set may be empty.
identifiers result;
for (const auto& poller: pollers_)
if (!is_zero(poller.revents & ZMQ_POLLIN))
result.push(poller.socket);
return result;
}
bool poller::expired() const NOEXCEPT
{
return expired_;
}
bool poller::terminated() const NOEXCEPT
{
return terminated_;
}
} // namespace zmq
} // namespace protocol
} // namespace libbitcoin