My Project 3.7.1
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
88time_point from_time_t(std::time_t t);
89std::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
129template<typename Duration = duration>
130class uniform_duration_distribution : public std::uniform_int_distribution<typename Duration::rep>
131{
132 using Base = std::uniform_int_distribution<typename Duration::rep>;
133 using param_type = typename Base::param_type;
134
135public:
136 uniform_duration_distribution(Duration min, Duration max)
137 : Base(min.count(), max.count())
138 {}
139 template<class Generator>
140 Duration operator()(Generator&& g)
141 {
142 return Duration(Base::operator()(g));
143 }
144 template<class Generator>
145 Duration operator()(Generator&& g, const param_type& params)
146 {
147 return Duration(Base::operator()(g, params));
148 }
149};
150
151// Serialization related definitions and utility functions
152
156using Blob = std::vector<uint8_t>;
157
161OPENDHT_PUBLIC Blob unpackBlob(const msgpack::object& o);
162
163template<typename Type>
164Blob
165packMsg(const Type& t)
166{
167 msgpack::sbuffer buffer;
168 msgpack::packer<msgpack::sbuffer> pk(&buffer);
169 pk.pack(t);
170 return {buffer.data(), buffer.data() + buffer.size()};
171}
172
173template<typename Type>
174Type
175unpackMsg(const Blob& b)
176{
177 msgpack::unpacked msg_res = msgpack::unpack((const char*) b.data(), b.size());
178 return msg_res.get().as<Type>();
179}
180
181inline msgpack::unpacked
182unpackMsg(const Blob& b)
183{
184 return msgpack::unpack((const char*) b.data(), b.size());
185}
186
187msgpack::object* findMapValue(const msgpack::object& map, const char* key, size_t length);
188
189inline msgpack::object*
190findMapValue(const msgpack::object& map, const char* key)
191{
192 return findMapValue(map, key, strlen(key));
193}
194inline msgpack::object*
195findMapValue(const msgpack::object& map, std::string_view key)
196{
197 return findMapValue(map, key.data(), key.size());
198}
199
200} // 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:156