mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-10 07:36:41 +02:00
use timeval instead of double for time sync
This commit is contained in:
parent
6ea10c77dd
commit
14b5abe5d7
6 changed files with 21 additions and 15 deletions
|
@ -72,10 +72,7 @@ void Controller::onMessageReceived(ClientConnection* connection, const msg::Base
|
||||||
{
|
{
|
||||||
msg::Time reply;
|
msg::Time reply;
|
||||||
reply.deserialize(baseMessage, buffer);
|
reply.deserialize(baseMessage, buffer);
|
||||||
double latency = (reply.received.sec - reply.sent.sec) + (reply.received.usec - reply.sent.usec) / 1000000.;
|
TimeProvider::getInstance().setDiff(reply.latency, reply.received - reply.sent);// ToServer(diff / 2);
|
||||||
// logO << "timeMsg: " << latency << "\n";
|
|
||||||
TimeProvider::getInstance().setDiffToServer((reply.latency - latency) * 1000 / 2);
|
|
||||||
// logO << "diff to server [ms]: " << (float)TimeProvider::getInstance().getDiffToServer<chronos::usec>().count() / 1000.f << "\n";
|
|
||||||
}
|
}
|
||||||
else if (baseMessage.type == message_type::kServerSettings)
|
else if (baseMessage.type == message_type::kServerSettings)
|
||||||
{
|
{
|
||||||
|
@ -185,8 +182,7 @@ void Controller::worker()
|
||||||
shared_ptr<msg::Time> reply = clientConnection_->sendReq<msg::Time>(&timeReq, chronos::msec(2000));
|
shared_ptr<msg::Time> reply = clientConnection_->sendReq<msg::Time>(&timeReq, chronos::msec(2000));
|
||||||
if (reply)
|
if (reply)
|
||||||
{
|
{
|
||||||
double latency = (reply->received.sec - reply->sent.sec) + (reply->received.usec - reply->sent.usec) / 1000000.;
|
TimeProvider::getInstance().setDiff(reply->latency, reply->received - reply->sent);
|
||||||
TimeProvider::getInstance().setDiffToServer((reply->latency - latency) * 1000 / 2);
|
|
||||||
usleep(100);
|
usleep(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,14 @@ TimeProvider::TimeProvider() : diffToServer_(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TimeProvider::setDiff(const tv& c2s, const tv& s2c)
|
||||||
|
{
|
||||||
|
tv latency = c2s - s2c;
|
||||||
|
double diff = latency.sec * 1000. + latency.usec / 1000.;
|
||||||
|
setDiffToServer(diff / 2.);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void TimeProvider::setDiffToServer(double ms)
|
void TimeProvider::setDiffToServer(double ms)
|
||||||
{
|
{
|
||||||
static int32_t lastTimeSync = 0;
|
static int32_t lastTimeSync = 0;
|
||||||
|
|
|
@ -42,6 +42,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDiffToServer(double ms);
|
void setDiffToServer(double ms);
|
||||||
|
void setDiff(const tv& c2s, const tv& s2c);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline T getDiffToServer() const
|
inline T getDiffToServer() const
|
||||||
|
|
|
@ -81,7 +81,7 @@ struct tv
|
||||||
int32_t sec;
|
int32_t sec;
|
||||||
int32_t usec;
|
int32_t usec;
|
||||||
|
|
||||||
tv operator+(const tv& other)
|
tv operator+(const tv& other) const
|
||||||
{
|
{
|
||||||
tv result(*this);
|
tv result(*this);
|
||||||
result.sec += other.sec;
|
result.sec += other.sec;
|
||||||
|
@ -94,7 +94,7 @@ struct tv
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
tv operator-(const tv& other)
|
tv operator-(const tv& other) const
|
||||||
{
|
{
|
||||||
tv result(*this);
|
tv result(*this);
|
||||||
result.sec -= other.sec;
|
result.sec -= other.sec;
|
||||||
|
|
|
@ -37,21 +37,22 @@ public:
|
||||||
|
|
||||||
virtual void read(std::istream& stream)
|
virtual void read(std::istream& stream)
|
||||||
{
|
{
|
||||||
readVal(stream, latency);
|
readVal(stream, latency.sec);
|
||||||
// stream.read(reinterpret_cast<char *>(&latency), sizeof(double));
|
readVal(stream, latency.usec);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual uint32_t getSize() const
|
virtual uint32_t getSize() const
|
||||||
{
|
{
|
||||||
return sizeof(double);
|
return sizeof(tv);
|
||||||
}
|
}
|
||||||
|
|
||||||
double latency;
|
tv latency;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void doserialize(std::ostream& stream) const
|
virtual void doserialize(std::ostream& stream) const
|
||||||
{
|
{
|
||||||
writeVal(stream, latency);//reinterpret_cast<const char *>(&latency), sizeof(double));
|
writeVal(stream, latency.sec);
|
||||||
|
writeVal(stream, latency.usec);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -250,8 +250,8 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
|
||||||
{
|
{
|
||||||
msg::Time timeMsg;
|
msg::Time timeMsg;
|
||||||
timeMsg.refersTo = requestMsg.id;
|
timeMsg.refersTo = requestMsg.id;
|
||||||
timeMsg.latency = (requestMsg.received.sec - requestMsg.sent.sec) + (requestMsg.received.usec - requestMsg.sent.usec) / 1000000.;
|
timeMsg.latency = requestMsg.received - requestMsg.sent;
|
||||||
logD << "Latency: " << timeMsg.latency << ", refers to: " << timeMsg.refersTo << "\n";
|
// logO << "Latency sec: " << timeMsg.latency.sec << ", usec: " << timeMsg.latency.usec << ", refers to: " << timeMsg.refersTo << "\n";
|
||||||
connection->send(&timeMsg);
|
connection->send(&timeMsg);
|
||||||
|
|
||||||
// refresh connection state
|
// refresh connection state
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue