blob: 637f473b821dd45f90ece301bd556516742e7867 [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
Nathaniel Manista6d41a052015-02-16 02:12:48 +000048/* Completion Queues enable notification of the completion of asynchronous
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049 actions. */
50typedef struct grpc_completion_queue grpc_completion_queue;
51
52/* The Channel interface allows creation of Call objects. */
53typedef struct grpc_channel grpc_channel;
54
55/* A server listens to some port and responds to request calls */
56typedef struct grpc_server grpc_server;
57
58/* A Call represents an RPC. When created, it is in a configuration state
59 allowing properties to be set until it is invoked. After invoke, the Call
60 can have messages written to it and read from it. */
61typedef struct grpc_call grpc_call;
62
63/* Type specifier for grpc_arg */
64typedef enum {
65 GRPC_ARG_STRING,
66 GRPC_ARG_INTEGER,
67 GRPC_ARG_POINTER
68} grpc_arg_type;
69
70/* A single argument... each argument has a key and a value
71
72 A note on naming keys:
73 Keys are namespaced into groups, usually grouped by library, and are
74 keys for module XYZ are named XYZ.key1, XYZ.key2, etc. Module names must
75 be restricted to the regex [A-Za-z][_A-Za-z0-9]{,15}.
76 Key names must be restricted to the regex [A-Za-z][_A-Za-z0-9]{,47}.
77
78 GRPC core library keys are prefixed by grpc.
79
80 Library authors are strongly encouraged to #define symbolic constants for
81 their keys so that it's possible to change them in the future. */
82typedef struct {
83 grpc_arg_type type;
84 char *key;
85 union {
86 char *string;
87 int integer;
88 struct {
89 void *p;
90 void *(*copy)(void *p);
91 void (*destroy)(void *p);
92 } pointer;
93 } value;
94} grpc_arg;
95
Craig Tillere793ba12015-05-18 09:37:22 -070096/** An array of arguments that can be passed around.
David Garcia Quintas02c677c2015-06-02 14:40:07 -070097
Craig Tillere793ba12015-05-18 09:37:22 -070098 Used to set optional channel-level configuration.
99 These configuration options are modelled as key-value pairs as defined
100 by grpc_arg; keys are strings to allow easy backwards-compatible extension
101 by arbitrary parties.
Masood Malekghassemi7128a902015-06-17 13:17:58 -0700102 All evaluation is performed at channel creation time (i.e. the values in
103 this structure need only live through the creation invocation). */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104typedef struct {
105 size_t num_args;
106 grpc_arg *args;
107} grpc_channel_args;
108
109/* Channel argument keys: */
110/* Enable census for tracing and stats collection */
111#define GRPC_ARG_ENABLE_CENSUS "grpc.census"
112/* Maximum number of concurrent incoming streams to allow on a http2
113 connection */
114#define GRPC_ARG_MAX_CONCURRENT_STREAMS "grpc.max_concurrent_streams"
115/* Maximum message length that the channel can receive */
116#define GRPC_ARG_MAX_MESSAGE_LENGTH "grpc.max_message_length"
Craig Tiller88025582015-05-04 09:41:10 -0700117/* Initial sequence number for http2 transports */
118#define GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER \
119 "grpc.http2.initial_sequence_number"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121/* Result of a grpc call. If the caller satisfies the prerequisites of a
122 particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
123 Receiving any other value listed here is an indication of a bug in the
124 caller. */
125typedef enum grpc_call_error {
126 /* everything went ok */
127 GRPC_CALL_OK = 0,
128 /* something failed, we don't know what */
129 GRPC_CALL_ERROR,
130 /* this method is not available on the server */
131 GRPC_CALL_ERROR_NOT_ON_SERVER,
132 /* this method is not available on the client */
133 GRPC_CALL_ERROR_NOT_ON_CLIENT,
nnoble0c475f02014-12-05 15:37:39 -0800134 /* this method must be called before server_accept */
135 GRPC_CALL_ERROR_ALREADY_ACCEPTED,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800136 /* this method must be called before invoke */
137 GRPC_CALL_ERROR_ALREADY_INVOKED,
138 /* this method must be called after invoke */
139 GRPC_CALL_ERROR_NOT_INVOKED,
140 /* this call is already finished
141 (writes_done or write_status has already been called) */
142 GRPC_CALL_ERROR_ALREADY_FINISHED,
143 /* there is already an outstanding read/write operation on the call */
144 GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
145 /* the flags value was illegal for this call */
Craig Tillerb96d0012015-05-06 15:33:23 -0700146 GRPC_CALL_ERROR_INVALID_FLAGS,
147 /* invalid metadata was passed to this call */
Craig Tillerb56975c2015-06-15 10:11:16 -0700148 GRPC_CALL_ERROR_INVALID_METADATA,
149 /* completion queue for notification has not been registered with the server
150 */
151 GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152} grpc_call_error;
153
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800154/* Write Flags: */
155/* Hint that the write may be buffered and need not go out on the wire
156 immediately. GRPC is free to buffer the message until the next non-buffered
157 write, or until writes_done, but it need not buffer completely or at all. */
158#define GRPC_WRITE_BUFFER_HINT (0x00000001u)
159/* Force compression to be disabled for a particular write
160 (start_write/add_metadata). Illegal on invoke/accept. */
161#define GRPC_WRITE_NO_COMPRESS (0x00000002u)
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700162/* Mask of all valid flags. */
163#define GRPC_WRITE_USED_MASK (GRPC_WRITE_BUFFER_HINT | GRPC_WRITE_NO_COMPRESS)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800164
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800165/* A single metadata element */
166typedef struct grpc_metadata {
Craig Tiller4f972732015-02-05 12:40:20 -0800167 const char *key;
168 const char *value;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169 size_t value_length;
Craig Tiller6902ad22015-04-16 08:01:49 -0700170
171 /* The following fields are reserved for grpc internal use.
172 There is no need to initialize them, and they will be set to garbage during
173 calls to grpc. */
174 struct {
Craig Tiller9c9d4e02015-04-20 09:03:29 -0700175 void *obfuscated[3];
Craig Tiller6902ad22015-04-16 08:01:49 -0700176 } internal_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177} grpc_metadata;
178
Craig Tiller73b66ef2015-05-18 20:46:47 -0700179/** The type of completion (for grpc_event) */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180typedef enum grpc_completion_type {
Craig Tillere793ba12015-05-18 09:37:22 -0700181 /** Shutting down */
Craig Tiller8674cb12015-06-05 07:09:25 -0700182 GRPC_QUEUE_SHUTDOWN,
Craig Tillere793ba12015-05-18 09:37:22 -0700183 /** No event before timeout */
Craig Tiller8674cb12015-06-05 07:09:25 -0700184 GRPC_QUEUE_TIMEOUT,
Craig Tillere793ba12015-05-18 09:37:22 -0700185 /** Operation completion */
Craig Tiller8674cb12015-06-05 07:09:25 -0700186 GRPC_OP_COMPLETE
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187} grpc_completion_type;
188
Craig Tillere793ba12015-05-18 09:37:22 -0700189/** The result of an operation.
190
191 Returned by a completion queue when the operation started with tag. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192typedef struct grpc_event {
Craig Tillere793ba12015-05-18 09:37:22 -0700193 /** The type of the completion. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194 grpc_completion_type type;
Craig Tiller8674cb12015-06-05 07:09:25 -0700195 /** non-zero if the operation was successful, 0 upon failure.
Craig Tillere793ba12015-05-18 09:37:22 -0700196 Only GRPC_OP_COMPLETE can succeed or fail. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700197 int success;
Craig Tillere793ba12015-05-18 09:37:22 -0700198 /** The tag passed to grpc_call_start_batch etc to start this operation.
199 Only GRPC_OP_COMPLETE has a tag. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200 void *tag;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201} grpc_event;
202
Craig Tillerfef76692015-02-02 16:44:26 -0800203typedef struct {
204 size_t count;
205 size_t capacity;
206 grpc_metadata *metadata;
207} grpc_metadata_array;
208
Craig Tillerea61b072015-02-03 19:19:27 -0800209void grpc_metadata_array_init(grpc_metadata_array *array);
210void grpc_metadata_array_destroy(grpc_metadata_array *array);
211
Craig Tillerfef76692015-02-02 16:44:26 -0800212typedef struct {
Craig Tillerea61b072015-02-03 19:19:27 -0800213 char *method;
Craig Tiller034929c2015-02-02 16:56:15 -0800214 size_t method_capacity;
Craig Tillerea61b072015-02-03 19:19:27 -0800215 char *host;
Craig Tiller034929c2015-02-02 16:56:15 -0800216 size_t host_capacity;
Craig Tillerfef76692015-02-02 16:44:26 -0800217 gpr_timespec deadline;
218} grpc_call_details;
219
Craig Tillerea61b072015-02-03 19:19:27 -0800220void grpc_call_details_init(grpc_call_details *details);
221void grpc_call_details_destroy(grpc_call_details *details);
222
Craig Tillerfef76692015-02-02 16:44:26 -0800223typedef enum {
Craig Tiller24be0f72015-02-10 14:04:22 -0800224 /* Send initial metadata: one and only one instance MUST be sent for each
225 call,
Craig Tillerb7800c12015-02-04 18:25:38 -0800226 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800227 GRPC_OP_SEND_INITIAL_METADATA = 0,
Craig Tillerb7800c12015-02-04 18:25:38 -0800228 /* Send a message: 0 or more of these operations can occur for each call */
Craig Tillerfef76692015-02-02 16:44:26 -0800229 GRPC_OP_SEND_MESSAGE,
Eric Dobson255971d2015-06-14 16:04:15 -0700230 /* Send a close from the client: one and only one instance MUST be sent from
Craig Tiller24be0f72015-02-10 14:04:22 -0800231 the client,
Craig Tillerb7800c12015-02-04 18:25:38 -0800232 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800233 GRPC_OP_SEND_CLOSE_FROM_CLIENT,
Craig Tiller24be0f72015-02-10 14:04:22 -0800234 /* Send status from the server: one and only one instance MUST be sent from
235 the server
Craig Tillerb7800c12015-02-04 18:25:38 -0800236 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800237 GRPC_OP_SEND_STATUS_FROM_SERVER,
Craig Tillerb7800c12015-02-04 18:25:38 -0800238 /* Receive initial metadata: one and only one MUST be made on the client, must
239 not be made on the server */
Craig Tillerfef76692015-02-02 16:44:26 -0800240 GRPC_OP_RECV_INITIAL_METADATA,
Craig Tillerb7800c12015-02-04 18:25:38 -0800241 /* Receive a message: 0 or more of these operations can occur for each call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800242 GRPC_OP_RECV_MESSAGE,
Craig Tiller69eed292015-05-19 10:43:05 -0700243 /* Receive status on the client: one and only one must be made on the client.
244 This operation always succeeds, meaning ops paired with this operation
245 will also appear to succeed, even though they may not have. In that case
246 the status will indicate some failure.
Craig Tiller24be0f72015-02-10 14:04:22 -0800247 */
Craig Tillerfef76692015-02-02 16:44:26 -0800248 GRPC_OP_RECV_STATUS_ON_CLIENT,
Eric Dobson255971d2015-06-14 16:04:15 -0700249 /* Receive close on the server: one and only one must be made on the server
Craig Tiller24be0f72015-02-10 14:04:22 -0800250 */
Craig Tillerfef76692015-02-02 16:44:26 -0800251 GRPC_OP_RECV_CLOSE_ON_SERVER
252} grpc_op_type;
253
Craig Tiller24be0f72015-02-10 14:04:22 -0800254/* Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT
255 which has
Craig Tillerb7800c12015-02-04 18:25:38 -0800256 no arguments) */
Craig Tillerfef76692015-02-02 16:44:26 -0800257typedef struct grpc_op {
258 grpc_op_type op;
Craig Tiller9a576332015-06-17 10:21:49 -0700259 gpr_uint32 flags; /**< Write flags bitset for grpc_begin_messages */
Craig Tillerfef76692015-02-02 16:44:26 -0800260 union {
261 struct {
262 size_t count;
Craig Tiller6902ad22015-04-16 08:01:49 -0700263 grpc_metadata *metadata;
Craig Tillerfef76692015-02-02 16:44:26 -0800264 } send_initial_metadata;
265 grpc_byte_buffer *send_message;
266 struct {
267 size_t trailing_metadata_count;
268 grpc_metadata *trailing_metadata;
269 grpc_status_code status;
270 const char *status_details;
271 } send_status_from_server;
Craig Tiller4f972732015-02-05 12:40:20 -0800272 /* ownership of the array is with the caller, but ownership of the elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800273 stays with the call object (ie key, value members are owned by the call
274 object, recv_initial_metadata->array is owned by the caller).
275 After the operation completes, call grpc_metadata_array_destroy on this
276 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800277 grpc_metadata_array *recv_initial_metadata;
Masood Malekghassemi7128a902015-06-17 13:17:58 -0700278 /* ownership of the byte buffer is moved to the caller; the caller must call
279 grpc_byte_buffer_destroy on this value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800280 grpc_byte_buffer **recv_message;
281 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800282 /* ownership of the array is with the caller, but ownership of the
283 elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800284 stays with the call object (ie key, value members are owned by the call
285 object, trailing_metadata->array is owned by the caller).
286 After the operation completes, call grpc_metadata_array_destroy on this
287 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800288 grpc_metadata_array *trailing_metadata;
289 grpc_status_code *status;
Craig Tiller24be0f72015-02-10 14:04:22 -0800290 /* status_details is a buffer owned by the application before the op
291 completes
292 and after the op has completed. During the operation status_details may
293 be
294 reallocated to a size larger than *status_details_capacity, in which
295 case
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800296 *status_details_capacity will be updated with the new array capacity.
297
298 Pre-allocating space:
299 size_t my_capacity = 8;
300 char *my_details = gpr_malloc(my_capacity);
301 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800302 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800303
304 Not pre-allocating space:
305 size_t my_capacity = 0;
306 char *my_details = NULL;
307 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800308 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800309
310 After the call:
311 gpr_free(my_details); */
Craig Tillerfef76692015-02-02 16:44:26 -0800312 char **status_details;
313 size_t *status_details_capacity;
314 } recv_status_on_client;
315 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800316 /* out argument, set to 1 if the call failed in any way (seen as a
317 cancellation
Craig Tiller0a5bcbc2015-02-09 21:39:04 -0800318 on the server), or 0 if the call succeeded */
Craig Tillerfef76692015-02-02 16:44:26 -0800319 int *cancelled;
320 } recv_close_on_server;
321 } data;
322} grpc_op;
323
Craig Tillere793ba12015-05-18 09:37:22 -0700324/** Initialize the grpc library.
Craig Tiller8674cb12015-06-05 07:09:25 -0700325
Craig Tillere793ba12015-05-18 09:37:22 -0700326 It is not safe to call any other grpc functions before calling this.
327 (To avoid overhead, little checking is done, and some things may work. We
328 do not warrant that they will continue to do so in future revisions of this
329 library). */
Craig Tiller32946d32015-01-15 11:37:30 -0800330void grpc_init(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800331
Craig Tillere793ba12015-05-18 09:37:22 -0700332/** Shut down the grpc library.
Craig Tiller8674cb12015-06-05 07:09:25 -0700333
Craig Tillere793ba12015-05-18 09:37:22 -0700334 No memory is used by grpc after this call returns, nor are any instructions
335 executing within the grpc library.
336 Prior to calling, all application owned grpc objects must have been
337 destroyed. */
Craig Tiller32946d32015-01-15 11:37:30 -0800338void grpc_shutdown(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800339
Craig Tillere793ba12015-05-18 09:37:22 -0700340/** Create a completion queue */
Craig Tiller32946d32015-01-15 11:37:30 -0800341grpc_completion_queue *grpc_completion_queue_create(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800342
Craig Tillere793ba12015-05-18 09:37:22 -0700343/** Blocks until an event is available, the completion queue is being shut down,
Craig Tiller8674cb12015-06-05 07:09:25 -0700344 or deadline is reached.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800345
vjpai1854d772015-06-08 01:12:29 -0700346 Returns a grpc_event with type GRPC_QUEUE_TIMEOUT on timeout,
347 otherwise a grpc_event describing the event that occurred.
Craig Tillere793ba12015-05-18 09:37:22 -0700348
349 Callers must not call grpc_completion_queue_next and
350 grpc_completion_queue_pluck simultaneously on the same completion queue. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700351grpc_event grpc_completion_queue_next(grpc_completion_queue *cq,
352 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800353
Craig Tillere793ba12015-05-18 09:37:22 -0700354/** Blocks until an event with tag 'tag' is available, the completion queue is
Craig Tiller8674cb12015-06-05 07:09:25 -0700355 being shutdown or deadline is reached.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800356
vjpai1854d772015-06-08 01:12:29 -0700357 Returns a grpc_event with type GRPC_QUEUE_TIMEOUT on timeout,
358 otherwise a grpc_event describing the event that occurred.
Craig Tillere793ba12015-05-18 09:37:22 -0700359
360 Callers must not call grpc_completion_queue_next and
361 grpc_completion_queue_pluck simultaneously on the same completion queue. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700362grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag,
363 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800364
365/* Begin destruction of a completion queue. Once all possible events are
Craig Tiller8ac56c92015-02-17 22:51:36 -0800366 drained then grpc_completion_queue_next will start to produce
Craig Tillerb20111c2015-04-10 23:27:11 +0000367 GRPC_QUEUE_SHUTDOWN events only. At that point it's safe to call
368 grpc_completion_queue_destroy.
369
Craig Tiller8ac56c92015-02-17 22:51:36 -0800370 After calling this function applications should ensure that no
371 NEW work is added to be published on this completion queue. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800372void grpc_completion_queue_shutdown(grpc_completion_queue *cq);
373
374/* Destroy a completion queue. The caller must ensure that the queue is
375 drained and no threads are executing grpc_completion_queue_next */
376void grpc_completion_queue_destroy(grpc_completion_queue *cq);
377
Jan Tattermusch4bf27c52015-06-22 14:24:59 -0700378/* Create a call given a grpc_channel, in order to call 'method'. All
379 completions are sent to 'completion_queue'. 'method' and 'host' need only
380 live through the invocation of this function. */
Craig Tillerfb189f82015-02-03 12:07:07 -0800381grpc_call *grpc_channel_create_call(grpc_channel *channel,
382 grpc_completion_queue *completion_queue,
383 const char *method, const char *host,
384 gpr_timespec deadline);
Craig Tiller034929c2015-02-02 16:56:15 -0800385
Craig Tiller08453372015-04-10 16:05:38 -0700386/* Pre-register a method/host pair on a channel. */
Craig Tillerb20111c2015-04-10 23:27:11 +0000387void *grpc_channel_register_call(grpc_channel *channel, const char *method,
Craig Tiller08453372015-04-10 16:05:38 -0700388 const char *host);
389
390/* Create a call given a handle returned from grpc_channel_register_call */
Craig Tillerb20111c2015-04-10 23:27:11 +0000391grpc_call *grpc_channel_create_registered_call(
392 grpc_channel *channel, grpc_completion_queue *completion_queue,
393 void *registered_call_handle, gpr_timespec deadline);
Craig Tiller08453372015-04-10 16:05:38 -0700394
Craig Tiller034929c2015-02-02 16:56:15 -0800395/* Start a batch of operations defined in the array ops; when complete, post a
Craig Tiller24be0f72015-02-10 14:04:22 -0800396 completion of type 'tag' to the completion queue bound to the call.
Craig Tillerb7800c12015-02-04 18:25:38 -0800397 The order of ops specified in the batch has no significance.
398 Only one operation of each type can be active at once in any given
Masood Malekghassemib70b7092015-06-26 16:25:11 -0700399 batch. You must call grpc_completion_queue_next or
400 grpc_completion_queue_pluck on the completion queue associated with 'call'
401 for work to be performed.
Jan Tattermuschaadc13a2015-06-22 12:25:55 -0700402 THREAD SAFETY: access to grpc_call_start_batch in multi-threaded environment
403 needs to be synchronized. As an optimization, you may synchronize batches
404 containing just send operations independently from batches containing just
405 receive operations. */
Craig Tillerfef76692015-02-02 16:44:26 -0800406grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
407 size_t nops, void *tag);
408
Craig Tiller29f2b212015-02-17 17:01:24 -0800409/* Create a client channel to 'target'. Additional channel level configuration
410 MAY be provided by grpc_channel_args, though the expectation is that most
Masood Malekghassemi7128a902015-06-17 13:17:58 -0700411 clients will want to simply pass NULL. See grpc_channel_args definition for
412 more on this. The data in 'args' need only live through the invocation of
413 this function. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800414grpc_channel *grpc_channel_create(const char *target,
415 const grpc_channel_args *args);
416
Craig Tiller42bc87c2015-02-23 08:50:19 -0800417/* Create a lame client: this client fails every operation attempted on it. */
418grpc_channel *grpc_lame_client_channel_create(void);
419
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800420/* Close and destroy a grpc channel */
421void grpc_channel_destroy(grpc_channel *channel);
422
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800423/* Error handling for grpc_call
424 Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
425 then the operation failed due to some unsatisfied precondition.
426 If a grpc_call fails, it's guaranteed that no change to the call state
427 has been made. */
428
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800429/* Called by clients to cancel an RPC on the server.
Jan Tattermuschc878e2a2015-06-12 13:52:28 -0700430 Can be called multiple times, from any thread.
431 THREAD-SAFETY grpc_call_cancel and grpc_call_cancel_with_status
432 are thread-safe, and can be called at any point before grpc_call_destroy
433 is called.*/
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800434grpc_call_error grpc_call_cancel(grpc_call *call);
435
Craig Tillerd248c242015-01-14 11:49:12 -0800436/* Called by clients to cancel an RPC on the server.
Craig Tiller6046dc32015-01-14 12:55:45 -0800437 Can be called multiple times, from any thread.
Craig Tillerd248c242015-01-14 11:49:12 -0800438 If a status has not been received for the call, set it to the status code
Craig Tiller6046dc32015-01-14 12:55:45 -0800439 and description passed in.
Craig Tillerd248c242015-01-14 11:49:12 -0800440 Importantly, this function does not send status nor description to the
441 remote endpoint. */
Craig Tiller6046dc32015-01-14 12:55:45 -0800442grpc_call_error grpc_call_cancel_with_status(grpc_call *call,
443 grpc_status_code status,
444 const char *description);
Craig Tillerd248c242015-01-14 11:49:12 -0800445
Jan Tattermuschc878e2a2015-06-12 13:52:28 -0700446/* Destroy a call.
447 THREAD SAFETY: grpc_call_destroy is thread-compatible */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800448void grpc_call_destroy(grpc_call *call);
449
Masood Malekghassemiaeb1a9f2015-06-18 16:23:24 -0700450/* Request notification of a new call. 'cq_for_notification' must
451 have been registered to the server via grpc_server_register_completion_queue.
452 */
Craig Tillerfb189f82015-02-03 12:07:07 -0800453grpc_call_error grpc_server_request_call(
Craig Tillerea61b072015-02-03 19:19:27 -0800454 grpc_server *server, grpc_call **call, grpc_call_details *details,
Craig Tillerfb189f82015-02-03 12:07:07 -0800455 grpc_metadata_array *request_metadata,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700456 grpc_completion_queue *cq_bound_to_call,
457 grpc_completion_queue *cq_for_notification, void *tag_new);
Craig Tiller034929c2015-02-02 16:56:15 -0800458
Craig Tillerfada7d42015-02-11 23:03:55 -0800459/* Registers a method in the server.
460 Methods to this (host, method) pair will not be reported by
Craig Tiller06059952015-02-18 08:34:56 -0800461 grpc_server_request_call, but instead be reported by
Craig Tillerfada7d42015-02-11 23:03:55 -0800462 grpc_server_request_registered_call when passed the appropriate
463 registered_method (as returned by this function).
464 Must be called before grpc_server_start.
465 Returns NULL on failure. */
Craig Tiller24be0f72015-02-10 14:04:22 -0800466void *grpc_server_register_method(grpc_server *server, const char *method,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700467 const char *host);
Craig Tiller24be0f72015-02-10 14:04:22 -0800468
Masood Malekghassemiaeb1a9f2015-06-18 16:23:24 -0700469/* Request notification of a new pre-registered call. 'cq_for_notification' must
470 have been registered to the server via grpc_server_register_completion_queue.
471 */
Craig Tiller24be0f72015-02-10 14:04:22 -0800472grpc_call_error grpc_server_request_registered_call(
473 grpc_server *server, void *registered_method, grpc_call **call,
474 gpr_timespec *deadline, grpc_metadata_array *request_metadata,
475 grpc_byte_buffer **optional_payload,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700476 grpc_completion_queue *cq_bound_to_call,
477 grpc_completion_queue *cq_for_notification, void *tag_new);
Craig Tiller24be0f72015-02-10 14:04:22 -0800478
Craig Tiller29f2b212015-02-17 17:01:24 -0800479/* Create a server. Additional configuration for each incoming channel can
Craig Tillere7163ab2015-02-17 20:46:08 -0800480 be specified with args. If no additional configuration is needed, args can
Masood Malekghassemi7128a902015-06-17 13:17:58 -0700481 be NULL. See grpc_channel_args for more. The data in 'args' need only live
482 through the invocation of this function. */
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700483grpc_server *grpc_server_create(const grpc_channel_args *args);
484
Masood Malekghassemiaeb1a9f2015-06-18 16:23:24 -0700485/* Register a completion queue with the server. Must be done for any
486 notification completion queue that is passed to grpc_server_request_*_call
487 and to grpc_server_shutdown_and_notify. Must be performed prior to
488 grpc_server_start. */
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700489void grpc_server_register_completion_queue(grpc_server *server,
490 grpc_completion_queue *cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800491
Craig Tillerd251ab92015-02-17 17:22:14 -0800492/* Add a HTTP2 over plaintext over tcp listener.
ctiller570d1f42015-01-12 16:29:52 -0800493 Returns bound port number on success, 0 on failure.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800494 REQUIRES: server not started */
495int grpc_server_add_http2_port(grpc_server *server, const char *addr);
496
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800497/* Start a server - tells all listeners to start listening */
498void grpc_server_start(grpc_server *server);
499
ctiller9a58df02014-12-11 16:26:49 -0800500/* Begin shutting down a server.
501 After completion, no new calls or connections will be admitted.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800502 Existing calls will be allowed to complete.
Craig Tilleree945e82015-05-26 16:15:34 -0700503 Send a GRPC_OP_COMPLETE event when there are no more calls being serviced.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800504 Shutdown is idempotent, and all tags will be notified at once if multiple
Masood Malekghassemiaeb1a9f2015-06-18 16:23:24 -0700505 grpc_server_shutdown_and_notify calls are made. 'cq' must have been
506 registered to this server via grpc_server_register_completion_queue. */
Craig Tillerbce999f2015-05-27 09:55:51 -0700507void grpc_server_shutdown_and_notify(grpc_server *server,
508 grpc_completion_queue *cq, void *tag);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800509
Craig Tiller8674cb12015-06-05 07:09:25 -0700510/* Cancel all in-progress calls.
Craig Tilleree945e82015-05-26 16:15:34 -0700511 Only usable after shutdown. */
512void grpc_server_cancel_all_calls(grpc_server *server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800513
514/* Destroy a server.
Craig Tiller6ec402b2015-05-28 08:36:41 -0700515 Shutdown must have completed beforehand (i.e. all tags generated by
516 grpc_server_shutdown_and_notify must have been received, and at least
517 one call to grpc_server_shutdown_and_notify must have been made). */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800518void grpc_server_destroy(grpc_server *server);
519
Craig Tilleraf7abf92015-06-03 17:18:58 -0700520/** Enable or disable a tracer.
521
522 Tracers (usually controlled by the environment variable GRPC_TRACE)
523 allow printf-style debugging on GRPC internals, and are useful for
Craig Tiller9a576332015-06-17 10:21:49 -0700524 tracking down problems in the field.
Craig Tilleraf7abf92015-06-03 17:18:58 -0700525
Craig Tiller9a576332015-06-17 10:21:49 -0700526 Use of this function is not strictly thread-safe, but the
Craig Tilleraf7abf92015-06-03 17:18:58 -0700527 thread-safety issues raised by it should not be of concern. */
528int grpc_tracer_set_enabled(const char *name, int enabled);
529
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800530#ifdef __cplusplus
531}
532#endif
533
Craig Tillerb20111c2015-04-10 23:27:11 +0000534#endif /* GRPC_GRPC_H */