blob: ea1e96cf1d5b1e1a7b0adc44e45bb177fbe9ad69 [file] [log] [blame]
David Garcia Quintas2425bbb2016-01-25 17:32:48 -08001/*
2 *
3 * Copyright 2015-2016, 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_IMPL_CODEGEN_GRPC_TYPES_H
35#define GRPC_IMPL_CODEGEN_GRPC_TYPES_H
36
37#include <grpc/impl/codegen/byte_buffer.h>
38#include <grpc/impl/codegen/status.h>
39
40#include <stddef.h>
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/** Completion Queues enable notification of the completion of asynchronous
47 actions. */
48typedef struct grpc_completion_queue grpc_completion_queue;
49
50/** An alarm associated with a completion queue. */
51typedef struct grpc_alarm grpc_alarm;
52
53/** The Channel interface allows creation of Call objects. */
54typedef struct grpc_channel grpc_channel;
55
56/** A server listens to some port and responds to request calls */
57typedef struct grpc_server grpc_server;
58
59/** A Call represents an RPC. When created, it is in a configuration state
60 allowing properties to be set until it is invoked. After invoke, the Call
61 can have messages written to it and read from it. */
62typedef struct grpc_call grpc_call;
63
64/** Type specifier for grpc_arg */
65typedef enum {
66 GRPC_ARG_STRING,
67 GRPC_ARG_INTEGER,
68 GRPC_ARG_POINTER
69} grpc_arg_type;
70
71/** A single argument... each argument has a key and a value
72
73 A note on naming keys:
74 Keys are namespaced into groups, usually grouped by library, and are
75 keys for module XYZ are named XYZ.key1, XYZ.key2, etc. Module names must
76 be restricted to the regex [A-Za-z][_A-Za-z0-9]{,15}.
77 Key names must be restricted to the regex [A-Za-z][_A-Za-z0-9]{,47}.
78
79 GRPC core library keys are prefixed by grpc.
80
81 Library authors are strongly encouraged to \#define symbolic constants for
82 their keys so that it's possible to change them in the future. */
83typedef struct {
84 grpc_arg_type type;
85 char *key;
86 union {
87 char *string;
88 int integer;
89 struct {
90 void *p;
91 void *(*copy)(void *p);
92 void (*destroy)(void *p);
93 } pointer;
94 } value;
95} grpc_arg;
96
97/** An array of arguments that can be passed around.
98
99 Used to set optional channel-level configuration.
100 These configuration options are modelled as key-value pairs as defined
101 by grpc_arg; keys are strings to allow easy backwards-compatible extension
102 by arbitrary parties.
103 All evaluation is performed at channel creation time (i.e. the values in
104 this structure need only live through the creation invocation). */
105typedef struct {
106 size_t num_args;
107 grpc_arg *args;
108} grpc_channel_args;
109
110/* Channel argument keys: */
111/** Enable census for tracing and stats collection */
112#define GRPC_ARG_ENABLE_CENSUS "grpc.census"
113/** Maximum number of concurrent incoming streams to allow on a http2
114 connection */
115#define GRPC_ARG_MAX_CONCURRENT_STREAMS "grpc.max_concurrent_streams"
116/** Maximum message length that the channel can receive */
117#define GRPC_ARG_MAX_MESSAGE_LENGTH "grpc.max_message_length"
118/** Initial sequence number for http2 transports */
119#define GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER \
120 "grpc.http2.initial_sequence_number"
121/** Amount to read ahead on individual streams. Defaults to 64kb, larger
122 values can help throughput on high-latency connections.
123 NOTE: at some point we'd like to auto-tune this, and this parameter
124 will become a no-op. */
125#define GRPC_ARG_HTTP2_STREAM_LOOKAHEAD_BYTES "grpc.http2.lookahead_bytes"
126/** How much memory to use for hpack decoding */
127#define GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER \
128 "grpc.http2.hpack_table_size.decoder"
129/** How much memory to use for hpack encoding */
130#define GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER \
131 "grpc.http2.hpack_table_size.encoder"
132/** Default authority to pass if none specified on call construction */
133#define GRPC_ARG_DEFAULT_AUTHORITY "grpc.default_authority"
134/** Primary user agent: goes at the start of the user-agent metadata
135 sent on each request */
136#define GRPC_ARG_PRIMARY_USER_AGENT_STRING "grpc.primary_user_agent"
137/** Secondary user agent: goes at the end of the user-agent metadata
138 sent on each request */
139#define GRPC_ARG_SECONDARY_USER_AGENT_STRING "grpc.secondary_user_agent"
140/* The caller of the secure_channel_create functions may override the target
141 name used for SSL host name checking using this channel argument which is of
142 type GRPC_ARG_STRING. This *should* be used for testing only.
143 If this argument is not specified, the name used for SSL host name checking
144 will be the target parameter (assuming that the secure channel is an SSL
145 channel). If this parameter is specified and the underlying is not an SSL
146 channel, it will just be ignored. */
147#define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG "grpc.ssl_target_name_override"
148
149/** Result of a grpc call. If the caller satisfies the prerequisites of a
150 particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
151 Receiving any other value listed here is an indication of a bug in the
152 caller. */
153typedef enum grpc_call_error {
154 /** everything went ok */
155 GRPC_CALL_OK = 0,
156 /** something failed, we don't know what */
157 GRPC_CALL_ERROR,
158 /** this method is not available on the server */
159 GRPC_CALL_ERROR_NOT_ON_SERVER,
160 /** this method is not available on the client */
161 GRPC_CALL_ERROR_NOT_ON_CLIENT,
162 /** this method must be called before server_accept */
163 GRPC_CALL_ERROR_ALREADY_ACCEPTED,
164 /** this method must be called before invoke */
165 GRPC_CALL_ERROR_ALREADY_INVOKED,
166 /** this method must be called after invoke */
167 GRPC_CALL_ERROR_NOT_INVOKED,
168 /** this call is already finished
169 (writes_done or write_status has already been called) */
170 GRPC_CALL_ERROR_ALREADY_FINISHED,
171 /** there is already an outstanding read/write operation on the call */
172 GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
173 /** the flags value was illegal for this call */
174 GRPC_CALL_ERROR_INVALID_FLAGS,
175 /** invalid metadata was passed to this call */
176 GRPC_CALL_ERROR_INVALID_METADATA,
177 /** invalid message was passed to this call */
178 GRPC_CALL_ERROR_INVALID_MESSAGE,
179 /** completion queue for notification has not been registered with the
180 server */
181 GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE,
182 /** this batch of operations leads to more operations than allowed */
183 GRPC_CALL_ERROR_BATCH_TOO_BIG
184} grpc_call_error;
185
186/* Write Flags: */
187/** Hint that the write may be buffered and need not go out on the wire
188 immediately. GRPC is free to buffer the message until the next non-buffered
189 write, or until writes_done, but it need not buffer completely or at all. */
190#define GRPC_WRITE_BUFFER_HINT (0x00000001u)
191/** Force compression to be disabled for a particular write
192 (start_write/add_metadata). Illegal on invoke/accept. */
193#define GRPC_WRITE_NO_COMPRESS (0x00000002u)
194/** Mask of all valid flags. */
195#define GRPC_WRITE_USED_MASK (GRPC_WRITE_BUFFER_HINT | GRPC_WRITE_NO_COMPRESS)
196
197/** A single metadata element */
198typedef struct grpc_metadata {
199 const char *key;
200 const char *value;
201 size_t value_length;
202 uint32_t flags;
203
204 /** The following fields are reserved for grpc internal use.
205 There is no need to initialize them, and they will be set to garbage
206 during calls to grpc. */
207 struct {
208 void *obfuscated[4];
209 } internal_data;
210} grpc_metadata;
211
212/** The type of completion (for grpc_event) */
213typedef enum grpc_completion_type {
214 /** Shutting down */
215 GRPC_QUEUE_SHUTDOWN,
216 /** No event before timeout */
217 GRPC_QUEUE_TIMEOUT,
218 /** Operation completion */
219 GRPC_OP_COMPLETE
220} grpc_completion_type;
221
222/** The result of an operation.
223
224 Returned by a completion queue when the operation started with tag. */
225typedef struct grpc_event {
226 /** The type of the completion. */
227 grpc_completion_type type;
228 /** non-zero if the operation was successful, 0 upon failure.
229 Only GRPC_OP_COMPLETE can succeed or fail. */
230 int success;
231 /** The tag passed to grpc_call_start_batch etc to start this operation.
232 Only GRPC_OP_COMPLETE has a tag. */
233 void *tag;
234} grpc_event;
235
236typedef struct {
237 size_t count;
238 size_t capacity;
239 grpc_metadata *metadata;
240} grpc_metadata_array;
241
242typedef struct {
243 char *method;
244 size_t method_capacity;
245 char *host;
246 size_t host_capacity;
247 gpr_timespec deadline;
248 void *reserved;
249} grpc_call_details;
250
251typedef enum {
252 /** Send initial metadata: one and only one instance MUST be sent for each
253 call, unless the call was cancelled - in which case this can be skipped.
254 This op completes after all bytes of metadata have been accepted by
255 outgoing flow control. */
256 GRPC_OP_SEND_INITIAL_METADATA = 0,
257 /** Send a message: 0 or more of these operations can occur for each call.
258 This op completes after all bytes for the message have been accepted by
259 outgoing flow control. */
260 GRPC_OP_SEND_MESSAGE,
261 /** Send a close from the client: one and only one instance MUST be sent from
262 the client, unless the call was cancelled - in which case this can be
263 skipped.
264 This op completes after all bytes for the call (including the close)
265 have passed outgoing flow control. */
266 GRPC_OP_SEND_CLOSE_FROM_CLIENT,
267 /** Send status from the server: one and only one instance MUST be sent from
268 the server unless the call was cancelled - in which case this can be
269 skipped.
270 This op completes after all bytes for the call (including the status)
271 have passed outgoing flow control. */
272 GRPC_OP_SEND_STATUS_FROM_SERVER,
273 /** Receive initial metadata: one and only one MUST be made on the client,
274 must not be made on the server.
275 This op completes after all initial metadata has been read from the
276 peer. */
277 GRPC_OP_RECV_INITIAL_METADATA,
278 /** Receive a message: 0 or more of these operations can occur for each call.
279 This op completes after all bytes of the received message have been
280 read, or after a half-close has been received on this call. */
281 GRPC_OP_RECV_MESSAGE,
282 /** Receive status on the client: one and only one must be made on the client.
283 This operation always succeeds, meaning ops paired with this operation
284 will also appear to succeed, even though they may not have. In that case
285 the status will indicate some failure.
286 This op completes after all activity on the call has completed. */
287 GRPC_OP_RECV_STATUS_ON_CLIENT,
288 /** Receive close on the server: one and only one must be made on the
289 server.
290 This op completes after the close has been received by the server. */
291 GRPC_OP_RECV_CLOSE_ON_SERVER
292} grpc_op_type;
293
294/** Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT
295 which has no arguments) */
296typedef struct grpc_op {
297 /** Operation type, as defined by grpc_op_type */
298 grpc_op_type op;
299 /** Write flags bitset for grpc_begin_messages */
300 uint32_t flags;
301 /** Reserved for future usage */
302 void *reserved;
303 union {
304 /** Reserved for future usage */
305 struct {
306 void *reserved[8];
307 } reserved;
308 struct {
309 size_t count;
310 grpc_metadata *metadata;
311 } send_initial_metadata;
312 grpc_byte_buffer *send_message;
313 struct {
314 size_t trailing_metadata_count;
315 grpc_metadata *trailing_metadata;
316 grpc_status_code status;
317 const char *status_details;
318 } send_status_from_server;
319 /** ownership of the array is with the caller, but ownership of the elements
320 stays with the call object (ie key, value members are owned by the call
321 object, recv_initial_metadata->array is owned by the caller).
322 After the operation completes, call grpc_metadata_array_destroy on this
323 value, or reuse it in a future op. */
324 grpc_metadata_array *recv_initial_metadata;
325 /** ownership of the byte buffer is moved to the caller; the caller must
326 call grpc_byte_buffer_destroy on this value, or reuse it in a future op.
327 */
328 grpc_byte_buffer **recv_message;
329 struct {
330 /** ownership of the array is with the caller, but ownership of the
331 elements stays with the call object (ie key, value members are owned
332 by the call object, trailing_metadata->array is owned by the caller).
333 After the operation completes, call grpc_metadata_array_destroy on
334 this
335 value, or reuse it in a future op. */
336 grpc_metadata_array *trailing_metadata;
337 grpc_status_code *status;
338 /** status_details is a buffer owned by the application before the op
339 completes and after the op has completed. During the operation
340 status_details may be reallocated to a size larger than
341 *status_details_capacity, in which case *status_details_capacity will
342 be updated with the new array capacity.
343
344 Pre-allocating space:
345 size_t my_capacity = 8;
346 char *my_details = gpr_malloc(my_capacity);
347 x.status_details = &my_details;
348 x.status_details_capacity = &my_capacity;
349
350 Not pre-allocating space:
351 size_t my_capacity = 0;
352 char *my_details = NULL;
353 x.status_details = &my_details;
354 x.status_details_capacity = &my_capacity;
355
356 After the call:
357 gpr_free(my_details); */
358 char **status_details;
359 size_t *status_details_capacity;
360 } recv_status_on_client;
361 struct {
362 /** out argument, set to 1 if the call failed in any way (seen as a
363 cancellation on the server), or 0 if the call succeeded */
364 int *cancelled;
365 } recv_close_on_server;
366 } data;
367} grpc_op;
368
369#ifdef __cplusplus
370}
371#endif
372
373#endif /* GRPC_IMPL_CODEGEN_GRPC_TYPES_H */