blob: ca269be218fba5ed66d360310a9474607e723ea4 [file] [log] [blame]
yang-g8c2be9f2015-08-19 16:28:09 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
yang-g8c2be9f2015-08-19 16:28:09 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#ifndef GRPCXX_CHANNEL_H
35#define GRPCXX_CHANNEL_H
36
37#include <memory>
38
yang-g8c2be9f2015-08-19 16:28:09 -070039#include <grpc++/impl/call.h>
David Garcia Quintase50c6c22016-01-13 16:02:00 -080040#include <grpc++/impl/codegen/channel_interface.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080041#include <grpc++/impl/codegen/config.h>
42#include <grpc++/impl/codegen/grpc_library.h>
43#include <grpc/grpc.h>
yang-g8c2be9f2015-08-19 16:28:09 -070044
45struct grpc_channel;
46
47namespace grpc {
Craig Tillerd6599a32015-09-03 09:37:02 -070048/// Channels represent a connection to an endpoint. Created by \a CreateChannel.
Vijay Paic0b2acb2016-11-01 16:31:56 -070049class Channel final : public ChannelInterface,
Vijay Pai713c7b82016-11-01 16:33:18 -070050 public CallHook,
51 public std::enable_shared_from_this<Channel>,
52 private GrpcLibraryCodegen {
yang-g8c2be9f2015-08-19 16:28:09 -070053 public:
yang-g8c2be9f2015-08-19 16:28:09 -070054 ~Channel();
55
Craig Tillerd6599a32015-09-03 09:37:02 -070056 /// Get the current channel state. If the channel is in IDLE and
57 /// \a try_to_connect is set to true, try to connect.
Vijay Paic0b2acb2016-11-01 16:31:56 -070058 grpc_connectivity_state GetState(bool try_to_connect) override;
yang-g8c2be9f2015-08-19 16:28:09 -070059
Mark D. Roth4bbdda42016-11-09 14:40:22 -080060 /// Returns the LB policy name, or the empty string if not yet available.
61 grpc::string GetLoadBalancingPolicyName() const;
62
63 /// Returns the service config in JSON form, or the empty string if
64 /// not available.
65 grpc::string GetServiceConfigJSON() const;
66
yang-g8c2be9f2015-08-19 16:28:09 -070067 private:
yang-g8c2be9f2015-08-19 16:28:09 -070068 template <class InputMessage, class OutputMessage>
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -080069 friend Status BlockingUnaryCall(ChannelInterface* channel,
70 const RpcMethod& method,
yang-g8c2be9f2015-08-19 16:28:09 -070071 ClientContext* context,
72 const InputMessage& request,
73 OutputMessage* result);
yang-gc317f072015-08-20 12:18:08 -070074 friend std::shared_ptr<Channel> CreateChannelInternal(
75 const grpc::string& host, grpc_channel* c_channel);
yang-gc317f072015-08-20 12:18:08 -070076 Channel(const grpc::string& host, grpc_channel* c_channel);
yang-g8c2be9f2015-08-19 16:28:09 -070077
78 Call CreateCall(const RpcMethod& method, ClientContext* context,
Vijay Paic0b2acb2016-11-01 16:31:56 -070079 CompletionQueue* cq) override;
80 void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) override;
81 void* RegisterMethod(const char* method) override;
yang-g8c2be9f2015-08-19 16:28:09 -070082
83 void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
84 gpr_timespec deadline, CompletionQueue* cq,
Vijay Paic0b2acb2016-11-01 16:31:56 -070085 void* tag) override;
yang-g8c2be9f2015-08-19 16:28:09 -070086 bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
Vijay Paic0b2acb2016-11-01 16:31:56 -070087 gpr_timespec deadline) override;
yang-g8c2be9f2015-08-19 16:28:09 -070088
89 const grpc::string host_;
90 grpc_channel* const c_channel_; // owned
91};
92
93} // namespace grpc
94
95#endif // GRPCXX_CHANNEL_H