From 03c01da74dc246358f397fb44e6280c3a766a3f7 Mon Sep 17 00:00:00 2001 From: Jens Diewald Date: Thu, 16 Feb 2023 15:56:24 +0100 Subject: [PATCH] Make use of async_handshake being a coroutine Before async_handshake was mostly a state machine and the fact that it is also a coroutine was not used. This removes the state machine part and makes use of the coroutine. --- include/wintls/detail/async_handshake.hpp | 35 ++++++----------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/include/wintls/detail/async_handshake.hpp b/include/wintls/detail/async_handshake.hpp index 8b42d48..e7f5e04 100644 --- a/include/wintls/detail/async_handshake.hpp +++ b/include/wintls/detail/async_handshake.hpp @@ -17,17 +17,16 @@ namespace wintls { namespace detail { -template +template struct async_handshake : net::coroutine { async_handshake(NextLayer& next_layer, detail::sspi_handshake& handshake, handshake_type type) - : next_layer_(next_layer) - , handshake_(handshake) - , entry_count_(0) - , state_(state::idle) { + : next_layer_(next_layer) + , handshake_(handshake) + , entry_count_(0) { handshake_(type); } - template + template void operator()(Self& self, wintls::error_code ec = {}, std::size_t length = 0) { if (ec) { self.complete(ec); @@ -39,35 +38,22 @@ struct async_handshake : net::coroutine { return entry_count_ > 1; }; - switch(state_) { - case state::reading: - handshake_.size_read(length); - state_ = state::idle; - break; - case state::writing: - handshake_.size_written(length); - state_ = state::idle; - break; - case state::idle: - break; - } - detail::sspi_handshake::state handshake_state; WINTLS_ASIO_CORO_REENTER(*this) { - while((handshake_state = handshake_()) != detail::sspi_handshake::state::done) { + while ((handshake_state = handshake_()) != detail::sspi_handshake::state::done) { if (handshake_state == detail::sspi_handshake::state::data_needed) { WINTLS_ASIO_CORO_YIELD { - state_ = state::reading; next_layer_.async_read_some(handshake_.in_buffer(), std::move(self)); } + handshake_.size_read(length); continue; } if (handshake_state == detail::sspi_handshake::state::data_available) { WINTLS_ASIO_CORO_YIELD { - state_ = state::writing; net::async_write(next_layer_, handshake_.out_buffer(), std::move(self)); } + handshake_.size_written(length); continue; } @@ -99,11 +85,6 @@ struct async_handshake : net::coroutine { NextLayer& next_layer_; detail::sspi_handshake& handshake_; int entry_count_; - enum class state { - idle, - reading, - writing - } state_; }; } // namespace detail