xref: /aosp_15_r20/external/cronet/net/socket/transport_connect_sub_job.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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_SOCKET_TRANSPORT_CONNECT_SUB_JOB_H_
6 #define NET_SOCKET_TRANSPORT_CONNECT_SUB_JOB_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <utility>
12 #include <vector>
13 
14 #include "base/memory/raw_ptr.h"
15 #include "net/base/address_list.h"
16 #include "net/base/load_states.h"
17 #include "net/socket/transport_connect_job.h"
18 #include "net/socket/websocket_endpoint_lock_manager.h"
19 
20 namespace net {
21 
22 class IPEndPoint;
23 class StreamSocket;
24 
25 // Attempts to connect to a subset of the addresses required by a
26 // TransportConnectJob, specifically either the IPv4 or IPv6 addresses. Each
27 // address is tried in turn, and parent_job->OnSubJobComplete() is called when
28 // the first address succeeds or the last address fails.
29 class TransportConnectSubJob : public WebSocketEndpointLockManager::Waiter {
30  public:
31   using SubJobType = TransportConnectJob::SubJobType;
32 
33   TransportConnectSubJob(std::vector<IPEndPoint> addresses,
34                          TransportConnectJob* parent_job,
35                          SubJobType type);
36 
37   TransportConnectSubJob(const TransportConnectSubJob&) = delete;
38   TransportConnectSubJob& operator=(const TransportConnectSubJob&) = delete;
39 
40   ~TransportConnectSubJob() override;
41 
42   // Start connecting.
43   int Start();
44 
started()45   bool started() { return next_state_ != STATE_NONE; }
46 
47   LoadState GetLoadState() const;
48 
type()49   SubJobType type() const { return type_; }
50 
PassSocket()51   std::unique_ptr<StreamSocket> PassSocket() {
52     return std::move(transport_socket_);
53   }
54 
55   // Implementation of WebSocketEndpointLockManager::EndpointWaiter.
56   void GotEndpointLock() override;
57 
58  private:
59   enum State {
60     STATE_NONE,
61     STATE_OBTAIN_LOCK,
62     STATE_OBTAIN_LOCK_COMPLETE,
63     STATE_TRANSPORT_CONNECT_COMPLETE,
64     STATE_DONE,
65   };
66 
67   const IPEndPoint& CurrentAddress() const;
68 
69   void OnIOComplete(int result);
70   int DoLoop(int result);
71   int DoEndpointLock();
72   int DoEndpointLockComplete();
73   int DoTransportConnectComplete(int result);
74 
75   const raw_ptr<TransportConnectJob> parent_job_;
76 
77   std::vector<IPEndPoint> addresses_;
78   size_t current_address_index_ = 0;
79 
80   State next_state_ = STATE_NONE;
81   const SubJobType type_;
82 
83   std::unique_ptr<StreamSocket> transport_socket_;
84 };
85 
86 }  // namespace net
87 
88 #endif  // NET_SOCKET_TRANSPORT_CONNECT_SUB_JOB_H_
89