xref: /aosp_15_r20/external/cronet/net/dns/address_sorter.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DNS_ADDRESS_SORTER_H_
6 #define NET_DNS_ADDRESS_SORTER_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/functional/callback.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/net_export.h"
14 
15 namespace net {
16 
17 class AddressList;
18 
19 // Sorts AddressList according to RFC3484, by likelihood of successful
20 // connection. Depending on the platform, the sort could be performed
21 // asynchronously by the OS, or synchronously by local implementation.
22 // AddressSorter does not necessarily preserve port numbers on the sorted list.
23 class NET_EXPORT AddressSorter {
24  public:
25   using CallbackType =
26       base::OnceCallback<void(bool success, std::vector<IPEndPoint> sorted)>;
27 
28   AddressSorter(const AddressSorter&) = delete;
29   AddressSorter& operator=(const AddressSorter&) = delete;
30 
31   virtual ~AddressSorter() = default;
32 
33   // Sorts `endpoints`, which must include at least one IPv6 address.
34   // Calls `callback` upon completion. Could complete synchronously. Could
35   // complete after this AddressSorter is destroyed.
36   virtual void Sort(const std::vector<IPEndPoint>& endpoints,
37                     CallbackType callback) const = 0;
38 
39   // Creates platform-dependent AddressSorter.
40   static std::unique_ptr<AddressSorter> CreateAddressSorter();
41 
42  protected:
43   AddressSorter() = default;
44 };
45 
46 }  // namespace net
47 
48 #endif  // NET_DNS_ADDRESS_SORTER_H_
49