yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 4 | * 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-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 39 | #include <grpc++/impl/call.h> |
David Garcia Quintas | e50c6c2 | 2016-01-13 16:02:00 -0800 | [diff] [blame] | 40 | #include <grpc++/impl/codegen/channel_interface.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 41 | #include <grpc++/impl/codegen/config.h> |
| 42 | #include <grpc++/impl/codegen/grpc_library.h> |
| 43 | #include <grpc/grpc.h> |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 44 | |
| 45 | struct grpc_channel; |
| 46 | |
| 47 | namespace grpc { |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 48 | /// Channels represent a connection to an endpoint. Created by \a CreateChannel. |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 49 | class Channel final : public ChannelInterface, |
Vijay Pai | 713c7b8 | 2016-11-01 16:33:18 -0700 | [diff] [blame] | 50 | public CallHook, |
| 51 | public std::enable_shared_from_this<Channel>, |
| 52 | private GrpcLibraryCodegen { |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 53 | public: |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 54 | ~Channel(); |
| 55 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 56 | /// 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 Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 58 | grpc_connectivity_state GetState(bool try_to_connect) override; |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 59 | |
Mark D. Roth | 4bbdda4 | 2016-11-09 14:40:22 -0800 | [diff] [blame] | 60 | /// 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-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 67 | private: |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 68 | template <class InputMessage, class OutputMessage> |
David Garcia Quintas | f3ddb7c | 2016-01-20 16:02:22 -0800 | [diff] [blame] | 69 | friend Status BlockingUnaryCall(ChannelInterface* channel, |
| 70 | const RpcMethod& method, |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 71 | ClientContext* context, |
| 72 | const InputMessage& request, |
| 73 | OutputMessage* result); |
yang-g | c317f07 | 2015-08-20 12:18:08 -0700 | [diff] [blame] | 74 | friend std::shared_ptr<Channel> CreateChannelInternal( |
| 75 | const grpc::string& host, grpc_channel* c_channel); |
yang-g | c317f07 | 2015-08-20 12:18:08 -0700 | [diff] [blame] | 76 | Channel(const grpc::string& host, grpc_channel* c_channel); |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 77 | |
| 78 | Call CreateCall(const RpcMethod& method, ClientContext* context, |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 79 | CompletionQueue* cq) override; |
| 80 | void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) override; |
| 81 | void* RegisterMethod(const char* method) override; |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 82 | |
| 83 | void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed, |
| 84 | gpr_timespec deadline, CompletionQueue* cq, |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 85 | void* tag) override; |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 86 | bool WaitForStateChangeImpl(grpc_connectivity_state last_observed, |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 87 | gpr_timespec deadline) override; |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 88 | |
| 89 | const grpc::string host_; |
| 90 | grpc_channel* const c_channel_; // owned |
| 91 | }; |
| 92 | |
| 93 | } // namespace grpc |
| 94 | |
| 95 | #endif // GRPCXX_CHANNEL_H |