Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [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 | #include "src/cpp/client/channel.h" |
| 35 | |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 36 | #include <memory> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | |
| 38 | #include <grpc/grpc.h> |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 39 | #include <grpc/grpc_security.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/log.h> |
| 41 | #include <grpc/support/slice.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | |
Vijay Pai | 0163460 | 2015-04-15 02:09:52 -0700 | [diff] [blame] | 43 | #include "src/core/profiling/timers.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include "src/cpp/proto/proto_utils.h" |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 45 | #include <grpc++/channel_arguments.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <grpc++/client_context.h> |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 47 | #include <grpc++/completion_queue.h> |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 48 | #include <grpc++/config.h> |
| 49 | #include <grpc++/credentials.h> |
Craig Tiller | 20f4af2 | 2015-02-10 09:52:15 -0800 | [diff] [blame] | 50 | #include <grpc++/impl/call.h> |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 51 | #include <grpc++/impl/rpc_method.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | #include <grpc++/status.h> |
| 53 | |
| 54 | namespace grpc { |
| 55 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 56 | Channel::Channel(const grpc::string& target, grpc_channel* channel) |
| 57 | : target_(target), c_channel_(channel) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | |
| 59 | Channel::~Channel() { grpc_channel_destroy(c_channel_); } |
| 60 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 61 | Call Channel::CreateCall(const RpcMethod& method, ClientContext* context, |
| 62 | CompletionQueue* cq) { |
Craig Tiller | 277d3cf | 2015-04-14 14:04:51 -0700 | [diff] [blame] | 63 | auto c_call = |
| 64 | method.channel_tag() |
| 65 | ? grpc_channel_create_registered_call(c_channel_, cq->cq(), |
| 66 | method.channel_tag(), |
Nicolas "Pixel" Noble | 23be280 | 2015-04-24 00:12:30 +0200 | [diff] [blame] | 67 | context->raw_deadline()) |
Craig Tiller | 277d3cf | 2015-04-14 14:04:51 -0700 | [diff] [blame] | 68 | : grpc_channel_create_call(c_channel_, cq->cq(), method.name(), |
| 69 | context->authority().empty() |
| 70 | ? target_.c_str() |
| 71 | : context->authority().c_str(), |
Nicolas "Pixel" Noble | 23be280 | 2015-04-24 00:12:30 +0200 | [diff] [blame] | 72 | context->raw_deadline()); |
Craig Tiller | 277d3cf | 2015-04-14 14:04:51 -0700 | [diff] [blame] | 73 | GRPC_TIMER_MARK(CALL_CREATED, c_call); |
Craig Tiller | 1fb9955 | 2015-04-27 08:55:08 -0700 | [diff] [blame^] | 74 | context->set_call(c_call, shared_from_this()); |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 75 | return Call(c_call, this, cq); |
| 76 | } |
| 77 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 78 | void Channel::PerformOpsOnCall(CallOpBuffer* buf, Call* call) { |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 79 | static const size_t MAX_OPS = 8; |
| 80 | size_t nops = MAX_OPS; |
| 81 | grpc_op ops[MAX_OPS]; |
Vijay Pai | 0163460 | 2015-04-15 02:09:52 -0700 | [diff] [blame] | 82 | GRPC_TIMER_MARK(PERFORM_OPS_BEGIN, call->call()); |
Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 83 | buf->FillOps(ops, &nops); |
| 84 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 85 | grpc_call_start_batch(call->call(), ops, nops, buf)); |
Vijay Pai | 0163460 | 2015-04-15 02:09:52 -0700 | [diff] [blame] | 86 | GRPC_TIMER_MARK(PERFORM_OPS_END, call->call()); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Craig Tiller | 277d3cf | 2015-04-14 14:04:51 -0700 | [diff] [blame] | 89 | void* Channel::RegisterMethod(const char* method) { |
Craig Tiller | 62d2896 | 2015-04-10 17:11:51 -0700 | [diff] [blame] | 90 | return grpc_channel_register_call(c_channel_, method, target_.c_str()); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 93 | } // namespace grpc |