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 #ifndef GRPC_CORE_LIB_TRANSPORT_METADATA_BATCH_H
     20 #define GRPC_CORE_LIB_TRANSPORT_METADATA_BATCH_H
     21 
     22 #include <grpc/support/port_platform.h>
     23 
     24 #include <stdbool.h>
     25 
     26 #include <grpc/grpc.h>
     27 #include <grpc/slice.h>
     28 #include <grpc/support/time.h>
     29 #include "src/core/lib/iomgr/exec_ctx.h"
     30 #include "src/core/lib/transport/metadata.h"
     31 #include "src/core/lib/transport/static_metadata.h"
     32 
     33 typedef struct grpc_linked_mdelem {
     34   grpc_mdelem md;
     35   struct grpc_linked_mdelem* next;
     36   struct grpc_linked_mdelem* prev;
     37   void* reserved;
     38 } grpc_linked_mdelem;
     39 
     40 typedef struct grpc_mdelem_list {
     41   size_t count;
     42   size_t default_count;  // Number of default keys.
     43   grpc_linked_mdelem* head;
     44   grpc_linked_mdelem* tail;
     45 } grpc_mdelem_list;
     46 
     47 typedef struct grpc_metadata_batch {
     48   /** Metadata elements in this batch */
     49   grpc_mdelem_list list;
     50   grpc_metadata_batch_callouts idx;
     51   /** Used to calculate grpc-timeout at the point of sending,
     52       or GRPC_MILLIS_INF_FUTURE if this batch does not need to send a
     53       grpc-timeout */
     54   grpc_millis deadline;
     55 } grpc_metadata_batch;
     56 
     57 void grpc_metadata_batch_init(grpc_metadata_batch* batch);
     58 void grpc_metadata_batch_destroy(grpc_metadata_batch* batch);
     59 void grpc_metadata_batch_clear(grpc_metadata_batch* batch);
     60 bool grpc_metadata_batch_is_empty(grpc_metadata_batch* batch);
     61 
     62 /* Returns the transport size of the batch. */
     63 size_t grpc_metadata_batch_size(grpc_metadata_batch* batch);
     64 
     65 /** Remove \a storage from the batch, unreffing the mdelem contained */
     66 void grpc_metadata_batch_remove(grpc_metadata_batch* batch,
     67                                 grpc_linked_mdelem* storage);
     68 
     69 /** Substitute a new mdelem for an old value */
     70 grpc_error* grpc_metadata_batch_substitute(grpc_metadata_batch* batch,
     71                                            grpc_linked_mdelem* storage,
     72                                            grpc_mdelem new_value);
     73 
     74 void grpc_metadata_batch_set_value(grpc_linked_mdelem* storage,
     75                                    grpc_slice value);
     76 
     77 /** Add \a storage to the beginning of \a batch. storage->md is
     78     assumed to be valid.
     79     \a storage is owned by the caller and must survive for the
     80     lifetime of batch. This usually means it should be around
     81     for the lifetime of the call. */
     82 grpc_error* grpc_metadata_batch_link_head(grpc_metadata_batch* batch,
     83                                           grpc_linked_mdelem* storage)
     84     GRPC_MUST_USE_RESULT;
     85 
     86 /** Add \a storage to the end of \a batch. storage->md is
     87     assumed to be valid.
     88     \a storage is owned by the caller and must survive for the
     89     lifetime of batch. This usually means it should be around
     90     for the lifetime of the call. */
     91 grpc_error* grpc_metadata_batch_link_tail(grpc_metadata_batch* batch,
     92                                           grpc_linked_mdelem* storage)
     93     GRPC_MUST_USE_RESULT;
     94 
     95 /** Add \a elem_to_add as the first element in \a batch, using
     96     \a storage as backing storage for the linked list element.
     97     \a storage is owned by the caller and must survive for the
     98     lifetime of batch. This usually means it should be around
     99     for the lifetime of the call.
    100     Takes ownership of \a elem_to_add */
    101 grpc_error* grpc_metadata_batch_add_head(
    102     grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
    103     grpc_mdelem elem_to_add) GRPC_MUST_USE_RESULT;
    104 
    105 /** Add \a elem_to_add as the last element in \a batch, using
    106     \a storage as backing storage for the linked list element.
    107     \a storage is owned by the caller and must survive for the
    108     lifetime of batch. This usually means it should be around
    109     for the lifetime of the call.
    110     Takes ownership of \a elem_to_add */
    111 grpc_error* grpc_metadata_batch_add_tail(
    112     grpc_metadata_batch* batch, grpc_linked_mdelem* storage,
    113     grpc_mdelem elem_to_add) GRPC_MUST_USE_RESULT;
    114 
    115 grpc_error* grpc_attach_md_to_error(grpc_error* src, grpc_mdelem md);
    116 
    117 typedef struct {
    118   grpc_error* error;
    119   grpc_mdelem md;
    120 } grpc_filtered_mdelem;
    121 
    122 #define GRPC_FILTERED_ERROR(error) \
    123   { (error), GRPC_MDNULL }
    124 #define GRPC_FILTERED_MDELEM(md) \
    125   { GRPC_ERROR_NONE, (md) }
    126 #define GRPC_FILTERED_REMOVE() \
    127   { GRPC_ERROR_NONE, GRPC_MDNULL }
    128 
    129 typedef grpc_filtered_mdelem (*grpc_metadata_batch_filter_func)(
    130     void* user_data, grpc_mdelem elem);
    131 grpc_error* grpc_metadata_batch_filter(
    132     grpc_metadata_batch* batch, grpc_metadata_batch_filter_func func,
    133     void* user_data, const char* composite_error_string) GRPC_MUST_USE_RESULT;
    134 
    135 #ifndef NDEBUG
    136 void grpc_metadata_batch_assert_ok(grpc_metadata_batch* comd);
    137 #else
    138 #define grpc_metadata_batch_assert_ok(comd) \
    139   do {                                      \
    140   } while (0)
    141 #endif
    142 
    143 /// Copies \a src to \a dst.  \a storage must point to an array of
    144 /// \a grpc_linked_mdelem structs of at least the same size as \a src.
    145 void grpc_metadata_batch_copy(grpc_metadata_batch* src,
    146                               grpc_metadata_batch* dst,
    147                               grpc_linked_mdelem* storage);
    148 
    149 void grpc_metadata_batch_move(grpc_metadata_batch* src,
    150                               grpc_metadata_batch* dst);
    151 
    152 #endif /* GRPC_CORE_LIB_TRANSPORT_METADATA_BATCH_H */
    153