Home | History | Annotate | Download | only in transport
      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 "src/core/ext/transport/chttp2/transport/incoming_metadata.h"
     22 
     23 #include <string.h>
     24 
     25 #include "src/core/ext/transport/chttp2/transport/internal.h"
     26 
     27 #include <grpc/support/alloc.h>
     28 #include <grpc/support/log.h>
     29 
     30 void grpc_chttp2_incoming_metadata_buffer_init(
     31     grpc_chttp2_incoming_metadata_buffer* buffer, gpr_arena* arena) {
     32   buffer->arena = arena;
     33   grpc_metadata_batch_init(&buffer->batch);
     34   buffer->batch.deadline = GRPC_MILLIS_INF_FUTURE;
     35 }
     36 
     37 void grpc_chttp2_incoming_metadata_buffer_destroy(
     38     grpc_chttp2_incoming_metadata_buffer* buffer) {
     39   grpc_metadata_batch_destroy(&buffer->batch);
     40 }
     41 
     42 grpc_error* grpc_chttp2_incoming_metadata_buffer_add(
     43     grpc_chttp2_incoming_metadata_buffer* buffer, grpc_mdelem elem) {
     44   buffer->size += GRPC_MDELEM_LENGTH(elem);
     45   return grpc_metadata_batch_add_tail(
     46       &buffer->batch,
     47       static_cast<grpc_linked_mdelem*>(
     48           gpr_arena_alloc(buffer->arena, sizeof(grpc_linked_mdelem))),
     49       elem);
     50 }
     51 
     52 grpc_error* grpc_chttp2_incoming_metadata_buffer_replace_or_add(
     53     grpc_chttp2_incoming_metadata_buffer* buffer, grpc_mdelem elem) {
     54   for (grpc_linked_mdelem* l = buffer->batch.list.head; l != nullptr;
     55        l = l->next) {
     56     if (grpc_slice_eq(GRPC_MDKEY(l->md), GRPC_MDKEY(elem))) {
     57       GRPC_MDELEM_UNREF(l->md);
     58       l->md = elem;
     59       return GRPC_ERROR_NONE;
     60     }
     61   }
     62   return grpc_chttp2_incoming_metadata_buffer_add(buffer, elem);
     63 }
     64 
     65 void grpc_chttp2_incoming_metadata_buffer_set_deadline(
     66     grpc_chttp2_incoming_metadata_buffer* buffer, grpc_millis deadline) {
     67   buffer->batch.deadline = deadline;
     68 }
     69 
     70 void grpc_chttp2_incoming_metadata_buffer_publish(
     71     grpc_chttp2_incoming_metadata_buffer* buffer, grpc_metadata_batch* batch) {
     72   grpc_metadata_batch_move(&buffer->batch, batch);
     73 }
     74