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 : #include <grpc++/channel.h>
35 :
36 : #include <memory>
37 :
38 : #include <grpc/grpc.h>
39 : #include <grpc/support/log.h>
40 : #include <grpc/support/slice.h>
41 : #include <grpc++/client_context.h>
42 : #include <grpc++/completion_queue.h>
43 : #include <grpc++/security/credentials.h>
44 : #include <grpc++/impl/call.h>
45 : #include <grpc++/impl/rpc_method.h>
46 : #include <grpc++/support/channel_arguments.h>
47 : #include <grpc++/support/config.h>
48 : #include <grpc++/support/status.h>
49 : #include <grpc++/support/time.h>
50 : #include "src/core/profiling/timers.h"
51 :
52 : namespace grpc {
53 :
54 175 : Channel::Channel(const grpc::string& host, grpc_channel* channel)
55 175 : : host_(host), c_channel_(channel) {}
56 :
57 350 : Channel::~Channel() { grpc_channel_destroy(c_channel_); }
58 :
59 1646545 : Call Channel::CreateCall(const RpcMethod& method, ClientContext* context,
60 : CompletionQueue* cq) {
61 1646545 : const bool kRegistered = method.channel_tag() && context->authority().empty();
62 1646521 : grpc_call* c_call = NULL;
63 1646521 : if (kRegistered) {
64 : c_call = grpc_channel_create_registered_call(
65 : c_channel_, context->propagate_from_call_,
66 : context->propagation_options_.c_bitmask(), cq->cq(),
67 1646507 : method.channel_tag(), context->raw_deadline(), nullptr);
68 : } else {
69 14 : const char* host_str = NULL;
70 14 : if (!context->authority().empty()) {
71 1 : host_str = context->authority_.c_str();
72 13 : } else if (!host_.empty()) {
73 0 : host_str = host_.c_str();
74 : }
75 : c_call = grpc_channel_create_call(c_channel_, context->propagate_from_call_,
76 : context->propagation_options_.c_bitmask(),
77 : cq->cq(), method.name(), host_str,
78 14 : context->raw_deadline(), nullptr);
79 : }
80 1646795 : grpc_census_call_set_context(c_call, context->census_context());
81 1646796 : context->set_call(c_call, shared_from_this());
82 1646805 : return Call(c_call, this, cq);
83 : }
84 :
85 3543938 : void Channel::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) {
86 : static const size_t MAX_OPS = 8;
87 3543938 : size_t nops = 0;
88 : grpc_op cops[MAX_OPS];
89 3543938 : ops->FillOps(cops, &nops);
90 3546957 : GPR_ASSERT(GRPC_CALL_OK ==
91 : grpc_call_start_batch(call->call(), cops, nops, ops, nullptr));
92 3544830 : }
93 :
94 770 : void* Channel::RegisterMethod(const char* method) {
95 : return grpc_channel_register_call(
96 770 : c_channel_, method, host_.empty() ? NULL : host_.c_str(), nullptr);
97 : }
98 :
99 17 : grpc_connectivity_state Channel::GetState(bool try_to_connect) {
100 17 : return grpc_channel_check_connectivity_state(c_channel_, try_to_connect);
101 : }
102 :
103 : namespace {
104 : class TagSaver GRPC_FINAL : public CompletionQueueTag {
105 : public:
106 14 : explicit TagSaver(void* tag) : tag_(tag) {}
107 28 : ~TagSaver() GRPC_OVERRIDE {}
108 14 : bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
109 14 : *tag = tag_;
110 14 : delete this;
111 14 : return true;
112 : }
113 :
114 : private:
115 : void* tag_;
116 : };
117 :
118 : } // namespace
119 :
120 14 : void Channel::NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
121 : gpr_timespec deadline,
122 : CompletionQueue* cq, void* tag) {
123 14 : TagSaver* tag_saver = new TagSaver(tag);
124 : grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline,
125 14 : cq->cq(), tag_saver);
126 14 : }
127 :
128 12 : bool Channel::WaitForStateChangeImpl(grpc_connectivity_state last_observed,
129 : gpr_timespec deadline) {
130 12 : CompletionQueue cq;
131 12 : bool ok = false;
132 12 : void* tag = NULL;
133 12 : NotifyOnStateChangeImpl(last_observed, deadline, &cq, NULL);
134 12 : cq.Next(&tag, &ok);
135 12 : GPR_ASSERT(tag == NULL);
136 12 : return ok;
137 : }
138 :
139 : } // namespace grpc
|