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* 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,grpc_core::Timestamps * ts,grpc_error * error)43 void ContextList::Execute(void* arg, grpc_core::Timestamps* ts,
44                           grpc_error* error) {
45   ContextList* head = static_cast<ContextList*>(arg);
46   ContextList* to_be_freed;
47   while (head != nullptr) {
48     if (write_timestamps_callback_g) {
49       if (ts) {
50         ts->byte_offset = static_cast<uint32_t>(head->byte_offset_);
51       }
52       write_timestamps_callback_g(head->trace_context_, ts, error);
53     }
54     to_be_freed = head;
55     head = head->next_;
56     delete to_be_freed;
57   }
58 }
59 
grpc_http2_set_write_timestamps_callback(void (* fn)(void *,grpc_core::Timestamps *,grpc_error * error))60 void grpc_http2_set_write_timestamps_callback(void (*fn)(void*,
61                                                          grpc_core::Timestamps*,
62                                                          grpc_error* error)) {
63   write_timestamps_callback_g = fn;
64 }
65 
grpc_http2_set_fn_get_copied_context(void * (* fn)(void *))66 void grpc_http2_set_fn_get_copied_context(void* (*fn)(void*)) {
67   get_copied_context_fn_g = fn;
68 }
69 } /* namespace grpc_core */
70