Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
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 :
39 : #include <grpc/grpc.h>
40 : #include <grpc++/impl/call.h>
41 : #include <grpc++/impl/grpc_library.h>
42 : #include <grpc++/support/config.h>
43 :
44 : struct grpc_channel;
45 :
46 : namespace grpc {
47 : class CallOpSetInterface;
48 : class ChannelArguments;
49 : class CompletionQueue;
50 : class ChannelCredentials;
51 : class SecureChannelCredentials;
52 :
53 : template <class R>
54 : class ClientReader;
55 : template <class W>
56 : class ClientWriter;
57 : template <class R, class W>
58 : class ClientReaderWriter;
59 : template <class R>
60 : class ClientAsyncReader;
61 : template <class W>
62 : class ClientAsyncWriter;
63 : template <class R, class W>
64 : class ClientAsyncReaderWriter;
65 : template <class R>
66 : class ClientAsyncResponseReader;
67 :
68 : /// Channels represent a connection to an endpoint. Created by \a CreateChannel.
69 : class Channel GRPC_FINAL : public GrpcLibrary,
70 : public CallHook,
71 : public std::enable_shared_from_this<Channel> {
72 : public:
73 : ~Channel();
74 :
75 : /// Get the current channel state. If the channel is in IDLE and
76 : /// \a try_to_connect is set to true, try to connect.
77 : grpc_connectivity_state GetState(bool try_to_connect);
78 :
79 : /// Return the \a tag on \a cq when the channel state is changed or \a
80 : /// deadline expires. \a GetState needs to called to get the current state.
81 : template <typename T>
82 2 : void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline,
83 : CompletionQueue* cq, void* tag) {
84 2 : TimePoint<T> deadline_tp(deadline);
85 2 : NotifyOnStateChangeImpl(last_observed, deadline_tp.raw_time(), cq, tag);
86 2 : }
87 :
88 : /// Blocking wait for channel state change or \a deadline expiration.
89 : /// \a GetState needs to called to get the current state.
90 : template <typename T>
91 12 : bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline) {
92 12 : TimePoint<T> deadline_tp(deadline);
93 12 : return WaitForStateChangeImpl(last_observed, deadline_tp.raw_time());
94 : }
95 :
96 : private:
97 : template <class R>
98 : friend class ::grpc::ClientReader;
99 : template <class W>
100 : friend class ::grpc::ClientWriter;
101 : template <class R, class W>
102 : friend class ::grpc::ClientReaderWriter;
103 : template <class R>
104 : friend class ::grpc::ClientAsyncReader;
105 : template <class W>
106 : friend class ::grpc::ClientAsyncWriter;
107 : template <class R, class W>
108 : friend class ::grpc::ClientAsyncReaderWriter;
109 : template <class R>
110 : friend class ::grpc::ClientAsyncResponseReader;
111 : template <class InputMessage, class OutputMessage>
112 : friend Status BlockingUnaryCall(Channel* channel, const RpcMethod& method,
113 : ClientContext* context,
114 : const InputMessage& request,
115 : OutputMessage* result);
116 : friend class ::grpc::RpcMethod;
117 : friend std::shared_ptr<Channel> CreateChannelInternal(
118 : const grpc::string& host, grpc_channel* c_channel);
119 :
120 : Channel(const grpc::string& host, grpc_channel* c_channel);
121 :
122 : Call CreateCall(const RpcMethod& method, ClientContext* context,
123 : CompletionQueue* cq);
124 : void PerformOpsOnCall(CallOpSetInterface* ops, Call* call);
125 : void* RegisterMethod(const char* method);
126 :
127 : void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
128 : gpr_timespec deadline, CompletionQueue* cq,
129 : void* tag);
130 : bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
131 : gpr_timespec deadline);
132 :
133 : const grpc::string host_;
134 : grpc_channel* const c_channel_; // owned
135 : };
136 :
137 : } // namespace grpc
138 :
139 : #endif // GRPCXX_CHANNEL_H
|