1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * 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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * 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.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include <string.h>
22
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25
26 #include "src/core/lib/surface/api_trace.h"
27 #include "src/core/lib/surface/channel.h"
28 #include "src/core/lib/surface/completion_queue.h"
29
30 struct ping_result {
31 grpc_closure closure;
32 void* tag;
33 grpc_completion_queue* cq;
34 grpc_cq_completion completion_storage;
35 };
ping_destroy(void * arg,grpc_cq_completion *)36 static void ping_destroy(void* arg, grpc_cq_completion* /*storage*/) {
37 gpr_free(arg);
38 }
39
ping_done(void * arg,grpc_error_handle error)40 static void ping_done(void* arg, grpc_error_handle error) {
41 ping_result* pr = static_cast<ping_result*>(arg);
42 grpc_cq_end_op(pr->cq, pr->tag, GRPC_ERROR_REF(error), ping_destroy, pr,
43 &pr->completion_storage);
44 }
45
grpc_channel_ping(grpc_channel * channel,grpc_completion_queue * cq,void * tag,void * reserved)46 void grpc_channel_ping(grpc_channel* channel, grpc_completion_queue* cq,
47 void* tag, void* reserved) {
48 GRPC_API_TRACE("grpc_channel_ping(channel=%p, cq=%p, tag=%p, reserved=%p)", 4,
49 (channel, cq, tag, reserved));
50 grpc_transport_op* op = grpc_make_transport_op(nullptr);
51 ping_result* pr = static_cast<ping_result*>(gpr_malloc(sizeof(*pr)));
52 grpc_channel_element* top_elem =
53 grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
54 grpc_core::ExecCtx exec_ctx;
55 GPR_ASSERT(reserved == nullptr);
56 pr->tag = tag;
57 pr->cq = cq;
58 GRPC_CLOSURE_INIT(&pr->closure, ping_done, pr, grpc_schedule_on_exec_ctx);
59 op->send_ping.on_ack = &pr->closure;
60 op->bind_pollset = grpc_cq_pollset(cq);
61 GPR_ASSERT(grpc_cq_begin_op(cq, tag));
62 top_elem->filter->start_transport_op(top_elem, op);
63 }
64