10#ifndef BOTAN_ASIO_ASYNC_OPS_H_
11#define BOTAN_ASIO_ASYNC_OPS_H_
13#include <botan/asio_compat.h>
14#if defined(BOTAN_FOUND_COMPATIBLE_BOOST_ASIO_VERSION)
16 #include <botan/asio_error.h>
20 #define BOOST_ASIO_DISABLE_SERIAL_PORT
21 #include <boost/asio.hpp>
22 #include <boost/asio/yield.hpp>
23 #include <boost/beast/core/bind_handler.hpp>
72template <
class Handler,
class Executor1,
class Allocator>
73class AsyncBase :
public boost::asio::coroutine {
75 using allocator_type = boost::asio::associated_allocator_t<Handler, Allocator>;
76 using executor_type = boost::asio::associated_executor_t<Handler, Executor1>;
77 using cancellation_slot_type = boost::asio::cancellation_slot;
79 allocator_type get_allocator() const noexcept {
return boost::asio::get_associated_allocator(m_handler); }
81 executor_type get_executor() const noexcept {
82 return boost::asio::get_associated_executor(m_handler, m_work_guard_1.get_executor());
85 cancellation_slot_type get_cancellation_slot() const noexcept {
return m_cancellation_state.slot(); }
95 bool cancelled() const noexcept {
96 return m_cancellation_state.cancelled() != boost::asio::cancellation_type::none;
100 template <
class HandlerT>
101 AsyncBase(HandlerT&& handler,
const Executor1& executor) :
102 m_handler(std::forward<HandlerT>(handler)),
103 m_work_guard_1(executor),
104 m_cancellation_state(boost::asio::get_associated_cancellation_slot(m_handler)) {}
112 void reset_cancellation_slot() { m_cancellation_state.slot().clear(); }
121 template <
class... Args>
122 void complete_now(Args&&... args) {
123 reset_cancellation_slot();
124 m_work_guard_1.reset();
125 m_handler(std::forward<Args>(args)...);
129 boost::asio::executor_work_guard<Executor1> m_work_guard_1;
130 boost::asio::cancellation_state m_cancellation_state;
136template <
class Operation>
137void post_resumption(Operation&& operation, boost::system::error_code ec) {
139 const auto executor = operation.get_executor();
140 boost::asio::post(executor, boost::beast::bind_front_handler(std::forward<Operation>(operation), ec));
152template <
class Operation>
155 using allocator_type =
typename Operation::allocator_type;
156 using executor_type =
typename Operation::executor_type;
157 using cancellation_slot_type =
typename Operation::cancellation_slot_type;
159 allocator_type get_allocator() const noexcept {
return m_operation.get_allocator(); }
161 executor_type get_executor() const noexcept {
return m_operation.get_executor(); }
163 cancellation_slot_type get_cancellation_slot() const noexcept {
return m_operation.get_cancellation_slot(); }
166 explicit StepBase(Operation operation) : m_operation(std::move(operation)) {}
175 void resume(boost::system::error_code ec) {
176 if(!ec && m_operation.cancelled()) {
177 ec = boost::asio::error::operation_aborted;
180 auto operation = std::move(m_operation);
185 Operation m_operation;
191template <
class Operation>
192class WaitForWriteStep :
public StepBase<Operation> {
194 explicit WaitForWriteStep(Operation operation) : StepBase<Operation>(std::move(operation)) {}
197 void operator()(
const boost::system::error_code& ) { this->resume({}); }
203template <
class Operation,
class Stream>
204class WriteQueuedStep :
public StepBase<Operation> {
206 WriteQueuedStep(Operation operation, Stream& stream) :
207 StepBase<Operation>(std::move(operation)), m_stream(stream) {}
209 void operator()(boost::system::error_code ec, std::size_t bytes_transferred) {
210 m_stream.translate_eof(ec);
211 m_stream.end_write(bytes_transferred);
222template <
class Operation,
class Stream>
223class ReadAndProcessStep :
public StepBase<Operation> {
225 ReadAndProcessStep(Operation operation, Stream& stream) :
226 StepBase<Operation>(std::move(operation)), m_stream(stream) {}
228 void operator()(boost::system::error_code ec, std::size_t bytes_transferred) {
229 m_stream.translate_eof(ec);
230 if(!ec && bytes_transferred > 0) {
231 m_stream.process_encrypted_data(
232 boost::asio::const_buffer(m_stream.input_buffer().data(), bytes_transferred));
241template <
class Handler,
class Stream,
class MutableBufferSequence,
class Allocator = std::allocator<
void>>
242class AsyncReadOperation :
public AsyncBase<Handler, typename Stream::executor_type, Allocator> {
252 template <
class HandlerT>
253 AsyncReadOperation(HandlerT&& handler,
255 const MutableBufferSequence& buffers,
256 const boost::system::error_code& ec = {}) :
257 AsyncBase<Handler, typename Stream::executor_type, Allocator>(std::forward<HandlerT>(handler),
258 stream.get_executor()),
262 this->operator()(ec,
false);
265 AsyncReadOperation(
const AsyncReadOperation& other) =
delete;
266 AsyncReadOperation(AsyncReadOperation&& other) =
default;
267 AsyncReadOperation& operator=(
const AsyncReadOperation& other) =
delete;
268 AsyncReadOperation& operator=(AsyncReadOperation&& other) =
delete;
269 ~AsyncReadOperation() =
default;
287 void operator()(boost::system::error_code ec,
bool isContinuation =
true) {
288 this->reset_cancellation_slot();
293 while(!ec && boost::asio::buffer_size(m_buffers) > 0) {
299 if(m_stream.has_received_data()) {
300 m_decodedBytes = m_stream.copy_received_data(m_buffers);
310 m_stream.mark_output_to_send();
311 if(m_stream.output_pending()) {
312 yield m_stream.async_send_queued(std::move(*
this));
318 m_stream.handle_tls_protocol_errors(ec);
327 yield m_stream.async_read_and_process(std::move(*
this));
330 if(!isContinuation) {
332 yield post_resumption(std::move(*
this), ec);
335 this->complete_now(ec, m_decodedBytes);
341 MutableBufferSequence m_buffers;
342 std::size_t m_decodedBytes;
345template <
typename Handler,
class Stream,
class ConstBufferSequence,
class Allocator = std::allocator<
void>>
346class AsyncWriteOperation :
public AsyncBase<Handler, typename Stream::executor_type, Allocator> {
356 template <
class HandlerT>
357 AsyncWriteOperation(HandlerT&& handler, Stream& stream, std::optional<ConstBufferSequence> plaintext) :
358 AsyncBase<Handler, typename Stream::executor_type, Allocator>(std::forward<HandlerT>(handler),
359 stream.get_executor()),
361 m_plaintext(std::move(plaintext)),
362 m_plainBytesTransferred(0) {
363 this->operator()(boost::system::error_code{},
false);
366 AsyncWriteOperation(
const AsyncWriteOperation& other) =
delete;
367 AsyncWriteOperation(AsyncWriteOperation&& other) =
default;
368 AsyncWriteOperation& operator=(
const AsyncWriteOperation& other) =
delete;
369 AsyncWriteOperation& operator=(AsyncWriteOperation&& other) =
delete;
370 ~AsyncWriteOperation() =
default;
383 void operator()(boost::system::error_code ec,
bool isContinuation =
true) {
384 this->reset_cancellation_slot();
394 while(!ec && m_stream.write_in_progress()) {
395 yield m_stream.async_send_queued(std::move(*
this));
399 if(m_plaintext.has_value()) {
403 ec = m_stream.tls_encrypt(*m_plaintext);
408 m_plainBytesTransferred = boost::asio::buffer_size(*m_plaintext);
411 ec = m_stream.tls_close();
419 while(!ec && m_stream.has_data_to_send()) {
420 yield m_stream.async_send_queued(std::move(*
this));
424 if(!isContinuation) {
426 yield post_resumption(std::move(*
this), ec);
429 this->complete_now(ec, m_plainBytesTransferred);
434 bool empty_write()
const {
return m_plaintext.has_value() && boost::asio::buffer_size(*m_plaintext) == 0; }
437 std::optional<ConstBufferSequence> m_plaintext;
438 std::size_t m_plainBytesTransferred;
441template <
class Handler,
class Stream,
class Allocator = std::allocator<
void>>
442class AsyncHandshakeOperation :
public AsyncBase<Handler, typename Stream::executor_type, Allocator> {
451 template <
class HandlerT>
452 AsyncHandshakeOperation(HandlerT&& handler, Stream& stream,
const boost::system::error_code& ec = {}) :
453 AsyncBase<Handler, typename Stream::executor_type, Allocator>(std::forward<HandlerT>(handler),
454 stream.get_executor()),
456 this->operator()(ec,
false);
459 AsyncHandshakeOperation(
const AsyncHandshakeOperation& other) =
delete;
460 AsyncHandshakeOperation(AsyncHandshakeOperation&& other) =
default;
461 AsyncHandshakeOperation& operator=(
const AsyncHandshakeOperation& other) =
delete;
462 AsyncHandshakeOperation& operator=(AsyncHandshakeOperation&& other) =
delete;
463 ~AsyncHandshakeOperation() =
default;
477 void operator()(boost::system::error_code ec,
bool isContinuation =
true) {
478 this->reset_cancellation_slot();
485 if(m_stream.has_data_to_send()) {
486 yield m_stream.async_send_queued(std::move(*
this));
492 if(m_stream.native_handle()->is_handshake_complete()) {
498 m_stream.handle_tls_protocol_errors(ec);
507 yield m_stream.async_read_and_process(std::move(*
this));
510 if(!isContinuation) {
512 yield post_resumption(std::move(*
this), ec);
515 this->complete_now(ec);
525 #include <boost/asio/unyield.hpp>