gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
grpc.h
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * 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 
34 #ifndef GRPC_GRPC_H
35 #define GRPC_GRPC_H
36 
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
44 extern "C" {
45 #endif
46 
47 /* Completion Queues enable notification of the completion of asynchronous
48  actions. */
50 
51 /* The Channel interface allows creation of Call objects. */
52 typedef struct grpc_channel grpc_channel;
53 
54 /* A server listens to some port and responds to request calls */
55 typedef 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. */
60 typedef struct grpc_call grpc_call;
61 
62 /* Type specifier for grpc_arg */
63 typedef 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. */
81 typedef 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 
95 /* 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. */
101 typedef struct {
102  size_t num_args;
103  grpc_arg *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 /* Initial sequence number for http2 transports */
115 #define GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER \
116  "grpc.http2.initial_sequence_number"
117 
118 /* Result of a grpc call. If the caller satisfies the prerequisites of a
119  particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
120  Receiving any other value listed here is an indication of a bug in the
121  caller. */
122 typedef enum grpc_call_error {
123  /* everything went ok */
124  GRPC_CALL_OK = 0,
125  /* something failed, we don't know what */
126  GRPC_CALL_ERROR,
127  /* this method is not available on the server */
128  GRPC_CALL_ERROR_NOT_ON_SERVER,
129  /* this method is not available on the client */
130  GRPC_CALL_ERROR_NOT_ON_CLIENT,
131  /* this method must be called before server_accept */
132  GRPC_CALL_ERROR_ALREADY_ACCEPTED,
133  /* this method must be called before invoke */
134  GRPC_CALL_ERROR_ALREADY_INVOKED,
135  /* this method must be called after invoke */
136  GRPC_CALL_ERROR_NOT_INVOKED,
137  /* this call is already finished
138  (writes_done or write_status has already been called) */
139  GRPC_CALL_ERROR_ALREADY_FINISHED,
140  /* there is already an outstanding read/write operation on the call */
141  GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
142  /* the flags value was illegal for this call */
143  GRPC_CALL_ERROR_INVALID_FLAGS,
144  /* invalid metadata was passed to this call */
145  GRPC_CALL_ERROR_INVALID_METADATA
146 } grpc_call_error;
147 
148 /* Write Flags: */
149 /* Hint that the write may be buffered and need not go out on the wire
150  immediately. GRPC is free to buffer the message until the next non-buffered
151  write, or until writes_done, but it need not buffer completely or at all. */
152 #define GRPC_WRITE_BUFFER_HINT (0x00000001u)
153 /* Force compression to be disabled for a particular write
154  (start_write/add_metadata). Illegal on invoke/accept. */
155 #define GRPC_WRITE_NO_COMPRESS (0x00000002u)
156 
157 /* A buffer of bytes */
158 struct grpc_byte_buffer;
159 typedef struct grpc_byte_buffer grpc_byte_buffer;
160 
161 /* Sample helpers to obtain byte buffers (these will certainly move
162  someplace else) */
163 grpc_byte_buffer *grpc_byte_buffer_create(gpr_slice *slices, size_t nslices);
164 grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb);
165 size_t grpc_byte_buffer_length(grpc_byte_buffer *bb);
166 void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer);
167 
168 /* Reader for byte buffers. Iterates over slices in the byte buffer */
171 
172 grpc_byte_buffer_reader *grpc_byte_buffer_reader_create(
173  grpc_byte_buffer *buffer);
174 /* At the end of the stream, returns 0. Otherwise, returns 1 and sets slice to
175  be the returned slice. Caller is responsible for calling gpr_slice_unref on
176  the result. */
177 int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
178  gpr_slice *slice);
179 void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
180 
181 /* A single metadata element */
182 typedef struct grpc_metadata {
183  const char *key;
184  const char *value;
185  size_t value_length;
186 
187  /* The following fields are reserved for grpc internal use.
188  There is no need to initialize them, and they will be set to garbage during
189  calls to grpc. */
190  struct {
191  void *obfuscated[3];
192  } internal_data;
193 } grpc_metadata;
194 
195 typedef enum grpc_completion_type {
196  GRPC_QUEUE_SHUTDOWN, /* Shutting down */
197  GRPC_QUEUE_TIMEOUT, /* No event before timeout */
198  GRPC_OP_COMPLETE /* operation completion */
199 } grpc_completion_type;
200 
201 typedef struct grpc_event {
202  grpc_completion_type type;
203  int success;
204  void *tag;
205 } grpc_event;
206 
207 typedef struct {
208  size_t count;
209  size_t capacity;
210  grpc_metadata *metadata;
212 
213 void grpc_metadata_array_init(grpc_metadata_array *array);
214 void grpc_metadata_array_destroy(grpc_metadata_array *array);
215 
216 typedef struct {
217  char *method;
218  size_t method_capacity;
219  char *host;
220  size_t host_capacity;
221  gpr_timespec deadline;
223 
224 void grpc_call_details_init(grpc_call_details *details);
225 void grpc_call_details_destroy(grpc_call_details *details);
226 
227 typedef enum {
228  /* Send initial metadata: one and only one instance MUST be sent for each
229  call,
230  unless the call was cancelled - in which case this can be skipped */
231  GRPC_OP_SEND_INITIAL_METADATA = 0,
232  /* Send a message: 0 or more of these operations can occur for each call */
233  GRPC_OP_SEND_MESSAGE,
234  /* Send a close from the server: one and only one instance MUST be sent from
235  the client,
236  unless the call was cancelled - in which case this can be skipped */
237  GRPC_OP_SEND_CLOSE_FROM_CLIENT,
238  /* Send status from the server: one and only one instance MUST be sent from
239  the server
240  unless the call was cancelled - in which case this can be skipped */
241  GRPC_OP_SEND_STATUS_FROM_SERVER,
242  /* Receive initial metadata: one and only one MUST be made on the client, must
243  not be made on the server */
244  GRPC_OP_RECV_INITIAL_METADATA,
245  /* Receive a message: 0 or more of these operations can occur for each call */
246  GRPC_OP_RECV_MESSAGE,
247  /* Receive status on the client: one and only one must be made on the client
248  */
249  GRPC_OP_RECV_STATUS_ON_CLIENT,
250  /* Receive status on the server: one and only one must be made on the server
251  */
252  GRPC_OP_RECV_CLOSE_ON_SERVER
253 } grpc_op_type;
254 
255 /* Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT
256  which has
257  no arguments) */
258 typedef struct grpc_op {
259  grpc_op_type op;
260  union {
261  struct {
262  size_t count;
263  grpc_metadata *metadata;
264  } send_initial_metadata;
265  grpc_byte_buffer *send_message;
266  struct {
267  size_t trailing_metadata_count;
268  grpc_metadata *trailing_metadata;
269  grpc_status_code status;
270  const char *status_details;
271  } send_status_from_server;
272  /* ownership of the array is with the caller, but ownership of the elements
273  stays with the call object (ie key, value members are owned by the call
274  object, recv_initial_metadata->array is owned by the caller).
275  After the operation completes, call grpc_metadata_array_destroy on this
276  value, or reuse it in a future op. */
277  grpc_metadata_array *recv_initial_metadata;
278  grpc_byte_buffer **recv_message;
279  struct {
280  /* ownership of the array is with the caller, but ownership of the
281  elements
282  stays with the call object (ie key, value members are owned by the call
283  object, trailing_metadata->array is owned by the caller).
284  After the operation completes, call grpc_metadata_array_destroy on this
285  value, or reuse it in a future op. */
286  grpc_metadata_array *trailing_metadata;
287  grpc_status_code *status;
288  /* status_details is a buffer owned by the application before the op
289  completes
290  and after the op has completed. During the operation status_details may
291  be
292  reallocated to a size larger than *status_details_capacity, in which
293  case
294  *status_details_capacity will be updated with the new array capacity.
295 
296  Pre-allocating space:
297  size_t my_capacity = 8;
298  char *my_details = gpr_malloc(my_capacity);
299  x.status_details = &my_details;
300  x.status_details_capacity = &my_capacity;
301 
302  Not pre-allocating space:
303  size_t my_capacity = 0;
304  char *my_details = NULL;
305  x.status_details = &my_details;
306  x.status_details_capacity = &my_capacity;
307 
308  After the call:
309  gpr_free(my_details); */
310  char **status_details;
311  size_t *status_details_capacity;
312  } recv_status_on_client;
313  struct {
314  /* out argument, set to 1 if the call failed in any way (seen as a
315  cancellation
316  on the server), or 0 if the call succeeded */
317  int *cancelled;
318  } recv_close_on_server;
319  } data;
320 } grpc_op;
321 
322 /* Initialize the grpc library.
323  It is not safe to call any other grpc functions before calling this.
324  (To avoid overhead, little checking is done, and some things may work. We
325  do not warrant that they will continue to do so in future revisions of this
326  library). */
327 void grpc_init(void);
328 
329 /* Shut down the grpc library.
330  No memory is used by grpc after this call returns, nor are any instructions
331  executing within the grpc library.
332  Prior to calling, all application owned grpc objects must have been
333  destroyed. */
334 void grpc_shutdown(void);
335 
336 grpc_completion_queue *grpc_completion_queue_create(void);
337 
338 /* Blocks until an event is available, the completion queue is being shut down,
339  or deadline is reached. Returns NULL on timeout, otherwise the event that
340  occurred.
341 
342  Callers must not call grpc_completion_queue_next and
343  grpc_completion_queue_pluck simultaneously on the same completion queue. */
344 grpc_event grpc_completion_queue_next(grpc_completion_queue *cq,
345  gpr_timespec deadline);
346 
347 /* Blocks until an event with tag 'tag' is available, the completion queue is
348  being shutdown or deadline is reached. Returns NULL on timeout, or a pointer
349  to the event that occurred.
350 
351  Callers must not call grpc_completion_queue_next and
352  grpc_completion_queue_pluck simultaneously on the same completion queue. */
353 grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag,
354  gpr_timespec deadline);
355 
356 /* Begin destruction of a completion queue. Once all possible events are
357  drained then grpc_completion_queue_next will start to produce
358  GRPC_QUEUE_SHUTDOWN events only. At that point it's safe to call
359  grpc_completion_queue_destroy.
360 
361  After calling this function applications should ensure that no
362  NEW work is added to be published on this completion queue. */
363 void grpc_completion_queue_shutdown(grpc_completion_queue *cq);
364 
365 /* Destroy a completion queue. The caller must ensure that the queue is
366  drained and no threads are executing grpc_completion_queue_next */
367 void grpc_completion_queue_destroy(grpc_completion_queue *cq);
368 
369 /* Create a call given a grpc_channel, in order to call 'method'. The request
370  is not sent until grpc_call_invoke is called. All completions are sent to
371  'completion_queue'. */
372 grpc_call *grpc_channel_create_call(grpc_channel *channel,
373  grpc_completion_queue *completion_queue,
374  const char *method, const char *host,
375  gpr_timespec deadline);
376 
377 /* Pre-register a method/host pair on a channel. */
378 void *grpc_channel_register_call(grpc_channel *channel, const char *method,
379  const char *host);
380 
381 /* Create a call given a handle returned from grpc_channel_register_call */
382 grpc_call *grpc_channel_create_registered_call(
383  grpc_channel *channel, grpc_completion_queue *completion_queue,
384  void *registered_call_handle, gpr_timespec deadline);
385 
386 /* Start a batch of operations defined in the array ops; when complete, post a
387  completion of type 'tag' to the completion queue bound to the call.
388  The order of ops specified in the batch has no significance.
389  Only one operation of each type can be active at once in any given
390  batch. */
391 grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
392  size_t nops, void *tag);
393 
394 /* Create a client channel to 'target'. Additional channel level configuration
395  MAY be provided by grpc_channel_args, though the expectation is that most
396  clients will want to simply pass NULL. See grpc_channel_args definition
397  for more on this. */
398 grpc_channel *grpc_channel_create(const char *target,
399  const grpc_channel_args *args);
400 
401 /* Create a lame client: this client fails every operation attempted on it. */
402 grpc_channel *grpc_lame_client_channel_create(void);
403 
404 /* Close and destroy a grpc channel */
405 void grpc_channel_destroy(grpc_channel *channel);
406 
407 /* THREAD-SAFETY for grpc_call
408  The following functions are thread-compatible for any given call:
409  grpc_call_add_metadata
410  grpc_call_invoke
411  grpc_call_start_write
412  grpc_call_writes_done
413  grpc_call_start_read
414  grpc_call_destroy
415  The function grpc_call_cancel is thread-safe, and can be called at any
416  point before grpc_call_destroy is called. */
417 
418 /* Error handling for grpc_call
419  Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
420  then the operation failed due to some unsatisfied precondition.
421  If a grpc_call fails, it's guaranteed that no change to the call state
422  has been made. */
423 
424 /* Called by clients to cancel an RPC on the server.
425  Can be called multiple times, from any thread. */
426 grpc_call_error grpc_call_cancel(grpc_call *call);
427 
428 /* Called by clients to cancel an RPC on the server.
429  Can be called multiple times, from any thread.
430  If a status has not been received for the call, set it to the status code
431  and description passed in.
432  Importantly, this function does not send status nor description to the
433  remote endpoint. */
434 grpc_call_error grpc_call_cancel_with_status(grpc_call *call,
435  grpc_status_code status,
436  const char *description);
437 
438 /* Destroy a call. */
439 void grpc_call_destroy(grpc_call *call);
440 
441 /* Request notification of a new call */
442 grpc_call_error grpc_server_request_call(
443  grpc_server *server, grpc_call **call, grpc_call_details *details,
444  grpc_metadata_array *request_metadata,
445  grpc_completion_queue *cq_bound_to_call,
446  grpc_completion_queue *cq_for_notification, void *tag_new);
447 
448 /* Registers a method in the server.
449  Methods to this (host, method) pair will not be reported by
450  grpc_server_request_call, but instead be reported by
451  grpc_server_request_registered_call when passed the appropriate
452  registered_method (as returned by this function).
453  Must be called before grpc_server_start.
454  Returns NULL on failure. */
455 void *grpc_server_register_method(grpc_server *server, const char *method,
456  const char *host);
457 
458 /* Request notification of a new pre-registered call */
459 grpc_call_error grpc_server_request_registered_call(
460  grpc_server *server, void *registered_method, grpc_call **call,
461  gpr_timespec *deadline, grpc_metadata_array *request_metadata,
462  grpc_byte_buffer **optional_payload,
463  grpc_completion_queue *cq_bound_to_call,
464  grpc_completion_queue *cq_for_notification, void *tag_new);
465 
466 /* Create a server. Additional configuration for each incoming channel can
467  be specified with args. If no additional configuration is needed, args can
468  be NULL. See grpc_channel_args for more. */
469 grpc_server *grpc_server_create(const grpc_channel_args *args);
470 
471 /* Register a completion queue with the server. Must be done for any completion
472  queue that is passed to grpc_server_request_* call. Must be performed prior
473  to grpc_server_start. */
474 void grpc_server_register_completion_queue(grpc_server *server,
476 
477 /* Add a HTTP2 over plaintext over tcp listener.
478  Returns bound port number on success, 0 on failure.
479  REQUIRES: server not started */
480 int grpc_server_add_http2_port(grpc_server *server, const char *addr);
481 
482 /* Start a server - tells all listeners to start listening */
483 void grpc_server_start(grpc_server *server);
484 
485 /* Begin shutting down a server.
486  After completion, no new calls or connections will be admitted.
487  Existing calls will be allowed to complete.
488  Shutdown is idempotent. */
489 void grpc_server_shutdown(grpc_server *server);
490 
491 /* As per grpc_server_shutdown, but send a GRPC_OP_COMPLETE event when
492  there are no more calls being serviced.
493  Shutdown is idempotent, and all tags will be notified at once if multiple
494  grpc_server_shutdown_and_notify calls are made. */
495 void grpc_server_shutdown_and_notify(grpc_server *server, void *tag);
496 
497 /* Destroy a server.
498  Forcefully cancels all existing calls.
499  Implies grpc_server_shutdown() if one was not previously performed. */
500 void grpc_server_destroy(grpc_server *server);
501 
502 #ifdef __cplusplus
503 }
504 #endif
505 
506 #endif /* GRPC_GRPC_H */
Definition: grpc.h:101
Definition: channel.c:52
Definition: grpc.h:201
Definition: grpc.h:81
Definition: grpc.h:216
Definition: server.c:100
Definition: byte_buffer.h:43
Definition: grpc.h:207
Definition: grpc.h:182
Definition: grpc.h:258
Definition: completion_queue.c:61
Definition: time.h:48
Definition: byte_buffer_reader.h:40
Definition: slice.h:79
Definition: call.c:128
Definition: server.c:127