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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
//
// prioritised_handlers.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/asio.hpp>
#include <iostream>
#include <memory>
#include <queue>
using boost::asio::ip::tcp;
class handler_priority_queue : public boost::asio::execution_context
{
public:
template <typename Function>
void add(int priority, Function function)
{
std::unique_ptr<queued_handler_base> handler(
new queued_handler<Function>(
priority, std::move(function)));
handlers_.push(std::move(handler));
}
void execute_all()
{
while (!handlers_.empty())
{
handlers_.top()->execute();
handlers_.pop();
}
}
class executor
{
public:
executor(handler_priority_queue& q, int p)
: context_(q), priority_(p)
{
}
handler_priority_queue& context() const noexcept
{
return context_;
}
template <typename Function, typename Allocator>
void dispatch(Function f, const Allocator&) const
{
context_.add(priority_, std::move(f));
}
template <typename Function, typename Allocator>
void post(Function f, const Allocator&) const
{
context_.add(priority_, std::move(f));
}
template <typename Function, typename Allocator>
void defer(Function f, const Allocator&) const
{
context_.add(priority_, std::move(f));
}
void on_work_started() const noexcept {}
void on_work_finished() const noexcept {}
bool operator==(const executor& other) const noexcept
{
return &context_ == &other.context_ && priority_ == other.priority_;
}
bool operator!=(const executor& other) const noexcept
{
return !operator==(other);
}
private:
handler_priority_queue& context_;
int priority_;
};
template <typename Handler>
boost::asio::executor_binder<Handler, executor>
wrap(int priority, Handler handler)
{
return boost::asio::bind_executor(
executor(*this, priority), std::move(handler));
}
private:
class queued_handler_base
{
public:
queued_handler_base(int p)
: priority_(p)
{
}
virtual ~queued_handler_base()
{
}
virtual void execute() = 0;
friend bool operator<(const std::unique_ptr<queued_handler_base>& a,
const std::unique_ptr<queued_handler_base>& b) noexcept
{
return a->priority_ < b->priority_;
}
private:
int priority_;
};
template <typename Function>
class queued_handler : public queued_handler_base
{
public:
queued_handler(int p, Function f)
: queued_handler_base(p), function_(std::move(f))
{
}
void execute() override
{
function_();
}
private:
Function function_;
};
std::priority_queue<std::unique_ptr<queued_handler_base>> handlers_;
};
//----------------------------------------------------------------------
void high_priority_handler(const boost::system::error_code& /*ec*/,
tcp::socket /*socket*/)
{
std::cout << "High priority handler\n";
}
void middle_priority_handler(const boost::system::error_code& /*ec*/)
{
std::cout << "Middle priority handler\n";
}
struct low_priority_handler
{
// Make the handler a move-only type.
low_priority_handler() = default;
low_priority_handler(const low_priority_handler&) = delete;
low_priority_handler(low_priority_handler&&) = default;
void operator()()
{
std::cout << "Low priority handler\n";
}
};
int main()
{
boost::asio::io_context io_context;
handler_priority_queue pri_queue;
// Post a completion handler to be run immediately.
boost::asio::post(io_context, pri_queue.wrap(0, low_priority_handler()));
// Start an asynchronous accept that will complete immediately.
tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), 0);
tcp::acceptor acceptor(io_context, endpoint);
tcp::socket server_socket(io_context);
acceptor.async_accept(pri_queue.wrap(100, high_priority_handler));
tcp::socket client_socket(io_context);
client_socket.connect(acceptor.local_endpoint());
// Set a deadline timer to expire immediately.
boost::asio::steady_timer timer(io_context);
timer.expires_at(boost::asio::steady_timer::clock_type::time_point::min());
timer.async_wait(pri_queue.wrap(42, middle_priority_handler));
while (io_context.run_one())
{
// The custom invocation hook adds the handlers to the priority queue
// rather than executing them from within the poll_one() call.
while (io_context.poll_one())
;
pri_queue.execute_all();
}
return 0;
}
|