Send hello message as request

This commit is contained in:
badaix 2020-05-15 08:54:15 +02:00
parent d9661f6267
commit ed3d9e5ebc
2 changed files with 22 additions and 20 deletions

View file

@ -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); tcp::resolver::query query(server_.host, cpt::to_string(server_.port), boost::asio::ip::resolver_query_base::numeric_service);
boost::system::error_code ec; 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); auto iterator = resolver_.resolve(query, ec);
if (ec) if (ec)
{ {
@ -71,7 +71,7 @@ void ClientConnection::connect(const ResultHandler& handler)
return; return;
} }
LOG(DEBUG, LOG_TAG) << "Connecting\n"; LOG(INFO, LOG_TAG) << "Connecting\n";
socket_.connect(*iterator, ec); socket_.connect(*iterator, ec);
if (ec) if (ec)
{ {

View file

@ -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<msg::Time>(std::move(response));
TimeProvider::getInstance().setDiff(reply->latency, reply->received - reply->sent); // ToServer(diff / 2);
}
else if (response->type == message_type::kServerSettings) else if (response->type == message_type::kServerSettings)
{ {
serverSettings_ = msg::message_cast<msg::ServerSettings>(std::move(response)); serverSettings_ = msg::message_cast<msg::ServerSettings>(std::move(response));
@ -151,7 +144,6 @@ void Controller::getNextMessage()
sampleFormat_ = decoder_->setHeader(headerChunk_.get()); sampleFormat_ = decoder_->setHeader(headerChunk_.get());
LOG(INFO, LOG_TAG) << "Codec: " << headerChunk_->codec << ", sampleformat: " << sampleFormat_.toString() << "\n"; LOG(INFO, LOG_TAG) << "Codec: " << headerChunk_->codec << ", sampleformat: " << sampleFormat_.toString() << "\n";
LOG(NOTICE, LOG_TAG) << TAG("state") << "sampleformat: " << sampleFormat_.toString() << "\n";
stream_ = make_shared<Stream>(sampleFormat_, settings_.player.sample_format); stream_ = make_shared<Stream>(sampleFormat_, settings_.player.sample_format);
stream_->setBufferLen(std::max(0, serverSettings_->getBufferMs() - serverSettings_->getLatency() - settings_.player.latency)); stream_->setBufferLen(std::max(0, serverSettings_->getBufferMs() - serverSettings_->getLatency() - settings_.player.latency));
@ -215,6 +207,10 @@ void Controller::getNextMessage()
meta_->push(stream_tags->msg); meta_->push(stream_tags->msg);
} }
} }
else
{
LOG(WARNING, LOG_TAG) << "Unexpected message received, type: " << response->type << "\n";
}
getNextMessage(); getNextMessage();
} }
else else
@ -350,21 +346,27 @@ void Controller::worker()
clientConnection_->connect([this](const boost::system::error_code& ec) { clientConnection_->connect([this](const boost::system::error_code& ec) {
if (!ec) if (!ec)
{ {
LOG(INFO, LOG_TAG) << "Connected!\n"; // LOG(INFO, LOG_TAG) << "Connected!\n";
string macAddress = clientConnection_->getMacAddress(); string macAddress = clientConnection_->getMacAddress();
if (settings_.host_id.empty()) if (settings_.host_id.empty())
settings_.host_id = ::getHostId(macAddress); settings_.host_id = ::getHostId(macAddress);
// Say hello to the server // Say hello to the server
auto hello = std::make_shared<msg::Hello>(macAddress, settings_.host_id, settings_.instance); auto hello = std::make_shared<msg::Hello>(macAddress, settings_.host_id, settings_.instance);
clientConnection_->send(hello, [this](const boost::system::error_code& ec) { clientConnection_->sendRequest<msg::ServerSettings>(
hello, 2s, [this](const boost::system::error_code& ec, std::unique_ptr<msg::ServerSettings> response) mutable {
if (ec) if (ec)
{ {
LOG(ERROR, LOG_TAG) << "Failed to send hello, error: " << ec.message() << "\n"; LOG(ERROR, LOG_TAG) << "Failed to send hello request, error: " << ec.message() << "\n";
reconnect(); reconnect();
return; 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 // Do initial time sync with the server