blob: b7d14a521eb9a305e2bce2c2a0b870a243e5deca [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 Tiller29f2b212015-02-17 17:01:24 -080095/* An array of arguments that can be passed around.
96 Used to set optional channel-level configuration.
97 These configuration options are modelled as key-value pairs as defined
98 by grpc_arg; keys are strings to allow easy backwards-compatible extension
99 by arbitrary parties.
100 All evaluation is performed at channel creation time. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101typedef struct {
102 size_t num_args;
103 grpc_arg *args;
104} grpc_channel_args;
105
106/* Channel argument keys: */
107/* Enable census for tracing and stats collection */
108#define GRPC_ARG_ENABLE_CENSUS "grpc.census"
109/* Maximum number of concurrent incoming streams to allow on a http2
110 connection */
111#define GRPC_ARG_MAX_CONCURRENT_STREAMS "grpc.max_concurrent_streams"
112/* Maximum message length that the channel can receive */
113#define GRPC_ARG_MAX_MESSAGE_LENGTH "grpc.max_message_length"
114
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115/* Result of a grpc call. If the caller satisfies the prerequisites of a
116 particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
117 Receiving any other value listed here is an indication of a bug in the
118 caller. */
119typedef enum grpc_call_error {
120 /* everything went ok */
121 GRPC_CALL_OK = 0,
122 /* something failed, we don't know what */
123 GRPC_CALL_ERROR,
124 /* this method is not available on the server */
125 GRPC_CALL_ERROR_NOT_ON_SERVER,
126 /* this method is not available on the client */
127 GRPC_CALL_ERROR_NOT_ON_CLIENT,
nnoble0c475f02014-12-05 15:37:39 -0800128 /* this method must be called before server_accept */
129 GRPC_CALL_ERROR_ALREADY_ACCEPTED,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130 /* this method must be called before invoke */
131 GRPC_CALL_ERROR_ALREADY_INVOKED,
132 /* this method must be called after invoke */
133 GRPC_CALL_ERROR_NOT_INVOKED,
134 /* this call is already finished
135 (writes_done or write_status has already been called) */
136 GRPC_CALL_ERROR_ALREADY_FINISHED,
137 /* there is already an outstanding read/write operation on the call */
138 GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
139 /* the flags value was illegal for this call */
140 GRPC_CALL_ERROR_INVALID_FLAGS
141} grpc_call_error;
142
143/* Result of a grpc operation */
144typedef enum grpc_op_error {
145 /* everything went ok */
146 GRPC_OP_OK = 0,
147 /* something failed, we don't know what */
148 GRPC_OP_ERROR
149} grpc_op_error;
150
151/* 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
198typedef enum grpc_completion_type {
Craig Tillerfef76692015-02-02 16:44:26 -0800199 GRPC_QUEUE_SHUTDOWN, /* Shutting down */
Craig Tillerfb189f82015-02-03 12:07:07 -0800200 GRPC_OP_COMPLETE, /* operation completion */
Craig Tillerfef76692015-02-02 16:44:26 -0800201 GRPC_READ, /* A read has completed */
202 GRPC_WRITE_ACCEPTED, /* A write has been accepted by
203 flow control */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204 GRPC_FINISH_ACCEPTED, /* writes_done or write_status has been accepted */
205 GRPC_CLIENT_METADATA_READ, /* The metadata array sent by server received at
206 client */
Craig Tillerfef76692015-02-02 16:44:26 -0800207 GRPC_FINISHED, /* An RPC has finished. The event contains status.
208 On the server this will be OK or Cancelled. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 GRPC_SERVER_RPC_NEW, /* A new RPC has arrived at the server */
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800210 GRPC_SERVER_SHUTDOWN, /* The server has finished shutting down */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800211 GRPC_COMPLETION_DO_NOT_USE /* must be last, forces users to include
212 a default: case */
213} grpc_completion_type;
214
215typedef struct grpc_event {
216 grpc_completion_type type;
217 void *tag;
218 grpc_call *call;
219 /* Data associated with the completion type. Field names match the type of
220 completion as listed in grpc_completion_type. */
221 union {
222 /* Contains a pointer to the buffer that was read, or NULL at the end of a
223 stream. */
224 grpc_byte_buffer *read;
225 grpc_op_error write_accepted;
226 grpc_op_error finish_accepted;
227 grpc_op_error invoke_accepted;
Craig Tillerfb189f82015-02-03 12:07:07 -0800228 grpc_op_error op_complete;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800229 struct {
230 size_t count;
231 grpc_metadata *elements;
232 } client_metadata_read;
ctiller2845cad2014-12-15 15:14:12 -0800233 struct {
234 grpc_status_code status;
235 const char *details;
236 size_t metadata_count;
237 grpc_metadata *metadata_elements;
238 } finished;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239 struct {
240 const char *method;
241 const char *host;
242 gpr_timespec deadline;
243 size_t metadata_count;
244 grpc_metadata *metadata_elements;
245 } server_rpc_new;
246 } data;
247} grpc_event;
248
Craig Tillerfef76692015-02-02 16:44:26 -0800249typedef struct {
250 size_t count;
251 size_t capacity;
252 grpc_metadata *metadata;
253} grpc_metadata_array;
254
Craig Tillerea61b072015-02-03 19:19:27 -0800255void grpc_metadata_array_init(grpc_metadata_array *array);
256void grpc_metadata_array_destroy(grpc_metadata_array *array);
257
Craig Tillerfef76692015-02-02 16:44:26 -0800258typedef struct {
Craig Tillerea61b072015-02-03 19:19:27 -0800259 char *method;
Craig Tiller034929c2015-02-02 16:56:15 -0800260 size_t method_capacity;
Craig Tillerea61b072015-02-03 19:19:27 -0800261 char *host;
Craig Tiller034929c2015-02-02 16:56:15 -0800262 size_t host_capacity;
Craig Tillerfef76692015-02-02 16:44:26 -0800263 gpr_timespec deadline;
264} grpc_call_details;
265
Craig Tillerea61b072015-02-03 19:19:27 -0800266void grpc_call_details_init(grpc_call_details *details);
267void grpc_call_details_destroy(grpc_call_details *details);
268
Craig Tillerfef76692015-02-02 16:44:26 -0800269typedef enum {
Craig Tiller24be0f72015-02-10 14:04:22 -0800270 /* Send initial metadata: one and only one instance MUST be sent for each
271 call,
Craig Tillerb7800c12015-02-04 18:25:38 -0800272 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800273 GRPC_OP_SEND_INITIAL_METADATA = 0,
Craig Tillerb7800c12015-02-04 18:25:38 -0800274 /* Send a message: 0 or more of these operations can occur for each call */
Craig Tillerfef76692015-02-02 16:44:26 -0800275 GRPC_OP_SEND_MESSAGE,
Craig Tiller24be0f72015-02-10 14:04:22 -0800276 /* Send a close from the server: one and only one instance MUST be sent from
277 the client,
Craig Tillerb7800c12015-02-04 18:25:38 -0800278 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800279 GRPC_OP_SEND_CLOSE_FROM_CLIENT,
Craig Tiller24be0f72015-02-10 14:04:22 -0800280 /* Send status from the server: one and only one instance MUST be sent from
281 the server
Craig Tillerb7800c12015-02-04 18:25:38 -0800282 unless the call was cancelled - in which case this can be skipped */
Craig Tillerfef76692015-02-02 16:44:26 -0800283 GRPC_OP_SEND_STATUS_FROM_SERVER,
Craig Tillerb7800c12015-02-04 18:25:38 -0800284 /* Receive initial metadata: one and only one MUST be made on the client, must
285 not be made on the server */
Craig Tillerfef76692015-02-02 16:44:26 -0800286 GRPC_OP_RECV_INITIAL_METADATA,
Craig Tillerb7800c12015-02-04 18:25:38 -0800287 /* Receive a message: 0 or more of these operations can occur for each call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800288 GRPC_OP_RECV_MESSAGE,
Craig Tiller24be0f72015-02-10 14:04:22 -0800289 /* Receive status on the client: one and only one must be made on the client
290 */
Craig Tillerfef76692015-02-02 16:44:26 -0800291 GRPC_OP_RECV_STATUS_ON_CLIENT,
Craig Tiller24be0f72015-02-10 14:04:22 -0800292 /* Receive status on the server: one and only one must be made on the server
293 */
Craig Tillerfef76692015-02-02 16:44:26 -0800294 GRPC_OP_RECV_CLOSE_ON_SERVER
295} grpc_op_type;
296
Craig Tiller24be0f72015-02-10 14:04:22 -0800297/* Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT
298 which has
Craig Tillerb7800c12015-02-04 18:25:38 -0800299 no arguments) */
Craig Tillerfef76692015-02-02 16:44:26 -0800300typedef struct grpc_op {
301 grpc_op_type op;
302 union {
303 struct {
304 size_t count;
Craig Tiller6902ad22015-04-16 08:01:49 -0700305 grpc_metadata *metadata;
Craig Tillerfef76692015-02-02 16:44:26 -0800306 } send_initial_metadata;
307 grpc_byte_buffer *send_message;
308 struct {
309 size_t trailing_metadata_count;
310 grpc_metadata *trailing_metadata;
311 grpc_status_code status;
312 const char *status_details;
313 } send_status_from_server;
Craig Tiller4f972732015-02-05 12:40:20 -0800314 /* ownership of the array is with the caller, but ownership of the elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800315 stays with the call object (ie key, value members are owned by the call
316 object, recv_initial_metadata->array is owned by the caller).
317 After the operation completes, call grpc_metadata_array_destroy on this
318 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800319 grpc_metadata_array *recv_initial_metadata;
320 grpc_byte_buffer **recv_message;
321 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800322 /* ownership of the array is with the caller, but ownership of the
323 elements
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800324 stays with the call object (ie key, value members are owned by the call
325 object, trailing_metadata->array is owned by the caller).
326 After the operation completes, call grpc_metadata_array_destroy on this
327 value, or reuse it in a future op. */
Craig Tillerfef76692015-02-02 16:44:26 -0800328 grpc_metadata_array *trailing_metadata;
329 grpc_status_code *status;
Craig Tiller24be0f72015-02-10 14:04:22 -0800330 /* status_details is a buffer owned by the application before the op
331 completes
332 and after the op has completed. During the operation status_details may
333 be
334 reallocated to a size larger than *status_details_capacity, in which
335 case
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800336 *status_details_capacity will be updated with the new array capacity.
337
338 Pre-allocating space:
339 size_t my_capacity = 8;
340 char *my_details = gpr_malloc(my_capacity);
341 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800342 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800343
344 Not pre-allocating space:
345 size_t my_capacity = 0;
346 char *my_details = NULL;
347 x.status_details = &my_details;
Craig Tiller24be0f72015-02-10 14:04:22 -0800348 x.status_details_capacity = &my_capacity;
Craig Tillerc56b2cb2015-02-05 16:35:38 -0800349
350 After the call:
351 gpr_free(my_details); */
Craig Tillerfef76692015-02-02 16:44:26 -0800352 char **status_details;
353 size_t *status_details_capacity;
354 } recv_status_on_client;
355 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -0800356 /* out argument, set to 1 if the call failed in any way (seen as a
357 cancellation
Craig Tiller0a5bcbc2015-02-09 21:39:04 -0800358 on the server), or 0 if the call succeeded */
Craig Tillerfef76692015-02-02 16:44:26 -0800359 int *cancelled;
360 } recv_close_on_server;
361 } data;
362} grpc_op;
363
Craig Tillera0e34a02015-02-17 17:06:23 -0800364/* Initialize the grpc library.
365 It is not safe to call any other grpc functions before calling this.
366 (To avoid overhead, little checking is done, and some things may work. We
367 do not warrant that they will continue to do so in future revisions of this
368 library). */
Craig Tiller32946d32015-01-15 11:37:30 -0800369void grpc_init(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800370
Craig Tillerb20111c2015-04-10 23:27:11 +0000371/* Shut down the grpc library.
Craig Tillera0e34a02015-02-17 17:06:23 -0800372 No memory is used by grpc after this call returns, nor are any instructions
373 executing within the grpc library.
374 Prior to calling, all application owned grpc objects must have been
375 destroyed. */
Craig Tiller32946d32015-01-15 11:37:30 -0800376void grpc_shutdown(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800377
Craig Tiller32946d32015-01-15 11:37:30 -0800378grpc_completion_queue *grpc_completion_queue_create(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800379
Nathaniel Manista6d41a052015-02-16 02:12:48 +0000380/* Blocks until an event is available, the completion queue is being shut down,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800381 or deadline is reached. Returns NULL on timeout, otherwise the event that
382 occurred. Callers should call grpc_event_finish once they have processed
383 the event.
384
385 Callers must not call grpc_completion_queue_next and
386 grpc_completion_queue_pluck simultaneously on the same completion queue. */
387grpc_event *grpc_completion_queue_next(grpc_completion_queue *cq,
388 gpr_timespec deadline);
389
390/* Blocks until an event with tag 'tag' is available, the completion queue is
391 being shutdown or deadline is reached. Returns NULL on timeout, or a pointer
392 to the event that occurred. Callers should call grpc_event_finish once they
393 have processed the event.
394
395 Callers must not call grpc_completion_queue_next and
396 grpc_completion_queue_pluck simultaneously on the same completion queue. */
397grpc_event *grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag,
398 gpr_timespec deadline);
399
Nathaniel Manista6d41a052015-02-16 02:12:48 +0000400/* Clean up any data owned by the event */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800401void grpc_event_finish(grpc_event *event);
402
403/* Begin destruction of a completion queue. Once all possible events are
Craig Tiller8ac56c92015-02-17 22:51:36 -0800404 drained then grpc_completion_queue_next will start to produce
Craig Tillerb20111c2015-04-10 23:27:11 +0000405 GRPC_QUEUE_SHUTDOWN events only. At that point it's safe to call
406 grpc_completion_queue_destroy.
407
Craig Tiller8ac56c92015-02-17 22:51:36 -0800408 After calling this function applications should ensure that no
409 NEW work is added to be published on this completion queue. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800410void grpc_completion_queue_shutdown(grpc_completion_queue *cq);
411
412/* Destroy a completion queue. The caller must ensure that the queue is
413 drained and no threads are executing grpc_completion_queue_next */
414void grpc_completion_queue_destroy(grpc_completion_queue *cq);
415
416/* Create a call given a grpc_channel, in order to call 'method'. The request
417 is not sent until grpc_call_invoke is called. All completions are sent to
418 'completion_queue'. */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800419grpc_call *grpc_channel_create_call_old(grpc_channel *channel,
420 const char *method, const char *host,
421 gpr_timespec deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800422
Craig Tiller034929c2015-02-02 16:56:15 -0800423/* Create a call given a grpc_channel, in order to call 'method'. The request
424 is not sent until grpc_call_invoke is called. All completions are sent to
425 'completion_queue'. */
Craig Tillerfb189f82015-02-03 12:07:07 -0800426grpc_call *grpc_channel_create_call(grpc_channel *channel,
427 grpc_completion_queue *completion_queue,
428 const char *method, const char *host,
429 gpr_timespec deadline);
Craig Tiller034929c2015-02-02 16:56:15 -0800430
Craig Tiller08453372015-04-10 16:05:38 -0700431/* Pre-register a method/host pair on a channel. */
Craig Tillerb20111c2015-04-10 23:27:11 +0000432void *grpc_channel_register_call(grpc_channel *channel, const char *method,
Craig Tiller08453372015-04-10 16:05:38 -0700433 const char *host);
434
435/* Create a call given a handle returned from grpc_channel_register_call */
Craig Tillerb20111c2015-04-10 23:27:11 +0000436grpc_call *grpc_channel_create_registered_call(
437 grpc_channel *channel, grpc_completion_queue *completion_queue,
438 void *registered_call_handle, gpr_timespec deadline);
Craig Tiller08453372015-04-10 16:05:38 -0700439
Craig Tiller034929c2015-02-02 16:56:15 -0800440/* Start a batch of operations defined in the array ops; when complete, post a
Craig Tiller24be0f72015-02-10 14:04:22 -0800441 completion of type 'tag' to the completion queue bound to the call.
Craig Tillerb7800c12015-02-04 18:25:38 -0800442 The order of ops specified in the batch has no significance.
443 Only one operation of each type can be active at once in any given
444 batch. */
Craig Tillerfef76692015-02-02 16:44:26 -0800445grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
446 size_t nops, void *tag);
447
Craig Tiller29f2b212015-02-17 17:01:24 -0800448/* Create a client channel to 'target'. Additional channel level configuration
449 MAY be provided by grpc_channel_args, though the expectation is that most
450 clients will want to simply pass NULL. See grpc_channel_args definition
451 for more on this. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800452grpc_channel *grpc_channel_create(const char *target,
453 const grpc_channel_args *args);
454
Craig Tiller42bc87c2015-02-23 08:50:19 -0800455/* Create a lame client: this client fails every operation attempted on it. */
456grpc_channel *grpc_lame_client_channel_create(void);
457
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800458/* Close and destroy a grpc channel */
459void grpc_channel_destroy(grpc_channel *channel);
460
461/* THREAD-SAFETY for grpc_call
462 The following functions are thread-compatible for any given call:
463 grpc_call_add_metadata
464 grpc_call_invoke
465 grpc_call_start_write
466 grpc_call_writes_done
467 grpc_call_start_read
468 grpc_call_destroy
469 The function grpc_call_cancel is thread-safe, and can be called at any
470 point before grpc_call_destroy is called. */
471
472/* Error handling for grpc_call
473 Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
474 then the operation failed due to some unsatisfied precondition.
475 If a grpc_call fails, it's guaranteed that no change to the call state
476 has been made. */
477
478/* Add a single metadata element to the call, to be sent upon invocation.
479 flags is a bit-field combination of the write flags defined above.
yanggfb3aa262014-12-16 15:29:57 -0800480 REQUIRES: grpc_call_start_invoke/grpc_call_server_end_initial_metadata have
481 not been called on this call.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800482 Produces no events. */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800483grpc_call_error grpc_call_add_metadata_old(grpc_call *call,
484 grpc_metadata *metadata,
485 gpr_uint32 flags);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800486
487/* Invoke the RPC. Starts sending metadata and request headers on the wire.
488 flags is a bit-field combination of the write flags defined above.
489 REQUIRES: Can be called at most once per call.
490 Can only be called on the client.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800491 Produces a GRPC_CLIENT_METADATA_READ event with metadata_read_tag when
492 the servers initial metadata has been read.
493 Produces a GRPC_FINISHED event with finished_tag when the call has been
494 completed (there may be other events for the call pending at this
495 time) */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800496grpc_call_error grpc_call_invoke_old(grpc_call *call, grpc_completion_queue *cq,
497 void *metadata_read_tag,
498 void *finished_tag, gpr_uint32 flags);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800499
nnoble0c475f02014-12-05 15:37:39 -0800500/* Accept an incoming RPC, binding a completion queue to it.
501 To be called before sending or receiving messages.
nnoble0c475f02014-12-05 15:37:39 -0800502 REQUIRES: Can be called at most once per call.
503 Can only be called on the server.
504 Produces a GRPC_FINISHED event with finished_tag when the call has been
505 completed (there may be other events for the call pending at this
506 time) */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800507grpc_call_error grpc_call_server_accept_old(grpc_call *call,
508 grpc_completion_queue *cq,
509 void *finished_tag);
nnoble0c475f02014-12-05 15:37:39 -0800510
yanggfb3aa262014-12-16 15:29:57 -0800511/* Start sending metadata.
nnoble0c475f02014-12-05 15:37:39 -0800512 To be called before sending messages.
513 flags is a bit-field combination of the write flags defined above.
514 REQUIRES: Can be called at most once per call.
515 Can only be called on the server.
516 Must be called after grpc_call_server_accept */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800517grpc_call_error grpc_call_server_end_initial_metadata_old(grpc_call *call,
518 gpr_uint32 flags);
nnoble0c475f02014-12-05 15:37:39 -0800519
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800520/* Called by clients to cancel an RPC on the server.
521 Can be called multiple times, from any thread. */
522grpc_call_error grpc_call_cancel(grpc_call *call);
523
Craig Tillerd248c242015-01-14 11:49:12 -0800524/* Called by clients to cancel an RPC on the server.
Craig Tiller6046dc32015-01-14 12:55:45 -0800525 Can be called multiple times, from any thread.
Craig Tillerd248c242015-01-14 11:49:12 -0800526 If a status has not been received for the call, set it to the status code
Craig Tiller6046dc32015-01-14 12:55:45 -0800527 and description passed in.
Craig Tillerd248c242015-01-14 11:49:12 -0800528 Importantly, this function does not send status nor description to the
529 remote endpoint. */
Craig Tiller6046dc32015-01-14 12:55:45 -0800530grpc_call_error grpc_call_cancel_with_status(grpc_call *call,
531 grpc_status_code status,
532 const char *description);
Craig Tillerd248c242015-01-14 11:49:12 -0800533
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800534/* Queue a byte buffer for writing.
535 flags is a bit-field combination of the write flags defined above.
536 A write with byte_buffer null is allowed, and will not send any bytes on the
537 wire. If this is performed without GRPC_WRITE_BUFFER_HINT flag it provides
538 a mechanism to flush any previously buffered writes to outgoing flow control.
539 REQUIRES: No other writes are pending on the call. It is only safe to
540 start the next write after the corresponding write_accepted event
541 is received.
542 GRPC_INVOKE_ACCEPTED must have been received by the application
543 prior to calling this on the client. On the server,
nnoble0c475f02014-12-05 15:37:39 -0800544 grpc_call_server_end_of_initial_metadata must have been called
545 successfully.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800546 Produces a GRPC_WRITE_ACCEPTED event. */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800547grpc_call_error grpc_call_start_write_old(grpc_call *call,
548 grpc_byte_buffer *byte_buffer,
549 void *tag, gpr_uint32 flags);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800550
551/* Queue a status for writing.
552 REQUIRES: No other writes are pending on the call.
yanggfb3aa262014-12-16 15:29:57 -0800553 grpc_call_server_end_initial_metadata must have been called on the
554 call prior to calling this.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800555 Only callable on the server.
556 Produces a GRPC_FINISH_ACCEPTED event when the status is sent. */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800557grpc_call_error grpc_call_start_write_status_old(grpc_call *call,
558 grpc_status_code status_code,
559 const char *status_message,
560 void *tag);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800561
562/* No more messages to send.
563 REQUIRES: No other writes are pending on the call.
564 Only callable on the client.
565 Produces a GRPC_FINISH_ACCEPTED event when all bytes for the call have passed
566 outgoing flow control. */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800567grpc_call_error grpc_call_writes_done_old(grpc_call *call, void *tag);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800568
569/* Initiate a read on a call. Output event contains a byte buffer with the
570 result of the read.
571 REQUIRES: No other reads are pending on the call. It is only safe to start
572 the next read after the corresponding read event is received.
nnoble0c475f02014-12-05 15:37:39 -0800573 On the client:
574 GRPC_INVOKE_ACCEPTED must have been received by the application
575 prior to calling this.
576 On the server:
577 grpc_call_server_accept must be called before calling this.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800578 Produces a single GRPC_READ event. */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800579grpc_call_error grpc_call_start_read_old(grpc_call *call, void *tag);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800580
581/* Destroy a call. */
582void grpc_call_destroy(grpc_call *call);
583
584/* Request a call on a server.
585 Allows the server to create a single GRPC_SERVER_RPC_NEW event, with tag
586 tag_new.
587 If the call is subsequently cancelled, the cancellation will occur with tag
588 tag_cancel.
589 REQUIRES: Server must not have been shutdown.
590 NOTE: calling this is the only way to obtain GRPC_SERVER_RPC_NEW events. */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800591grpc_call_error grpc_server_request_call_old(grpc_server *server,
592 void *tag_new);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800593
Craig Tillerfada7d42015-02-11 23:03:55 -0800594/* Request notification of a new call */
Craig Tillerfb189f82015-02-03 12:07:07 -0800595grpc_call_error grpc_server_request_call(
Craig Tillerea61b072015-02-03 19:19:27 -0800596 grpc_server *server, grpc_call **call, grpc_call_details *details,
Craig Tillerfb189f82015-02-03 12:07:07 -0800597 grpc_metadata_array *request_metadata,
Craig Tillerb20111c2015-04-10 23:27:11 +0000598 grpc_completion_queue *cq_bound_to_call, void *tag_new);
Craig Tiller034929c2015-02-02 16:56:15 -0800599
Craig Tillerfada7d42015-02-11 23:03:55 -0800600/* Registers a method in the server.
601 Methods to this (host, method) pair will not be reported by
Craig Tiller06059952015-02-18 08:34:56 -0800602 grpc_server_request_call, but instead be reported by
Craig Tillerfada7d42015-02-11 23:03:55 -0800603 grpc_server_request_registered_call when passed the appropriate
604 registered_method (as returned by this function).
605 Must be called before grpc_server_start.
606 Returns NULL on failure. */
Craig Tiller24be0f72015-02-10 14:04:22 -0800607void *grpc_server_register_method(grpc_server *server, const char *method,
Craig Tiller06059952015-02-18 08:34:56 -0800608 const char *host,
Craig Tiller20bc56d2015-02-12 09:02:56 -0800609 grpc_completion_queue *new_call_cq);
Craig Tiller24be0f72015-02-10 14:04:22 -0800610
Craig Tillerfada7d42015-02-11 23:03:55 -0800611/* Request notification of a new pre-registered call */
Craig Tiller24be0f72015-02-10 14:04:22 -0800612grpc_call_error grpc_server_request_registered_call(
613 grpc_server *server, void *registered_method, grpc_call **call,
614 gpr_timespec *deadline, grpc_metadata_array *request_metadata,
615 grpc_byte_buffer **optional_payload,
Craig Tiller8e8fd892015-02-10 17:02:08 -0800616 grpc_completion_queue *cq_bound_to_call, void *tag_new);
Craig Tiller24be0f72015-02-10 14:04:22 -0800617
Craig Tiller29f2b212015-02-17 17:01:24 -0800618/* Create a server. Additional configuration for each incoming channel can
Craig Tillere7163ab2015-02-17 20:46:08 -0800619 be specified with args. If no additional configuration is needed, args can
620 be NULL. See grpc_channel_args for more. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800621grpc_server *grpc_server_create(grpc_completion_queue *cq,
622 const grpc_channel_args *args);
623
Craig Tillerd251ab92015-02-17 17:22:14 -0800624/* Add a HTTP2 over plaintext over tcp listener.
ctiller570d1f42015-01-12 16:29:52 -0800625 Returns bound port number on success, 0 on failure.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800626 REQUIRES: server not started */
627int grpc_server_add_http2_port(grpc_server *server, const char *addr);
628
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800629/* Start a server - tells all listeners to start listening */
630void grpc_server_start(grpc_server *server);
631
ctiller9a58df02014-12-11 16:26:49 -0800632/* Begin shutting down a server.
633 After completion, no new calls or connections will be admitted.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800634 Existing calls will be allowed to complete.
635 Shutdown is idempotent. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800636void grpc_server_shutdown(grpc_server *server);
637
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800638/* As per grpc_server_shutdown, but send a GRPC_SERVER_SHUTDOWN event when
Craig Tilleraea2fc02015-02-17 16:54:53 -0800639 there are no more calls being serviced.
640 Shutdown is idempotent, and all tags will be notified at once if multiple
641 grpc_server_shutdown_and_notify calls are made. */
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800642void grpc_server_shutdown_and_notify(grpc_server *server, void *tag);
643
ctiller9a58df02014-12-11 16:26:49 -0800644/* Destroy a server.
Craig Tilleraea2fc02015-02-17 16:54:53 -0800645 Forcefully cancels all existing calls.
646 Implies grpc_server_shutdown() if one was not previously performed. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800647void grpc_server_destroy(grpc_server *server);
648
649#ifdef __cplusplus
650}
651#endif
652
Craig Tillerb20111c2015-04-10 23:27:11 +0000653#endif /* GRPC_GRPC_H */