diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2015-06-08 02:03:31 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2015-06-09 02:05:34 +0900 |
| commit | 99cd73c0926f0c07d27777fde2f15816e8040030 (patch) | |
| tree | 7c6a077b0590c49029c76686afb9c2ab1d00bb6d /src/DownloadEngine.cc | |
| parent | 2874f6ab095a30a4d297cdc85fa3730bec41b1c2 (diff) | |
Replace timer facility with chrono lib
Diffstat (limited to 'src/DownloadEngine.cc')
| -rw-r--r-- | src/DownloadEngine.cc | 54 |
1 files changed, 30 insertions, 24 deletions
diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index cfc0848d..377befe2 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -90,12 +90,16 @@ volatile sig_atomic_t globalHaltRequested = 0; } // namespace global +namespace { +constexpr auto DEFAULT_REFRESH_INTERVAL = std::chrono::seconds(1); +} // namespace + DownloadEngine::DownloadEngine(std::unique_ptr<EventPoll> eventPoll) : eventPoll_(std::move(eventPoll)), haltRequested_(0), noWait_(true), refreshInterval_(DEFAULT_REFRESH_INTERVAL), - lastRefresh_(0), + lastRefresh_(Timer::zero()), cookieStorage_(make_unique<CookieStorage>()), #ifdef ENABLE_BITTORRENT btRegistry_(make_unique<BtRegistry>()), @@ -164,7 +168,7 @@ int DownloadEngine::run(bool oneshot) noWait_ = false; global::wallclock().reset(); calculateStatistics(); - if(lastRefresh_.differenceInMillis(global::wallclock())+A2_DELTA_MILLIS >= + if(lastRefresh_.difference(global::wallclock())+A2_DELTA_MILLIS >= refreshInterval_) { refreshInterval_ = DEFAULT_REFRESH_INTERVAL; lastRefresh_ = global::wallclock(); @@ -188,9 +192,10 @@ void DownloadEngine::waitData() if(noWait_) { tv.tv_sec = tv.tv_usec = 0; } else { - lldiv_t qr = lldiv(refreshInterval_*1000, 1000000); - tv.tv_sec = qr.quot; - tv.tv_usec = qr.rem; + auto t = + std::chrono::duration_cast<std::chrono::microseconds>(refreshInterval_); + tv.tv_sec = t.count() / 1000000; + tv.tv_usec = t.count() % 1000000; } eventPoll_->poll(tv); } @@ -245,7 +250,7 @@ void DownloadEngine::afterEachIteration() requestHalt(); global::globalHaltRequested = 2; setNoWait(true); - setRefreshInterval(0); + setRefreshInterval(std::chrono::milliseconds(0)); return; } @@ -254,7 +259,7 @@ void DownloadEngine::afterEachIteration() requestForceHalt(); global::globalHaltRequested = 4; setNoWait(true); - setRefreshInterval(0); + setRefreshInterval(std::chrono::milliseconds(0)); return; } } @@ -307,7 +312,8 @@ void DownloadEngine::poolSocket(const std::string& key, std::multimap<std::string, SocketPoolEntry>::value_type p(key, entry); socketPool_.insert(p); - if(lastSocketPoolScan_.difference(global::wallclock()) < 60) { + if(lastSocketPoolScan_.difference(global::wallclock()) < + std::chrono::minutes(1)) { return; } std::multimap<std::string, SocketPoolEntry> newPool; @@ -351,9 +357,9 @@ void DownloadEngine::poolSocket uint16_t proxyport, const std::shared_ptr<SocketCore>& sock, const std::string& options, - time_t timeout) + std::chrono::seconds timeout) { - SocketPoolEntry e(sock, options, timeout); + SocketPoolEntry e(sock, options, std::move(timeout)); poolSocket(createSockPoolKey(ipaddr, port, username, proxyhost, proxyport),e); } @@ -363,9 +369,9 @@ void DownloadEngine::poolSocket const std::string& proxyhost, uint16_t proxyport, const std::shared_ptr<SocketCore>& sock, - time_t timeout) + std::chrono::seconds timeout) { - SocketPoolEntry e(sock, timeout); + SocketPoolEntry e(sock, std::move(timeout)); poolSocket(createSockPoolKey(ipaddr, port, A2STR::NIL,proxyhost,proxyport),e); } @@ -388,20 +394,20 @@ bool getPeerInfo(std::pair<std::string, uint16_t>& res, void DownloadEngine::poolSocket(const std::shared_ptr<Request>& request, const std::shared_ptr<Request>& proxyRequest, const std::shared_ptr<SocketCore>& socket, - time_t timeout) + std::chrono::seconds timeout) { if(proxyRequest) { // If proxy is defined, then pool socket with its hostname. poolSocket(request->getHost(), request->getPort(), proxyRequest->getHost(), proxyRequest->getPort(), - socket, timeout); + socket, std::move(timeout)); return; } std::pair<std::string, uint16_t> peerInfo; if(getPeerInfo(peerInfo, socket)) { poolSocket(peerInfo.first, peerInfo.second, - A2STR::NIL, 0, socket, timeout); + A2STR::NIL, 0, socket, std::move(timeout)); } } @@ -411,20 +417,20 @@ void DownloadEngine::poolSocket const std::shared_ptr<Request>& proxyRequest, const std::shared_ptr<SocketCore>& socket, const std::string& options, - time_t timeout) + std::chrono::seconds timeout) { if(proxyRequest) { // If proxy is defined, then pool socket with its hostname. poolSocket(request->getHost(), request->getPort(), username, proxyRequest->getHost(), proxyRequest->getPort(), - socket, options, timeout); + socket, options, std::move(timeout)); return; } std::pair<std::string, uint16_t> peerInfo; if(getPeerInfo(peerInfo, socket)) { poolSocket(peerInfo.first, peerInfo.second, username, - A2STR::NIL, 0, socket, options, timeout); + A2STR::NIL, 0, socket, options, std::move(timeout)); } } @@ -512,16 +518,16 @@ DownloadEngine::popPooledSocket DownloadEngine::SocketPoolEntry::SocketPoolEntry (const std::shared_ptr<SocketCore>& socket, const std::string& options, - time_t timeout) + std::chrono::seconds timeout) : socket_(socket), options_(options), - timeout_(timeout) + timeout_(std::move(timeout)) {} DownloadEngine::SocketPoolEntry::SocketPoolEntry -(const std::shared_ptr<SocketCore>& socket, time_t timeout) +(const std::shared_ptr<SocketCore>& socket, std::chrono::seconds timeout) : socket_(socket), - timeout_(timeout) + timeout_(std::move(timeout)) {} DownloadEngine::SocketPoolEntry::~SocketPoolEntry() {} @@ -577,9 +583,9 @@ const std::unique_ptr<CookieStorage>& DownloadEngine::getCookieStorage() const return cookieStorage_; } -void DownloadEngine::setRefreshInterval(int64_t interval) +void DownloadEngine::setRefreshInterval(std::chrono::milliseconds interval) { - refreshInterval_ = std::min(static_cast<int64_t>(999), interval); + refreshInterval_ = std::move(interval); } void DownloadEngine::addCommand |
