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