1 /*
2 *
3 * Copyright 2018 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 "src/core/ext/transport/chttp2/transport/context_list.h"
22
23 namespace {
24 void (*write_timestamps_callback_g)(void*, grpc_core::Timestamps*,
25 grpc_error_handle error) = nullptr;
26 void* (*get_copied_context_fn_g)(void*) = nullptr;
27 } // namespace
28
29 namespace grpc_core {
Append(ContextList ** head,grpc_chttp2_stream * s)30 void ContextList::Append(ContextList** head, grpc_chttp2_stream* s) {
31 if (get_copied_context_fn_g == nullptr ||
32 write_timestamps_callback_g == nullptr) {
33 return;
34 }
35 /* Create a new element in the list and add it at the front */
36 ContextList* elem = new ContextList();
37 elem->trace_context_ = get_copied_context_fn_g(s->context);
38 elem->byte_offset_ = s->byte_counter;
39 elem->next_ = *head;
40 *head = elem;
41 }
42
Execute(void * arg,Timestamps * ts,grpc_error_handle error)43 void ContextList::Execute(void* arg, Timestamps* ts, grpc_error_handle error) {
44 ContextList* head = static_cast<ContextList*>(arg);
45 ContextList* to_be_freed;
46 while (head != nullptr) {
47 if (write_timestamps_callback_g) {
48 if (ts) {
49 ts->byte_offset = static_cast<uint32_t>(head->byte_offset_);
50 }
51 write_timestamps_callback_g(head->trace_context_, ts, error);
52 }
53 to_be_freed = head;
54 head = head->next_;
55 delete to_be_freed;
56 }
57 }
58
grpc_http2_set_write_timestamps_callback(void (* fn)(void *,Timestamps *,grpc_error_handle error))59 void grpc_http2_set_write_timestamps_callback(
60 void (*fn)(void*, Timestamps*, grpc_error_handle error)) {
61 write_timestamps_callback_g = fn;
62 }
63
grpc_http2_set_fn_get_copied_context(void * (* fn)(void *))64 void grpc_http2_set_fn_get_copied_context(void* (*fn)(void*)) {
65 get_copied_context_fn_g = fn;
66 }
67 } /* namespace grpc_core */
68