diff --git a/client/clientConnection.cpp b/client/clientConnection.cpp index 1f890fba..35d40502 100644 --- a/client/clientConnection.cpp +++ b/client/clientConnection.cpp @@ -28,7 +28,7 @@ using namespace std; -ClientConnection::ClientConnection(MessageReceiver* receiver, const std::string& host, size_t port) : active_(false), connected_(false), messageReceiver_(receiver), reqId_(1), host_(host), port_(port), readerThread_(NULL), sumTimeout_(chronos::msec(0)) +ClientConnection::ClientConnection(MessageReceiver* receiver, const std::string& host, size_t port) : socket_(nullptr), active_(false), connected_(false), messageReceiver_(receiver), reqId_(1), host_(host), port_(port), readerThread_(NULL), sumTimeout_(chronos::msec(0)) { } @@ -54,6 +54,19 @@ void ClientConnection::socketRead(void* _to, size_t _bytes) } +std::string ClientConnection::getMacAddress() const +{ + if (socket_ == nullptr) + throw SnapException("socket not connected"); + + std::string mac = ::getMacAddress(socket_->native()); + if (mac.empty()) + mac = "00:00:00:00:00:00"; + logO << "My MAC: \"" << mac << "\", socket: " << socket_->native() << "\n"; + return mac; +} + + void ClientConnection::start() { tcp::resolver resolver(io_service_); @@ -68,13 +81,6 @@ void ClientConnection::start() // setsockopt(socket->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); // setsockopt(socket->native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); socket_->connect(*iterator); - - std::string mac = getMacAddress(socket_->native()); - if (mac.empty()) - mac = "00:00:00:00:00:00"; - logO << "My MAC: \"" << mac << "\", socket: " << socket_->native() << "\n"; - msg::Hello hello(mac); - send(&hello); connected_ = true; logS(kLogNotice) << "Connected to " << socket_->remote_endpoint().address().to_string() << endl; active_ = true; diff --git a/client/clientConnection.h b/client/clientConnection.h index b96b16ac..a4c05feb 100644 --- a/client/clientConnection.h +++ b/client/clientConnection.h @@ -88,6 +88,8 @@ public: return msg; } + std::string getMacAddress() const; + virtual bool active() const { return active_; @@ -95,7 +97,7 @@ public: virtual bool connected() const { - return (socket_ != 0); + return (socket_ != nullptr); // return (connected_ && socket); } diff --git a/client/controller.cpp b/client/controller.cpp index b901355d..31077260 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -141,9 +141,11 @@ void Controller::worker() try { clientConnection_->start(); + + msg::Hello hello(clientConnection_->getMacAddress()); msg::Request requestMsg(kServerSettings); shared_ptr serverSettings(NULL); - while (active_ && !(serverSettings = clientConnection_->sendReq(&requestMsg))); + while (active_ && !(serverSettings = clientConnection_->sendReq(&hello))); logO << "ServerSettings - buffer: " << serverSettings->bufferMs << ", latency: " << serverSettings->latency << ", volume: " << serverSettings->volume << ", muted: " << serverSettings->muted << "\n"; requestMsg.request = kHeader; @@ -199,12 +201,6 @@ void Controller::worker() if (sendTimeSyncMessage(5000)) logO << "time sync main loop\n"; -// shared_ptr reply = clientConnection_->sendReq(&timeReq); -// if (reply) -// { -// double latency = (reply->received.sec - reply->sent.sec) + (reply->received.usec - reply->sent.usec) / 1000000.; -// TimeProvider::getInstance().setDiffToServer((reply->latency - latency) * 1000 / 2); -// } } } catch (const std::exception& e) diff --git a/server/streamServer.cpp b/server/streamServer.cpp index 19bf67dd..4514caf1 100644 --- a/server/streamServer.cpp +++ b/server/streamServer.cpp @@ -249,24 +249,6 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM } else if (requestMsg.request == kServerSettings) { - logD << "request kServerSettings: " << connection->macAddress << "\n"; - std::unique_lock mlock(mutex_); - ClientInfoPtr clientInfo = Config::instance().getClientInfo(connection->macAddress, true); - if (clientInfo == nullptr) - { - logE << "could not get client info for MAC: " << connection->macAddress << "\n"; - } - else - { - logD << "request kServerSettings\n"; - msg::ServerSettings serverSettings; - serverSettings.volume = clientInfo->volume.percent; - serverSettings.muted = clientInfo->volume.muted; - serverSettings.latency = clientInfo->latency; - serverSettings.refersTo = requestMsg.id; - serverSettings.bufferMs = settings_.bufferMs; - connection->send(&serverSettings); - } } else if (requestMsg.request == kHeader) { @@ -284,6 +266,25 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM connection->macAddress = helloMsg.getMacAddress(); logO << "Hello from " << connection->macAddress << ", host: " << helloMsg.getHostName() << ", v" << helloMsg.getVersion() << "\n"; + logD << "request kServerSettings: " << connection->macAddress << "\n"; + std::unique_lock mlock(mutex_); + ClientInfoPtr clientInfo = Config::instance().getClientInfo(connection->macAddress, true); + if (clientInfo == nullptr) + { + logE << "could not get client info for MAC: " << connection->macAddress << "\n"; + } + else + { + logD << "request kServerSettings\n"; + msg::ServerSettings serverSettings; + serverSettings.volume = clientInfo->volume.percent; + serverSettings.muted = clientInfo->volume.muted; + serverSettings.latency = clientInfo->latency; + serverSettings.refersTo = helloMsg.id; + serverSettings.bufferMs = settings_.bufferMs; + connection->send(&serverSettings); + } + ClientInfoPtr client = Config::instance().getClientInfo(connection->macAddress); client->ipAddress = connection->getIP(); client->hostName = helloMsg.getHostName();