blob: e8f47f01cf4b15415d77a00846f6b4f152b9ec4a [file] [log] [blame]
Craig Tiller26dab312015-12-07 14:43:47 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller26dab312015-12-07 14:43:47 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Craig Tiller26dab312015-12-07 14:43:47 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller26dab312015-12-07 14:43:47 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Craig Tiller26dab312015-12-07 14:43:47 -080016 *
17 */
18
Craig Tiller9533d042016-03-25 17:11:06 -070019#include "src/core/lib/surface/channel.h"
Craig Tiller26dab312015-12-07 14:43:47 -080020
21#include <string.h>
22
23#include <grpc/support/alloc.h>
24#include <grpc/support/log.h>
25
Craig Tiller9533d042016-03-25 17:11:06 -070026#include "src/core/lib/surface/api_trace.h"
27#include "src/core/lib/surface/completion_queue.h"
Craig Tiller26dab312015-12-07 14:43:47 -080028
29typedef struct {
30 grpc_closure closure;
Craig Tillerbaa14a92017-11-03 09:09:36 -070031 void* tag;
32 grpc_completion_queue* cq;
Craig Tiller26dab312015-12-07 14:43:47 -080033 grpc_cq_completion completion_storage;
34} ping_result;
35
Craig Tillerbaa14a92017-11-03 09:09:36 -070036static void ping_destroy(grpc_exec_ctx* exec_ctx, void* arg,
37 grpc_cq_completion* storage) {
Craig Tiller26dab312015-12-07 14:43:47 -080038 gpr_free(arg);
39}
40
Craig Tillerbaa14a92017-11-03 09:09:36 -070041static void ping_done(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {
42 ping_result* pr = (ping_result*)arg;
Craig Tiller10dd6f22016-05-10 13:06:15 -070043 grpc_cq_end_op(exec_ctx, pr->cq, pr->tag, GRPC_ERROR_REF(error), ping_destroy,
44 pr, &pr->completion_storage);
Craig Tiller26dab312015-12-07 14:43:47 -080045}
46
Craig Tillerbaa14a92017-11-03 09:09:36 -070047void grpc_channel_ping(grpc_channel* channel, grpc_completion_queue* cq,
48 void* tag, void* reserved) {
Craig Tiller57726ca2016-09-12 11:59:45 -070049 GRPC_API_TRACE("grpc_channel_ping(channel=%p, cq=%p, tag=%p, reserved=%p)", 4,
50 (channel, cq, tag, reserved));
Craig Tiller4782d922017-11-10 09:53:21 -080051 grpc_transport_op* op = grpc_make_transport_op(nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -070052 ping_result* pr = (ping_result*)gpr_malloc(sizeof(*pr));
53 grpc_channel_element* top_elem =
Craig Tillere2c62372015-12-07 16:11:03 -080054 grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
Craig Tiller26dab312015-12-07 14:43:47 -080055 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller4782d922017-11-10 09:53:21 -080056 GPR_ASSERT(reserved == nullptr);
Craig Tiller26dab312015-12-07 14:43:47 -080057 pr->tag = tag;
58 pr->cq = cq;
ncteisen274bbbe2017-06-08 14:57:11 -070059 GRPC_CLOSURE_INIT(&pr->closure, ping_done, pr, grpc_schedule_on_exec_ctx);
Craig Tiller57726ca2016-09-12 11:59:45 -070060 op->send_ping = &pr->closure;
61 op->bind_pollset = grpc_cq_pollset(cq);
yang-g7d6b9142017-07-13 11:48:56 -070062 GPR_ASSERT(grpc_cq_begin_op(cq, tag));
Craig Tiller57726ca2016-09-12 11:59:45 -070063 top_elem->filter->start_transport_op(&exec_ctx, top_elem, op);
Craig Tiller26dab312015-12-07 14:43:47 -080064 grpc_exec_ctx_finish(&exec_ctx);
65}