From ed3d9e5ebcb71aa3111c2b715d595205380af92e Mon Sep 17 00:00:00 2001 From: badaix Date: Fri, 15 May 2020 08:54:15 +0200 Subject: [PATCH] Send hello message as request --- client/client_connection.cpp | 4 ++-- client/controller.cpp | 38 +++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/client/client_connection.cpp b/client/client_connection.cpp index 073a6444..273b8419 100644 --- a/client/client_connection.cpp +++ b/client/client_connection.cpp @@ -62,7 +62,7 @@ void ClientConnection::connect(const ResultHandler& handler) { tcp::resolver::query query(server_.host, cpt::to_string(server_.port), boost::asio::ip::resolver_query_base::numeric_service); boost::system::error_code ec; - LOG(DEBUG, LOG_TAG) << "Resolving host IP\n"; + LOG(INFO, LOG_TAG) << "Resolving host IP\n"; auto iterator = resolver_.resolve(query, ec); if (ec) { @@ -71,7 +71,7 @@ void ClientConnection::connect(const ResultHandler& handler) return; } - LOG(DEBUG, LOG_TAG) << "Connecting\n"; + LOG(INFO, LOG_TAG) << "Connecting\n"; socket_.connect(*iterator, ec); if (ec) { diff --git a/client/controller.cpp b/client/controller.cpp index c5ce045f..48755bf4 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -107,13 +107,6 @@ void Controller::getNextMessage() // }); } } - else if (response->type == message_type::kTime) - { - // should not be called, because timeSync messages are sent as request, so that the response will go there - LOG(DEBUG, LOG_TAG) << "Received time sync message\n"; - auto reply = msg::message_cast(std::move(response)); - TimeProvider::getInstance().setDiff(reply->latency, reply->received - reply->sent); // ToServer(diff / 2); - } else if (response->type == message_type::kServerSettings) { serverSettings_ = msg::message_cast(std::move(response)); @@ -151,7 +144,6 @@ void Controller::getNextMessage() sampleFormat_ = decoder_->setHeader(headerChunk_.get()); LOG(INFO, LOG_TAG) << "Codec: " << headerChunk_->codec << ", sampleformat: " << sampleFormat_.toString() << "\n"; - LOG(NOTICE, LOG_TAG) << TAG("state") << "sampleformat: " << sampleFormat_.toString() << "\n"; stream_ = make_shared(sampleFormat_, settings_.player.sample_format); stream_->setBufferLen(std::max(0, serverSettings_->getBufferMs() - serverSettings_->getLatency() - settings_.player.latency)); @@ -215,6 +207,10 @@ void Controller::getNextMessage() meta_->push(stream_tags->msg); } } + else + { + LOG(WARNING, LOG_TAG) << "Unexpected message received, type: " << response->type << "\n"; + } getNextMessage(); } else @@ -350,22 +346,28 @@ void Controller::worker() clientConnection_->connect([this](const boost::system::error_code& ec) { if (!ec) { - LOG(INFO, LOG_TAG) << "Connected!\n"; - + // LOG(INFO, LOG_TAG) << "Connected!\n"; string macAddress = clientConnection_->getMacAddress(); if (settings_.host_id.empty()) settings_.host_id = ::getHostId(macAddress); // Say hello to the server auto hello = std::make_shared(macAddress, settings_.host_id, settings_.instance); - clientConnection_->send(hello, [this](const boost::system::error_code& ec) { - if (ec) - { - LOG(ERROR, LOG_TAG) << "Failed to send hello, error: " << ec.message() << "\n"; - reconnect(); - return; - } - }); + clientConnection_->sendRequest( + hello, 2s, [this](const boost::system::error_code& ec, std::unique_ptr response) mutable { + if (ec) + { + LOG(ERROR, LOG_TAG) << "Failed to send hello request, error: " << ec.message() << "\n"; + reconnect(); + return; + } + else + { + serverSettings_ = std::move(response); + LOG(INFO, LOG_TAG) << "ServerSettings - buffer: " << serverSettings_->getBufferMs() << ", latency: " << serverSettings_->getLatency() + << ", volume: " << serverSettings_->getVolume() << ", muted: " << serverSettings_->isMuted() << "\n"; + } + }); // Do initial time sync with the server sendTimeSyncMessage(50);