24#include <fmt/format.h>
42using NetId = uint32_t;
43using want_t = int_fast8_t;
45OPENDHT_PUBLIC
const char* version();
49using Sp = std::shared_ptr<T>;
51template <
typename Key,
typename Item,
typename Condition>
52void erase_if(std::map<Key, Item>& map,
const Condition& condition)
54 for (
auto it = map.begin(); it != map.end(); ) {
64OPENDHT_PUBLIC std::pair<std::string, std::string>
67class OPENDHT_PUBLIC DhtException :
public std::runtime_error {
69 DhtException(
const std::string &str =
"") :
70 std::runtime_error(
"DhtException occurred: " + str) {}
73class OPENDHT_PUBLIC SocketException :
public DhtException {
75 SocketException(
int err) :
76 DhtException(strerror(err)) {}
81using clock = std::chrono::steady_clock;
82using system_clock = std::chrono::system_clock;
83using time_point = clock::time_point;
84using duration = clock::duration;
86time_point from_time_t(std::time_t t);
87std::time_t to_time_t(time_point t);
92 if (d < std::chrono::seconds(0)) {
93 return "-" + print_duration(-d);
94 }
else if (d < std::chrono::milliseconds(1)) {
95 return fmt::format(
"{:.3g} us", std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(d).count());
96 }
else if (d < std::chrono::seconds(1)) {
97 return fmt::format(
"{:.3g} ms", std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(d).count());
98 }
else if (d < std::chrono::minutes(1)) {
99 return fmt::format(
"{:.3g} s", std::chrono::duration_cast<std::chrono::duration<double>>(d).count());
100 }
else if (d < std::chrono::hours(1)) {
101 return fmt::format(
"{:.3g} min", std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<60>>>(d).count());
102 }
else if (d < std::chrono::hours(72)) {
103 return fmt::format(
"{:.3g} h", std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<3600>>>(d).count());
105 return fmt::format(
"{:.3g} days", std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<86400>>>(d).count());
109template <
class TimePo
int>
111print_time_relative(TimePoint now, TimePoint d) {
112 if (d == TimePoint::min())
return "never";
113 if (d == now)
return "now";
114 return (d > now) ? std::string(
"in ") + print_duration(d - now)
115 : print_duration(now - d) + std::string(
" ago");
118template <
typename Duration = duration>
119class uniform_duration_distribution :
public std::uniform_int_distribution<typename Duration::rep> {
120 using Base = std::uniform_int_distribution<typename Duration::rep>;
121 using param_type =
typename Base::param_type;
123 uniform_duration_distribution(Duration min, Duration max) : Base(min.count(), max.count()) {}
124 template <
class Generator>
125 Duration operator()(Generator && g) {
126 return Duration(Base::operator()(g));
128 template<
class Generator >
129 Duration operator()( Generator && g,
const param_type& params ) {
130 return Duration(Base::operator()(g, params));
139using Blob = std::vector<uint8_t>;
146template <
typename Type>
148packMsg(
const Type& t) {
149 msgpack::sbuffer buffer;
150 msgpack::packer<msgpack::sbuffer> pk(&buffer);
152 return {buffer.data(), buffer.data()+buffer.size()};
155template <
typename Type>
158 msgpack::unpacked msg_res = msgpack::unpack((
const char*)b.data(), b.size());
159 return msg_res.get().as<Type>();
162msgpack::unpacked unpackMsg(
Blob b);
164msgpack::object* findMapValue(
const msgpack::object& map,
const char* key,
size_t length);
166inline msgpack::object* findMapValue(
const msgpack::object& map,
const char* key) {
167 return findMapValue(map, key, strlen(key));
169inline msgpack::object* findMapValue(
const msgpack::object& map, std::string_view key) {
170 return findMapValue(map, key.data(), key.size());
OPENDHT_PUBLIC Blob unpackBlob(const msgpack::object &o)
std::vector< uint8_t > Blob
OPENDHT_PUBLIC std::pair< std::string, std::string > splitPort(std::string_view s)