1 //
2 //
3 // Copyright 2015 gRPC authors.
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 //
18 
19 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_CLIENT_CHTTP2_CONNECTOR_H
20 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_CLIENT_CHTTP2_CONNECTOR_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include "absl/base/thread_annotations.h"
25 #include "absl/types/optional.h"
26 
27 #include <grpc/event_engine/event_engine.h>
28 
29 #include "src/core/ext/filters/client_channel/connector.h"
30 #include "src/core/lib/gprpp/ref_counted_ptr.h"
31 #include "src/core/lib/gprpp/sync.h"
32 #include "src/core/lib/iomgr/closure.h"
33 #include "src/core/lib/iomgr/endpoint.h"
34 #include "src/core/lib/iomgr/error.h"
35 #include "src/core/lib/transport/handshaker.h"
36 
37 namespace grpc_core {
38 
39 class Chttp2Connector : public SubchannelConnector {
40  public:
41   ~Chttp2Connector() override;
42 
43   void Connect(const Args& args, Result* result, grpc_closure* notify) override;
44   void Shutdown(grpc_error_handle error) override;
45 
46  private:
47   static void OnHandshakeDone(void* arg, grpc_error_handle error);
48   static void OnReceiveSettings(void* arg, grpc_error_handle error);
49   void OnTimeout() ABSL_LOCKS_EXCLUDED(mu_);
50 
51   // We cannot invoke notify_ until both OnTimeout() and OnReceiveSettings()
52   // have been called since that is an indicator to the upper layer that we are
53   // done with the connection attempt. So, the notification process is broken
54   // into two steps. 1) Either OnTimeout() or OnReceiveSettings() gets invoked
55   // first. Whichever gets invoked, calls MaybeNotify() to set the result and
56   // triggers the other callback to be invoked. 2) When the other callback is
57   // invoked, we call MaybeNotify() again to actually invoke the notify_
58   // callback. Note that this only happens if the handshake is done and the
59   // connector is waiting on the SETTINGS frame.
60   void MaybeNotify(grpc_error_handle error);
61 
62   Mutex mu_;
63   Args args_;
64   Result* result_ = nullptr;
65   grpc_closure* notify_ = nullptr;
66   bool shutdown_ = false;
67   // Holds the endpoint when first created before being handed off to
68   // the handshake manager, and then again after handshake is done.
69   grpc_endpoint* endpoint_ = nullptr;
70   grpc_closure on_receive_settings_;
71   absl::optional<grpc_event_engine::experimental::EventEngine::TaskHandle>
72       timer_handle_ ABSL_GUARDED_BY(mu_);
73   // A raw pointer will suffice since args_ holds a copy of the ChannelArgs
74   // which holds an std::shared_ptr of the EventEngine.
75   grpc_event_engine::experimental::EventEngine* event_engine_
76       ABSL_GUARDED_BY(mu_);
77   absl::optional<grpc_error_handle> notify_error_;
78   RefCountedPtr<HandshakeManager> handshake_mgr_;
79 };
80 
81 }  // namespace grpc_core
82 
83 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_CLIENT_CHTTP2_CONNECTOR_H
84