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_CORE_EXT_TRANSPORT_CHTTP2_CLIENT_CHTTP2_CONNECTOR_H
20 #define GRPC_CORE_EXT_TRANSPORT_CHTTP2_CLIENT_CHTTP2_CONNECTOR_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include "src/core/ext/filters/client_channel/connector.h"
25 #include "src/core/lib/channel/handshaker.h"
26 #include "src/core/lib/channel/handshaker_registry.h"
27 #include "src/core/lib/iomgr/resource_quota.h"
28 
29 namespace grpc_core {
30 
31 class Chttp2Connector : public SubchannelConnector {
32  public:
33   Chttp2Connector();
34   ~Chttp2Connector() override;
35 
36   void Connect(const Args& args, Result* result, grpc_closure* notify) override;
37   void Shutdown(grpc_error_handle error) override;
38 
39  private:
40   static void Connected(void* arg, grpc_error_handle error);
41   void StartHandshakeLocked();
42   static void OnHandshakeDone(void* arg, grpc_error_handle error);
43   static void OnReceiveSettings(void* arg, grpc_error_handle error);
44   static void OnTimeout(void* arg, grpc_error_handle error);
45 
46   // We cannot invoke notify_ until both OnTimeout() and OnReceiveSettings()
47   // have been called since that is an indicator to the upper layer that we are
48   // done with the connection attempt. So, the notification process is broken
49   // into two steps. 1) Either OnTimeout() or OnReceiveSettings() gets invoked
50   // first. Whichever gets invoked, calls MaybeNotify() to set the result and
51   // triggers the other callback to be invoked. 2) When the other callback is
52   // invoked, we call MaybeNotify() again to actually invoke the notify_
53   // callback. Note that this only happens if the handshake is done and the
54   // connector is waiting on the SETTINGS frame.
55   void MaybeNotify(grpc_error_handle error);
56 
57   Mutex mu_;
58   Args args_;
59   Result* result_ = nullptr;
60   grpc_closure* notify_ = nullptr;
61   bool shutdown_ = false;
62   bool connecting_ = false;
63   // Holds the endpoint when first created before being handed off to
64   // the handshake manager, and then again after handshake is done.
65   grpc_endpoint* endpoint_ = nullptr;
66   grpc_closure connected_;
67   grpc_closure on_receive_settings_;
68   grpc_timer timer_;
69   grpc_closure on_timeout_;
70   absl::optional<grpc_error_handle> notify_error_;
71   RefCountedPtr<HandshakeManager> handshake_mgr_;
72   grpc_resource_quota* resource_quota_ = nullptr;
73 };
74 
75 }  // namespace grpc_core
76 
77 #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_CLIENT_CHTTP2_CONNECTOR_H */
78