Created
June 19, 2020 20:52
-
-
Save madmongo1/11aba6eaeea42159ba10fd9e2e028588 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto connection_impl::async_co_connect(std::string host, | |
std::string port, | |
std::string path) | |
-> net::awaitable< void, executor_type > | |
{ | |
assert(websocket_state_.active< websocket_initial >()); | |
try | |
{ | |
websocket_state_.enter< websocket_connecting >(); | |
auto &connecting = websocket_state_.get< websocket_connecting >(); | |
assert(connecting.active< resolving >()); | |
auto resolver = | |
net::ip::basic_resolver< net::ip::tcp, executor_type >( | |
this->get_executor()); | |
auto endpoints = co_await resolver.async_resolve( | |
host, port, net::use_awaitable_t< executor_type >()); | |
// it is possible that a cancel event arrived between the resolve | |
// completion and the execution of its handler | |
if (error_) | |
throw system_error(error_); | |
connecting.enter< tcp_connecting >(); | |
auto ep = co_await net::async_connect(ws_.next_layer().next_layer(), | |
endpoints); | |
// it is possible that a cancel event arrived between the connect | |
// completion and the execution of its handler | |
if (error_) | |
throw system_error(error_); | |
spdlog::debug( | |
"connection_impl::async_co_connect - connected to endpoint {}", | |
ep); | |
connecting.enter<ssl_connecting>(); | |
if (not ::SSL_set_tlsext_host_name(ws_.next_layer().native_handle(), | |
host.c_str())) | |
throw system_error( | |
error_code(::ERR_get_error(), net::error::ssl_category)); | |
co_await ws_.next_layer().async_handshake(net::ssl::stream_base::client); | |
if (error_) | |
throw system_error(error_); | |
connecting.enter<handshaking>(); | |
// ... to be continued | |
} | |
catch (system_error &se) | |
{ | |
error_ = se.code(); | |
websocket_state_.enter< websocket_closed >(); | |
throw; | |
} | |
catch (...) | |
{ | |
error_ = net::error::fault; | |
websocket_state_.enter< websocket_closed >(); | |
throw; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment