27using NetId = uint32_t;
28using want_t = int_fast8_t;
30OPENDHT_PUBLIC
const char* version();
34using Sp = std::shared_ptr<T>;
36template<
typename Key,
typename Item,
typename Condition>
38erase_if(std::map<Key, Item>& map,
const Condition& condition)
40 for (
auto it = map.begin(); it != map.end();) {
49template<
typename... Args>
53 static_assert((std::is_constructible_v<std::string_view, Args&&> && ...));
55 s.reserve((std::string_view {args}.size() + ...));
56 (s.append(std::forward<Args>(args)), ...);
63OPENDHT_PUBLIC std::pair<std::string_view, std::string_view>
splitPort(std::string_view s);
65class OPENDHT_PUBLIC DhtException :
public std::runtime_error
68 DhtException(
const std::string& str =
"")
69 : std::runtime_error(
"DhtException occurred: " + str)
73class OPENDHT_PUBLIC SocketException :
public DhtException
76 SocketException(
int err)
77 : DhtException(strerror(err))
83using clock = std::chrono::steady_clock;
84using system_clock = std::chrono::system_clock;
85using time_point = clock::time_point;
86using duration = clock::duration;
88OPENDHT_PUBLIC time_point from_system_time(system_clock::time_point t,
89 time_point steady_now,
90 system_clock::time_point system_now);
91OPENDHT_PUBLIC time_point from_system_time(system_clock::time_point t);
92OPENDHT_PUBLIC system_clock::time_point to_system_time(time_point t,
93 time_point steady_now,
94 system_clock::time_point system_now);
95OPENDHT_PUBLIC system_clock::time_point to_system_time(time_point t);
96OPENDHT_PUBLIC time_point from_time_t(std::time_t t);
97OPENDHT_PUBLIC std::time_t to_time_t(time_point t);
103 if (d < std::chrono::seconds(0)) {
104 return "-" + print_duration(-d);
105 }
else if (d < std::chrono::milliseconds(1)) {
106 return fmt::format(
"{:.3g} us",
107 std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(d).count());
108 }
else if (d < std::chrono::seconds(1)) {
109 return fmt::format(
"{:.3g} ms",
110 std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(d).count());
111 }
else if (d < std::chrono::minutes(1)) {
112 return fmt::format(
"{:.3g} s", std::chrono::duration_cast<std::chrono::duration<double>>(d).count());
113 }
else if (d < std::chrono::hours(1)) {
114 return fmt::format(
"{:.3g} min",
115 std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<60>>>(d).count());
116 }
else if (d < std::chrono::hours(72)) {
117 return fmt::format(
"{:.3g} h",
118 std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<3600>>>(d).count());
120 return fmt::format(
"{:.3g} days",
121 std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<86400>>>(d).count());
125template<
class TimePo
int>
127print_time_relative(TimePoint now, TimePoint d)
129 using namespace std::literals;
130 if (d == TimePoint::min())
134 return (d > now) ? concat(
"in "sv, print_duration(d - now)) : concat(print_duration(now - d),
" ago"sv);
137OPENDHT_PUBLIC std::string printByteCount(
size_t bytes);
139template<
typename Duration = duration>
140class uniform_duration_distribution :
public std::uniform_int_distribution<typename Duration::rep>
142 using Base = std::uniform_int_distribution<typename Duration::rep>;
143 using param_type =
typename Base::param_type;
146 uniform_duration_distribution(Duration min, Duration max)
147 : Base(min.count(), max.count())
149 template<
class Generator>
150 Duration operator()(Generator&& g)
152 return Duration(Base::operator()(g));
154 template<
class Generator>
155 Duration operator()(Generator&& g,
const param_type& params)
157 return Duration(Base::operator()(g, params));
166using Blob = std::vector<uint8_t>;
173template<
typename Type>
175packMsg(
const Type& t)
177 msgpack::sbuffer buffer;
178 msgpack::packer<msgpack::sbuffer> pk(&buffer);
180 return {buffer.data(), buffer.data() + buffer.size()};
183template<
typename Type>
185unpackMsg(
const Blob& b)
187 msgpack::unpacked msg_res = msgpack::unpack((
const char*) b.data(), b.size());
188 return msg_res.get().as<Type>();
191inline msgpack::unpacked
192unpackMsg(
const Blob& b)
194 return msgpack::unpack((
const char*) b.data(), b.size());
197OPENDHT_PUBLIC msgpack::object* findMapValue(
const msgpack::object& map,
const char* key,
size_t length);
199inline msgpack::object*
200findMapValue(
const msgpack::object& map,
const char* key)
202 return findMapValue(map, key, strlen(key));
204inline msgpack::object*
205findMapValue(
const msgpack::object& map, std::string_view key)
207 return findMapValue(map, key.data(), key.size());
OPENDHT_PUBLIC Blob unpackBlob(const msgpack::object &o)
OPENDHT_PUBLIC std::pair< std::string_view, std::string_view > splitPort(std::string_view s)
std::vector< uint8_t > Blob