• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_SRC_CORE_CLIENT_CHANNEL_CONNECTOR_H
18 #define GRPC_SRC_CORE_CLIENT_CHANNEL_CONNECTOR_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include "src/core/channelz/channelz.h"
23 #include "src/core/lib/channel/channel_args.h"
24 #include "src/core/lib/iomgr/closure.h"
25 #include "src/core/lib/iomgr/error.h"
26 #include "src/core/lib/iomgr/iomgr_fwd.h"
27 #include "src/core/lib/iomgr/resolved_address.h"
28 #include "src/core/lib/transport/transport.h"
29 #include "src/core/util/orphanable.h"
30 #include "src/core/util/ref_counted_ptr.h"
31 #include "src/core/util/time.h"
32 
33 namespace grpc_core {
34 
35 // Interface for connection-establishment functionality.
36 // Each transport that supports client channels (e.g., not inproc) must
37 // supply an implementation of this.
38 class SubchannelConnector : public InternallyRefCounted<SubchannelConnector> {
39  public:
40   struct Args {
41     // Address to connect to.
42     grpc_resolved_address* address;
43     // Set of pollsets interested in this connection.
44     grpc_pollset_set* interested_parties;
45     // Deadline for connection.
46     Timestamp deadline;
47     // Channel args to be passed to handshakers and transport.
48     ChannelArgs channel_args;
49   };
50 
51   struct Result {
52     // The connected transport.
53     Transport* transport = nullptr;
54     // Channel args to be passed to filters.
55     ChannelArgs channel_args;
56     // Channelz socket node of the connected transport, if any.
57     RefCountedPtr<channelz::SocketNode> socket_node;
58 
ResetResult59     void Reset() {
60       if (transport != nullptr) {
61         transport->Orphan();
62         transport = nullptr;
63       }
64       channel_args = ChannelArgs();
65       socket_node.reset();
66     }
67   };
68 
69   // Attempts to connect.
70   // When complete, populates *result and invokes notify.
71   // Only one connection attempt may be in progress at any one time.
72   virtual void Connect(const Args& args, Result* result,
73                        grpc_closure* notify) = 0;
74 
75   // Cancels any in-flight connection attempt and shuts down the
76   // connector.
77   virtual void Shutdown(grpc_error_handle error) = 0;
78 
Orphan()79   void Orphan() override {
80     Shutdown(GRPC_ERROR_CREATE("Subchannel disconnected"));
81     Unref();
82   }
83 };
84 
85 }  // namespace grpc_core
86 
87 #endif  // GRPC_SRC_CORE_CLIENT_CHANNEL_CONNECTOR_H
88