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 <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 
25 #include "src/core/lib/channel/channel_fwd.h"
26 #include "src/core/lib/channel/channel_stack.h"
27 #include "src/core/lib/debug/trace.h"
28 #include "src/core/lib/iomgr/closure.h"
29 #include "src/core/lib/iomgr/error.h"
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "src/core/lib/surface/api_trace.h"
32 #include "src/core/lib/surface/channel.h"
33 #include "src/core/lib/surface/completion_queue.h"
34 #include "src/core/lib/transport/transport.h"
35 
36 struct ping_result {
37   grpc_closure closure;
38   void* tag;
39   grpc_completion_queue* cq;
40   grpc_cq_completion completion_storage;
41 };
ping_destroy(void * arg,grpc_cq_completion *)42 static void ping_destroy(void* arg, grpc_cq_completion* /*storage*/) {
43   gpr_free(arg);
44 }
45 
ping_done(void * arg,grpc_error_handle error)46 static void ping_done(void* arg, grpc_error_handle error) {
47   ping_result* pr = static_cast<ping_result*>(arg);
48   grpc_cq_end_op(pr->cq, pr->tag, error, ping_destroy, pr,
49                  &pr->completion_storage);
50 }
51 
grpc_channel_ping(grpc_channel * channel,grpc_completion_queue * cq,void * tag,void * reserved)52 void grpc_channel_ping(grpc_channel* channel, grpc_completion_queue* cq,
53                        void* tag, void* reserved) {
54   GRPC_API_TRACE("grpc_channel_ping(channel=%p, cq=%p, tag=%p, reserved=%p)", 4,
55                  (channel, cq, tag, reserved));
56   grpc_transport_op* op = grpc_make_transport_op(nullptr);
57   ping_result* pr = static_cast<ping_result*>(gpr_malloc(sizeof(*pr)));
58   grpc_channel_element* top_elem =
59       grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
60   grpc_core::ExecCtx exec_ctx;
61   GPR_ASSERT(reserved == nullptr);
62   pr->tag = tag;
63   pr->cq = cq;
64   GRPC_CLOSURE_INIT(&pr->closure, ping_done, pr, grpc_schedule_on_exec_ctx);
65   op->send_ping.on_ack = &pr->closure;
66   op->bind_pollset = grpc_cq_pollset(cq);
67   GPR_ASSERT(grpc_cq_begin_op(cq, tag));
68   top_elem->filter->start_transport_op(top_elem, op);
69 }
70