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