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