blob: b05e4d65de547f04d462a4bf7dba8a1e2312c23b [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPC_GRPC_H
35#define GRPC_GRPC_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <grpc/status.h>
38
39#include <stddef.h>
David Garcia Quintas25d02d52015-06-04 17:40:54 -070040#include <grpc/byte_buffer.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include <grpc/support/slice.h>
42#include <grpc/support/time.h>
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
Craig Tiller2d984bf2015-07-20 15:01:38 -070048/*! \mainpage GRPC Core
49 *
50 * \section intro_sec The GRPC Core library is a low-level library designed
51 * to be wrapped by higher level libraries.
52 *
53 * The top-level API is provided in grpc.h.
54 * Security related functionality lives in grpc_security.h.
55 */
56
57/** Completion Queues enable notification of the completion of asynchronous
58 actions. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059typedef struct grpc_completion_queue grpc_completion_queue;
60
Craig Tiller2d984bf2015-07-20 15:01:38 -070061/** The Channel interface allows creation of Call objects. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062typedef struct grpc_channel grpc_channel;
63
Craig Tiller2d984bf2015-07-20 15:01:38 -070064/** A server listens to some port and responds to request calls */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065typedef struct grpc_server grpc_server;
66
Craig Tiller2d984bf2015-07-20 15:01:38 -070067/** A Call represents an RPC. When created, it is in a configuration state
68 allowing properties to be set until it is invoked. After invoke, the Call
69 can have messages written to it and read from it. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070typedef struct grpc_call grpc_call;
71
Craig Tiller2d984bf2015-07-20 15:01:38 -070072/** Type specifier for grpc_arg */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073typedef enum {
74 GRPC_ARG_STRING,
75 GRPC_ARG_INTEGER,
76 GRPC_ARG_POINTER
77} grpc_arg_type;
78
Craig Tiller2d984bf2015-07-20 15:01:38 -070079/** A single argument... each argument has a key and a value
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080
Craig Tiller2d984bf2015-07-20 15:01:38 -070081 A note on naming keys:
82 Keys are namespaced into groups, usually grouped by library, and are
83 keys for module XYZ are named XYZ.key1, XYZ.key2, etc. Module names must
84 be restricted to the regex [A-Za-z][_A-Za-z0-9]{,15}.
85 Key names must be restricted to the regex [A-Za-z][_A-Za-z0-9]{,47}.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086
Craig Tiller2d984bf2015-07-20 15:01:38 -070087 GRPC core library keys are prefixed by grpc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088
Craig Tiller2d984bf2015-07-20 15:01:38 -070089 Library authors are strongly encouraged to \#define symbolic constants for
90 their keys so that it's possible to change them in the future. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091typedef struct {
92 grpc_arg_type type;
93 char *key;
94 union {
95 char *string;
96 int integer;
97 struct {
98 void *p;
99 void *(*copy)(void *p);
100 void (*destroy)(void *p);
101 } pointer;
102 } value;
103} grpc_arg;
104
Craig Tillere793ba12015-05-18 09:37:22 -0700105/** An array of arguments that can be passed around.
David Garcia Quintas02c677c2015-06-02 14:40:07 -0700106
Craig Tillere793ba12015-05-18 09:37:22 -0700107 Used to set optional channel-level configuration.
108 These configuration options are modelled as key-value pairs as defined
109 by grpc_arg; keys are strings to allow easy backwards-compatible extension
110 by arbitrary parties.
Masood Malekghassemi7128a902015-06-17 13:17:58 -0700111 All evaluation is performed at channel creation time (i.e. the values in
112 this structure need only live through the creation invocation). */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113typedef struct {
114 size_t num_args;
115 grpc_arg *args;
116} grpc_channel_args;
117
118/* Channel argument keys: */
Craig Tiller2d984bf2015-07-20 15:01:38 -0700119/** Enable census for tracing and stats collection */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120#define GRPC_ARG_ENABLE_CENSUS "grpc.census"
Craig Tiller2d984bf2015-07-20 15:01:38 -0700121/** Maximum number of concurrent incoming streams to allow on a http2
122 connection */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800123#define GRPC_ARG_MAX_CONCURRENT_STREAMS "grpc.max_concurrent_streams"
Craig Tiller2d984bf2015-07-20 15:01:38 -0700124/** Maximum message length that the channel can receive */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125#define GRPC_ARG_MAX_MESSAGE_LENGTH "grpc.max_message_length"
Craig Tiller2d984bf2015-07-20 15:01:38 -0700126/** Initial sequence number for http2 transports */
Craig Tiller88025582015-05-04 09:41:10 -0700127#define GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER \
128 "grpc.http2.initial_sequence_number"
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700129/** Primary user agent: goes at the start of the user-agent metadata
130 sent on each request */
131#define GRPC_ARG_PRIMARY_USER_AGENT_STRING "grpc.primary_user_agent"
132/** Secondary user agent: goes at the end of the user-agent metadata
133 sent on each request */
134#define GRPC_ARG_SECONDARY_USER_AGENT_STRING "grpc.secondary_user_agent"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135
Craig Tiller3f475422015-06-25 10:43:05 -0700136/** Connectivity state of a channel. */
137typedef enum {
Craig Tiller11bf14e2015-06-29 16:35:41 -0700138 /** channel is idle */
139 GRPC_CHANNEL_IDLE,
Craig Tiller3f475422015-06-25 10:43:05 -0700140 /** channel is connecting */
141 GRPC_CHANNEL_CONNECTING,
142 /** channel is ready for work */
143 GRPC_CHANNEL_READY,
144 /** channel has seen a failure but expects to recover */
145 GRPC_CHANNEL_TRANSIENT_FAILURE,
Craig Tiller3f475422015-06-25 10:43:05 -0700146 /** channel has seen a failure that it cannot recover from */
147 GRPC_CHANNEL_FATAL_FAILURE
148} grpc_connectivity_state;
149
Craig Tiller2d984bf2015-07-20 15:01:38 -0700150/** Result of a grpc call. If the caller satisfies the prerequisites of a
151 particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
152 Receiving any other value listed here is an indication of a bug in the
153 caller. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800154typedef enum grpc_call_error {
Craig Tiller2d984bf2015-07-20 15:01:38 -0700155 /** everything went ok */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800156 GRPC_CALL_OK = 0,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700157 /** something failed, we don't know what */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158 GRPC_CALL_ERROR,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700159 /** this method is not available on the server */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160 GRPC_CALL_ERROR_NOT_ON_SERVER,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700161 /** this method is not available on the client */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800162 GRPC_CALL_ERROR_NOT_ON_CLIENT,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700163 /** this method must be called before server_accept */
nnoble0c475f02014-12-05 15:37:39 -0800164 GRPC_CALL_ERROR_ALREADY_ACCEPTED,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700165 /** this method must be called before invoke */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166 GRPC_CALL_ERROR_ALREADY_INVOKED,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700167 /** this method must be called after invoke */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800168 GRPC_CALL_ERROR_NOT_INVOKED,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700169 /** this call is already finished
170 (writes_done or write_status has already been called) */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800171 GRPC_CALL_ERROR_ALREADY_FINISHED,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700172 /** there is already an outstanding read/write operation on the call */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800173 GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700174 /** the flags value was illegal for this call */
Craig Tillerb96d0012015-05-06 15:33:23 -0700175 GRPC_CALL_ERROR_INVALID_FLAGS,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700176 /** invalid metadata was passed to this call */
Craig Tillerb56975c2015-06-15 10:11:16 -0700177 GRPC_CALL_ERROR_INVALID_METADATA,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700178 /** completion queue for notification has not been registered with the
179 server */
Craig Tillerb56975c2015-06-15 10:11:16 -0700180 GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181} grpc_call_error;
182
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800183/* Write Flags: */
Craig Tiller2d984bf2015-07-20 15:01:38 -0700184/** Hint that the write may be buffered and need not go out on the wire
185 immediately. GRPC is free to buffer the message until the next non-buffered
186 write, or until writes_done, but it need not buffer completely or at all. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187#define GRPC_WRITE_BUFFER_HINT (0x00000001u)
Craig Tiller2d984bf2015-07-20 15:01:38 -0700188/** Force compression to be disabled for a particular write
189 (start_write/add_metadata). Illegal on invoke/accept. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800190#define GRPC_WRITE_NO_COMPRESS (0x00000002u)
Craig Tiller2d984bf2015-07-20 15:01:38 -0700191/** Mask of all valid flags. */
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700192#define GRPC_WRITE_USED_MASK (GRPC_WRITE_BUFFER_HINT | GRPC_WRITE_NO_COMPRESS)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800193
Craig Tiller2d984bf2015-07-20 15:01:38 -0700194/** A single metadata element */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195typedef struct grpc_metadata {
Craig Tiller4f972732015-02-05 12:40:20 -0800196 const char *key;
197 const char *value;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 size_t value_length;
Craig Tiller6902ad22015-04-16 08:01:49 -0700199
Craig Tiller2d984bf2015-07-20 15:01:38 -0700200 /** The following fields are reserved for grpc internal use.
201 There is no need to initialize them, and they will be set to garbage during
202 calls to grpc. */
Craig Tiller6902ad22015-04-16 08:01:49 -0700203 struct {
Craig Tiller9c9d4e02015-04-20 09:03:29 -0700204 void *obfuscated[3];
Craig Tiller6902ad22015-04-16 08:01:49 -0700205 } internal_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206} grpc_metadata;
207
Craig Tiller73b66ef2015-05-18 20:46:47 -0700208/** The type of completion (for grpc_event) */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209typedef enum grpc_completion_type {
Craig Tillere793ba12015-05-18 09:37:22 -0700210 /** Shutting down */
Craig Tiller8674cb12015-06-05 07:09:25 -0700211 GRPC_QUEUE_SHUTDOWN,
Craig Tillere793ba12015-05-18 09:37:22 -0700212 /** No event before timeout */
Craig Tiller8674cb12015-06-05 07:09:25 -0700213 GRPC_QUEUE_TIMEOUT,
Craig Tillere793ba12015-05-18 09:37:22 -0700214 /** Operation completion */
Craig Tiller8674cb12015-06-05 07:09:25 -0700215 GRPC_OP_COMPLETE
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800216} grpc_completion_type;
217
Craig Tillere793ba12015-05-18 09:37:22 -0700218/** The result of an operation.
219
220 Returned by a completion queue when the operation started with tag. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800221typedef struct grpc_event {
Craig Tillere793ba12015-05-18 09:37:22 -0700222 /** The type of the completion. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800223 grpc_completion_type type;
Craig Tiller8674cb12015-06-05 07:09:25 -0700224 /** non-zero if the operation was successful, 0 upon failure.
Craig Tillere793ba12015-05-18 09:37:22 -0700225 Only GRPC_OP_COMPLETE can succeed or fail. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700226 int success;
Craig Tillere793ba12015-05-18 09:37:22 -0700227 /** The tag passed to grpc_call_start_batch etc to start this operation.
228 Only GRPC_OP_COMPLETE has a tag. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800229 void *tag;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230} grpc_event;
231
Craig Tillerfef76692015-02-02 16:44:26 -0800232typedef struct {
233 size_t count;
234 size_t capacity;
235 grpc_metadata *metadata;
236} grpc_metadata_array;
237
Craig Tillerea61b072015-02-03 19:19:27 -0800238void grpc_metadata_array_init(grpc_metadata_array *array);
239void grpc_metadata_array_destroy(grpc_metadata_array *array);
240
Craig Tillerfef76692015-02-02 16:44:26 -0800241typedef struct {
Craig Tillerea61b072015-02-03 19:19:27 -0800242 char *method;
Craig Tiller034929c2015-02-02 16:56:15 -0800243 size_t method_capacity;
Craig Tillerea61b072015-02-03 19:19:27 -0800244 char *host;
Craig Tiller034929c2015-02-02 16:56:15 -0800245 size_t host_capacity;
Craig Tillerfef76692015-02-02 16:44:26 -0800246 gpr_timespec deadline;
247} grpc_call_details;
248
Craig Tillerea61b072015-02-03 19:19:27 -0800249void grpc_call_details_init(grpc_call_details *details);
250void grpc_call_details_destroy(grpc_call_details *details);
251
Craig Tillerfef76692015-02-02 16:44:26 -0800252typedef enum {
Craig Tiller2d984bf2015-07-20 15:01:38 -0700253 /** Send initial metadata: one and only one instance MUST be sent for each
254 call, unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800255 GRPC_OP_SEND_INITIAL_METADATA = 0,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700256 /** Send a message: 0 or more of these operations can occur for each call */
Craig Tillerfef76692015-02-02 16:44:26 -0800257 GRPC_OP_SEND_MESSAGE,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700258 /** Send a close from the client: one and only one instance MUST be sent from
259 the client, unless the call was cancelled - in which case this can be
260 skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800261 GRPC_OP_SEND_CLOSE_FROM_CLIENT,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700262 /** Send status from the server: one and only one instance MUST be sent from
263 the server unless the call was cancelled - in which case this can be
264 skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800265 GRPC_OP_SEND_STATUS_FROM_SERVER,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700266 /** Receive initial metadata: one and only one MUST be made on the client,
267 must not be made on the server */
Craig Tillerfef76692015-02-02 16:44:26 -0800268 GRPC_OP_RECV_INITIAL_METADATA,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700269 /** Receive a message: 0 or more of these operations can occur for each call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800270 GRPC_OP_RECV_MESSAGE,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700271 /** Receive status on the client: one and only one must be made on the client.
Craig Tiller69eed292015-05-19 10:43:05 -0700272 This operation always succeeds, meaning ops paired with this operation
273 will also appear to succeed, even though they may not have. In that case
Craig Tiller2d984bf2015-07-20 15:01:38 -0700274 the status will indicate some failure. */
Craig Tillerfef76692015-02-02 16:44:26 -0800275 GRPC_OP_RECV_STATUS_ON_CLIENT,
Craig Tiller2d984bf2015-07-20 15:01:38 -0700276 /** Receive close on the server: one and only one must be made on the
277 server */
Craig Tillerfef76692015-02-02 16:44:26 -0800278 GRPC_OP_RECV_CLOSE_ON_SERVER
279} grpc_op_type;
280
Craig Tiller2d984bf2015-07-20 15:01:38 -0700281/** Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT
282 which has no arguments) */
Craig Tillerfef76692015-02-02 16:44:26 -0800283typedef struct grpc_op {
Craig Tiller2d984bf2015-07-20 15:01:38 -0700284 /** Operation type, as defined by grpc_op_type */
Craig Tillerfef76692015-02-02 16:44:26 -0800285 grpc_op_type op;
Craig Tiller2d984bf2015-07-20 15:01:38 -0700286 /** Write flags bitset for grpc_begin_messages */
287 gpr_uint32 flags;
Craig Tillerfef76692015-02-02 16:44:26 -0800288 union {
289 struct {
290 size_t count;
Craig Tiller6902ad22015-04-16 08:01:49 -0700291 grpc_metadata *metadata;
Craig Tillerfef76692015-02-02 16:44:26 -0800292 } send_initial_metadata;
293 grpc_byte_buffer *send_message;
294 struct {
295 size_t trailing_metadata_count;
296 grpc_metadata *trailing_metadata;
297 grpc_status_code status;
298 const char *status_details;
299 } send_status_from_server;
Craig Tiller2d984bf2015-07-20 15:01:38 -0700300 /** ownership of the array is with the caller, but ownership of the elements
301 stays with the call object (ie key, value members are owned by the call
302 object, recv_initial_metadata->array is owned by the caller).
303 After the operation completes, call grpc_metadata_array_destroy on this
304 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800305 grpc_metadata_array *recv_initial_metadata;
Craig Tiller2d984bf2015-07-20 15:01:38 -0700306 /** ownership of the byte buffer is moved to the caller; the caller must call
307 grpc_byte_buffer_destroy on this value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800308 grpc_byte_buffer **recv_message;
309 struct {
Craig Tiller2d984bf2015-07-20 15:01:38 -0700310 /** ownership of the array is with the caller, but ownership of the
311 elements stays with the call object (ie key, value members are owned
312 by the call object, trailing_metadata->array is owned by the caller).
313 After the operation completes, call grpc_metadata_array_destroy on this
314 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800315 grpc_metadata_array *trailing_metadata;
316 grpc_status_code *status;
Craig Tiller2d984bf2015-07-20 15:01:38 -0700317 /** status_details is a buffer owned by the application before the op
318 completes and after the op has completed. During the operation
319 status_details may be reallocated to a size larger than
320 *status_details_capacity, in which case *status_details_capacity will
321 be updated with the new array capacity.
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800322
Craig Tiller2d984bf2015-07-20 15:01:38 -0700323 Pre-allocating space:
324 size_t my_capacity = 8;
325 char *my_details = gpr_malloc(my_capacity);
326 x.status_details = &my_details;
327 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800328
Craig Tiller2d984bf2015-07-20 15:01:38 -0700329 Not pre-allocating space:
330 size_t my_capacity = 0;
331 char *my_details = NULL;
332 x.status_details = &my_details;
333 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800334
Craig Tiller2d984bf2015-07-20 15:01:38 -0700335 After the call:
336 gpr_free(my_details); */
Craig Tillerfef76692015-02-02 16:44:26 -0800337 char **status_details;
338 size_t *status_details_capacity;
339 } recv_status_on_client;
340 struct {
Craig Tiller2d984bf2015-07-20 15:01:38 -0700341 /** out argument, set to 1 if the call failed in any way (seen as a
342 cancellation on the server), or 0 if the call succeeded */
Craig Tillerfef76692015-02-02 16:44:26 -0800343 int *cancelled;
344 } recv_close_on_server;
345 } data;
346} grpc_op;
347
Craig Tillere793ba12015-05-18 09:37:22 -0700348/** Initialize the grpc library.
Craig Tiller8674cb12015-06-05 07:09:25 -0700349
Craig Tillere793ba12015-05-18 09:37:22 -0700350 It is not safe to call any other grpc functions before calling this.
351 (To avoid overhead, little checking is done, and some things may work. We
352 do not warrant that they will continue to do so in future revisions of this
353 library). */
Craig Tiller32946d32015-01-15 11:37:30 -0800354void grpc_init(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800355
Craig Tillere793ba12015-05-18 09:37:22 -0700356/** Shut down the grpc library.
Craig Tiller8674cb12015-06-05 07:09:25 -0700357
Craig Tillere793ba12015-05-18 09:37:22 -0700358 No memory is used by grpc after this call returns, nor are any instructions
359 executing within the grpc library.
360 Prior to calling, all application owned grpc objects must have been
361 destroyed. */
Craig Tiller32946d32015-01-15 11:37:30 -0800362void grpc_shutdown(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800363
Craig Tiller2e622bc2015-07-10 07:46:03 -0700364/** Return a string representing the current version of grpc */
365const char *grpc_version_string(void);
366
Craig Tillere793ba12015-05-18 09:37:22 -0700367/** Create a completion queue */
Craig Tiller32946d32015-01-15 11:37:30 -0800368grpc_completion_queue *grpc_completion_queue_create(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800369
Craig Tillere793ba12015-05-18 09:37:22 -0700370/** Blocks until an event is available, the completion queue is being shut down,
Craig Tiller8674cb12015-06-05 07:09:25 -0700371 or deadline is reached.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800372
vjpai1854d772015-06-08 01:12:29 -0700373 Returns a grpc_event with type GRPC_QUEUE_TIMEOUT on timeout,
374 otherwise a grpc_event describing the event that occurred.
Craig Tillere793ba12015-05-18 09:37:22 -0700375
376 Callers must not call grpc_completion_queue_next and
377 grpc_completion_queue_pluck simultaneously on the same completion queue. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700378grpc_event grpc_completion_queue_next(grpc_completion_queue *cq,
379 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800380
Craig Tillere793ba12015-05-18 09:37:22 -0700381/** Blocks until an event with tag 'tag' is available, the completion queue is
Craig Tiller8674cb12015-06-05 07:09:25 -0700382 being shutdown or deadline is reached.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800383
vjpai1854d772015-06-08 01:12:29 -0700384 Returns a grpc_event with type GRPC_QUEUE_TIMEOUT on timeout,
385 otherwise a grpc_event describing the event that occurred.
Craig Tillere793ba12015-05-18 09:37:22 -0700386
387 Callers must not call grpc_completion_queue_next and
388 grpc_completion_queue_pluck simultaneously on the same completion queue. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700389grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag,
390 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800391
Craig Tiller2d984bf2015-07-20 15:01:38 -0700392/** Begin destruction of a completion queue. Once all possible events are
393 drained then grpc_completion_queue_next will start to produce
394 GRPC_QUEUE_SHUTDOWN events only. At that point it's safe to call
395 grpc_completion_queue_destroy.
Craig Tillerb20111c2015-04-10 23:27:11 +0000396
Craig Tiller2d984bf2015-07-20 15:01:38 -0700397 After calling this function applications should ensure that no
398 NEW work is added to be published on this completion queue. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800399void grpc_completion_queue_shutdown(grpc_completion_queue *cq);
400
Craig Tiller2d984bf2015-07-20 15:01:38 -0700401/** Destroy a completion queue. The caller must ensure that the queue is
402 drained and no threads are executing grpc_completion_queue_next */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800403void grpc_completion_queue_destroy(grpc_completion_queue *cq);
404
Craig Tiller2d984bf2015-07-20 15:01:38 -0700405/** Create a call given a grpc_channel, in order to call 'method'. All
406 completions are sent to 'completion_queue'. 'method' and 'host' need only
407 live through the invocation of this function. */
Craig Tillerfb189f82015-02-03 12:07:07 -0800408grpc_call *grpc_channel_create_call(grpc_channel *channel,
409 grpc_completion_queue *completion_queue,
410 const char *method, const char *host,
411 gpr_timespec deadline);
Craig Tiller034929c2015-02-02 16:56:15 -0800412
Craig Tiller2d984bf2015-07-20 15:01:38 -0700413/** Pre-register a method/host pair on a channel. */
Craig Tillerb20111c2015-04-10 23:27:11 +0000414void *grpc_channel_register_call(grpc_channel *channel, const char *method,
Craig Tiller08453372015-04-10 16:05:38 -0700415 const char *host);
416
Craig Tiller2d984bf2015-07-20 15:01:38 -0700417/** Create a call given a handle returned from grpc_channel_register_call */
Craig Tillerb20111c2015-04-10 23:27:11 +0000418grpc_call *grpc_channel_create_registered_call(
419 grpc_channel *channel, grpc_completion_queue *completion_queue,
420 void *registered_call_handle, gpr_timespec deadline);
Craig Tiller08453372015-04-10 16:05:38 -0700421
Craig Tiller2d984bf2015-07-20 15:01:38 -0700422/** Start a batch of operations defined in the array ops; when complete, post a
423 completion of type 'tag' to the completion queue bound to the call.
424 The order of ops specified in the batch has no significance.
425 Only one operation of each type can be active at once in any given
426 batch. You must call grpc_completion_queue_next or
427 grpc_completion_queue_pluck on the completion queue associated with 'call'
428 for work to be performed.
429 THREAD SAFETY: access to grpc_call_start_batch in multi-threaded environment
430 needs to be synchronized. As an optimization, you may synchronize batches
431 containing just send operations independently from batches containing just
432 receive operations. */
Craig Tillerfef76692015-02-02 16:44:26 -0800433grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
434 size_t nops, void *tag);
435
Craig Tiller45249422015-07-20 16:16:35 -0700436/** Returns a newly allocated string representing the endpoint to which this
437 call is communicating with. The string is in the uri format accepted by
438 grpc_channel_create.
439 The returned string should be disposed of with gpr_free().
440
441 WARNING: this value is never authenticated or subject to any security
442 related code. It must not be used for any authentication related
443 functionality. Instead, use grpc_auth_context. */
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700444char *grpc_call_get_peer(grpc_call *call);
445
Craig Tiller45249422015-07-20 16:16:35 -0700446/** Return a newly allocated string representing the target a channel was
447 created for. */
448char *grpc_channel_get_target(grpc_channel *channel);
449
Craig Tiller2d984bf2015-07-20 15:01:38 -0700450/** Create a client channel to 'target'. Additional channel level configuration
451 MAY be provided by grpc_channel_args, though the expectation is that most
452 clients will want to simply pass NULL. See grpc_channel_args definition for
453 more on this. The data in 'args' need only live through the invocation of
454 this function. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800455grpc_channel *grpc_channel_create(const char *target,
456 const grpc_channel_args *args);
457
Craig Tiller2d984bf2015-07-20 15:01:38 -0700458/** Create a lame client: this client fails every operation attempted on it. */
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700459grpc_channel *grpc_lame_client_channel_create(const char *target);
Craig Tiller42bc87c2015-02-23 08:50:19 -0800460
Craig Tiller2d984bf2015-07-20 15:01:38 -0700461/** Close and destroy a grpc channel */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800462void grpc_channel_destroy(grpc_channel *channel);
463
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800464/* Error handling for grpc_call
465 Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
466 then the operation failed due to some unsatisfied precondition.
467 If a grpc_call fails, it's guaranteed that no change to the call state
468 has been made. */
469
Craig Tiller2d984bf2015-07-20 15:01:38 -0700470/** Called by clients to cancel an RPC on the server.
471 Can be called multiple times, from any thread.
472 THREAD-SAFETY grpc_call_cancel and grpc_call_cancel_with_status
473 are thread-safe, and can be called at any point before grpc_call_destroy
474 is called.*/
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800475grpc_call_error grpc_call_cancel(grpc_call *call);
476
Craig Tiller2d984bf2015-07-20 15:01:38 -0700477/** Called by clients to cancel an RPC on the server.
478 Can be called multiple times, from any thread.
479 If a status has not been received for the call, set it to the status code
480 and description passed in.
481 Importantly, this function does not send status nor description to the
482 remote endpoint. */
Craig Tiller6046dc32015-01-14 12:55:45 -0800483grpc_call_error grpc_call_cancel_with_status(grpc_call *call,
484 grpc_status_code status,
485 const char *description);
Craig Tillerd248c242015-01-14 11:49:12 -0800486
Craig Tiller2d984bf2015-07-20 15:01:38 -0700487/** Destroy a call.
488 THREAD SAFETY: grpc_call_destroy is thread-compatible */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800489void grpc_call_destroy(grpc_call *call);
490
Craig Tiller2d984bf2015-07-20 15:01:38 -0700491/** Request notification of a new call. 'cq_for_notification' must
492 have been registered to the server via
493 grpc_server_register_completion_queue. */
Craig Tillerfb189f82015-02-03 12:07:07 -0800494grpc_call_error grpc_server_request_call(
Craig Tillerea61b072015-02-03 19:19:27 -0800495 grpc_server *server, grpc_call **call, grpc_call_details *details,
Craig Tillerfb189f82015-02-03 12:07:07 -0800496 grpc_metadata_array *request_metadata,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700497 grpc_completion_queue *cq_bound_to_call,
498 grpc_completion_queue *cq_for_notification, void *tag_new);
Craig Tiller034929c2015-02-02 16:56:15 -0800499
Craig Tiller2d984bf2015-07-20 15:01:38 -0700500/** Registers a method in the server.
501 Methods to this (host, method) pair will not be reported by
502 grpc_server_request_call, but instead be reported by
503 grpc_server_request_registered_call when passed the appropriate
504 registered_method (as returned by this function).
505 Must be called before grpc_server_start.
506 Returns NULL on failure. */
Craig Tiller24be0f72015-02-10 14:04:22 -0800507void *grpc_server_register_method(grpc_server *server, const char *method,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700508 const char *host);
Craig Tiller24be0f72015-02-10 14:04:22 -0800509
Craig Tiller2d984bf2015-07-20 15:01:38 -0700510/** Request notification of a new pre-registered call. 'cq_for_notification'
511 must have been registered to the server via
512 grpc_server_register_completion_queue. */
Craig Tiller24be0f72015-02-10 14:04:22 -0800513grpc_call_error grpc_server_request_registered_call(
514 grpc_server *server, void *registered_method, grpc_call **call,
515 gpr_timespec *deadline, grpc_metadata_array *request_metadata,
516 grpc_byte_buffer **optional_payload,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700517 grpc_completion_queue *cq_bound_to_call,
518 grpc_completion_queue *cq_for_notification, void *tag_new);
Craig Tiller24be0f72015-02-10 14:04:22 -0800519
Craig Tiller2d984bf2015-07-20 15:01:38 -0700520/** Create a server. Additional configuration for each incoming channel can
521 be specified with args. If no additional configuration is needed, args can
522 be NULL. See grpc_channel_args for more. The data in 'args' need only live
523 through the invocation of this function. */
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700524grpc_server *grpc_server_create(const grpc_channel_args *args);
525
Craig Tiller2d984bf2015-07-20 15:01:38 -0700526/** Register a completion queue with the server. Must be done for any
527 notification completion queue that is passed to grpc_server_request_*_call
528 and to grpc_server_shutdown_and_notify. Must be performed prior to
529 grpc_server_start. */
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700530void grpc_server_register_completion_queue(grpc_server *server,
531 grpc_completion_queue *cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800532
Craig Tiller2d984bf2015-07-20 15:01:38 -0700533/** Add a HTTP2 over plaintext over tcp listener.
534 Returns bound port number on success, 0 on failure.
535 REQUIRES: server not started */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800536int grpc_server_add_http2_port(grpc_server *server, const char *addr);
537
Craig Tiller2d984bf2015-07-20 15:01:38 -0700538/** Start a server - tells all listeners to start listening */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800539void grpc_server_start(grpc_server *server);
540
Craig Tiller2d984bf2015-07-20 15:01:38 -0700541/** Begin shutting down a server.
542 After completion, no new calls or connections will be admitted.
543 Existing calls will be allowed to complete.
544 Send a GRPC_OP_COMPLETE event when there are no more calls being serviced.
545 Shutdown is idempotent, and all tags will be notified at once if multiple
546 grpc_server_shutdown_and_notify calls are made. 'cq' must have been
547 registered to this server via grpc_server_register_completion_queue. */
Craig Tillerbce999f2015-05-27 09:55:51 -0700548void grpc_server_shutdown_and_notify(grpc_server *server,
549 grpc_completion_queue *cq, void *tag);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800550
Craig Tiller2d984bf2015-07-20 15:01:38 -0700551/** Cancel all in-progress calls.
552 Only usable after shutdown. */
Craig Tilleree945e82015-05-26 16:15:34 -0700553void grpc_server_cancel_all_calls(grpc_server *server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800554
Craig Tiller2d984bf2015-07-20 15:01:38 -0700555/** Destroy a server.
556 Shutdown must have completed beforehand (i.e. all tags generated by
557 grpc_server_shutdown_and_notify must have been received, and at least
558 one call to grpc_server_shutdown_and_notify must have been made). */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800559void grpc_server_destroy(grpc_server *server);
560
Craig Tilleraf7abf92015-06-03 17:18:58 -0700561/** Enable or disable a tracer.
562
563 Tracers (usually controlled by the environment variable GRPC_TRACE)
564 allow printf-style debugging on GRPC internals, and are useful for
Craig Tiller9a576332015-06-17 10:21:49 -0700565 tracking down problems in the field.
Craig Tilleraf7abf92015-06-03 17:18:58 -0700566
Craig Tiller9a576332015-06-17 10:21:49 -0700567 Use of this function is not strictly thread-safe, but the
Craig Tilleraf7abf92015-06-03 17:18:58 -0700568 thread-safety issues raised by it should not be of concern. */
569int grpc_tracer_set_enabled(const char *name, int enabled);
570
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800571#ifdef __cplusplus
572}
573#endif
574
Craig Tillerb20111c2015-04-10 23:27:11 +0000575#endif /* GRPC_GRPC_H */