blob: c541ddfb487ac43f0cb60d95167e2f732326aa7a [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
yangg59dfc902014-12-19 14:00:14 -080036#include <memory>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037
38#include <grpc/grpc.h>
yangg59dfc902014-12-19 14:00:14 -080039#include <grpc/grpc_security.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/support/log.h>
41#include <grpc/support/slice.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042
Vijay Pai01634602015-04-15 02:09:52 -070043#include "src/core/profiling/timers.h"
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>
53
54namespace grpc {
55
Craig Tiller47c83fd2015-02-21 22:45:35 -080056Channel::Channel(const grpc::string& target, grpc_channel* channel)
57 : target_(target), c_channel_(channel) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058
59Channel::~Channel() { grpc_channel_destroy(c_channel_); }
60
Craig Tiller47c83fd2015-02-21 22:45:35 -080061Call Channel::CreateCall(const RpcMethod& method, ClientContext* context,
62 CompletionQueue* cq) {
Craig Tiller277d3cf2015-04-14 14:04:51 -070063 auto c_call =
64 method.channel_tag()
65 ? grpc_channel_create_registered_call(c_channel_, cq->cq(),
66 method.channel_tag(),
Nicolas "Pixel" Noble23be2802015-04-24 00:12:30 +020067 context->raw_deadline())
Craig Tiller277d3cf2015-04-14 14:04:51 -070068 : 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" Noble23be2802015-04-24 00:12:30 +020072 context->raw_deadline());
Craig Tiller277d3cf2015-04-14 14:04:51 -070073 GRPC_TIMER_MARK(CALL_CREATED, c_call);
Craig Tiller1fb99552015-04-27 08:55:08 -070074 context->set_call(c_call, shared_from_this());
Craig Tiller50950712015-02-09 10:38:38 -080075 return Call(c_call, this, cq);
76}
77
Craig Tiller47c83fd2015-02-21 22:45:35 -080078void Channel::PerformOpsOnCall(CallOpBuffer* buf, Call* call) {
Craig Tiller50950712015-02-09 10:38:38 -080079 static const size_t MAX_OPS = 8;
80 size_t nops = MAX_OPS;
81 grpc_op ops[MAX_OPS];
Vijay Pai01634602015-04-15 02:09:52 -070082 GRPC_TIMER_MARK(PERFORM_OPS_BEGIN, call->call());
Craig Tiller50950712015-02-09 10:38:38 -080083 buf->FillOps(ops, &nops);
84 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tiller573523f2015-02-17 07:38:26 -080085 grpc_call_start_batch(call->call(), ops, nops, buf));
Vijay Pai01634602015-04-15 02:09:52 -070086 GRPC_TIMER_MARK(PERFORM_OPS_END, call->call());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087}
88
Craig Tiller277d3cf2015-04-14 14:04:51 -070089void* Channel::RegisterMethod(const char* method) {
Craig Tiller62d28962015-04-10 17:11:51 -070090 return grpc_channel_register_call(c_channel_, method, target_.c_str());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091}
92
Craig Tiller190d3602015-02-18 09:23:38 -080093} // namespace grpc