| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 "src/cpp/client/channel.h" |
| 35 | |
| 36 | #include <chrono> |
| yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 37 | #include <memory> |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
| 39 | #include <grpc/grpc.h> |
| yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 40 | #include <grpc/grpc_security.h> |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/slice.h> |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | |
| 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> |
| yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 53 | #include <google/protobuf/message.h> |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | |
| 55 | namespace grpc { |
| 56 | |
| Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 57 | Channel::Channel(const grpc::string &target, const ChannelArguments &args) |
| yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 58 | : target_(target) { |
| 59 | grpc_channel_args channel_args; |
| 60 | args.SetChannelArgs(&channel_args); |
| 61 | c_channel_ = grpc_channel_create( |
| 62 | target_.c_str(), channel_args.num_args > 0 ? &channel_args : nullptr); |
| 63 | } |
| 64 | |
| Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 65 | Channel::Channel(const grpc::string &target, |
| 66 | const std::unique_ptr<Credentials> &creds, |
| 67 | const ChannelArguments &args) |
| yangg | 7cebec7 | 2014-12-22 12:47:45 -0800 | [diff] [blame] | 68 | : target_(args.GetSslTargetNameOverride().empty() |
| 69 | ? target |
| 70 | : args.GetSslTargetNameOverride()) { |
| yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 71 | grpc_channel_args channel_args; |
| 72 | args.SetChannelArgs(&channel_args); |
| Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 73 | grpc_credentials *c_creds = creds ? creds->GetRawCreds() : nullptr; |
| yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 74 | c_channel_ = grpc_secure_channel_create( |
| yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 75 | c_creds, target.c_str(), |
| yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 76 | channel_args.num_args > 0 ? &channel_args : nullptr); |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | Channel::~Channel() { grpc_channel_destroy(c_channel_); } |
| 80 | |
| Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 81 | Call Channel::CreateCall(const RpcMethod &method, ClientContext *context, |
| 82 | CompletionQueue *cq) { |
| Craig Tiller | c496575 | 2015-02-09 09:51:00 -0800 | [diff] [blame] | 83 | auto c_call = |
| 84 | grpc_channel_create_call(c_channel_, cq->cq(), method.name(), |
| 85 | target_.c_str(), context->RawDeadline()); |
| 86 | context->set_call(c_call); |
| Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 87 | return Call(c_call, this, cq); |
| 88 | } |
| 89 | |
| Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 90 | void Channel::PerformOpsOnCall(CallOpBuffer *buf, Call *call) { |
| Craig Tiller | 5095071 | 2015-02-09 10:38:38 -0800 | [diff] [blame] | 91 | static const size_t MAX_OPS = 8; |
| 92 | size_t nops = MAX_OPS; |
| 93 | grpc_op ops[MAX_OPS]; |
| 94 | buf->FillOps(ops, &nops); |
| 95 | GPR_ASSERT(GRPC_CALL_OK == |
| 96 | grpc_call_start_batch(call->call(), ops, nops, |
| Craig Tiller | de91706 | 2015-02-09 17:15:03 -0800 | [diff] [blame] | 97 | buf)); |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace grpc |