blob: 57bf2fad5abb62348c4c99b82934516fac3e68a1 [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>
40#include <grpc/support/slice.h>
41#include <grpc/support/time.h>
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
Nathaniel Manista6d41a052015-02-16 02:12:48 +000047/* Completion Queues enable notification of the completion of asynchronous
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048 actions. */
49typedef struct grpc_completion_queue grpc_completion_queue;
50
51/* The Channel interface allows creation of Call objects. */
52typedef struct grpc_channel grpc_channel;
53
54/* A server listens to some port and responds to request calls */
55typedef struct grpc_server grpc_server;
56
57/* A Call represents an RPC. When created, it is in a configuration state
58 allowing properties to be set until it is invoked. After invoke, the Call
59 can have messages written to it and read from it. */
60typedef struct grpc_call grpc_call;
61
62/* Type specifier for grpc_arg */
63typedef enum {
64 GRPC_ARG_STRING,
65 GRPC_ARG_INTEGER,
66 GRPC_ARG_POINTER
67} grpc_arg_type;
68
69/* A single argument... each argument has a key and a value
70
71 A note on naming keys:
72 Keys are namespaced into groups, usually grouped by library, and are
73 keys for module XYZ are named XYZ.key1, XYZ.key2, etc. Module names must
74 be restricted to the regex [A-Za-z][_A-Za-z0-9]{,15}.
75 Key names must be restricted to the regex [A-Za-z][_A-Za-z0-9]{,47}.
76
77 GRPC core library keys are prefixed by grpc.
78
79 Library authors are strongly encouraged to #define symbolic constants for
80 their keys so that it's possible to change them in the future. */
81typedef struct {
82 grpc_arg_type type;
83 char *key;
84 union {
85 char *string;
86 int integer;
87 struct {
88 void *p;
89 void *(*copy)(void *p);
90 void (*destroy)(void *p);
91 } pointer;
92 } value;
93} grpc_arg;
94
Craig Tillere793ba12015-05-18 09:37:22 -070095/** An array of arguments that can be passed around.
David Garcia Quintas02c677c2015-06-02 14:40:07 -070096
Craig Tillere793ba12015-05-18 09:37:22 -070097 Used to set optional channel-level configuration.
98 These configuration options are modelled as key-value pairs as defined
99 by grpc_arg; keys are strings to allow easy backwards-compatible extension
100 by arbitrary parties.
101 All evaluation is performed at channel creation time. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102typedef struct {
103 size_t num_args;
104 grpc_arg *args;
105} grpc_channel_args;
106
107/* Channel argument keys: */
108/* Enable census for tracing and stats collection */
109#define GRPC_ARG_ENABLE_CENSUS "grpc.census"
110/* Maximum number of concurrent incoming streams to allow on a http2
111 connection */
112#define GRPC_ARG_MAX_CONCURRENT_STREAMS "grpc.max_concurrent_streams"
113/* Maximum message length that the channel can receive */
114#define GRPC_ARG_MAX_MESSAGE_LENGTH "grpc.max_message_length"
Craig Tiller88025582015-05-04 09:41:10 -0700115/* Initial sequence number for http2 transports */
116#define GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER \
117 "grpc.http2.initial_sequence_number"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800118
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119/* Result of a grpc call. If the caller satisfies the prerequisites of a
120 particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
121 Receiving any other value listed here is an indication of a bug in the
122 caller. */
123typedef enum grpc_call_error {
124 /* everything went ok */
125 GRPC_CALL_OK = 0,
126 /* something failed, we don't know what */
127 GRPC_CALL_ERROR,
128 /* this method is not available on the server */
129 GRPC_CALL_ERROR_NOT_ON_SERVER,
130 /* this method is not available on the client */
131 GRPC_CALL_ERROR_NOT_ON_CLIENT,
nnoble0c475f02014-12-05 15:37:39 -0800132 /* this method must be called before server_accept */
133 GRPC_CALL_ERROR_ALREADY_ACCEPTED,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134 /* this method must be called before invoke */
135 GRPC_CALL_ERROR_ALREADY_INVOKED,
136 /* this method must be called after invoke */
137 GRPC_CALL_ERROR_NOT_INVOKED,
138 /* this call is already finished
139 (writes_done or write_status has already been called) */
140 GRPC_CALL_ERROR_ALREADY_FINISHED,
141 /* there is already an outstanding read/write operation on the call */
142 GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
143 /* the flags value was illegal for this call */
Craig Tillerb96d0012015-05-06 15:33:23 -0700144 GRPC_CALL_ERROR_INVALID_FLAGS,
145 /* invalid metadata was passed to this call */
146 GRPC_CALL_ERROR_INVALID_METADATA
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147} grpc_call_error;
148
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149/* Write Flags: */
150/* Hint that the write may be buffered and need not go out on the wire
151 immediately. GRPC is free to buffer the message until the next non-buffered
152 write, or until writes_done, but it need not buffer completely or at all. */
153#define GRPC_WRITE_BUFFER_HINT (0x00000001u)
154/* Force compression to be disabled for a particular write
155 (start_write/add_metadata). Illegal on invoke/accept. */
156#define GRPC_WRITE_NO_COMPRESS (0x00000002u)
157
158/* A buffer of bytes */
159struct grpc_byte_buffer;
160typedef struct grpc_byte_buffer grpc_byte_buffer;
161
Nathaniel Manista6d41a052015-02-16 02:12:48 +0000162/* Sample helpers to obtain byte buffers (these will certainly move
163 someplace else) */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800164grpc_byte_buffer *grpc_byte_buffer_create(gpr_slice *slices, size_t nslices);
Craig Tiller80fa15c2015-01-13 16:10:49 -0800165grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166size_t grpc_byte_buffer_length(grpc_byte_buffer *bb);
167void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer);
168
169/* Reader for byte buffers. Iterates over slices in the byte buffer */
170struct grpc_byte_buffer_reader;
171typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
172
David Garcia Quintas30bd4eb2015-06-01 21:08:59 -0700173/** Initialize \a reader to read over \a buffer */
174void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader,
175 grpc_byte_buffer *buffer);
176
David Garcia Quintas02c677c2015-06-02 14:40:07 -0700177/** Cleanup and destroy \a reader */
178void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
179
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180/* At the end of the stream, returns 0. Otherwise, returns 1 and sets slice to
181 be the returned slice. Caller is responsible for calling gpr_slice_unref on
182 the result. */
183int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
184 gpr_slice *slice);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185
186/* A single metadata element */
187typedef struct grpc_metadata {
Craig Tiller4f972732015-02-05 12:40:20 -0800188 const char *key;
189 const char *value;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800190 size_t value_length;
Craig Tiller6902ad22015-04-16 08:01:49 -0700191
192 /* The following fields are reserved for grpc internal use.
193 There is no need to initialize them, and they will be set to garbage during
194 calls to grpc. */
195 struct {
Craig Tiller9c9d4e02015-04-20 09:03:29 -0700196 void *obfuscated[3];
Craig Tiller6902ad22015-04-16 08:01:49 -0700197 } internal_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198} grpc_metadata;
199
Craig Tiller73b66ef2015-05-18 20:46:47 -0700200/** The type of completion (for grpc_event) */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201typedef enum grpc_completion_type {
Craig Tillere793ba12015-05-18 09:37:22 -0700202 /** Shutting down */
203 GRPC_QUEUE_SHUTDOWN,
204 /** No event before timeout */
205 GRPC_QUEUE_TIMEOUT,
206 /** Operation completion */
207 GRPC_OP_COMPLETE
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800208} grpc_completion_type;
209
Craig Tillere793ba12015-05-18 09:37:22 -0700210/** The result of an operation.
211
212 Returned by a completion queue when the operation started with tag. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213typedef struct grpc_event {
Craig Tillere793ba12015-05-18 09:37:22 -0700214 /** The type of the completion. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800215 grpc_completion_type type;
Craig Tillere793ba12015-05-18 09:37:22 -0700216 /** non-zero if the operation was successful, 0 upon failure.
217 Only GRPC_OP_COMPLETE can succeed or fail. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700218 int success;
Craig Tillere793ba12015-05-18 09:37:22 -0700219 /** The tag passed to grpc_call_start_batch etc to start this operation.
220 Only GRPC_OP_COMPLETE has a tag. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800221 void *tag;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222} grpc_event;
223
Craig Tillerfef76692015-02-02 16:44:26 -0800224typedef struct {
225 size_t count;
226 size_t capacity;
227 grpc_metadata *metadata;
228} grpc_metadata_array;
229
Craig Tillerea61b072015-02-03 19:19:27 -0800230void grpc_metadata_array_init(grpc_metadata_array *array);
231void grpc_metadata_array_destroy(grpc_metadata_array *array);
232
Craig Tillerfef76692015-02-02 16:44:26 -0800233typedef struct {
Craig Tillerea61b072015-02-03 19:19:27 -0800234 char *method;
Craig Tiller034929c2015-02-02 16:56:15 -0800235 size_t method_capacity;
Craig Tillerea61b072015-02-03 19:19:27 -0800236 char *host;
Craig Tiller034929c2015-02-02 16:56:15 -0800237 size_t host_capacity;
Craig Tillerfef76692015-02-02 16:44:26 -0800238 gpr_timespec deadline;
239} grpc_call_details;
240
Craig Tillerea61b072015-02-03 19:19:27 -0800241void grpc_call_details_init(grpc_call_details *details);
242void grpc_call_details_destroy(grpc_call_details *details);
243
Craig Tillerfef76692015-02-02 16:44:26 -0800244typedef enum {
Craig Tiller24be0f72015-02-10 14:04:22 -0800245 /* Send initial metadata: one and only one instance MUST be sent for each
246 call,
Craig Tillerb7800c12015-02-04 18:25:38 -0800247 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800248 GRPC_OP_SEND_INITIAL_METADATA = 0,
Craig Tillerb7800c12015-02-04 18:25:38 -0800249 /* Send a message: 0 or more of these operations can occur for each call */
Craig Tillerfef76692015-02-02 16:44:26 -0800250 GRPC_OP_SEND_MESSAGE,
Craig Tiller24be0f72015-02-10 14:04:22 -0800251 /* Send a close from the server: one and only one instance MUST be sent from
252 the client,
Craig Tillerb7800c12015-02-04 18:25:38 -0800253 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800254 GRPC_OP_SEND_CLOSE_FROM_CLIENT,
Craig Tiller24be0f72015-02-10 14:04:22 -0800255 /* Send status from the server: one and only one instance MUST be sent from
256 the server
Craig Tillerb7800c12015-02-04 18:25:38 -0800257 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800258 GRPC_OP_SEND_STATUS_FROM_SERVER,
Craig Tillerb7800c12015-02-04 18:25:38 -0800259 /* Receive initial metadata: one and only one MUST be made on the client, must
260 not be made on the server */
Craig Tillerfef76692015-02-02 16:44:26 -0800261 GRPC_OP_RECV_INITIAL_METADATA,
Craig Tillerb7800c12015-02-04 18:25:38 -0800262 /* Receive a message: 0 or more of these operations can occur for each call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800263 GRPC_OP_RECV_MESSAGE,
Craig Tiller69eed292015-05-19 10:43:05 -0700264 /* Receive status on the client: one and only one must be made on the client.
265 This operation always succeeds, meaning ops paired with this operation
266 will also appear to succeed, even though they may not have. In that case
267 the status will indicate some failure.
Craig Tiller24be0f72015-02-10 14:04:22 -0800268 */
Craig Tillerfef76692015-02-02 16:44:26 -0800269 GRPC_OP_RECV_STATUS_ON_CLIENT,
Craig Tiller24be0f72015-02-10 14:04:22 -0800270 /* Receive status on the server: one and only one must be made on the server
271 */
Craig Tillerfef76692015-02-02 16:44:26 -0800272 GRPC_OP_RECV_CLOSE_ON_SERVER
273} grpc_op_type;
274
Craig Tiller24be0f72015-02-10 14:04:22 -0800275/* Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT
276 which has
Craig Tillerb7800c12015-02-04 18:25:38 -0800277 no arguments) */
Craig Tillerfef76692015-02-02 16:44:26 -0800278typedef struct grpc_op {
279 grpc_op_type op;
280 union {
281 struct {
282 size_t count;
Craig Tiller6902ad22015-04-16 08:01:49 -0700283 grpc_metadata *metadata;
Craig Tillerfef76692015-02-02 16:44:26 -0800284 } send_initial_metadata;
285 grpc_byte_buffer *send_message;
286 struct {
287 size_t trailing_metadata_count;
288 grpc_metadata *trailing_metadata;
289 grpc_status_code status;
290 const char *status_details;
291 } send_status_from_server;
Craig Tiller4f972732015-02-05 12:40:20 -0800292 /* ownership of the array is with the caller, but ownership of the elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800293 stays with the call object (ie key, value members are owned by the call
294 object, recv_initial_metadata->array is owned by the caller).
295 After the operation completes, call grpc_metadata_array_destroy on this
296 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800297 grpc_metadata_array *recv_initial_metadata;
298 grpc_byte_buffer **recv_message;
299 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800300 /* ownership of the array is with the caller, but ownership of the
301 elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800302 stays with the call object (ie key, value members are owned by the call
303 object, trailing_metadata->array is owned by the caller).
304 After the operation completes, call grpc_metadata_array_destroy on this
305 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800306 grpc_metadata_array *trailing_metadata;
307 grpc_status_code *status;
Craig Tiller24be0f72015-02-10 14:04:22 -0800308 /* status_details is a buffer owned by the application before the op
309 completes
310 and after the op has completed. During the operation status_details may
311 be
312 reallocated to a size larger than *status_details_capacity, in which
313 case
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800314 *status_details_capacity will be updated with the new array capacity.
315
316 Pre-allocating space:
317 size_t my_capacity = 8;
318 char *my_details = gpr_malloc(my_capacity);
319 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800320 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800321
322 Not pre-allocating space:
323 size_t my_capacity = 0;
324 char *my_details = NULL;
325 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800326 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800327
328 After the call:
329 gpr_free(my_details); */
Craig Tillerfef76692015-02-02 16:44:26 -0800330 char **status_details;
331 size_t *status_details_capacity;
332 } recv_status_on_client;
333 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800334 /* out argument, set to 1 if the call failed in any way (seen as a
335 cancellation
Craig Tiller0a5bcbc2015-02-09 21:39:04 -0800336 on the server), or 0 if the call succeeded */
Craig Tillerfef76692015-02-02 16:44:26 -0800337 int *cancelled;
338 } recv_close_on_server;
339 } data;
340} grpc_op;
341
Craig Tillere793ba12015-05-18 09:37:22 -0700342/** Initialize the grpc library.
343
344 It is not safe to call any other grpc functions before calling this.
345 (To avoid overhead, little checking is done, and some things may work. We
346 do not warrant that they will continue to do so in future revisions of this
347 library). */
Craig Tiller32946d32015-01-15 11:37:30 -0800348void grpc_init(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800349
Craig Tillere793ba12015-05-18 09:37:22 -0700350/** Shut down the grpc library.
351
352 No memory is used by grpc after this call returns, nor are any instructions
353 executing within the grpc library.
354 Prior to calling, all application owned grpc objects must have been
355 destroyed. */
Craig Tiller32946d32015-01-15 11:37:30 -0800356void grpc_shutdown(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800357
Craig Tillere793ba12015-05-18 09:37:22 -0700358/** Create a completion queue */
Craig Tiller32946d32015-01-15 11:37:30 -0800359grpc_completion_queue *grpc_completion_queue_create(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800360
Craig Tillere793ba12015-05-18 09:37:22 -0700361/** Blocks until an event is available, the completion queue is being shut down,
362 or deadline is reached.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800363
Craig Tillere793ba12015-05-18 09:37:22 -0700364 Returns NULL on timeout, otherwise the event that occurred.
365
366 Callers must not call grpc_completion_queue_next and
367 grpc_completion_queue_pluck simultaneously on the same completion queue. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700368grpc_event grpc_completion_queue_next(grpc_completion_queue *cq,
369 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800370
Craig Tillere793ba12015-05-18 09:37:22 -0700371/** Blocks until an event with tag 'tag' is available, the completion queue is
372 being shutdown or deadline is reached.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800373
Craig Tillere793ba12015-05-18 09:37:22 -0700374 Returns NULL on timeout, or a pointer to the event that occurred.
375
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_pluck(grpc_completion_queue *cq, void *tag,
379 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800380
381/* Begin destruction of a completion queue. Once all possible events are
Craig Tiller8ac56c92015-02-17 22:51:36 -0800382 drained then grpc_completion_queue_next will start to produce
Craig Tillerb20111c2015-04-10 23:27:11 +0000383 GRPC_QUEUE_SHUTDOWN events only. At that point it's safe to call
384 grpc_completion_queue_destroy.
385
Craig Tiller8ac56c92015-02-17 22:51:36 -0800386 After calling this function applications should ensure that no
387 NEW work is added to be published on this completion queue. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800388void grpc_completion_queue_shutdown(grpc_completion_queue *cq);
389
390/* Destroy a completion queue. The caller must ensure that the queue is
391 drained and no threads are executing grpc_completion_queue_next */
392void grpc_completion_queue_destroy(grpc_completion_queue *cq);
393
394/* Create a call given a grpc_channel, in order to call 'method'. The request
395 is not sent until grpc_call_invoke is called. All completions are sent to
396 'completion_queue'. */
Craig Tillerfb189f82015-02-03 12:07:07 -0800397grpc_call *grpc_channel_create_call(grpc_channel *channel,
398 grpc_completion_queue *completion_queue,
399 const char *method, const char *host,
400 gpr_timespec deadline);
Craig Tiller034929c2015-02-02 16:56:15 -0800401
Craig Tiller08453372015-04-10 16:05:38 -0700402/* Pre-register a method/host pair on a channel. */
Craig Tillerb20111c2015-04-10 23:27:11 +0000403void *grpc_channel_register_call(grpc_channel *channel, const char *method,
Craig Tiller08453372015-04-10 16:05:38 -0700404 const char *host);
405
406/* Create a call given a handle returned from grpc_channel_register_call */
Craig Tillerb20111c2015-04-10 23:27:11 +0000407grpc_call *grpc_channel_create_registered_call(
408 grpc_channel *channel, grpc_completion_queue *completion_queue,
409 void *registered_call_handle, gpr_timespec deadline);
Craig Tiller08453372015-04-10 16:05:38 -0700410
Craig Tiller034929c2015-02-02 16:56:15 -0800411/* Start a batch of operations defined in the array ops; when complete, post a
Craig Tiller24be0f72015-02-10 14:04:22 -0800412 completion of type 'tag' to the completion queue bound to the call.
Craig Tillerb7800c12015-02-04 18:25:38 -0800413 The order of ops specified in the batch has no significance.
414 Only one operation of each type can be active at once in any given
415 batch. */
Craig Tillerfef76692015-02-02 16:44:26 -0800416grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
417 size_t nops, void *tag);
418
Craig Tiller29f2b212015-02-17 17:01:24 -0800419/* Create a client channel to 'target'. Additional channel level configuration
420 MAY be provided by grpc_channel_args, though the expectation is that most
421 clients will want to simply pass NULL. See grpc_channel_args definition
422 for more on this. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800423grpc_channel *grpc_channel_create(const char *target,
424 const grpc_channel_args *args);
425
Craig Tiller42bc87c2015-02-23 08:50:19 -0800426/* Create a lame client: this client fails every operation attempted on it. */
427grpc_channel *grpc_lame_client_channel_create(void);
428
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800429/* Close and destroy a grpc channel */
430void grpc_channel_destroy(grpc_channel *channel);
431
432/* THREAD-SAFETY for grpc_call
433 The following functions are thread-compatible for any given call:
434 grpc_call_add_metadata
435 grpc_call_invoke
436 grpc_call_start_write
437 grpc_call_writes_done
438 grpc_call_start_read
439 grpc_call_destroy
440 The function grpc_call_cancel is thread-safe, and can be called at any
441 point before grpc_call_destroy is called. */
442
443/* Error handling for grpc_call
444 Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
445 then the operation failed due to some unsatisfied precondition.
446 If a grpc_call fails, it's guaranteed that no change to the call state
447 has been made. */
448
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800449/* Called by clients to cancel an RPC on the server.
450 Can be called multiple times, from any thread. */
451grpc_call_error grpc_call_cancel(grpc_call *call);
452
Craig Tillerd248c242015-01-14 11:49:12 -0800453/* Called by clients to cancel an RPC on the server.
Craig Tiller6046dc32015-01-14 12:55:45 -0800454 Can be called multiple times, from any thread.
Craig Tillerd248c242015-01-14 11:49:12 -0800455 If a status has not been received for the call, set it to the status code
Craig Tiller6046dc32015-01-14 12:55:45 -0800456 and description passed in.
Craig Tillerd248c242015-01-14 11:49:12 -0800457 Importantly, this function does not send status nor description to the
458 remote endpoint. */
Craig Tiller6046dc32015-01-14 12:55:45 -0800459grpc_call_error grpc_call_cancel_with_status(grpc_call *call,
460 grpc_status_code status,
461 const char *description);
Craig Tillerd248c242015-01-14 11:49:12 -0800462
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800463/* Destroy a call. */
464void grpc_call_destroy(grpc_call *call);
465
Craig Tillerfada7d42015-02-11 23:03:55 -0800466/* Request notification of a new call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800467grpc_call_error grpc_server_request_call(
Craig Tillerea61b072015-02-03 19:19:27 -0800468 grpc_server *server, grpc_call **call, grpc_call_details *details,
Craig Tillerfb189f82015-02-03 12:07:07 -0800469 grpc_metadata_array *request_metadata,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700470 grpc_completion_queue *cq_bound_to_call,
471 grpc_completion_queue *cq_for_notification, void *tag_new);
Craig Tiller034929c2015-02-02 16:56:15 -0800472
Craig Tillerfada7d42015-02-11 23:03:55 -0800473/* Registers a method in the server.
474 Methods to this (host, method) pair will not be reported by
Craig Tiller06059952015-02-18 08:34:56 -0800475 grpc_server_request_call, but instead be reported by
Craig Tillerfada7d42015-02-11 23:03:55 -0800476 grpc_server_request_registered_call when passed the appropriate
477 registered_method (as returned by this function).
478 Must be called before grpc_server_start.
479 Returns NULL on failure. */
Craig Tiller24be0f72015-02-10 14:04:22 -0800480void *grpc_server_register_method(grpc_server *server, const char *method,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700481 const char *host);
Craig Tiller24be0f72015-02-10 14:04:22 -0800482
Craig Tillerfada7d42015-02-11 23:03:55 -0800483/* Request notification of a new pre-registered call */
Craig Tiller24be0f72015-02-10 14:04:22 -0800484grpc_call_error grpc_server_request_registered_call(
485 grpc_server *server, void *registered_method, grpc_call **call,
486 gpr_timespec *deadline, grpc_metadata_array *request_metadata,
487 grpc_byte_buffer **optional_payload,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700488 grpc_completion_queue *cq_bound_to_call,
489 grpc_completion_queue *cq_for_notification, void *tag_new);
Craig Tiller24be0f72015-02-10 14:04:22 -0800490
Craig Tiller29f2b212015-02-17 17:01:24 -0800491/* Create a server. Additional configuration for each incoming channel can
Craig Tillere7163ab2015-02-17 20:46:08 -0800492 be specified with args. If no additional configuration is needed, args can
493 be NULL. See grpc_channel_args for more. */
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700494grpc_server *grpc_server_create(const grpc_channel_args *args);
495
496/* Register a completion queue with the server. Must be done for any completion
497 queue that is passed to grpc_server_request_* call. Must be performed prior
498 to grpc_server_start. */
499void grpc_server_register_completion_queue(grpc_server *server,
500 grpc_completion_queue *cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800501
Craig Tillerd251ab92015-02-17 17:22:14 -0800502/* Add a HTTP2 over plaintext over tcp listener.
ctiller570d1f42015-01-12 16:29:52 -0800503 Returns bound port number on success, 0 on failure.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800504 REQUIRES: server not started */
505int grpc_server_add_http2_port(grpc_server *server, const char *addr);
506
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800507/* Start a server - tells all listeners to start listening */
508void grpc_server_start(grpc_server *server);
509
ctiller9a58df02014-12-11 16:26:49 -0800510/* Begin shutting down a server.
511 After completion, no new calls or connections will be admitted.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800512 Existing calls will be allowed to complete.
513 Shutdown is idempotent. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800514void grpc_server_shutdown(grpc_server *server);
515
Craig Tiller7d954702015-05-11 10:45:06 -0700516/* As per grpc_server_shutdown, but send a GRPC_OP_COMPLETE event when
Craig Tilleraea2fc02015-02-17 16:54:53 -0800517 there are no more calls being serviced.
518 Shutdown is idempotent, and all tags will be notified at once if multiple
519 grpc_server_shutdown_and_notify calls are made. */
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800520void grpc_server_shutdown_and_notify(grpc_server *server, void *tag);
521
ctiller9a58df02014-12-11 16:26:49 -0800522/* Destroy a server.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800523 Forcefully cancels all existing calls.
524 Implies grpc_server_shutdown() if one was not previously performed. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800525void grpc_server_destroy(grpc_server *server);
526
527#ifdef __cplusplus
528}
529#endif
530
Craig Tillerb20111c2015-04-10 23:27:11 +0000531#endif /* GRPC_GRPC_H */