blob: d6f55a8bf674eec50bfaa037fb7930c0322cf852 [file] [log] [blame]
yang-g8c2be9f2015-08-19 16:28:09 -07001/*
2 *
murgatroid993466c4b2016-01-12 10:26:04 -08003 * Copyright 2015-2016, 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
39#include <grpc/grpc.h>
yang-g8c2be9f2015-08-19 16:28:09 -070040#include <grpc++/impl/call.h>
David Garcia Quintase50c6c22016-01-13 16:02:00 -080041#include <grpc++/impl/codegen/channel_interface.h>
yang-g8c2be9f2015-08-19 16:28:09 -070042#include <grpc++/impl/grpc_library.h>
yang-g9e2f90c2015-08-21 15:35:03 -070043#include <grpc++/support/config.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.
David Garcia Quintase50c6c22016-01-13 16:02:00 -080049class Channel GRPC_FINAL : public ChannelInterface,
50 public GrpcLibrary,
yang-g8c2be9f2015-08-19 16:28:09 -070051 public CallHook,
52 public std::enable_shared_from_this<Channel> {
53 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.
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -080058 grpc_connectivity_state GetState(bool try_to_connect) GRPC_OVERRIDE;
yang-g8c2be9f2015-08-19 16:28:09 -070059
yang-g8c2be9f2015-08-19 16:28:09 -070060 private:
yang-g8c2be9f2015-08-19 16:28:09 -070061 template <class InputMessage, class OutputMessage>
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -080062 friend Status BlockingUnaryCall(ChannelInterface* channel,
63 const RpcMethod& method,
yang-g8c2be9f2015-08-19 16:28:09 -070064 ClientContext* context,
65 const InputMessage& request,
66 OutputMessage* result);
yang-gc317f072015-08-20 12:18:08 -070067 friend std::shared_ptr<Channel> CreateChannelInternal(
68 const grpc::string& host, grpc_channel* c_channel);
yang-gc317f072015-08-20 12:18:08 -070069 Channel(const grpc::string& host, grpc_channel* c_channel);
yang-g8c2be9f2015-08-19 16:28:09 -070070
71 Call CreateCall(const RpcMethod& method, ClientContext* context,
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -080072 CompletionQueue* cq) GRPC_OVERRIDE;
73 void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) GRPC_OVERRIDE;
74 void* RegisterMethod(const char* method) GRPC_OVERRIDE;
yang-g8c2be9f2015-08-19 16:28:09 -070075
76 void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
77 gpr_timespec deadline, CompletionQueue* cq,
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -080078 void* tag) GRPC_OVERRIDE;
yang-g8c2be9f2015-08-19 16:28:09 -070079 bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
David Garcia Quintas1f4e72c2016-01-19 14:45:32 -080080 gpr_timespec deadline) GRPC_OVERRIDE;
yang-g8c2be9f2015-08-19 16:28:09 -070081
82 const grpc::string host_;
83 grpc_channel* const c_channel_; // owned
84};
85
86} // namespace grpc
87
88#endif // GRPCXX_CHANNEL_H