Skip to content

Commit

Permalink
Refactor: replace macro checks by concepts
Browse files Browse the repository at this point in the history
Co-Authored-By: Fabian Albert <[email protected]>
  • Loading branch information
reneme and FAlbertDev committed Oct 18, 2023
1 parent 931e92c commit f2ab1c9
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/lib/tls/asio/asio_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ class StreamCallbacks : public Callbacks {
std::weak_ptr<TLS::Context> m_context;
};

namespace detail {

template <typename T>
concept basic_completion_token = boost::asio::completion_token_for<T, void(boost::system::error_code)>;

template <typename T>
concept byte_size_completion_token = boost::asio::completion_token_for<T, void(boost::system::error_code, size_t)>;

} // namespace detail

/**
* @brief boost::asio compatible SSL/TLS stream
*
Expand Down Expand Up @@ -346,15 +356,13 @@ class Stream {
* @param completion_token The completion handler to be called when the handshake operation completes.
* The completion signature of the handler must be: void(boost::system::error_code).
*/
template <typename CompletionToken = default_completion_token>
template <detail::basic_completion_token CompletionToken = default_completion_token>
auto async_handshake(Botan::TLS::Connection_Side side,
CompletionToken&& completion_token = default_completion_token{}) {
return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code)>(
[this](auto&& completion_handler, TLS::Connection_Side connection_side) {
using completion_handler_t = std::decay_t<decltype(completion_handler)>;

BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(completion_handler_t, completion_handler) type_check;

boost::system::error_code ec;
setup_native_handle(connection_side, ec);

Expand All @@ -366,11 +374,11 @@ class Stream {
}

//! @throws Not_Implemented
template <typename ConstBufferSequence, typename BufferedHandshakeHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(BufferedHandshakeHandler, void(boost::system::error_code, std::size_t))
async_handshake(Connection_Side side, const ConstBufferSequence& buffers, BufferedHandshakeHandler&& handler) {
template <typename ConstBufferSequence, detail::basic_completion_token BufferedHandshakeHandler>
auto async_handshake(Connection_Side side,
const ConstBufferSequence& buffers,
BufferedHandshakeHandler&& handler) {
BOTAN_UNUSED(side, buffers, handler);
BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(BufferedHandshakeHandler, handler) type_check;
throw Not_Implemented("buffered async handshake is not implemented");
}

Expand Down Expand Up @@ -447,14 +455,12 @@ class Stream {
* @param completion_token The completion handler to be called when the shutdown operation completes.
* The completion signature of the handler must be: void(boost::system::error_code).
*/
template <typename CompletionToken = default_completion_token>
template <detail::basic_completion_token CompletionToken = default_completion_token>
auto async_shutdown(CompletionToken&& completion_token = default_completion_token{}) {
return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code)>(
[this](auto&& completion_handler) {
using completion_handler_t = std::decay_t<decltype(completion_handler)>;

BOOST_ASIO_SHUTDOWN_HANDLER_CHECK(completion_handler_t, completion_handler) type_check;

boost::system::error_code ec;
try_with_error_code([&] { native_handle()->close(); }, ec);

Expand Down Expand Up @@ -574,15 +580,14 @@ class Stream {
* handler will be made as required. The completion signature of the handler must be:
* void(boost::system::error_code, std::size_t).
*/
template <typename ConstBufferSequence, typename CompletionToken = default_completion_token>
template <typename ConstBufferSequence,
detail::byte_size_completion_token CompletionToken = default_completion_token>
auto async_write_some(const ConstBufferSequence& buffers,
CompletionToken&& completion_token = default_completion_token{}) {
return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code, std::size_t)>(
[this](auto&& completion_handler, const auto& bufs) {
using completion_handler_t = std::decay_t<decltype(completion_handler)>;

BOOST_ASIO_WRITE_HANDLER_CHECK(completion_handler_t, completion_handler) type_check;

boost::system::error_code ec;
tls_encrypt(bufs, ec);

Expand Down Expand Up @@ -611,15 +616,14 @@ class Stream {
* @param completion_token The completion handler to be called when the read operation completes. The completion
* signature of the handler must be: void(boost::system::error_code, std::size_t).
*/
template <typename MutableBufferSequence, typename CompletionToken = default_completion_token>
template <typename MutableBufferSequence,
detail::byte_size_completion_token CompletionToken = default_completion_token>
auto async_read_some(const MutableBufferSequence& buffers,
CompletionToken&& completion_token = default_completion_token{}) {
return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code, std::size_t)>(
[this](auto&& completion_handler, const auto& bufs) {
using completion_handler_t = std::decay_t<decltype(completion_handler)>;

BOOST_ASIO_READ_HANDLER_CHECK(completion_handler_t, completion_handler) type_check;

detail::AsyncReadOperation<completion_handler_t, Stream, MutableBufferSequence> op{
std::forward<completion_handler_t>(completion_handler), *this, bufs};
},
Expand Down

0 comments on commit f2ab1c9

Please sign in to comment.