blob: 7d769d0bc2ca600b61d10c755c0f0f281a0e4731 [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
Craig Tiller73b66ef2015-05-18 20:46:47 -070034/** \file grpc.h */
35
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010036#ifndef GRPC_GRPC_H
37#define GRPC_GRPC_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
39#include <grpc/status.h>
40
41#include <stddef.h>
42#include <grpc/support/slice.h>
43#include <grpc/support/time.h>
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
Nathaniel Manista6d41a052015-02-16 02:12:48 +000049/* Completion Queues enable notification of the completion of asynchronous
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050 actions. */
51typedef struct grpc_completion_queue grpc_completion_queue;
52
53/* The Channel interface allows creation of Call objects. */
54typedef struct grpc_channel grpc_channel;
55
56/* A server listens to some port and responds to request calls */
57typedef struct grpc_server grpc_server;
58
59/* A Call represents an RPC. When created, it is in a configuration state
60 allowing properties to be set until it is invoked. After invoke, the Call
61 can have messages written to it and read from it. */
62typedef struct grpc_call grpc_call;
63
64/* Type specifier for grpc_arg */
65typedef enum {
66 GRPC_ARG_STRING,
67 GRPC_ARG_INTEGER,
68 GRPC_ARG_POINTER
69} grpc_arg_type;
70
71/* A single argument... each argument has a key and a value
72
73 A note on naming keys:
74 Keys are namespaced into groups, usually grouped by library, and are
75 keys for module XYZ are named XYZ.key1, XYZ.key2, etc. Module names must
76 be restricted to the regex [A-Za-z][_A-Za-z0-9]{,15}.
77 Key names must be restricted to the regex [A-Za-z][_A-Za-z0-9]{,47}.
78
79 GRPC core library keys are prefixed by grpc.
80
81 Library authors are strongly encouraged to #define symbolic constants for
82 their keys so that it's possible to change them in the future. */
83typedef struct {
84 grpc_arg_type type;
85 char *key;
86 union {
87 char *string;
88 int integer;
89 struct {
90 void *p;
91 void *(*copy)(void *p);
92 void (*destroy)(void *p);
93 } pointer;
94 } value;
95} grpc_arg;
96
Craig Tillere793ba12015-05-18 09:37:22 -070097/** An array of arguments that can be passed around.
98
99 Used to set optional channel-level configuration.
100 These configuration options are modelled as key-value pairs as defined
101 by grpc_arg; keys are strings to allow easy backwards-compatible extension
102 by arbitrary parties.
103 All evaluation is performed at channel creation time. */
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 */
148 GRPC_CALL_ERROR_INVALID_METADATA
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149} grpc_call_error;
150
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800151/* Write Flags: */
152/* Hint that the write may be buffered and need not go out on the wire
153 immediately. GRPC is free to buffer the message until the next non-buffered
154 write, or until writes_done, but it need not buffer completely or at all. */
155#define GRPC_WRITE_BUFFER_HINT (0x00000001u)
156/* Force compression to be disabled for a particular write
157 (start_write/add_metadata). Illegal on invoke/accept. */
158#define GRPC_WRITE_NO_COMPRESS (0x00000002u)
159
160/* A buffer of bytes */
161struct grpc_byte_buffer;
162typedef struct grpc_byte_buffer grpc_byte_buffer;
163
Nathaniel Manista6d41a052015-02-16 02:12:48 +0000164/* Sample helpers to obtain byte buffers (these will certainly move
165 someplace else) */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166grpc_byte_buffer *grpc_byte_buffer_create(gpr_slice *slices, size_t nslices);
Craig Tiller80fa15c2015-01-13 16:10:49 -0800167grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800168size_t grpc_byte_buffer_length(grpc_byte_buffer *bb);
169void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer);
170
171/* Reader for byte buffers. Iterates over slices in the byte buffer */
172struct grpc_byte_buffer_reader;
173typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
174
175grpc_byte_buffer_reader *grpc_byte_buffer_reader_create(
176 grpc_byte_buffer *buffer);
177/* At the end of the stream, returns 0. Otherwise, returns 1 and sets slice to
178 be the returned slice. Caller is responsible for calling gpr_slice_unref on
179 the result. */
180int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
181 gpr_slice *slice);
182void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
183
184/* A single metadata element */
185typedef struct grpc_metadata {
Craig Tiller4f972732015-02-05 12:40:20 -0800186 const char *key;
187 const char *value;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188 size_t value_length;
Craig Tiller6902ad22015-04-16 08:01:49 -0700189
190 /* The following fields are reserved for grpc internal use.
191 There is no need to initialize them, and they will be set to garbage during
192 calls to grpc. */
193 struct {
Craig Tiller9c9d4e02015-04-20 09:03:29 -0700194 void *obfuscated[3];
Craig Tiller6902ad22015-04-16 08:01:49 -0700195 } internal_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800196} grpc_metadata;
197
Craig Tiller73b66ef2015-05-18 20:46:47 -0700198/** The type of completion (for grpc_event) */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199typedef enum grpc_completion_type {
Craig Tillere793ba12015-05-18 09:37:22 -0700200 /** Shutting down */
201 GRPC_QUEUE_SHUTDOWN,
202 /** No event before timeout */
203 GRPC_QUEUE_TIMEOUT,
204 /** Operation completion */
205 GRPC_OP_COMPLETE
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206} grpc_completion_type;
207
Craig Tillere793ba12015-05-18 09:37:22 -0700208/** The result of an operation.
209
210 Returned by a completion queue when the operation started with tag. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800211typedef struct grpc_event {
Craig Tillere793ba12015-05-18 09:37:22 -0700212 /** The type of the completion. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213 grpc_completion_type type;
Craig Tillere793ba12015-05-18 09:37:22 -0700214 /** non-zero if the operation was successful, 0 upon failure.
215 Only GRPC_OP_COMPLETE can succeed or fail. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700216 int success;
Craig Tillere793ba12015-05-18 09:37:22 -0700217 /** The tag passed to grpc_call_start_batch etc to start this operation.
218 Only GRPC_OP_COMPLETE has a tag. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219 void *tag;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800220} grpc_event;
221
Craig Tillerfef76692015-02-02 16:44:26 -0800222typedef struct {
223 size_t count;
224 size_t capacity;
225 grpc_metadata *metadata;
226} grpc_metadata_array;
227
Craig Tillerea61b072015-02-03 19:19:27 -0800228void grpc_metadata_array_init(grpc_metadata_array *array);
229void grpc_metadata_array_destroy(grpc_metadata_array *array);
230
Craig Tillerfef76692015-02-02 16:44:26 -0800231typedef struct {
Craig Tillerea61b072015-02-03 19:19:27 -0800232 char *method;
Craig Tiller034929c2015-02-02 16:56:15 -0800233 size_t method_capacity;
Craig Tillerea61b072015-02-03 19:19:27 -0800234 char *host;
Craig Tiller034929c2015-02-02 16:56:15 -0800235 size_t host_capacity;
Craig Tillerfef76692015-02-02 16:44:26 -0800236 gpr_timespec deadline;
237} grpc_call_details;
238
Craig Tillerea61b072015-02-03 19:19:27 -0800239void grpc_call_details_init(grpc_call_details *details);
240void grpc_call_details_destroy(grpc_call_details *details);
241
Craig Tillerfef76692015-02-02 16:44:26 -0800242typedef enum {
Craig Tiller24be0f72015-02-10 14:04:22 -0800243 /* Send initial metadata: one and only one instance MUST be sent for each
244 call,
Craig Tillerb7800c12015-02-04 18:25:38 -0800245 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800246 GRPC_OP_SEND_INITIAL_METADATA = 0,
Craig Tillerb7800c12015-02-04 18:25:38 -0800247 /* Send a message: 0 or more of these operations can occur for each call */
Craig Tillerfef76692015-02-02 16:44:26 -0800248 GRPC_OP_SEND_MESSAGE,
Craig Tiller24be0f72015-02-10 14:04:22 -0800249 /* Send a close from the server: one and only one instance MUST be sent from
250 the client,
Craig Tillerb7800c12015-02-04 18:25:38 -0800251 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800252 GRPC_OP_SEND_CLOSE_FROM_CLIENT,
Craig Tiller24be0f72015-02-10 14:04:22 -0800253 /* Send status from the server: one and only one instance MUST be sent from
254 the server
Craig Tillerb7800c12015-02-04 18:25:38 -0800255 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800256 GRPC_OP_SEND_STATUS_FROM_SERVER,
Craig Tillerb7800c12015-02-04 18:25:38 -0800257 /* Receive initial metadata: one and only one MUST be made on the client, must
258 not be made on the server */
Craig Tillerfef76692015-02-02 16:44:26 -0800259 GRPC_OP_RECV_INITIAL_METADATA,
Craig Tillerb7800c12015-02-04 18:25:38 -0800260 /* Receive a message: 0 or more of these operations can occur for each call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800261 GRPC_OP_RECV_MESSAGE,
Craig Tiller24be0f72015-02-10 14:04:22 -0800262 /* Receive status on the client: one and only one must be made on the client
263 */
Craig Tillerfef76692015-02-02 16:44:26 -0800264 GRPC_OP_RECV_STATUS_ON_CLIENT,
Craig Tiller24be0f72015-02-10 14:04:22 -0800265 /* Receive status on the server: one and only one must be made on the server
266 */
Craig Tillerfef76692015-02-02 16:44:26 -0800267 GRPC_OP_RECV_CLOSE_ON_SERVER
268} grpc_op_type;
269
Craig Tiller24be0f72015-02-10 14:04:22 -0800270/* Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT
271 which has
Craig Tillerb7800c12015-02-04 18:25:38 -0800272 no arguments) */
Craig Tillerfef76692015-02-02 16:44:26 -0800273typedef struct grpc_op {
274 grpc_op_type op;
275 union {
276 struct {
277 size_t count;
Craig Tiller6902ad22015-04-16 08:01:49 -0700278 grpc_metadata *metadata;
Craig Tillerfef76692015-02-02 16:44:26 -0800279 } send_initial_metadata;
280 grpc_byte_buffer *send_message;
281 struct {
282 size_t trailing_metadata_count;
283 grpc_metadata *trailing_metadata;
284 grpc_status_code status;
285 const char *status_details;
286 } send_status_from_server;
Craig Tiller4f972732015-02-05 12:40:20 -0800287 /* ownership of the array is with the caller, but ownership of the elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800288 stays with the call object (ie key, value members are owned by the call
289 object, recv_initial_metadata->array is owned by the caller).
290 After the operation completes, call grpc_metadata_array_destroy on this
291 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800292 grpc_metadata_array *recv_initial_metadata;
293 grpc_byte_buffer **recv_message;
294 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800295 /* ownership of the array is with the caller, but ownership of the
296 elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800297 stays with the call object (ie key, value members are owned by the call
298 object, trailing_metadata->array is owned by the caller).
299 After the operation completes, call grpc_metadata_array_destroy on this
300 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800301 grpc_metadata_array *trailing_metadata;
302 grpc_status_code *status;
Craig Tiller24be0f72015-02-10 14:04:22 -0800303 /* status_details is a buffer owned by the application before the op
304 completes
305 and after the op has completed. During the operation status_details may
306 be
307 reallocated to a size larger than *status_details_capacity, in which
308 case
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800309 *status_details_capacity will be updated with the new array capacity.
310
311 Pre-allocating space:
312 size_t my_capacity = 8;
313 char *my_details = gpr_malloc(my_capacity);
314 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800315 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800316
317 Not pre-allocating space:
318 size_t my_capacity = 0;
319 char *my_details = NULL;
320 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800321 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800322
323 After the call:
324 gpr_free(my_details); */
Craig Tillerfef76692015-02-02 16:44:26 -0800325 char **status_details;
326 size_t *status_details_capacity;
327 } recv_status_on_client;
328 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800329 /* out argument, set to 1 if the call failed in any way (seen as a
330 cancellation
Craig Tiller0a5bcbc2015-02-09 21:39:04 -0800331 on the server), or 0 if the call succeeded */
Craig Tillerfef76692015-02-02 16:44:26 -0800332 int *cancelled;
333 } recv_close_on_server;
334 } data;
335} grpc_op;
336
Craig Tillere793ba12015-05-18 09:37:22 -0700337/** Initialize the grpc library.
338
339 It is not safe to call any other grpc functions before calling this.
340 (To avoid overhead, little checking is done, and some things may work. We
341 do not warrant that they will continue to do so in future revisions of this
342 library). */
Craig Tiller32946d32015-01-15 11:37:30 -0800343void grpc_init(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800344
Craig Tillere793ba12015-05-18 09:37:22 -0700345/** Shut down the grpc library.
346
347 No memory is used by grpc after this call returns, nor are any instructions
348 executing within the grpc library.
349 Prior to calling, all application owned grpc objects must have been
350 destroyed. */
Craig Tiller32946d32015-01-15 11:37:30 -0800351void grpc_shutdown(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800352
Craig Tillere793ba12015-05-18 09:37:22 -0700353/** Create a completion queue */
Craig Tiller32946d32015-01-15 11:37:30 -0800354grpc_completion_queue *grpc_completion_queue_create(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800355
Craig Tillere793ba12015-05-18 09:37:22 -0700356/** Blocks until an event is available, the completion queue is being shut down,
357 or deadline is reached.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800358
Craig Tillere793ba12015-05-18 09:37:22 -0700359 Returns NULL on timeout, otherwise the event that occurred.
360
361 Callers must not call grpc_completion_queue_next and
362 grpc_completion_queue_pluck simultaneously on the same completion queue. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700363grpc_event grpc_completion_queue_next(grpc_completion_queue *cq,
364 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800365
Craig Tillere793ba12015-05-18 09:37:22 -0700366/** Blocks until an event with tag 'tag' is available, the completion queue is
367 being shutdown or deadline is reached.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800368
Craig Tillere793ba12015-05-18 09:37:22 -0700369 Returns NULL on timeout, or a pointer to the event that occurred.
370
371 Callers must not call grpc_completion_queue_next and
372 grpc_completion_queue_pluck simultaneously on the same completion queue. */
Craig Tiller64be9f72015-05-04 14:53:51 -0700373grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag,
374 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800375
376/* Begin destruction of a completion queue. Once all possible events are
Craig Tiller8ac56c92015-02-17 22:51:36 -0800377 drained then grpc_completion_queue_next will start to produce
Craig Tillerb20111c2015-04-10 23:27:11 +0000378 GRPC_QUEUE_SHUTDOWN events only. At that point it's safe to call
379 grpc_completion_queue_destroy.
380
Craig Tiller8ac56c92015-02-17 22:51:36 -0800381 After calling this function applications should ensure that no
382 NEW work is added to be published on this completion queue. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800383void grpc_completion_queue_shutdown(grpc_completion_queue *cq);
384
385/* Destroy a completion queue. The caller must ensure that the queue is
386 drained and no threads are executing grpc_completion_queue_next */
387void grpc_completion_queue_destroy(grpc_completion_queue *cq);
388
389/* Create a call given a grpc_channel, in order to call 'method'. The request
390 is not sent until grpc_call_invoke is called. All completions are sent to
391 'completion_queue'. */
Craig Tillerfb189f82015-02-03 12:07:07 -0800392grpc_call *grpc_channel_create_call(grpc_channel *channel,
393 grpc_completion_queue *completion_queue,
394 const char *method, const char *host,
395 gpr_timespec deadline);
Craig Tiller034929c2015-02-02 16:56:15 -0800396
Craig Tiller08453372015-04-10 16:05:38 -0700397/* Pre-register a method/host pair on a channel. */
Craig Tillerb20111c2015-04-10 23:27:11 +0000398void *grpc_channel_register_call(grpc_channel *channel, const char *method,
Craig Tiller08453372015-04-10 16:05:38 -0700399 const char *host);
400
401/* Create a call given a handle returned from grpc_channel_register_call */
Craig Tillerb20111c2015-04-10 23:27:11 +0000402grpc_call *grpc_channel_create_registered_call(
403 grpc_channel *channel, grpc_completion_queue *completion_queue,
404 void *registered_call_handle, gpr_timespec deadline);
Craig Tiller08453372015-04-10 16:05:38 -0700405
Craig Tiller034929c2015-02-02 16:56:15 -0800406/* Start a batch of operations defined in the array ops; when complete, post a
Craig Tiller24be0f72015-02-10 14:04:22 -0800407 completion of type 'tag' to the completion queue bound to the call.
Craig Tillerb7800c12015-02-04 18:25:38 -0800408 The order of ops specified in the batch has no significance.
409 Only one operation of each type can be active at once in any given
410 batch. */
Craig Tillerfef76692015-02-02 16:44:26 -0800411grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
412 size_t nops, void *tag);
413
Craig Tiller29f2b212015-02-17 17:01:24 -0800414/* Create a client channel to 'target'. Additional channel level configuration
415 MAY be provided by grpc_channel_args, though the expectation is that most
416 clients will want to simply pass NULL. See grpc_channel_args definition
417 for more on this. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800418grpc_channel *grpc_channel_create(const char *target,
419 const grpc_channel_args *args);
420
Craig Tiller42bc87c2015-02-23 08:50:19 -0800421/* Create a lame client: this client fails every operation attempted on it. */
422grpc_channel *grpc_lame_client_channel_create(void);
423
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800424/* Close and destroy a grpc channel */
425void grpc_channel_destroy(grpc_channel *channel);
426
427/* THREAD-SAFETY for grpc_call
428 The following functions are thread-compatible for any given call:
429 grpc_call_add_metadata
430 grpc_call_invoke
431 grpc_call_start_write
432 grpc_call_writes_done
433 grpc_call_start_read
434 grpc_call_destroy
435 The function grpc_call_cancel is thread-safe, and can be called at any
436 point before grpc_call_destroy is called. */
437
438/* Error handling for grpc_call
439 Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
440 then the operation failed due to some unsatisfied precondition.
441 If a grpc_call fails, it's guaranteed that no change to the call state
442 has been made. */
443
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800444/* Called by clients to cancel an RPC on the server.
445 Can be called multiple times, from any thread. */
446grpc_call_error grpc_call_cancel(grpc_call *call);
447
Craig Tillerd248c242015-01-14 11:49:12 -0800448/* Called by clients to cancel an RPC on the server.
Craig Tiller6046dc32015-01-14 12:55:45 -0800449 Can be called multiple times, from any thread.
Craig Tillerd248c242015-01-14 11:49:12 -0800450 If a status has not been received for the call, set it to the status code
Craig Tiller6046dc32015-01-14 12:55:45 -0800451 and description passed in.
Craig Tillerd248c242015-01-14 11:49:12 -0800452 Importantly, this function does not send status nor description to the
453 remote endpoint. */
Craig Tiller6046dc32015-01-14 12:55:45 -0800454grpc_call_error grpc_call_cancel_with_status(grpc_call *call,
455 grpc_status_code status,
456 const char *description);
Craig Tillerd248c242015-01-14 11:49:12 -0800457
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800458/* Destroy a call. */
459void grpc_call_destroy(grpc_call *call);
460
Craig Tillerfada7d42015-02-11 23:03:55 -0800461/* Request notification of a new call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800462grpc_call_error grpc_server_request_call(
Craig Tillerea61b072015-02-03 19:19:27 -0800463 grpc_server *server, grpc_call **call, grpc_call_details *details,
Craig Tillerfb189f82015-02-03 12:07:07 -0800464 grpc_metadata_array *request_metadata,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700465 grpc_completion_queue *cq_bound_to_call,
466 grpc_completion_queue *cq_for_notification, void *tag_new);
Craig Tiller034929c2015-02-02 16:56:15 -0800467
Craig Tillerfada7d42015-02-11 23:03:55 -0800468/* Registers a method in the server.
469 Methods to this (host, method) pair will not be reported by
Craig Tiller06059952015-02-18 08:34:56 -0800470 grpc_server_request_call, but instead be reported by
Craig Tillerfada7d42015-02-11 23:03:55 -0800471 grpc_server_request_registered_call when passed the appropriate
472 registered_method (as returned by this function).
473 Must be called before grpc_server_start.
474 Returns NULL on failure. */
Craig Tiller24be0f72015-02-10 14:04:22 -0800475void *grpc_server_register_method(grpc_server *server, const char *method,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700476 const char *host);
Craig Tiller24be0f72015-02-10 14:04:22 -0800477
Craig Tillerfada7d42015-02-11 23:03:55 -0800478/* Request notification of a new pre-registered call */
Craig Tiller24be0f72015-02-10 14:04:22 -0800479grpc_call_error grpc_server_request_registered_call(
480 grpc_server *server, void *registered_method, grpc_call **call,
481 gpr_timespec *deadline, grpc_metadata_array *request_metadata,
482 grpc_byte_buffer **optional_payload,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700483 grpc_completion_queue *cq_bound_to_call,
484 grpc_completion_queue *cq_for_notification, void *tag_new);
Craig Tiller24be0f72015-02-10 14:04:22 -0800485
Craig Tiller29f2b212015-02-17 17:01:24 -0800486/* Create a server. Additional configuration for each incoming channel can
Craig Tillere7163ab2015-02-17 20:46:08 -0800487 be specified with args. If no additional configuration is needed, args can
488 be NULL. See grpc_channel_args for more. */
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700489grpc_server *grpc_server_create(const grpc_channel_args *args);
490
491/* Register a completion queue with the server. Must be done for any completion
492 queue that is passed to grpc_server_request_* call. Must be performed prior
493 to grpc_server_start. */
494void grpc_server_register_completion_queue(grpc_server *server,
495 grpc_completion_queue *cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800496
Craig Tillerd251ab92015-02-17 17:22:14 -0800497/* Add a HTTP2 over plaintext over tcp listener.
ctiller570d1f42015-01-12 16:29:52 -0800498 Returns bound port number on success, 0 on failure.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800499 REQUIRES: server not started */
500int grpc_server_add_http2_port(grpc_server *server, const char *addr);
501
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800502/* Start a server - tells all listeners to start listening */
503void grpc_server_start(grpc_server *server);
504
ctiller9a58df02014-12-11 16:26:49 -0800505/* Begin shutting down a server.
506 After completion, no new calls or connections will be admitted.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800507 Existing calls will be allowed to complete.
508 Shutdown is idempotent. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800509void grpc_server_shutdown(grpc_server *server);
510
Craig Tiller7d954702015-05-11 10:45:06 -0700511/* As per grpc_server_shutdown, but send a GRPC_OP_COMPLETE event when
Craig Tilleraea2fc02015-02-17 16:54:53 -0800512 there are no more calls being serviced.
513 Shutdown is idempotent, and all tags will be notified at once if multiple
514 grpc_server_shutdown_and_notify calls are made. */
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800515void grpc_server_shutdown_and_notify(grpc_server *server, void *tag);
516
ctiller9a58df02014-12-11 16:26:49 -0800517/* Destroy a server.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800518 Forcefully cancels all existing calls.
519 Implies grpc_server_shutdown() if one was not previously performed. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800520void grpc_server_destroy(grpc_server *server);
521
522#ifdef __cplusplus
523}
524#endif
525
Craig Tillerb20111c2015-04-10 23:27:11 +0000526#endif /* GRPC_GRPC_H */