GRPC Core  0.11.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grpc.h
Go to the documentation of this file.
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/byte_buffer.h>
41 #include <grpc/support/slice.h>
42 #include <grpc/support/time.h>
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
58 
60 typedef struct grpc_channel grpc_channel;
61 
63 typedef struct grpc_server grpc_server;
64 
68 typedef struct grpc_call grpc_call;
69 
71 typedef enum {
76 
89 typedef struct {
91  char *key;
92  union {
93  char *string;
94  int integer;
95  struct {
96  void *p;
97  void *(*copy)(void *p);
98  void (*destroy)(void *p);
99  } pointer;
100  } value;
101 } grpc_arg;
102 
111 typedef struct {
112  size_t num_args;
115 
116 /* Channel argument keys: */
118 #define GRPC_ARG_ENABLE_CENSUS "grpc.census"
119 
121 #define GRPC_ARG_MAX_CONCURRENT_STREAMS "grpc.max_concurrent_streams"
122 
123 #define GRPC_ARG_MAX_MESSAGE_LENGTH "grpc.max_message_length"
124 
125 #define GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER \
126  "grpc.http2.initial_sequence_number"
127 
128 #define GRPC_ARG_DEFAULT_AUTHORITY "grpc.default_authority"
129 
131 #define GRPC_ARG_PRIMARY_USER_AGENT_STRING "grpc.primary_user_agent"
132 
134 #define GRPC_ARG_SECONDARY_USER_AGENT_STRING "grpc.secondary_user_agent"
135 /* The caller of the secure_channel_create functions may override the target
136  name used for SSL host name checking using this channel argument which is of
137  type GRPC_ARG_STRING. This *should* be used for testing only.
138  If this argument is not specified, the name used for SSL host name checking
139  will be the target parameter (assuming that the secure channel is an SSL
140  channel). If this parameter is specified and the underlying is not an SSL
141  channel, it will just be ignored. */
142 #define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG "grpc.ssl_target_name_override"
143 
145 typedef enum {
157 
162 typedef enum grpc_call_error {
194 
195 /* Write Flags: */
199 #define GRPC_WRITE_BUFFER_HINT (0x00000001u)
200 
202 #define GRPC_WRITE_NO_COMPRESS (0x00000002u)
203 
204 #define GRPC_WRITE_USED_MASK (GRPC_WRITE_BUFFER_HINT | GRPC_WRITE_NO_COMPRESS)
205 
207 typedef struct grpc_metadata {
208  const char *key;
209  const char *value;
210  size_t value_length;
212 
216  struct {
217  void *obfuscated[4];
218  } internal_data;
219 } grpc_metadata;
220 
222 typedef enum grpc_completion_type {
230 
234 typedef struct grpc_event {
239  int success;
242  void *tag;
243 } grpc_event;
244 
245 typedef struct {
246  size_t count;
247  size_t capacity;
250 
253 
254 typedef struct {
255  char *method;
257  char *host;
260  void *reserved;
262 
265 
266 typedef enum {
307 } grpc_op_type;
308 
311 typedef struct grpc_op {
317  void *reserved;
318  union {
320  struct {
321  void *reserved[8];
322  } reserved;
323  struct {
324  size_t count;
328  struct {
332  const char *status_details;
344  struct {
376  struct {
379  int *cancelled;
381  } data;
382 } grpc_op;
383 
392 void grpc_register_plugin(void (*init)(void), void (*destroy)(void));
393 
394 /* Propagation bits: this can be bitwise or-ed to form propagation_mask for
395  * grpc_call */
397 #define GRPC_PROPAGATE_DEADLINE ((gpr_uint32)1)
398 
399 #define GRPC_PROPAGATE_CENSUS_STATS_CONTEXT ((gpr_uint32)2)
400 #define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT ((gpr_uint32)4)
401 
402 #define GRPC_PROPAGATE_CANCELLATION ((gpr_uint32)8)
403 
404 /* Default propagation mask: clients of the core API are encouraged to encode
405  deltas from this in their implementations... ie write:
406  GRPC_PROPAGATE_DEFAULTS & ~GRPC_PROPAGATE_DEADLINE to disable deadline
407  propagation. Doing so gives flexibility in the future to define new
408  propagation types that are default inherited or not. */
409 #define GRPC_PROPAGATE_DEFAULTS \
410  ((gpr_uint32)(( \
411  0xffff | GRPC_PROPAGATE_DEADLINE | GRPC_PROPAGATE_CENSUS_STATS_CONTEXT | \
412  GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT | GRPC_PROPAGATE_CANCELLATION)))
413 
420 void grpc_init(void);
421 
428 void grpc_shutdown(void);
429 
431 const char *grpc_version_string(void);
432 
435 
445  gpr_timespec deadline, void *reserved);
446 
459  gpr_timespec deadline, void *reserved);
460 
463 #define GRPC_MAX_COMPLETION_QUEUE_PLUCKERS 6
464 
473 
477 
480  grpc_channel *channel, int try_to_connect);
481 
488  grpc_channel *channel, grpc_connectivity_state last_observed_state,
489  gpr_timespec deadline, grpc_completion_queue *cq, void *tag);
490 
498  grpc_call *parent_call,
499  gpr_uint32 propagation_mask,
500  grpc_completion_queue *completion_queue,
501  const char *method, const char *host,
502  gpr_timespec deadline, void *reserved);
503 
505 void *grpc_channel_register_call(grpc_channel *channel, const char *method,
506  const char *host, void *reserved);
507 
510  grpc_channel *channel, grpc_call *parent_call, gpr_uint32 propagation_mask,
511  grpc_completion_queue *completion_queue, void *registered_call_handle,
512  gpr_timespec deadline, void *reserved);
513 
526  size_t nops, void *tag, void *reserved);
527 
536 char *grpc_call_get_peer(grpc_call *call);
537 
538 struct census_context;
539 
540 /* Set census context for a call; Must be called before first call to
541  grpc_call_start_batch(). */
543  struct census_context *context);
544 
545 /* Retrieve the calls current census context. */
547 
550 char *grpc_channel_get_target(grpc_channel *channel);
551 
557 grpc_channel *grpc_insecure_channel_create(const char *target,
558  const grpc_channel_args *args,
559  void *reserved);
560 
563  grpc_status_code error_code,
564  const char *error_message);
565 
567 void grpc_channel_destroy(grpc_channel *channel);
568 
569 /* Error handling for grpc_call
570  Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
571  then the operation failed due to some unsatisfied precondition.
572  If a grpc_call fails, it's guaranteed that no change to the call state
573  has been made. */
574 
580 grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved);
581 
589  grpc_status_code status,
590  const char *description,
591  void *reserved);
592 
595 void grpc_call_destroy(grpc_call *call);
596 
606  grpc_server *server, grpc_call **call, grpc_call_details *details,
607  grpc_metadata_array *request_metadata,
608  grpc_completion_queue *cq_bound_to_call,
609  grpc_completion_queue *cq_for_notification, void *tag_new);
610 
618 void *grpc_server_register_method(grpc_server *server, const char *method,
619  const char *host);
620 
625  grpc_server *server, void *registered_method, grpc_call **call,
626  gpr_timespec *deadline, grpc_metadata_array *request_metadata,
627  grpc_byte_buffer **optional_payload,
628  grpc_completion_queue *cq_bound_to_call,
629  grpc_completion_queue *cq_for_notification, void *tag_new);
630 
635 grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved);
636 
643  void *reserved);
644 
648 int grpc_server_add_insecure_http2_port(grpc_server *server, const char *addr);
649 
651 void grpc_server_start(grpc_server *server);
652 
661  grpc_completion_queue *cq, void *tag);
662 
666 
671 void grpc_server_destroy(grpc_server *server);
672 
681 int grpc_tracer_set_enabled(const char *name, int enabled);
682 
683 #ifdef __cplusplus
684 }
685 #endif
686 
687 #endif /* GRPC_GRPC_H */
completion queue for notification has not been registered with the server
Definition: grpc.h:190
Operation completion.
Definition: grpc.h:228
void * tag
The tag passed to grpc_call_start_batch etc to start this operation.
Definition: grpc.h:242
grpc_op_type op
Operation type, as defined by grpc_op_type.
Definition: grpc.h:313
grpc_connectivity_state
Connectivity state of a channel.
Definition: grpc.h:145
struct grpc_metadata::@6 internal_data
The following fields are reserved for grpc internal use.
Send a close from the client: one and only one instance MUST be sent from the client, unless the call was cancelled - in which case this can be skipped.
Definition: grpc.h:281
void grpc_server_destroy(grpc_server *server)
Destroy a server.
char ** status_details
status_details is a buffer owned by the application before the op completes and after the op has comp...
Definition: grpc.h:373
grpc_op_type
Definition: grpc.h:266
gpr_uint32 flags
Write flags bitset for grpc_begin_messages.
Definition: grpc.h:315
struct grpc_op grpc_op
Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT which has no arguments) ...
grpc_call * grpc_channel_create_registered_call(grpc_channel *channel, grpc_call *parent_call, gpr_uint32 propagation_mask, grpc_completion_queue *completion_queue, void *registered_call_handle, gpr_timespec deadline, void *reserved)
Create a call given a handle returned from grpc_channel_register_call.
grpc_metadata_array * recv_initial_metadata
ownership of the array is with the caller, but ownership of the elements stays with the call object (...
Definition: grpc.h:339
grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag, gpr_timespec deadline, void *reserved)
Blocks until an event with tag 'tag' is available, the completion queue is being shutdown or deadline...
struct grpc_completion_queue grpc_completion_queue
Completion Queues enable notification of the completion of asynchronous actions.
Definition: grpc.h:57
void grpc_call_details_destroy(grpc_call_details *details)
An array of arguments that can be passed around.
Definition: grpc.h:111
grpc_arg * args
Definition: grpc.h:113
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc.h:63
grpc_arg_type
Type specifier for grpc_arg.
Definition: grpc.h:71
gpr_uint32 flags
Definition: grpc.h:211
the flags value was illegal for this call
Definition: grpc.h:183
grpc_status_code status
Definition: grpc.h:331
union grpc_op::@7 data
gpr_timespec deadline
Definition: grpc.h:259
grpc_channel * grpc_lame_client_channel_create(const char *target, grpc_status_code error_code, const char *error_message)
Create a lame client: this client fails every operation attempted on it.
Definition: grpc.h:74
The result of an operation.
Definition: grpc.h:234
int integer
Definition: grpc.h:94
Send status from the server: one and only one instance MUST be sent from the server unless the call w...
Definition: grpc.h:287
char * string
Definition: grpc.h:93
void grpc_call_details_init(grpc_call_details *details)
char * host
Definition: grpc.h:257
int success
non-zero if the operation was successful, 0 upon failure.
Definition: grpc.h:239
void grpc_call_destroy(grpc_call *call)
Destroy a call.
size_t count
Definition: grpc.h:324
A single argument...
Definition: grpc.h:89
Definition: grpc.h:254
something failed, we don't know what
Definition: grpc.h:166
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc.h:68
Definition: byte_buffer.h:49
grpc_byte_buffer ** recv_message
ownership of the byte buffer is moved to the caller; the caller must call grpc_byte_buffer_destroy on...
Definition: grpc.h:343
grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved)
Called by clients to cancel an RPC on the server.
void grpc_server_start(grpc_server *server)
Start a server - tells all listeners to start listening.
Definition: grpc.h:245
uint32_t gpr_uint32
Definition: port_platform.h:312
void * p
Definition: grpc.h:96
size_t * status_details_capacity
Definition: grpc.h:374
Receive a message: 0 or more of these operations can occur for each call.
Definition: grpc.h:296
size_t method_capacity
Definition: grpc.h:256
grpc_connectivity_state grpc_channel_check_connectivity_state(grpc_channel *channel, int try_to_connect)
Check the connectivity state of a channel.
grpc_status_code * status
Definition: grpc.h:352
void grpc_server_shutdown_and_notify(grpc_server *server, grpc_completion_queue *cq, void *tag)
Begin shutting down a server.
void grpc_channel_watch_connectivity_state(grpc_channel *channel, grpc_connectivity_state last_observed_state, gpr_timespec deadline, grpc_completion_queue *cq, void *tag)
Watch for a change in connectivity state.
grpc_metadata * metadata
Definition: grpc.h:248
this method must be called before invoke
Definition: grpc.h:174
invalid message was passed to this call
Definition: grpc.h:187
Send initial metadata: one and only one instance MUST be sent for each call, unless the call was canc...
Definition: grpc.h:271
Definition: grpc.h:73
grpc_call_error
Result of a grpc call.
Definition: grpc.h:162
grpc_metadata * trailing_metadata
Definition: grpc.h:330
size_t capacity
Definition: grpc.h:247
const char * key
Definition: grpc.h:208
void grpc_server_cancel_all_calls(grpc_server *server)
Cancel all in-progress calls.
invalid metadata was passed to this call
Definition: grpc.h:185
const char * status_details
Definition: grpc.h:332
void * obfuscated[4]
Definition: grpc.h:217
struct grpc_channel grpc_channel
The Channel interface allows creation of Call objects.
Definition: grpc.h:60
grpc_call * grpc_channel_create_call(grpc_channel *channel, grpc_call *parent_call, gpr_uint32 propagation_mask, grpc_completion_queue *completion_queue, const char *method, const char *host, gpr_timespec deadline, void *reserved)
Create a call given a grpc_channel, in order to call 'method'.
grpc_completion_queue * grpc_completion_queue_create(void *reserved)
Create a completion queue.
A single metadata element.
Definition: grpc.h:207
struct grpc_op::@7::@10 send_status_from_server
struct grpc_op::@7::@9 send_initial_metadata
this method is not available on the server
Definition: grpc.h:168
Shutting down.
Definition: grpc.h:224
Receive initial metadata: one and only one MUST be made on the client, must not be made on the server...
Definition: grpc.h:292
Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT which has no arguments) ...
Definition: grpc.h:311
void * reserved
Definition: grpc.h:260
grpc_call_error grpc_server_request_call(grpc_server *server, grpc_call **call, grpc_call_details *details, grpc_metadata_array *request_metadata, grpc_completion_queue *cq_bound_to_call, grpc_completion_queue *cq_for_notification, void *tag_new)
Request notification of a new call.
Send a message: 0 or more of these operations can occur for each call.
Definition: grpc.h:275
everything went ok
Definition: grpc.h:164
this call is already finished (writes_done or write_status has already been called) ...
Definition: grpc.h:179
struct grpc_event grpc_event
The result of an operation.
grpc_completion_type
The type of completion (for grpc_event)
Definition: grpc.h:222
void grpc_server_register_completion_queue(grpc_server *server, grpc_completion_queue *cq, void *reserved)
Register a completion queue with the server.
size_t trailing_metadata_count
Definition: grpc.h:329
grpc_server * grpc_server_create(const grpc_channel_args *args, void *reserved)
Create a server.
size_t host_capacity
Definition: grpc.h:258
void * grpc_server_register_method(grpc_server *server, const char *method, const char *host)
Registers a method in the server.
char * grpc_channel_get_target(grpc_channel *channel)
Return a newly allocated string representing the target a channel was created for.
void grpc_channel_destroy(grpc_channel *channel)
Close and destroy a grpc channel.
const char * grpc_version_string(void)
Return a string representing the current version of grpc.
this batch of operations leads to more operations than allowed
Definition: grpc.h:192
size_t num_args
Definition: grpc.h:112
size_t count
Definition: grpc.h:246
Receive status on the client: one and only one must be made on the client.
Definition: grpc.h:302
channel is ready for work
Definition: grpc.h:151
char * key
Definition: grpc.h:91
char * grpc_call_get_peer(grpc_call *call)
Returns a newly allocated string representing the endpoint to which this call is communicating with...
struct grpc_op::@7::@11 recv_status_on_client
channel has seen a failure but expects to recover
Definition: grpc.h:153
grpc_channel * grpc_insecure_channel_create(const char *target, const grpc_channel_args *args, void *reserved)
Create a client channel to 'target'.
struct grpc_metadata grpc_metadata
A single metadata element.
const char * value
Definition: grpc.h:209
this method is not available on the client
Definition: grpc.h:170
void grpc_register_plugin(void(*init)(void), void(*destroy)(void))
Registers a plugin to be initialized and destroyed with the library.
channel is idle
Definition: grpc.h:147
channel has seen a failure that it cannot recover from
Definition: grpc.h:155
int grpc_server_add_insecure_http2_port(grpc_server *server, const char *addr)
Add a HTTP2 over plaintext over tcp listener.
grpc_event grpc_completion_queue_next(grpc_completion_queue *cq, gpr_timespec deadline, void *reserved)
Blocks until an event is available, the completion queue is being shut down, or deadline is reached...
Definition: time.h:63
grpc_status_code
Definition: status.h:41
struct census_context * grpc_census_call_get_context(grpc_call *call)
void grpc_metadata_array_destroy(grpc_metadata_array *array)
No event before timeout.
Definition: grpc.h:226
grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, size_t nops, void *tag, void *reserved)
Start a batch of operations defined in the array ops; when complete, post a completion of type 'tag' ...
void grpc_init(void)
Initialize the grpc library.
void * reserved
Reserved for future usage.
Definition: grpc.h:317
channel is connecting
Definition: grpc.h:149
grpc_call_error grpc_call_cancel_with_status(grpc_call *call, grpc_status_code status, const char *description, void *reserved)
Called by clients to cancel an RPC on the server.
grpc_byte_buffer * send_message
Definition: grpc.h:327
grpc_metadata_array * trailing_metadata
ownership of the array is with the caller, but ownership of the elements stays with the call object (...
Definition: grpc.h:351
grpc_call_error grpc_server_request_registered_call(grpc_server *server, void *registered_method, grpc_call **call, gpr_timespec *deadline, grpc_metadata_array *request_metadata, grpc_byte_buffer **optional_payload, grpc_completion_queue *cq_bound_to_call, grpc_completion_queue *cq_for_notification, void *tag_new)
Request notification of a new pre-registered call.
void grpc_shutdown(void)
Shut down the grpc library.
void grpc_metadata_array_init(grpc_metadata_array *array)
void grpc_completion_queue_destroy(grpc_completion_queue *cq)
Destroy a completion queue.
size_t value_length
Definition: grpc.h:210
there is already an outstanding read/write operation on the call
Definition: grpc.h:181
Receive close on the server: one and only one must be made on the server.
Definition: grpc.h:306
grpc_completion_type type
The type of the completion.
Definition: grpc.h:236
this method must be called before server_accept
Definition: grpc.h:172
grpc_arg_type type
Definition: grpc.h:90
void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Begin destruction of a completion queue.
Definition: grpc.h:72
grpc_metadata * metadata
Definition: grpc.h:325
int * cancelled
out argument, set to 1 if the call failed in any way (seen as a cancellation on the server)...
Definition: grpc.h:379
this method must be called after invoke
Definition: grpc.h:176
void * grpc_channel_register_call(grpc_channel *channel, const char *method, const char *host, void *reserved)
Pre-register a method/host pair on a channel.
char * method
Definition: grpc.h:255
struct census_context census_context
Context is a handle used by census to represent the current tracing and tagging information.
Definition: census.h:80
struct grpc_op::@7::@12 recv_close_on_server
int grpc_tracer_set_enabled(const char *name, int enabled)
Enable or disable a tracer.
void grpc_census_call_set_context(grpc_call *call, struct census_context *context)