gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
stream_op.h
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_INTERNAL_CORE_TRANSPORT_STREAM_OP_H
35 #define GRPC_INTERNAL_CORE_TRANSPORT_STREAM_OP_H
36 
37 #include <grpc/grpc.h>
38 #include <grpc/support/port_platform.h>
39 #include <grpc/support/slice.h>
40 #include <grpc/support/time.h>
41 #include "src/core/transport/metadata.h"
42 
43 /* this many stream ops are inlined into a sopb before allocating */
44 #define GRPC_SOPB_INLINE_ELEMENTS 16
45 
46 /* Operations that can be performed on a stream.
47  Used by grpc_stream_op. */
48 typedef enum grpc_stream_op_code {
49  /* Do nothing code. Useful if rewriting a batch to exclude some operations.
50  Must be ignored by receivers */
51  GRPC_NO_OP,
52  GRPC_OP_METADATA,
53  /* Begin a message/metadata element/status - as defined by
54  grpc_message_type. */
55  GRPC_OP_BEGIN_MESSAGE,
56  /* Add a slice of data to the current message/metadata element/status.
57  Must not overflow the forward declared length. */
58  GRPC_OP_SLICE
59 } grpc_stream_op_code;
60 
61 /* Arguments for GRPC_OP_BEGIN */
62 typedef struct grpc_begin_message {
63  /* How many bytes of data will this message contain */
64  gpr_uint32 length;
65  /* Write flags for the message: see grpc.h GRPC_WRITE_xxx */
66  gpr_uint32 flags;
68 
69 typedef struct grpc_linked_mdelem {
70  grpc_mdelem *md;
71  struct grpc_linked_mdelem *next;
72  struct grpc_linked_mdelem *prev;
74 
75 typedef struct grpc_mdelem_list {
76  grpc_linked_mdelem *head;
77  grpc_linked_mdelem *tail;
79 
80 typedef struct grpc_metadata_batch {
81  grpc_mdelem_list list;
82  grpc_mdelem_list garbage;
83  gpr_timespec deadline;
85 
86 void grpc_metadata_batch_init(grpc_metadata_batch *comd);
87 void grpc_metadata_batch_destroy(grpc_metadata_batch *comd);
88 void grpc_metadata_batch_merge(grpc_metadata_batch *target,
89  grpc_metadata_batch *add);
90 
91 void grpc_metadata_batch_link_head(grpc_metadata_batch *comd,
92  grpc_linked_mdelem *storage);
93 void grpc_metadata_batch_link_tail(grpc_metadata_batch *comd,
94  grpc_linked_mdelem *storage);
95 
96 void grpc_metadata_batch_add_head(grpc_metadata_batch *comd,
97  grpc_linked_mdelem *storage,
98  grpc_mdelem *elem_to_add);
99 void grpc_metadata_batch_add_tail(grpc_metadata_batch *comd,
100  grpc_linked_mdelem *storage,
101  grpc_mdelem *elem_to_add);
102 
103 void grpc_metadata_batch_filter(grpc_metadata_batch *comd,
104  grpc_mdelem *(*filter)(void *user_data,
105  grpc_mdelem *elem),
106  void *user_data);
107 
108 #ifndef NDEBUG
109 void grpc_metadata_batch_assert_ok(grpc_metadata_batch *comd);
110 #else
111 #define grpc_metadata_batch_assert_ok(comd) \
112  do { \
113  } while (0)
114 #endif
115 
116 /* Represents a single operation performed on a stream/transport */
117 typedef struct grpc_stream_op {
118  /* the operation to be applied */
119  enum grpc_stream_op_code type;
120  /* the arguments to this operation. union fields are named according to the
121  associated op-code */
122  union {
123  grpc_begin_message begin_message;
124  grpc_metadata_batch metadata;
125  gpr_slice slice;
126  } data;
128 
129 /* A stream op buffer is a wrapper around stream operations that is dynamically
130  extendable.
131  TODO(ctiller): inline a few elements into the struct, to avoid common case
132  per-call allocations. */
133 typedef struct grpc_stream_op_buffer {
134  grpc_stream_op *ops;
135  size_t nops;
136  size_t capacity;
137  grpc_stream_op inlined_ops[GRPC_SOPB_INLINE_ELEMENTS];
139 
140 /* Initialize a stream op buffer */
141 void grpc_sopb_init(grpc_stream_op_buffer *sopb);
142 /* Destroy a stream op buffer */
143 void grpc_sopb_destroy(grpc_stream_op_buffer *sopb);
144 /* Reset a sopb to no elements */
145 void grpc_sopb_reset(grpc_stream_op_buffer *sopb);
146 /* Swap two sopbs */
147 void grpc_sopb_swap(grpc_stream_op_buffer *a, grpc_stream_op_buffer *b);
148 
149 void grpc_stream_ops_unref_owned_objects(grpc_stream_op *ops, size_t nops);
150 
151 /* Append a GRPC_NO_OP to a buffer */
152 void grpc_sopb_add_no_op(grpc_stream_op_buffer *sopb);
153 /* Append a GRPC_OP_BEGIN to a buffer */
154 void grpc_sopb_add_begin_message(grpc_stream_op_buffer *sopb, gpr_uint32 length,
155  gpr_uint32 flags);
156 void grpc_sopb_add_metadata(grpc_stream_op_buffer *sopb,
157  grpc_metadata_batch metadata);
158 /* Append a GRPC_SLICE to a buffer - does not ref/unref the slice */
159 void grpc_sopb_add_slice(grpc_stream_op_buffer *sopb, gpr_slice slice);
160 /* Append a buffer to a buffer - does not ref/unref any internal objects */
161 void grpc_sopb_append(grpc_stream_op_buffer *sopb, grpc_stream_op *ops,
162  size_t nops);
163 
164 char *grpc_sopb_string(grpc_stream_op_buffer *sopb);
165 
166 #endif /* GRPC_INTERNAL_CORE_TRANSPORT_STREAM_OP_H */
Definition: stream_op.h:80
Definition: metadata.h:78
Definition: stream_op.h:69
Definition: stream_op.h:62
Definition: time.h:48
Definition: stream_op.h:117
Definition: stream_op.h:133
Definition: slice.h:79
Definition: stream_op.h:75