Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Resolver.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_NET_RESOLVER_HPP
18#define TOOLBOX_NET_RESOLVER_HPP
19
21#include <toolbox/sys/Time.hpp>
23
24#include <cassert>
25#include <future>
26
27namespace toolbox {
28inline namespace net {
29
30using AddrInfoFuture = std::future<AddrInfoPtr>;
31
35 using Task = std::packaged_task<AddrInfoPtr()>;
36
37 public:
38 Resolver() = default;
39 ~Resolver() = default;
40
41 // Copy.
42 Resolver(const Resolver& rhs) = delete;
43 Resolver& operator=(const Resolver& rhs) = delete;
44
45 // Move.
46 Resolver(Resolver&&) = delete;
48
52 bool run();
53
55 void stop();
56
58 void clear();
59
61 AddrInfoFuture resolve(std::string uri, int type);
62
63 private:
65};
66
68template <typename EndpointT>
70{
71 const auto ai = future.get();
72 assert(!future.valid());
73 return {ai->ai_addr, ai->ai_addrlen, ai->ai_protocol};
74}
75
77template <typename ResultT>
78bool is_ready(std::future<ResultT>& future)
79{
80 switch (future.wait_for(Seconds{0})) {
81 case std::future_status::ready:
82 return true;
83 case std::future_status::deferred:
84 case std::future_status::timeout:
85 break;
86 }
87 return false;
88}
89
90} // namespace net
91} // namespace toolbox
92
93#endif // TOOLBOX_NET_RESOLVER_HPP
#define TOOLBOX_API
Definition Config.h:39
Resolver(Resolver &&)=delete
Resolver & operator=(const Resolver &rhs)=delete
Resolver(const Resolver &rhs)=delete
Resolver & operator=(Resolver &&)=delete
A vector-based task queue for use in multi-threaded, producer-consumer components.
Definition TaskQueue.hpp:28
EndpointT get_endpoint(AddrInfoFuture &future)
Wait for future and convert result to endpoint.
Definition Resolver.hpp:69
std::unique_ptr< addrinfo, void(*)(addrinfo *)> AddrInfoPtr
Definition Socket.hpp:30
std::future< AddrInfoPtr > AddrInfoFuture
Definition Resolver.hpp:30
bool is_ready(std::future< ResultT > &future)
Returns true if future is ready.
Definition Resolver.hpp:78
std::chrono::seconds Seconds
Definition Time.hpp:33
void clear(timeval &tv) noexcept
Definition Time.hpp:294
constexpr auto bind() noexcept
Definition Slot.hpp:92