24#include <fmt/format.h>
41using NetId = uint32_t;
42using want_t = int_fast8_t;
44OPENDHT_PUBLIC
const char* version();
48using Sp = std::shared_ptr<T>;
50template <
typename Key,
typename Item,
typename Condition>
51void erase_if(std::map<Key, Item>& map,
const Condition& condition)
53 for (
auto it = map.begin(); it != map.end(); ) {
63OPENDHT_PUBLIC std::pair<std::string, std::string>
66class OPENDHT_PUBLIC DhtException :
public std::runtime_error {
68 DhtException(
const std::string &str =
"") :
69 std::runtime_error(
"DhtException occurred: " + str) {}
72class OPENDHT_PUBLIC SocketException :
public DhtException {
74 SocketException(
int err) :
75 DhtException(strerror(err)) {}
80using clock = std::chrono::steady_clock;
81using system_clock = std::chrono::system_clock;
82using time_point = clock::time_point;
83using duration = clock::duration;
85time_point from_time_t(std::time_t t);
86std::time_t to_time_t(time_point t);
91 if (d < std::chrono::seconds(0)) {
92 return "-" + print_duration(-d);
93 }
else if (d < std::chrono::milliseconds(1)) {
94 return fmt::format(
"{:.3g} us", std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(d).count());
95 }
else if (d < std::chrono::seconds(1)) {
96 return fmt::format(
"{:.3g} ms", std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(d).count());
97 }
else if (d < std::chrono::minutes(1)) {
98 return fmt::format(
"{:.3g} s", std::chrono::duration_cast<std::chrono::duration<double>>(d).count());
99 }
else if (d < std::chrono::hours(1)) {
100 return fmt::format(
"{:.3g} min", std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<60>>>(d).count());
101 }
else if (d < std::chrono::hours(72)) {
102 return fmt::format(
"{:.3g} h", std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<3600>>>(d).count());
104 return fmt::format(
"{:.3g} days", std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<86400>>>(d).count());
108template <
class TimePo
int>
110print_time_relative(TimePoint now, TimePoint d) {
111 if (d == TimePoint::min())
return "never";
112 if (d == now)
return "now";
113 return (d > now) ? std::string(
"in ") + print_duration(d - now)
114 : print_duration(now - d) + std::string(
" ago");
117template <
typename Duration = duration>
118class uniform_duration_distribution :
public std::uniform_int_distribution<typename Duration::rep> {
119 using Base = std::uniform_int_distribution<typename Duration::rep>;
120 using param_type =
typename Base::param_type;
122 uniform_duration_distribution(Duration min, Duration max) : Base(min.count(), max.count()) {}
123 template <
class Generator>
124 Duration operator()(Generator && g) {
125 return Duration(Base::operator()(g));
127 template<
class Generator >
128 Duration operator()( Generator && g,
const param_type& params ) {
129 return Duration(Base::operator()(g, params));
138using Blob = std::vector<uint8_t>;
145template <
typename Type>
147packMsg(
const Type& t) {
148 msgpack::sbuffer buffer;
149 msgpack::packer<msgpack::sbuffer> pk(&buffer);
151 return {buffer.data(), buffer.data()+buffer.size()};
154template <
typename Type>
157 msgpack::unpacked msg_res = msgpack::unpack((
const char*)b.data(), b.size());
158 return msg_res.get().as<Type>();
161msgpack::unpacked unpackMsg(
Blob b);
163msgpack::object* findMapValue(
const msgpack::object& map,
const char* key,
size_t length);
165inline msgpack::object* findMapValue(
const msgpack::object& map,
const char* key) {
166 return findMapValue(map, key, strlen(key));
168inline msgpack::object* findMapValue(
const msgpack::object& map, std::string_view key) {
169 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)