mirror of
https://github.com/badaix/snapcast.git
synced 2025-04-28 17:57:05 +02:00
Cleanup connect
This commit is contained in:
parent
bd424a3992
commit
0a8b737f9f
2 changed files with 93 additions and 113 deletions
|
@ -105,6 +105,66 @@ ClientConnection::ClientConnection(boost::asio::io_context& io_context, ClientSe
|
|||
}
|
||||
|
||||
|
||||
void ClientConnection::connect(const ResultHandler& handler)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
LOG(INFO, LOG_TAG) << "Resolving host IP for: " << server_.host << "\n";
|
||||
auto iterator = resolver_.resolve(server_.host, cpt::to_string(server_.port), boost::asio::ip::resolver_query_base::numeric_service, ec);
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Failed to resolve host '" << server_.host << "', error: " << ec.message() << "\n";
|
||||
handler(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& iter : iterator)
|
||||
LOG(DEBUG, LOG_TAG) << "Resolved IP: " << iter.endpoint().address().to_string() << "\n";
|
||||
|
||||
for (const auto& iter : iterator)
|
||||
{
|
||||
LOG(INFO, LOG_TAG) << "Connecting to " << iter.endpoint() << "\n";
|
||||
ec = doConnect(iter.endpoint());
|
||||
if (!ec || (ec == boost::system::errc::interrupted))
|
||||
{
|
||||
// We were successful or interrupted, e.g. by sig int
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ec)
|
||||
LOG(ERROR, LOG_TAG) << "Failed to connect to host '" << server_.host << "', error: " << ec.message() << "\n";
|
||||
else
|
||||
LOG(NOTICE, LOG_TAG) << "Connected to " << server_.host << "\n";
|
||||
|
||||
handler(ec);
|
||||
|
||||
#if 0
|
||||
resolver_.async_resolve(query, host_, cpt::to_string(port_), [this, handler](const boost::system::error_code& ec, tcp::resolver::results_type results) {
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Failed to resolve host '" << host_ << "', error: " << ec.message() << "\n";
|
||||
handler(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
resolver_.cancel();
|
||||
socket_.async_connect(*results, [this, handler](const boost::system::error_code& ec) {
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Failed to connect to host '" << host_ << "', error: " << ec.message() << "\n";
|
||||
handler(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG(NOTICE, LOG_TAG) << "Connected to " << socket_.remote_endpoint().address().to_string() << "\n";
|
||||
handler(ec);
|
||||
getNextMessage();
|
||||
});
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void ClientConnection::sendNext()
|
||||
{
|
||||
auto& message = messages_.front();
|
||||
|
@ -184,64 +244,6 @@ ClientConnectionTcp::~ClientConnectionTcp()
|
|||
disconnect();
|
||||
}
|
||||
|
||||
void ClientConnectionTcp::connect(const ResultHandler& handler)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
LOG(INFO, LOG_TAG) << "Resolving host IP for: " << server_.host << "\n";
|
||||
auto iterator = resolver_.resolve(server_.host, cpt::to_string(server_.port), boost::asio::ip::resolver_query_base::numeric_service, ec);
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Failed to resolve host '" << server_.host << "', error: " << ec.message() << "\n";
|
||||
handler(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& iter : iterator)
|
||||
LOG(DEBUG, LOG_TAG) << "Resolved IP: " << iter.endpoint().address().to_string() << "\n";
|
||||
|
||||
for (const auto& iter : iterator)
|
||||
{
|
||||
LOG(INFO, LOG_TAG) << "Connecting to " << iter.endpoint() << "\n";
|
||||
socket_.connect(iter, ec);
|
||||
if (!ec || (ec == boost::system::errc::interrupted))
|
||||
{
|
||||
// We were successful or interrupted, e.g. by sig int
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ec)
|
||||
LOG(ERROR, LOG_TAG) << "Failed to connect to host '" << server_.host << "', error: " << ec.message() << "\n";
|
||||
else
|
||||
LOG(NOTICE, LOG_TAG) << "Connected to " << socket_.remote_endpoint().address().to_string() << "\n";
|
||||
|
||||
handler(ec);
|
||||
|
||||
#if 0
|
||||
resolver_.async_resolve(query, host_, cpt::to_string(port_), [this, handler](const boost::system::error_code& ec, tcp::resolver::results_type results) {
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Failed to resolve host '" << host_ << "', error: " << ec.message() << "\n";
|
||||
handler(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
resolver_.cancel();
|
||||
socket_.async_connect(*results, [this, handler](const boost::system::error_code& ec) {
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Failed to connect to host '" << host_ << "', error: " << ec.message() << "\n";
|
||||
handler(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG(NOTICE, LOG_TAG) << "Connected to " << socket_.remote_endpoint().address().to_string() << "\n";
|
||||
handler(ec);
|
||||
getNextMessage();
|
||||
});
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
void ClientConnectionTcp::disconnect()
|
||||
{
|
||||
|
@ -349,6 +351,14 @@ void ClientConnectionTcp::getNextMessage(const MessageHandler<msg::BaseMessage>&
|
|||
}
|
||||
|
||||
|
||||
boost::system::error_code ClientConnectionTcp::doConnect(boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> endpoint)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
socket_.connect(endpoint, ec);
|
||||
return ec;
|
||||
}
|
||||
|
||||
|
||||
void ClientConnectionTcp::write(boost::asio::streambuf& buffer, WriteHandler&& write_handler)
|
||||
{
|
||||
boost::asio::async_write(socket_, buffer, write_handler);
|
||||
|
@ -370,58 +380,6 @@ ClientConnectionWs::~ClientConnectionWs()
|
|||
}
|
||||
|
||||
|
||||
void ClientConnectionWs::connect(const ResultHandler& handler)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
LOG(INFO, LOG_TAG) << "Resolving host IP for: " << server_.host << "\n";
|
||||
auto iterator = resolver_.resolve(server_.host, cpt::to_string(server_.port), boost::asio::ip::resolver_query_base::numeric_service, ec);
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Failed to resolve host '" << server_.host << "', error: " << ec.message() << "\n";
|
||||
handler(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& iter : iterator)
|
||||
LOG(DEBUG, LOG_TAG) << "Resolved IP: " << iter.endpoint().address().to_string() << "\n";
|
||||
|
||||
for (const auto& iter : iterator)
|
||||
{
|
||||
LOG(INFO, LOG_TAG) << "Connecting to " << iter.endpoint() << "\n";
|
||||
if (tcp_ws_)
|
||||
{
|
||||
tcp_ws_->binary(true);
|
||||
tcp_ws_->next_layer().connect(iter, ec);
|
||||
|
||||
// Set suggested timeout settings for the websocket
|
||||
tcp_ws_->set_option(websocket::stream_base::timeout::suggested(beast::role_type::client));
|
||||
|
||||
// Set a decorator to change the User-Agent of the handshake
|
||||
tcp_ws_->set_option(websocket::stream_base::decorator([](websocket::request_type& req)
|
||||
{ req.set(http::field::user_agent, std::string(BOOST_BEAST_VERSION_STRING) + " websocket-client-async"); }));
|
||||
|
||||
// Perform the websocket handshake
|
||||
tcp_ws_->handshake("127.0.0.1", "/stream", ec);
|
||||
handler(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ec || (ec == boost::system::errc::interrupted))
|
||||
{
|
||||
// We were successful or interrupted, e.g. by sig int
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ec)
|
||||
LOG(ERROR, LOG_TAG) << "Failed to connect to host '" << server_.host << "', error: " << ec.message() << "\n";
|
||||
else
|
||||
LOG(NOTICE, LOG_TAG) << "Connected to " << tcp_ws_->next_layer().remote_endpoint().address().to_string() << "\n";
|
||||
|
||||
handler(ec);
|
||||
}
|
||||
|
||||
|
||||
void ClientConnectionWs::disconnect()
|
||||
{
|
||||
LOG(DEBUG, LOG_TAG) << "Disconnecting\n";
|
||||
|
@ -512,6 +470,25 @@ void ClientConnectionWs::getNextMessage(const MessageHandler<msg::BaseMessage>&
|
|||
}
|
||||
|
||||
|
||||
boost::system::error_code ClientConnectionWs::doConnect(boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> endpoint)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
tcp_ws_->binary(true);
|
||||
tcp_ws_->next_layer().connect(endpoint, ec);
|
||||
|
||||
// Set suggested timeout settings for the websocket
|
||||
tcp_ws_->set_option(websocket::stream_base::timeout::suggested(beast::role_type::client));
|
||||
|
||||
// Set a decorator to change the User-Agent of the handshake
|
||||
tcp_ws_->set_option(websocket::stream_base::decorator([](websocket::request_type& req)
|
||||
{ req.set(http::field::user_agent, std::string(BOOST_BEAST_VERSION_STRING) + " websocket-client-async"); }));
|
||||
|
||||
// Perform the websocket handshake
|
||||
tcp_ws_->handshake("127.0.0.1", "/stream", ec);
|
||||
return ec;
|
||||
}
|
||||
|
||||
|
||||
void ClientConnectionWs::write(boost::asio::streambuf& buffer, WriteHandler&& write_handler)
|
||||
{
|
||||
tcp_ws_->async_write(boost::asio::buffer(buffer.data()), write_handler);
|
||||
|
|
|
@ -107,7 +107,7 @@ public:
|
|||
|
||||
/// async connect
|
||||
/// @param handler async result handler
|
||||
virtual void connect(const ResultHandler& handler) = 0;
|
||||
void connect(const ResultHandler& handler);
|
||||
/// disconnect the socket
|
||||
virtual void disconnect() = 0;
|
||||
|
||||
|
@ -147,6 +147,9 @@ public:
|
|||
protected:
|
||||
virtual void write(boost::asio::streambuf& buffer, WriteHandler&& write_handler) = 0;
|
||||
|
||||
/// Connect to @p endpoint
|
||||
virtual boost::system::error_code doConnect(boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> endpoint) = 0;
|
||||
|
||||
/// Send next pending message from messages_
|
||||
void sendNext();
|
||||
|
||||
|
@ -195,12 +198,12 @@ public:
|
|||
/// d'tor
|
||||
virtual ~ClientConnectionTcp();
|
||||
|
||||
void connect(const ResultHandler& handler) override;
|
||||
void disconnect() override;
|
||||
std::string getMacAddress() override;
|
||||
void getNextMessage(const MessageHandler<msg::BaseMessage>& handler) override;
|
||||
|
||||
private:
|
||||
boost::system::error_code doConnect(boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> endpoint) override;
|
||||
void write(boost::asio::streambuf& buffer, WriteHandler&& write_handler) override;
|
||||
|
||||
/// TCP socket
|
||||
|
@ -219,12 +222,12 @@ public:
|
|||
/// d'tor
|
||||
virtual ~ClientConnectionWs();
|
||||
|
||||
void connect(const ResultHandler& handler) override;
|
||||
void disconnect() override;
|
||||
std::string getMacAddress() override;
|
||||
void getNextMessage(const MessageHandler<msg::BaseMessage>& handler) override;
|
||||
|
||||
private:
|
||||
boost::system::error_code doConnect(boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> endpoint) override;
|
||||
void write(boost::asio::streambuf& buffer, WriteHandler&& write_handler) override;
|
||||
|
||||
/// SSL web socket
|
||||
|
|
Loading…
Add table
Reference in a new issue