From 5d9e53a37da290238ee291a64cf9934426f188cb Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Tue, 26 Apr 2016 19:32:57 -0400 Subject: [PATCH] Migrate off of posix_time and most uses of C time_t. --- Builds/VisualStudio2015/RippleD.vcxproj | 2 - .../VisualStudio2015/RippleD.vcxproj.filters | 3 - src/ripple/app/ledger/LedgerToJson.h | 2 - src/ripple/app/ledger/impl/InboundLedger.cpp | 10 +- src/ripple/app/ledger/impl/LedgerToJson.cpp | 3 +- .../app/ledger/impl/TransactionAcquire.cpp | 8 +- src/ripple/app/misc/NetworkOPs.cpp | 1 - src/ripple/app/misc/NetworkOPs.h | 1 - src/ripple/basics/Time.h | 34 ------- src/ripple/basics/chrono.h | 4 + src/ripple/basics/impl/Log.cpp | 7 +- src/ripple/basics/impl/Time.cpp | 81 ++++++++++------ src/ripple/beast/asio/io_latency_probe.h | 10 +- src/ripple/beast/clock/chrono_util.h | 2 + .../beast/insight/impl/StatsDCollector.cpp | 5 +- src/ripple/core/Config.h | 4 +- src/ripple/core/impl/Config.cpp | 2 +- src/ripple/core/impl/SNTPClock.cpp | 94 +++++++++++-------- src/ripple/net/HTTPClient.h | 8 +- src/ripple/net/impl/HTTPClient.cpp | 14 +-- src/ripple/net/impl/RPCCall.cpp | 5 +- src/ripple/overlay/PeerSet.h | 10 +- src/ripple/overlay/impl/PeerSet.cpp | 9 +- src/ripple/server/impl/JSONRPCUtil.cpp | 9 +- src/ripple/websocket/Connection.h | 6 +- src/ripple/websocket/WebSocket02.cpp | 6 +- 26 files changed, 174 insertions(+), 166 deletions(-) delete mode 100644 src/ripple/basics/Time.h diff --git a/Builds/VisualStudio2015/RippleD.vcxproj b/Builds/VisualStudio2015/RippleD.vcxproj index 8d5b61cab1..222b9dbd10 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj +++ b/Builds/VisualStudio2015/RippleD.vcxproj @@ -1467,8 +1467,6 @@ - - diff --git a/Builds/VisualStudio2015/RippleD.vcxproj.filters b/Builds/VisualStudio2015/RippleD.vcxproj.filters index dabb74c7f2..8049896884 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2015/RippleD.vcxproj.filters @@ -1935,9 +1935,6 @@ ripple\basics - - ripple\basics - ripple\basics diff --git a/src/ripple/app/ledger/LedgerToJson.h b/src/ripple/app/ledger/LedgerToJson.h index 268e595183..78bf6a3e50 100644 --- a/src/ripple/app/ledger/LedgerToJson.h +++ b/src/ripple/app/ledger/LedgerToJson.h @@ -21,12 +21,10 @@ #define RIPPLE_APP_LEDGER_LEDGERTOJSON_H_INCLUDED #include -#include #include #include #include #include -#include namespace ripple { diff --git a/src/ripple/app/ledger/impl/InboundLedger.cpp b/src/ripple/app/ledger/impl/InboundLedger.cpp index dab9c604b6..771f33dbb3 100644 --- a/src/ripple/app/ledger/impl/InboundLedger.cpp +++ b/src/ripple/app/ledger/impl/InboundLedger.cpp @@ -37,6 +37,8 @@ namespace ripple { +using namespace std::chrono_literals; + enum { // Number of peers to start with @@ -45,9 +47,6 @@ enum // Number of peers to add on a timeout ,peerCountAdd = 2 - // millisecond for each ledger timeout - ,ledgerAcquireTimeoutMillis = 2500 - // how many timeouts before we giveup ,ledgerTimeoutRetriesMax = 10 @@ -64,9 +63,12 @@ enum ,reqNodes = 8 }; +// millisecond for each ledger timeout +auto constexpr ledgerAcquireTimeout = 2500ms; + InboundLedger::InboundLedger ( Application& app, uint256 const& hash, std::uint32_t seq, fcReason reason, clock_type& clock) - : PeerSet (app, hash, ledgerAcquireTimeoutMillis, false, clock, + : PeerSet (app, hash, ledgerAcquireTimeout, false, clock, app.journal("InboundLedger")) , mHaveHeader (false) , mHaveState (false) diff --git a/src/ripple/app/ledger/impl/LedgerToJson.cpp b/src/ripple/app/ledger/impl/LedgerToJson.cpp index 44cde28e99..dbdec22236 100644 --- a/src/ripple/app/ledger/impl/LedgerToJson.cpp +++ b/src/ripple/app/ledger/impl/LedgerToJson.cpp @@ -74,8 +74,7 @@ void fillJson(Object& json, bool closed, LedgerInfo const& info, bool bFull) if (info.closeTime != NetClock::time_point{}) { - json[jss::close_time_human] = boost::posix_time::to_simple_string ( - ptFromSeconds (info.closeTime.time_since_epoch().count())); + json[jss::close_time_human] = to_string(info.closeTime); if (! getCloseAgree(info)) json[jss::close_time_estimated] = true; } diff --git a/src/ripple/app/ledger/impl/TransactionAcquire.cpp b/src/ripple/app/ledger/impl/TransactionAcquire.cpp index 81f12e9cda..11a2f0e975 100644 --- a/src/ripple/app/ledger/impl/TransactionAcquire.cpp +++ b/src/ripple/app/ledger/impl/TransactionAcquire.cpp @@ -30,11 +30,13 @@ namespace ripple { +using namespace std::chrono_literals; + +// Timeout interval in milliseconds +auto constexpr TX_ACQUIRE_TIMEOUT = 250ms; + enum { - // Timeout interval in milliseconds - TX_ACQUIRE_TIMEOUT = 250, - NORM_TIMEOUTS = 4, MAX_TIMEOUTS = 20, }; diff --git a/src/ripple/app/misc/NetworkOPs.cpp b/src/ripple/app/misc/NetworkOPs.cpp index 9323b96535..09117afff9 100644 --- a/src/ripple/app/misc/NetworkOPs.cpp +++ b/src/ripple/app/misc/NetworkOPs.cpp @@ -45,7 +45,6 @@ #include #include #include -#include #include #include #include diff --git a/src/ripple/app/misc/NetworkOPs.h b/src/ripple/app/misc/NetworkOPs.h index f0f7154782..c6da2f2112 100644 --- a/src/ripple/app/misc/NetworkOPs.h +++ b/src/ripple/app/misc/NetworkOPs.h @@ -28,7 +28,6 @@ #include #include #include -#include #include #include diff --git a/src/ripple/basics/Time.h b/src/ripple/basics/Time.h deleted file mode 100644 index 20688d60d9..0000000000 --- a/src/ripple/basics/Time.h +++ /dev/null @@ -1,34 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of rippled: https://github.com/ripple/rippled - Copyright (c) 2012, 2013 Ripple Labs Inc. - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef RIPPLE_BASICS_TIME_H_INCLUDED -#define RIPPLE_BASICS_TIME_H_INCLUDED - -#include - -namespace ripple { - -// VFALCO TODO Roll this into some utilities header -int iToSeconds (boost::posix_time::ptime ptWhen); -boost::posix_time::ptime ptFromSeconds (int iSeconds); -uint64_t utFromSeconds (int iSeconds); - -} // ripple - -#endif diff --git a/src/ripple/basics/chrono.h b/src/ripple/basics/chrono.h index 198fdce596..de3160110a 100644 --- a/src/ripple/basics/chrono.h +++ b/src/ripple/basics/chrono.h @@ -25,6 +25,7 @@ #include #include #include +#include namespace ripple { @@ -55,6 +56,9 @@ public: static bool const is_steady = false; }; +std::string to_string(NetClock::time_point tp); +std::string to_string(std::chrono::system_clock::time_point tp); + /** A clock for measuring elapsed time. The epoch is unspecified. diff --git a/src/ripple/basics/impl/Log.cpp b/src/ripple/basics/impl/Log.cpp index 693e11e1b9..13f8b91726 100644 --- a/src/ripple/basics/impl/Log.cpp +++ b/src/ripple/basics/impl/Log.cpp @@ -18,11 +18,11 @@ //============================================================================== #include +#include #include #include -// VFALCO TODO Use std::chrono -#include #include +#include #include namespace ripple { @@ -311,8 +311,7 @@ Logs::format (std::string& output, std::string const& message, { output.reserve (message.size() + partition.size() + 100); - output = boost::posix_time::to_simple_string ( - boost::posix_time::second_clock::universal_time ()); + output = to_string(std::chrono::system_clock::now()); output += " "; if (! partition.empty ()) diff --git a/src/ripple/basics/impl/Time.cpp b/src/ripple/basics/impl/Time.cpp index af9133b4de..93f3c3eab2 100644 --- a/src/ripple/basics/impl/Time.cpp +++ b/src/ripple/basics/impl/Time.cpp @@ -18,47 +18,66 @@ //============================================================================== #include -#include +#include +#include +#include +#include +#include namespace ripple { -// VFALCO TODO Tidy this up into a RippleTime object - -// -// Time support -// We have our own epoch. -// - -boost::posix_time::ptime ptEpoch () +static +std::tuple +civil_from_days(int z) noexcept { - return boost::posix_time::ptime (boost::gregorian::date (2000, boost::gregorian::Jan, 1)); + z += 719468; + const int era = (z >= 0 ? z : z - 146096) / 146097; + const unsigned doe = static_cast(z - era * 146097); // [0, 146096] + const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399] + const int y = static_cast(yoe) + era * 400; + const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365] + const unsigned mp = (5*doy + 2)/153; // [0, 11] + const unsigned d = doy - (153*mp+2)/5 + 1; // [1, 31] + const unsigned m = mp + (mp < 10 ? 3 : -9); // [1, 12] + return std::tuple(y + (m <= 2), m, d); } -int iToSeconds (boost::posix_time::ptime ptWhen) +std::string +to_string(std::chrono::system_clock::time_point tp) { - return ptWhen.is_not_a_date_time () - ? -1 - : (ptWhen - ptEpoch ()).total_seconds (); + const char* months[] = + { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + }; + using namespace std::chrono; + auto s = floor(tp.time_since_epoch()); + auto sd = floor(s); // number of days + s -= sd; // time of day in seconds + auto h = floor(s); + s -= h; + auto m = floor(s); + s -= m; + int y; + unsigned mn, d; + std::tie(y, mn, d) = civil_from_days(static_cast(sd.count())); + // Date-time in y/mn/d h:m:s + std::ostringstream str; + str.fill('0'); + str.flags(std::ios::dec | std::ios::right); + using std::setw; + str << y << '-' << months[mn-1] << '-' << setw(2) << d << ' ' + << setw(2) << h.count() << ':' + << setw(2) << m.count() << ':' + << setw(2) << s.count(); + return str.str(); } -// Convert our time in seconds to a ptime. -boost::posix_time::ptime ptFromSeconds (int iSeconds) +std::string +to_string(NetClock::time_point tp) { - return iSeconds < 0 - ? boost::posix_time::ptime (boost::posix_time::not_a_date_time) - : ptEpoch () + boost::posix_time::seconds (iSeconds); -} - -// Convert from our time to UNIX time in seconds. -uint64_t utFromSeconds (int iSeconds) -{ - boost::posix_time::time_duration tdDelta = - boost::posix_time::ptime (boost::gregorian::date (2000, boost::gregorian::Jan, 1)) - - boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1)) - + boost::posix_time::seconds (iSeconds) - ; - - return tdDelta.total_seconds (); + using namespace std::chrono; + return to_string(system_clock::time_point{tp.time_since_epoch() + 946684800s}); } } // ripple diff --git a/src/ripple/beast/asio/io_latency_probe.h b/src/ripple/beast/asio/io_latency_probe.h index 2ecd25248e..a7e934b080 100644 --- a/src/ripple/beast/asio/io_latency_probe.h +++ b/src/ripple/beast/asio/io_latency_probe.h @@ -20,7 +20,7 @@ #ifndef BEAST_ASIO_IO_LATENCY_PROBE_H_INCLUDED #define BEAST_ASIO_IO_LATENCY_PROBE_H_INCLUDED -#include +#include #include #include #include @@ -43,7 +43,7 @@ private: std::size_t m_count; duration const m_period; boost::asio::io_service& m_ios; - boost::asio::deadline_timer m_timer; + boost::asio::basic_waitable_timer m_timer; bool m_cancel; public: @@ -219,11 +219,7 @@ private: } else { - boost::posix_time::microseconds mms ( - std::chrono::duration_cast < - std::chrono::microseconds> ( - when - now).count ()); - m_probe->m_timer.expires_from_now (mms); + m_probe->m_timer.expires_from_now(when - now); m_probe->m_timer.async_wait (sample_op ( m_handler, now, m_repeat, m_probe)); } diff --git a/src/ripple/beast/clock/chrono_util.h b/src/ripple/beast/clock/chrono_util.h index 1adc676a4c..4a2c4f21cb 100644 --- a/src/ripple/beast/clock/chrono_util.h +++ b/src/ripple/beast/clock/chrono_util.h @@ -20,6 +20,8 @@ #ifndef BEAST_CHRONO_CHRONO_UTIL_H_INCLUDED #define BEAST_CHRONO_CHRONO_UTIL_H_INCLUDED +#include + // From Howard Hinnant // http://home.roadrunner.com/~hinnant/duration_io/chrono_util.html diff --git a/src/ripple/beast/insight/impl/StatsDCollector.cpp b/src/ripple/beast/insight/impl/StatsDCollector.cpp index f4fc6605c3..e8a0cd6c1c 100644 --- a/src/ripple/beast/insight/impl/StatsDCollector.cpp +++ b/src/ripple/beast/insight/impl/StatsDCollector.cpp @@ -204,7 +204,7 @@ private: boost::asio::io_service m_io_service; boost::optional m_work; boost::asio::io_service::strand m_strand; - boost::asio::deadline_timer m_timer; + boost::asio::basic_waitable_timer m_timer; boost::asio::ip::udp::socket m_socket; std::deque m_data; std::recursive_mutex metricsLock_; @@ -407,7 +407,8 @@ public: void set_timer () { - m_timer.expires_from_now (boost::posix_time::seconds (1)); + using namespace std::chrono_literals; + m_timer.expires_from_now(1s); m_timer.async_wait (std::bind ( &StatsDCollectorImp::on_timer, this, beast::asio::placeholders::error)); diff --git a/src/ripple/core/Config.h b/src/ripple/core/Config.h index 7358cf6a15..5fd0de9c69 100644 --- a/src/ripple/core/Config.h +++ b/src/ripple/core/Config.h @@ -42,6 +42,8 @@ namespace ripple { +using namespace std::chrono_literals; + class Rules; //------------------------------------------------------------------------------ @@ -144,7 +146,7 @@ public: bool PEER_PRIVATE = false; // True to ask peers not to relay current IP. int PEERS_MAX = 0; - int WEBSOCKET_PING_FREQ = 5 * 60; + std::chrono::seconds WEBSOCKET_PING_FREQ = 5min; // RPC parameters Json::Value RPC_STARTUP; diff --git a/src/ripple/core/impl/Config.cpp b/src/ripple/core/impl/Config.cpp index 9943ebd5b4..261d64f8a3 100644 --- a/src/ripple/core/impl/Config.cpp +++ b/src/ripple/core/impl/Config.cpp @@ -351,7 +351,7 @@ void Config::loadFromString (std::string const& fileContents) ELB_SUPPORT = beast::lexicalCastThrow (strTemp); if (getSingleSection (secConfig, SECTION_WEBSOCKET_PING_FREQ, strTemp, j_)) - WEBSOCKET_PING_FREQ = beast::lexicalCastThrow (strTemp); + WEBSOCKET_PING_FREQ = std::chrono::seconds{beast::lexicalCastThrow (strTemp)}; getSingleSection (secConfig, SECTION_SSL_VERIFY_FILE, SSL_VERIFY_FILE, j_); getSingleSection (secConfig, SECTION_SSL_VERIFY_DIR, SSL_VERIFY_DIR, j_); diff --git a/src/ripple/core/impl/SNTPClock.cpp b/src/ripple/core/impl/SNTPClock.cpp index 25e4015f32..d0ca946b83 100644 --- a/src/ripple/core/impl/SNTPClock.cpp +++ b/src/ripple/core/impl/SNTPClock.cpp @@ -40,20 +40,21 @@ namespace ripple { static uint8_t SNTPQueryData[48] = { 0x1B, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +using namespace std::chrono_literals; // NTP query frequency - 4 minutes -#define NTP_QUERY_FREQUENCY (4 * 60) +auto constexpr NTP_QUERY_FREQUENCY = 4min; // NTP minimum interval to query same servers - 3 minutes -#define NTP_MIN_QUERY (3 * 60) +auto constexpr NTP_MIN_QUERY = 3min; // NTP sample window (should be odd) #define NTP_SAMPLE_WINDOW 9 // NTP timestamp constant -#define NTP_UNIX_OFFSET 0x83AA7E80 +auto constexpr NTP_UNIX_OFFSET = 0x83AA7E80s; // NTP timestamp validity -#define NTP_TIMESTAMP_VALID ((NTP_QUERY_FREQUENCY + NTP_MIN_QUERY) * 2) +auto constexpr NTP_TIMESTAMP_VALID = (NTP_QUERY_FREQUENCY + NTP_MIN_QUERY) * 2; // SNTP packet offsets #define NTP_OFF_INFO 0 @@ -73,13 +74,18 @@ class SNTPClientImp : public SNTPClock { private: + template + using sys_time = std::chrono::time_point; + + using sys_seconds = sys_time; + struct Query { bool replied; - time_t sent; // VFALCO time_t, really? + sys_seconds sent; std::uint32_t nonce; - Query (time_t j = time_t(-1)) + Query (sys_seconds j = sys_seconds::max()) : replied (false) , sent (j) { @@ -95,12 +101,12 @@ private: std::map queries_; boost::asio::ip::udp::socket socket_; - boost::asio::deadline_timer timer_; + boost::asio::basic_waitable_timer timer_; boost::asio::ip::udp::resolver resolver_; - std::vector> servers_; - int offset_; - time_t lastUpdate_; - std::deque offsets_; + std::vector> servers_; + std::chrono::seconds offset_; + sys_seconds lastUpdate_; + std::deque offsets_; std::vector buf_; boost::asio::ip::udp::endpoint ep_; @@ -115,7 +121,7 @@ public: , timer_ (io_service_) , resolver_ (io_service_) , offset_ (0) - , lastUpdate_ (time_t(-1)) + , lastUpdate_ (sys_seconds::max()) , buf_ (256) { } @@ -150,7 +156,7 @@ public: std::lock_guard lock (mutex_); for (auto const& item : servers) servers_.emplace_back( - item, time_t(-1)); + item, sys_seconds::max()); } queryAll(); @@ -161,8 +167,7 @@ public: &SNTPClientImp::onRead, this, beast::asio::placeholders::error, beast::asio::placeholders::bytes_transferred)); - timer_.expires_from_now( - boost::posix_time::seconds(NTP_QUERY_FREQUENCY)); + timer_.expires_from_now(NTP_QUERY_FREQUENCY); timer_.async_wait(std::bind( &SNTPClientImp::onTimer, this, beast::asio::placeholders::error)); @@ -177,18 +182,20 @@ public: now() const override { std::lock_guard lock (mutex_); - auto const when = clock_type::now(); - if ((lastUpdate_ == (time_t)-1) || - ((lastUpdate_ + NTP_TIMESTAMP_VALID) < time(nullptr))) + using namespace std::chrono; + auto const when = time_point_cast(clock_type::now()); + if ((lastUpdate_ == sys_seconds::max()) || + ((lastUpdate_ + NTP_TIMESTAMP_VALID) < + time_point_cast(clock_type::now()))) return when; - return when + std::chrono::seconds(offset_); + return when + offset_; } duration offset() const override { std::lock_guard lock (mutex_); - return std::chrono::seconds(offset_); + return offset_; } //-------------------------------------------------------------------------- @@ -213,8 +220,7 @@ public: } doQuery (); - timer_.expires_from_now( - boost::posix_time::seconds (NTP_QUERY_FREQUENCY)); + timer_.expires_from_now(NTP_QUERY_FREQUENCY); timer_.async_wait(std::bind( &SNTPClientImp::onTimer, this, beast::asio::placeholders::error)); @@ -224,6 +230,7 @@ public: onRead (error_code const& ec, std::size_t bytes_xferd) { using namespace boost::asio; + using namespace std::chrono; if (ec == error::operation_aborted) return; @@ -253,7 +260,7 @@ public: { query->second.replied = true; - if (time (nullptr) > (query->second.sent + 1)) + if (time_point_cast(clock_type::now()) > (query->second.sent + 1s)) { JLOG(j_.warn()) << "SNTP: Late response from " << ep_; @@ -289,7 +296,7 @@ public: void addServer (std::string const& server) { std::lock_guard lock (mutex_); - servers_.push_back (std::make_pair (server, time_t(-1))); + servers_.push_back (std::make_pair (server, sys_seconds::max())); } void queryAll () @@ -302,11 +309,12 @@ public: bool doQuery () { std::lock_guard lock (mutex_); - std::vector< std::pair >::iterator best = servers_.end (); + auto best = servers_.end (); for (auto iter = servers_.begin (), end = best; iter != end; ++iter) - if ((best == end) || (iter->second == time_t(-1)) || (iter->second < best->second)) + if ((best == end) || (iter->second == sys_seconds::max()) || + (iter->second < best->second)) best = iter; if (best == servers_.end ()) @@ -316,9 +324,10 @@ public: return false; } - time_t now = time (nullptr); + using namespace std::chrono; + auto now = time_point_cast(clock_type::now()); - if ((best->second != time_t(-1)) && ((best->second + NTP_MIN_QUERY) >= now)) + if ((best->second != sys_seconds::max()) && ((best->second + NTP_MIN_QUERY) >= now)) { JLOG(j_.trace()) << "SNTP: All servers recently queried"; @@ -366,9 +375,10 @@ public: { std::lock_guard lock (mutex_); Query& query = queries_[*sel]; - time_t now = time (nullptr); + using namespace std::chrono; + auto now = time_point_cast(clock_type::now()); - if ((query.sent == now) || ((query.sent + 1) == now)) + if ((query.sent == now) || ((query.sent + 1s) == now)) { // This can happen if the same IP address is reached through multiple names JLOG(j_.trace()) << @@ -379,7 +389,11 @@ public: query.replied = false; query.sent = now; query.nonce = rand_int(); - reinterpret_cast (SNTPQueryData)[NTP_OFF_XMITTS_INT] = static_cast (time (nullptr)) + NTP_UNIX_OFFSET; + // The following line of code will overflow at 2036-02-07 06:28:16 UTC + // due to the 32 bit cast. + reinterpret_cast (SNTPQueryData)[NTP_OFF_XMITTS_INT] = + static_cast((time_point_cast(clock_type::now()) + + NTP_UNIX_OFFSET).time_since_epoch().count()); reinterpret_cast (SNTPQueryData)[NTP_OFF_XMITTS_FRAC] = query.nonce; socket_.async_send_to(buffer(SNTPQueryData, 48), *sel, std::bind (&SNTPClientImp::onSend, this, @@ -403,11 +417,12 @@ public: void processReply () { + using namespace std::chrono; assert (buf_.size () >= 48); std::uint32_t* recvBuffer = reinterpret_cast (&buf_.front ()); unsigned info = ntohl (recvBuffer[NTP_OFF_INFO]); - int64_t timev = ntohl (recvBuffer[NTP_OFF_RECVTS_INT]); + auto timev = seconds{ntohl(recvBuffer[NTP_OFF_RECVTS_INT])}; unsigned stratum = (info >> 16) & 0xff; if ((info >> 30) == 3) @@ -424,8 +439,9 @@ public: return; } - std::int64_t now = static_cast (time (nullptr)); - timev -= now; + using namespace std::chrono; + auto now = time_point_cast(clock_type::now()); + timev -= now.time_since_epoch(); timev -= NTP_UNIX_OFFSET; // add offset to list, replacing oldest one if appropriate @@ -448,13 +464,13 @@ public: // debounce: small corrections likely // do more harm than good - if ((offset_ == -1) || (offset_ == 1)) - offset_ = 0; + if ((offset_ == -1s) || (offset_ == 1s)) + offset_ = 0s; - if (timev || offset_) + if (timev != 0s || offset_ != 0s) { - JLOG(j_.trace()) << "SNTP: Offset is " << timev << - ", new system offset is " << offset_; + JLOG(j_.trace()) << "SNTP: Offset is " << timev.count() << + ", new system offset is " << offset_.count(); } } }; diff --git a/src/ripple/net/HTTPClient.h b/src/ripple/net/HTTPClient.h index 579cfb22a5..5ce74f5a74 100644 --- a/src/ripple/net/HTTPClient.h +++ b/src/ripple/net/HTTPClient.h @@ -23,7 +23,7 @@ #include #include #include -#include +#include namespace ripple { @@ -46,7 +46,7 @@ public: const unsigned short port, std::string const& strPath, std::size_t responseMax, - boost::posix_time::time_duration timeout, + std::chrono::seconds timeout, std::function complete, Logs& l); @@ -57,7 +57,7 @@ public: const unsigned short port, std::string const& strPath, std::size_t responseMax, - boost::posix_time::time_duration timeout, + std::chrono::seconds timeout, std::function complete, Logs& l); @@ -68,7 +68,7 @@ public: const unsigned short port, std::function build, std::size_t responseMax, - boost::posix_time::time_duration timeout, + std::chrono::seconds timeout, std::function complete, Logs& l); }; diff --git a/src/ripple/net/impl/HTTPClient.cpp b/src/ripple/net/impl/HTTPClient.cpp index eb30d37cc9..6797d70c12 100644 --- a/src/ripple/net/impl/HTTPClient.cpp +++ b/src/ripple/net/impl/HTTPClient.cpp @@ -138,7 +138,7 @@ public: bool bSSL, std::deque deqSites, std::function build, - boost::posix_time::time_duration timeout, + std::chrono::seconds timeout, std::function complete) { @@ -157,7 +157,7 @@ public: bool bSSL, std::deque deqSites, std::string const& strPath, - boost::posix_time::time_duration timeout, + std::chrono::seconds timeout, std::function complete) { @@ -518,13 +518,13 @@ private: std::function mBuild; std::function mComplete; - boost::asio::deadline_timer mDeadline; + boost::asio::basic_waitable_timer mDeadline; // If not success, we are shutting down. boost::system::error_code mShutdown; std::deque mDeqSites; - boost::posix_time::time_duration mTimeout; + std::chrono::seconds mTimeout; beast::Journal j_; }; @@ -537,7 +537,7 @@ void HTTPClient::get ( const unsigned short port, std::string const& strPath, std::size_t responseMax, - boost::posix_time::time_duration timeout, + std::chrono::seconds timeout, std::function complete, Logs& l) @@ -554,7 +554,7 @@ void HTTPClient::get ( const unsigned short port, std::string const& strPath, std::size_t responseMax, - boost::posix_time::time_duration timeout, + std::chrono::seconds timeout, std::function complete, Logs& l) @@ -573,7 +573,7 @@ void HTTPClient::request ( const unsigned short port, std::function setRequest, std::size_t responseMax, - boost::posix_time::time_duration timeout, + std::chrono::seconds timeout, std::function complete, Logs& l) diff --git a/src/ripple/net/impl/RPCCall.cpp b/src/ripple/net/impl/RPCCall.cpp index 2142480e88..57d7d624c8 100644 --- a/src/ripple/net/impl/RPCCall.cpp +++ b/src/ripple/net/impl/RPCCall.cpp @@ -1307,7 +1307,8 @@ void fromNetwork ( // Send request const int RPC_REPLY_MAX_BYTES (256*1024*1024); - const int RPC_NOTIFY_SECONDS (600); + using namespace std::chrono_literals; + auto constexpr RPC_NOTIFY = 10min; auto j = logs.journal ("HTTPClient"); @@ -1323,7 +1324,7 @@ void fromNetwork ( mapRequestHeaders, strPath, std::placeholders::_1, std::placeholders::_2, j), RPC_REPLY_MAX_BYTES, - boost::posix_time::seconds (RPC_NOTIFY_SECONDS), + RPC_NOTIFY, std::bind (&RPCCallImp::onResponse, callbackFuncP, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, j), diff --git a/src/ripple/overlay/PeerSet.h b/src/ripple/overlay/PeerSet.h index e529d9b811..8dc27d6a43 100644 --- a/src/ripple/overlay/PeerSet.h +++ b/src/ripple/overlay/PeerSet.h @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include namespace ripple { @@ -119,8 +119,8 @@ private: protected: using ScopedLockType = std::unique_lock ; - PeerSet (Application& app, uint256 const& hash, int interval, bool txnData, - clock_type& clock, beast::Journal journal); + PeerSet (Application& app, uint256 const& hash, std::chrono::milliseconds interval, + bool txnData, clock_type& clock, beast::Journal journal); virtual ~PeerSet() = 0; @@ -162,7 +162,7 @@ protected: std::recursive_mutex mLock; uint256 mHash; - int mTimerInterval; + std::chrono::milliseconds mTimerInterval; int mTimeouts; bool mComplete; bool mFailed; @@ -171,7 +171,7 @@ protected: bool mProgress; // VFALCO TODO move the responsibility for the timer to a higher level - boost::asio::deadline_timer mTimer; + boost::asio::basic_waitable_timer mTimer; // VFALCO TODO Verify that these are used in the way that the names suggest. using PeerIdentifier = Peer::id_t; diff --git a/src/ripple/overlay/impl/PeerSet.cpp b/src/ripple/overlay/impl/PeerSet.cpp index aa82b9463c..c174f7cd5c 100644 --- a/src/ripple/overlay/impl/PeerSet.cpp +++ b/src/ripple/overlay/impl/PeerSet.cpp @@ -26,6 +26,8 @@ namespace ripple { +using namespace std::chrono_literals; + class InboundLedger; // VFALCO NOTE The txnData constructor parameter is a code smell. @@ -35,7 +37,8 @@ class InboundLedger; // derived class. Why not just make the timer callback // function pure virtual? // -PeerSet::PeerSet (Application& app, uint256 const& hash, int interval, bool txnData, +PeerSet::PeerSet (Application& app, uint256 const& hash, + std::chrono::milliseconds interval, bool txnData, clock_type& clock, beast::Journal journal) : app_ (app) , m_journal (journal) @@ -50,7 +53,7 @@ PeerSet::PeerSet (Application& app, uint256 const& hash, int interval, bool txnD , mTimer (app_.getIOService ()) { mLastAction = m_clock.now(); - assert ((mTimerInterval > 10) && (mTimerInterval < 30000)); + assert ((mTimerInterval > 10ms) && (mTimerInterval < 30s)); } PeerSet::~PeerSet () @@ -70,7 +73,7 @@ bool PeerSet::insert (Peer::ptr const& ptr) void PeerSet::setTimer () { - mTimer.expires_from_now (boost::posix_time::milliseconds (mTimerInterval)); + mTimer.expires_from_now(mTimerInterval); mTimer.async_wait (std::bind (&PeerSet::timerEntry, pmDowncast (), beast::asio::placeholders::error, m_journal)); } diff --git a/src/ripple/server/impl/JSONRPCUtil.cpp b/src/ripple/server/impl/JSONRPCUtil.cpp index c96e7f5180..d0c7d57df8 100644 --- a/src/ripple/server/impl/JSONRPCUtil.cpp +++ b/src/ripple/server/impl/JSONRPCUtil.cpp @@ -36,10 +36,15 @@ std::string getHTTPHeaderTimestamp () char buffer[96]; time_t now; time (&now); - struct tm* now_gmt = gmtime (&now); + struct tm now_gmt{}; +#ifndef _MSC_VER + gmtime_r(&now, &now_gmt); +#else + gmtime_s(&now_gmt, &now); +#endif strftime (buffer, sizeof (buffer), "Date: %a, %d %b %Y %H:%M:%S +0000\r\n", - now_gmt); + &now_gmt); return std::string (buffer); } diff --git a/src/ripple/websocket/Connection.h b/src/ripple/websocket/Connection.h index 137e8e8184..35c5efceec 100644 --- a/src/ripple/websocket/Connection.h +++ b/src/ripple/websocket/Connection.h @@ -113,7 +113,7 @@ private: std::deque m_receiveQueue; NetworkOPs& m_netOPs; boost::asio::io_service& m_io_service; - boost::asio::deadline_timer m_pingTimer; + boost::asio::basic_waitable_timer m_pingTimer; bool m_sentPing = false; bool m_receiveQueueRunning = false; @@ -122,7 +122,7 @@ private: handler_type& m_handler; weak_connection_ptr m_connection; - int pingFreq_; + std::chrono::seconds pingFreq_; beast::Journal j_; }; @@ -155,7 +155,7 @@ ConnectionImpl ::ConnectionImpl ( , j_ (app.journal ("ConnectionImpl")) { // VFALCO Disabled since it might cause hangs - pingFreq_ = 0; + pingFreq_ = std::chrono::seconds{0}; if (! m_forwardedFor.empty() || ! m_user.empty()) { diff --git a/src/ripple/websocket/WebSocket02.cpp b/src/ripple/websocket/WebSocket02.cpp index 2d90206082..d7a23e423b 100644 --- a/src/ripple/websocket/WebSocket02.cpp +++ b/src/ripple/websocket/WebSocket02.cpp @@ -74,13 +74,13 @@ boost::asio::io_service::strand& WebSocket02::getStrand (Connection& con) template <> void ConnectionImpl ::setPingTimer () { - if (pingFreq_ <= 0) + using namespace std::chrono_literals; + if (pingFreq_ <= 0s) return; connection_ptr ptr = m_connection.lock (); if (ptr) { - this->m_pingTimer.expires_from_now ( - boost::posix_time::seconds (pingFreq_)); + this->m_pingTimer.expires_from_now(pingFreq_); this->m_pingTimer.async_wait ( ptr->get_strand ().wrap (