Skip to content

Commit

Permalink
Make use of async_handshake being a coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
jens-diewald committed Oct 24, 2024
1 parent 7e4ff74 commit ddfe4ee
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions include/wintls/detail/async_handshake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
namespace wintls {
namespace detail {

template <typename NextLayer>
template<typename NextLayer>
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 <typename Self>
template<typename Self>
void operator()(Self& self, wintls::error_code ec = {}, std::size_t length = 0) {
if (ec) {
self.complete(ec);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ddfe4ee

Please sign in to comment.