My Project 3.7.7
C++ Distributed Hash Table
Loading...
Searching...
No Matches
utils.h
1// Copyright (c) 2014-2026 Savoir-faire Linux Inc.
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "def.h"
6
7#include <type_traits>
8#include <msgpack.hpp>
9#include <fmt/format.h>
10
11#include <chrono>
12#include <random>
13#include <functional>
14#include <map>
15#include <string_view>
16
17#include <cstdarg>
18
19#define WANT4 1
20#define WANT6 2
21
25namespace dht {
26
27using NetId = uint32_t;
28using want_t = int_fast8_t;
29
30OPENDHT_PUBLIC const char* version();
31
32// shortcut for std::shared_ptr
33template<class T>
34using Sp = std::shared_ptr<T>;
35
36template<typename Key, typename Item, typename Condition>
37void
38erase_if(std::map<Key, Item>& map, const Condition& condition)
39{
40 for (auto it = map.begin(); it != map.end();) {
41 if (condition(*it)) {
42 it = map.erase(it);
43 } else {
44 ++it;
45 }
46 }
47}
48
49template<typename... Args>
50std::string
51concat(Args&&... args)
52{
53 static_assert((std::is_constructible_v<std::string_view, Args&&> && ...));
54 std::string s;
55 s.reserve((std::string_view {args}.size() + ...));
56 (s.append(std::forward<Args>(args)), ...);
57 return s;
58}
59
63OPENDHT_PUBLIC std::pair<std::string_view, std::string_view> splitPort(std::string_view s);
64
65class OPENDHT_PUBLIC DhtException : public std::runtime_error
66{
67public:
68 DhtException(const std::string& str = "")
69 : std::runtime_error("DhtException occurred: " + str)
70 {}
71};
72
73class OPENDHT_PUBLIC SocketException : public DhtException
74{
75public:
76 SocketException(int err)
77 : DhtException(strerror(err))
78 {}
79};
80
81// Time related definitions and utility functions
82
83using clock = std::chrono::steady_clock;
84using system_clock = std::chrono::system_clock;
85using time_point = clock::time_point;
86using duration = clock::duration;
87
88OPENDHT_PUBLIC time_point from_time_t(std::time_t t);
89OPENDHT_PUBLIC std::time_t to_time_t(time_point t);
90
91template<class DT>
92static std::string
93print_duration(DT d)
94{
95 if (d < std::chrono::seconds(0)) {
96 return "-" + print_duration(-d);
97 } else if (d < std::chrono::milliseconds(1)) {
98 return fmt::format("{:.3g} us",
99 std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(d).count());
100 } else if (d < std::chrono::seconds(1)) {
101 return fmt::format("{:.3g} ms",
102 std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(d).count());
103 } else if (d < std::chrono::minutes(1)) {
104 return fmt::format("{:.3g} s", std::chrono::duration_cast<std::chrono::duration<double>>(d).count());
105 } else if (d < std::chrono::hours(1)) {
106 return fmt::format("{:.3g} min",
107 std::chrono::duration_cast<std::chrono::duration<double, std::ratio<60>>>(d).count());
108 } else if (d < std::chrono::hours(72)) {
109 return fmt::format("{:.3g} h",
110 std::chrono::duration_cast<std::chrono::duration<double, std::ratio<3600>>>(d).count());
111 } else {
112 return fmt::format("{:.3g} days",
113 std::chrono::duration_cast<std::chrono::duration<double, std::ratio<86400>>>(d).count());
114 }
115}
116
117template<class TimePoint>
118static std::string
119print_time_relative(TimePoint now, TimePoint d)
120{
121 using namespace std::literals;
122 if (d == TimePoint::min())
123 return "never"s;
124 if (d == now)
125 return "now"s;
126 return (d > now) ? concat("in "sv, print_duration(d - now)) : concat(print_duration(now - d), " ago"sv);
127}
128
129OPENDHT_PUBLIC std::string printByteCount(size_t bytes);
130
131template<typename Duration = duration>
132class uniform_duration_distribution : public std::uniform_int_distribution<typename Duration::rep>
133{
134 using Base = std::uniform_int_distribution<typename Duration::rep>;
135 using param_type = typename Base::param_type;
136
137public:
138 uniform_duration_distribution(Duration min, Duration max)
139 : Base(min.count(), max.count())
140 {}
141 template<class Generator>
142 Duration operator()(Generator&& g)
143 {
144 return Duration(Base::operator()(g));
145 }
146 template<class Generator>
147 Duration operator()(Generator&& g, const param_type& params)
148 {
149 return Duration(Base::operator()(g, params));
150 }
151};
152
153// Serialization related definitions and utility functions
154
158using Blob = std::vector<uint8_t>;
159
163OPENDHT_PUBLIC Blob unpackBlob(const msgpack::object& o);
164
165template<typename Type>
166Blob
167packMsg(const Type& t)
168{
169 msgpack::sbuffer buffer;
170 msgpack::packer<msgpack::sbuffer> pk(&buffer);
171 pk.pack(t);
172 return {buffer.data(), buffer.data() + buffer.size()};
173}
174
175template<typename Type>
176Type
177unpackMsg(const Blob& b)
178{
179 msgpack::unpacked msg_res = msgpack::unpack((const char*) b.data(), b.size());
180 return msg_res.get().as<Type>();
181}
182
183inline msgpack::unpacked
184unpackMsg(const Blob& b)
185{
186 return msgpack::unpack((const char*) b.data(), b.size());
187}
188
189OPENDHT_PUBLIC msgpack::object* findMapValue(const msgpack::object& map, const char* key, size_t length);
190
191inline msgpack::object*
192findMapValue(const msgpack::object& map, const char* key)
193{
194 return findMapValue(map, key, strlen(key));
195}
196inline msgpack::object*
197findMapValue(const msgpack::object& map, std::string_view key)
198{
199 return findMapValue(map, key.data(), key.size());
200}
201
202} // namespace dht
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
Definition utils.h:158