blob: da94739ea0d2986ffb6a823930cb951431425324 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
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>
yangg59dfc902014-12-19 14:00:14 -080037#include <memory>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
39#include <grpc/grpc.h>
yangg59dfc902014-12-19 14:00:14 -080040#include <grpc/grpc_security.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include <grpc/support/log.h>
42#include <grpc/support/slice.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044#include "src/cpp/proto/proto_utils.h"
yangg59dfc902014-12-19 14:00:14 -080045#include <grpc++/channel_arguments.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046#include <grpc++/client_context.h>
Craig Tiller50950712015-02-09 10:38:38 -080047#include <grpc++/completion_queue.h>
yangg59dfc902014-12-19 14:00:14 -080048#include <grpc++/config.h>
49#include <grpc++/credentials.h>
Craig Tiller20f4af22015-02-10 09:52:15 -080050#include <grpc++/impl/call.h>
yangg1b151092015-01-09 15:31:05 -080051#include <grpc++/impl/rpc_method.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052#include <grpc++/status.h>
yangg59dfc902014-12-19 14:00:14 -080053#include <google/protobuf/message.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054
55namespace grpc {
56
Craig Tillerecd49342015-01-18 14:36:47 -080057Channel::Channel(const grpc::string &target, const ChannelArguments &args)
yangg59dfc902014-12-19 14:00:14 -080058 : 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 Tillerecd49342015-01-18 14:36:47 -080065Channel::Channel(const grpc::string &target,
66 const std::unique_ptr<Credentials> &creds,
67 const ChannelArguments &args)
yangg7cebec72014-12-22 12:47:45 -080068 : target_(args.GetSslTargetNameOverride().empty()
69 ? target
70 : args.GetSslTargetNameOverride()) {
yangg59dfc902014-12-19 14:00:14 -080071 grpc_channel_args channel_args;
72 args.SetChannelArgs(&channel_args);
Craig Tillerecd49342015-01-18 14:36:47 -080073 grpc_credentials *c_creds = creds ? creds->GetRawCreds() : nullptr;
yangg59dfc902014-12-19 14:00:14 -080074 c_channel_ = grpc_secure_channel_create(
yangg4105e2b2015-01-09 14:19:44 -080075 c_creds, target.c_str(),
yangg59dfc902014-12-19 14:00:14 -080076 channel_args.num_args > 0 ? &channel_args : nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077}
78
79Channel::~Channel() { grpc_channel_destroy(c_channel_); }
80
Craig Tiller50950712015-02-09 10:38:38 -080081Call Channel::CreateCall(const RpcMethod &method, ClientContext *context,
82 CompletionQueue *cq) {
Craig Tillerc4965752015-02-09 09:51:00 -080083 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 Tiller50950712015-02-09 10:38:38 -080087 return Call(c_call, this, cq);
88}
89
Craig Tillerde917062015-02-09 17:15:03 -080090void Channel::PerformOpsOnCall(CallOpBuffer *buf, Call *call) {
Craig Tiller50950712015-02-09 10:38:38 -080091 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 Tillerde917062015-02-09 17:15:03 -080097 buf));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080098}
99
100} // namespace grpc