25#include <fmt/format.h>
43using NetId = uint32_t;
44using want_t = int_fast8_t;
46OPENDHT_PUBLIC
const char* version();
50using Sp = std::shared_ptr<T>;
52template<
typename Key,
typename Item,
typename Condition>
54erase_if(std::map<Key, Item>& map,
const Condition& condition)
56 for (
auto it = map.begin(); it != map.end();) {
68OPENDHT_PUBLIC std::pair<std::string, std::string>
splitPort(std::string_view s);
70class OPENDHT_PUBLIC DhtException :
public std::runtime_error
73 DhtException(
const std::string& str =
"")
74 : std::runtime_error(
"DhtException occurred: " + str)
78class OPENDHT_PUBLIC SocketException :
public DhtException
81 SocketException(
int err)
82 : DhtException(strerror(err))
88using clock = std::chrono::steady_clock;
89using system_clock = std::chrono::system_clock;
90using time_point = clock::time_point;
91using duration = clock::duration;
93time_point from_time_t(std::time_t t);
94std::time_t to_time_t(time_point t);
100 if (d < std::chrono::seconds(0)) {
101 return "-" + print_duration(-d);
102 }
else if (d < std::chrono::milliseconds(1)) {
103 return fmt::format(
"{:.3g} us",
104 std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(d).count());
105 }
else if (d < std::chrono::seconds(1)) {
106 return fmt::format(
"{:.3g} ms",
107 std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(d).count());
108 }
else if (d < std::chrono::minutes(1)) {
109 return fmt::format(
"{:.3g} s", std::chrono::duration_cast<std::chrono::duration<double>>(d).count());
110 }
else if (d < std::chrono::hours(1)) {
111 return fmt::format(
"{:.3g} min",
112 std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<60>>>(d).count());
113 }
else if (d < std::chrono::hours(72)) {
114 return fmt::format(
"{:.3g} h",
115 std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<3600>>>(d).count());
117 return fmt::format(
"{:.3g} days",
118 std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<86400>>>(d).count());
122template<
class TimePo
int>
124print_time_relative(TimePoint now, TimePoint d)
126 if (d == TimePoint::min())
130 return (d > now) ? std::string(
"in ") + print_duration(d - now) : print_duration(now - d) + std::string(
" ago");
133template<
typename Duration = duration>
134class uniform_duration_distribution :
public std::uniform_int_distribution<typename Duration::rep>
136 using Base = std::uniform_int_distribution<typename Duration::rep>;
137 using param_type =
typename Base::param_type;
140 uniform_duration_distribution(Duration min, Duration max)
141 : Base(min.count(), max.count())
143 template<
class Generator>
144 Duration operator()(Generator&& g)
146 return Duration(Base::operator()(g));
148 template<
class Generator>
149 Duration operator()(Generator&& g,
const param_type& params)
151 return Duration(Base::operator()(g, params));
160using Blob = std::vector<uint8_t>;
167template<
typename Type>
169packMsg(
const Type& t)
171 msgpack::sbuffer buffer;
172 msgpack::packer<msgpack::sbuffer> pk(&buffer);
174 return {buffer.data(), buffer.data() + buffer.size()};
177template<
typename Type>
181 msgpack::unpacked msg_res = msgpack::unpack((
const char*) b.data(), b.size());
182 return msg_res.get().as<Type>();
185msgpack::unpacked unpackMsg(
Blob b);
187msgpack::object* findMapValue(
const msgpack::object& map,
const char* key,
size_t length);
189inline msgpack::object*
190findMapValue(
const msgpack::object& map,
const char* key)
192 return findMapValue(map, key, strlen(key));
194inline msgpack::object*
195findMapValue(
const msgpack::object& map, std::string_view key)
197 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)