replace gettimeofday with chronos::systemtimeofday

This commit is contained in:
badaix 2017-03-14 19:33:14 +01:00
parent b0f1bee219
commit 5579eabfe0
8 changed files with 27 additions and 12 deletions

View file

@ -38,7 +38,7 @@ void TimeProvider::setDiffToServer(double ms)
{ {
static int32_t lastTimeSync = 0; static int32_t lastTimeSync = 0;
timeval now; timeval now;
gettimeofday(&now, NULL); chronos::systemtimeofday(&now);
/// clear diffBuffer if last update is older than a minute /// clear diffBuffer if last update is older than a minute
if (!diffBuffer_.empty() && (std::abs(now.tv_sec - lastTimeSync) > 60)) if (!diffBuffer_.empty() && (std::abs(now.tv_sec - lastTimeSync) > 60))

View file

@ -36,6 +36,20 @@ namespace chronos
typedef std::chrono::microseconds usec; typedef std::chrono::microseconds usec;
typedef std::chrono::nanoseconds nsec; typedef std::chrono::nanoseconds nsec;
template <class Clock>
inline static void timeofday(struct timeval *tv)
{
auto now = Clock::now();
auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
tv->tv_sec = millisecs.count()/1000;
tv->tv_usec = (millisecs.count()%1000)*1000;
}
inline static void systemtimeofday(struct timeval *tv)
{
timeofday<std::chrono::system_clock>(tv);
}
inline static void addUs(timeval& tv, int us) inline static void addUs(timeval& tv, int us)
{ {
if (us < 0) if (us < 0)

View file

@ -252,7 +252,7 @@ static long uptime()
uptime = trim_copy(uptime.substr(uptime.find(" sec = ") + 7)); uptime = trim_copy(uptime.substr(uptime.find(" sec = ") + 7));
uptime.resize(uptime.find(",")); uptime.resize(uptime.find(","));
timeval now; timeval now;
gettimeofday(&now, NULL); chronos::systemtimeofday(&now);
try try
{ {
return now.tv_sec - cpt::stoul(uptime); return now.tv_sec - cpt::stoul(uptime);

View file

@ -26,6 +26,7 @@
#include <vector> #include <vector>
#include <sys/time.h> #include <sys/time.h>
#include "common/endian.h" #include "common/endian.h"
#include "common/timeDefs.h"
template<typename CharT, typename TraitsT = std::char_traits<CharT> > template<typename CharT, typename TraitsT = std::char_traits<CharT> >
@ -65,7 +66,7 @@ struct tv
tv() tv()
{ {
timeval t; timeval t;
gettimeofday(&t, NULL); chronos::systemtimeofday(&t);
sec = t.tv_sec; sec = t.tv_sec;
usec = t.tv_usec; usec = t.tv_usec;
} }

View file

@ -96,7 +96,7 @@ void StreamServer::onDisconnect(StreamSession* streamSession)
return; return;
clientInfo->connected = false; clientInfo->connected = false;
gettimeofday(&clientInfo->lastSeen, NULL); chronos::systemtimeofday(&clientInfo->lastSeen);
Config::instance().save(); Config::instance().save();
if (controlServer_ != nullptr) if (controlServer_ != nullptr)
{ {
@ -445,7 +445,7 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
ClientInfoPtr client = Config::instance().getClientInfo(connection->clientId); ClientInfoPtr client = Config::instance().getClientInfo(connection->clientId);
if (client != nullptr) if (client != nullptr)
{ {
gettimeofday(&client->lastSeen, NULL); chronos::systemtimeofday(&client->lastSeen);
client->connected = true; client->connected = true;
} }
} }
@ -489,7 +489,7 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
client->snapclient.protocolVersion = helloMsg.getProtocolVersion(); client->snapclient.protocolVersion = helloMsg.getProtocolVersion();
client->config.instance = helloMsg.getInstance(); client->config.instance = helloMsg.getInstance();
client->connected = true; client->connected = true;
gettimeofday(&client->lastSeen, NULL); chronos::systemtimeofday(&client->lastSeen);
// Assign and update stream // Assign and update stream
PcmStreamPtr stream = streamManager_->getStream(group->streamId); PcmStreamPtr stream = streamManager_->getStream(group->streamId);

View file

@ -61,7 +61,7 @@ void FileStream::worker()
while (active_) while (active_)
{ {
gettimeofday(&tvChunk, NULL); chronos::systemtimeofday(&tvChunk);
tvEncodedChunk_ = tvChunk; tvEncodedChunk_ = tvChunk;
long nextTick = chronos::getTickCount(); long nextTick = chronos::getTickCount();
try try
@ -97,7 +97,7 @@ void FileStream::worker()
} }
else else
{ {
gettimeofday(&tvChunk, NULL); chronos::systemtimeofday(&tvChunk);
tvEncodedChunk_ = tvChunk; tvEncodedChunk_ = tvChunk;
pcmListener_->onResync(this, currentTick - nextTick); pcmListener_->onResync(this, currentTick - nextTick);
nextTick = currentTick; nextTick = currentTick;

View file

@ -69,7 +69,7 @@ void PipeStream::worker()
if (fd_ != -1) if (fd_ != -1)
close(fd_); close(fd_);
fd_ = open(uri_.path.c_str(), O_RDONLY | O_NONBLOCK); fd_ = open(uri_.path.c_str(), O_RDONLY | O_NONBLOCK);
gettimeofday(&tvChunk, NULL); chronos::systemtimeofday(&tvChunk);
tvEncodedChunk_ = tvChunk; tvEncodedChunk_ = tvChunk;
long nextTick = chronos::getTickCount(); long nextTick = chronos::getTickCount();
try try
@ -117,7 +117,7 @@ void PipeStream::worker()
} }
else else
{ {
gettimeofday(&tvChunk, NULL); chronos::systemtimeofday(&tvChunk);
tvEncodedChunk_ = tvChunk; tvEncodedChunk_ = tvChunk;
pcmListener_->onResync(this, currentTick - nextTick); pcmListener_->onResync(this, currentTick - nextTick);
nextTick = currentTick; nextTick = currentTick;

View file

@ -154,7 +154,7 @@ void ProcessStream::worker()
stderrReaderThread_ = thread(&ProcessStream::stderrReader, this); stderrReaderThread_ = thread(&ProcessStream::stderrReader, this);
stderrReaderThread_.detach(); stderrReaderThread_.detach();
gettimeofday(&tvChunk, NULL); chronos::systemtimeofday(&tvChunk);
tvEncodedChunk_ = tvChunk; tvEncodedChunk_ = tvChunk;
long nextTick = chronos::getTickCount(); long nextTick = chronos::getTickCount();
try try
@ -199,7 +199,7 @@ void ProcessStream::worker()
} }
else else
{ {
gettimeofday(&tvChunk, NULL); chronos::systemtimeofday(&tvChunk);
tvEncodedChunk_ = tvChunk; tvEncodedChunk_ = tvChunk;
pcmListener_->onResync(this, currentTick - nextTick); pcmListener_->onResync(this, currentTick - nextTick);
nextTick = currentTick; nextTick = currentTick;