GRPC Core  0.12.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-2016, 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_alarm grpc_alarm;
61 
63 typedef struct grpc_channel grpc_channel;
64 
66 typedef struct grpc_server grpc_server;
67 
71 typedef struct grpc_call grpc_call;
72 
74 typedef enum {
79 
92 typedef struct {
94  char *key;
95  union {
96  char *string;
97  int integer;
98  struct {
99  void *p;
100  void *(*copy)(void *p);
101  void (*destroy)(void *p);
102  } pointer;
103  } value;
104 } grpc_arg;
105 
114 typedef struct {
115  size_t num_args;
118 
119 /* Channel argument keys: */
121 #define GRPC_ARG_ENABLE_CENSUS "grpc.census"
122 
124 #define GRPC_ARG_MAX_CONCURRENT_STREAMS "grpc.max_concurrent_streams"
125 
126 #define GRPC_ARG_MAX_MESSAGE_LENGTH "grpc.max_message_length"
127 
128 #define GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER \
129  "grpc.http2.initial_sequence_number"
130 
134 #define GRPC_ARG_HTTP2_STREAM_LOOKAHEAD_BYTES "grpc.http2.lookahead_bytes"
135 
136 #define GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER \
137  "grpc.http2.hpack_table_size.decoder"
138 
139 #define GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER \
140  "grpc.http2.hpack_table_size.encoder"
141 
142 #define GRPC_ARG_DEFAULT_AUTHORITY "grpc.default_authority"
143 
145 #define GRPC_ARG_PRIMARY_USER_AGENT_STRING "grpc.primary_user_agent"
146 
148 #define GRPC_ARG_SECONDARY_USER_AGENT_STRING "grpc.secondary_user_agent"
149 /* The caller of the secure_channel_create functions may override the target
150  name used for SSL host name checking using this channel argument which is of
151  type GRPC_ARG_STRING. This *should* be used for testing only.
152  If this argument is not specified, the name used for SSL host name checking
153  will be the target parameter (assuming that the secure channel is an SSL
154  channel). If this parameter is specified and the underlying is not an SSL
155  channel, it will just be ignored. */
156 #define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG "grpc.ssl_target_name_override"
157 
159 typedef enum {
171 
176 typedef enum grpc_call_error {
208 
209 /* Write Flags: */
213 #define GRPC_WRITE_BUFFER_HINT (0x00000001u)
214 
216 #define GRPC_WRITE_NO_COMPRESS (0x00000002u)
217 
218 #define GRPC_WRITE_USED_MASK (GRPC_WRITE_BUFFER_HINT | GRPC_WRITE_NO_COMPRESS)
219 
221 typedef struct grpc_metadata {
222  const char *key;
223  const char *value;
224  size_t value_length;
226 
230  struct {
231  void *obfuscated[4];
232  } internal_data;
233 } grpc_metadata;
234 
236 typedef enum grpc_completion_type {
244 
248 typedef struct grpc_event {
253  int success;
256  void *tag;
257 } grpc_event;
258 
259 typedef struct {
260  size_t count;
261  size_t capacity;
264 
267 
268 typedef struct {
269  char *method;
271  char *host;
274  void *reserved;
276 
279 
280 typedef enum {
321 } grpc_op_type;
322 
325 typedef struct grpc_op {
331  void *reserved;
332  union {
334  struct {
335  void *reserved[8];
336  } reserved;
337  struct {
338  size_t count;
342  struct {
346  const char *status_details;
358  struct {
390  struct {
393  int *cancelled;
395  } data;
396 } grpc_op;
397 
406 void grpc_register_plugin(void (*init)(void), void (*destroy)(void));
407 
408 /* Propagation bits: this can be bitwise or-ed to form propagation_mask for
409  * grpc_call */
411 #define GRPC_PROPAGATE_DEADLINE ((gpr_uint32)1)
412 
413 #define GRPC_PROPAGATE_CENSUS_STATS_CONTEXT ((gpr_uint32)2)
414 #define GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT ((gpr_uint32)4)
415 
416 #define GRPC_PROPAGATE_CANCELLATION ((gpr_uint32)8)
417 
418 /* Default propagation mask: clients of the core API are encouraged to encode
419  deltas from this in their implementations... ie write:
420  GRPC_PROPAGATE_DEFAULTS & ~GRPC_PROPAGATE_DEADLINE to disable deadline
421  propagation. Doing so gives flexibility in the future to define new
422  propagation types that are default inherited or not. */
423 #define GRPC_PROPAGATE_DEFAULTS \
424  ((gpr_uint32)(( \
425  0xffff | GRPC_PROPAGATE_DEADLINE | GRPC_PROPAGATE_CENSUS_STATS_CONTEXT | \
426  GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT | GRPC_PROPAGATE_CANCELLATION)))
427 
434 void grpc_init(void);
435 
442 void grpc_shutdown(void);
443 
445 const char *grpc_version_string(void);
446 
449 
459  gpr_timespec deadline, void *reserved);
460 
473  gpr_timespec deadline, void *reserved);
474 
477 #define GRPC_MAX_COMPLETION_QUEUE_PLUCKERS 6
478 
487 
491 
499  void *tag);
500 
503 void grpc_alarm_cancel(grpc_alarm *alarm);
504 
506 void grpc_alarm_destroy(grpc_alarm *alarm);
507 
510  grpc_channel *channel, int try_to_connect);
511 
518  grpc_channel *channel, grpc_connectivity_state last_observed_state,
519  gpr_timespec deadline, grpc_completion_queue *cq, void *tag);
520 
528  grpc_call *parent_call,
529  gpr_uint32 propagation_mask,
530  grpc_completion_queue *completion_queue,
531  const char *method, const char *host,
532  gpr_timespec deadline, void *reserved);
533 
537  void *tag, void *reserved);
538 
540 void *grpc_channel_register_call(grpc_channel *channel, const char *method,
541  const char *host, void *reserved);
542 
545  grpc_channel *channel, grpc_call *parent_call, gpr_uint32 propagation_mask,
546  grpc_completion_queue *completion_queue, void *registered_call_handle,
547  gpr_timespec deadline, void *reserved);
548 
561  size_t nops, void *tag, void *reserved);
562 
571 char *grpc_call_get_peer(grpc_call *call);
572 
573 struct census_context;
574 
575 /* Set census context for a call; Must be called before first call to
576  grpc_call_start_batch(). */
578  struct census_context *context);
579 
580 /* Retrieve the calls current census context. */
582 
585 char *grpc_channel_get_target(grpc_channel *channel);
586 
592 grpc_channel *grpc_insecure_channel_create(const char *target,
593  const grpc_channel_args *args,
594  void *reserved);
595 
598  grpc_status_code error_code,
599  const char *error_message);
600 
602 void grpc_channel_destroy(grpc_channel *channel);
603 
604 /* Error handling for grpc_call
605  Most grpc_call functions return a grpc_error. If the error is not GRPC_OK
606  then the operation failed due to some unsatisfied precondition.
607  If a grpc_call fails, it's guaranteed that no change to the call state
608  has been made. */
609 
615 grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved);
616 
624  grpc_status_code status,
625  const char *description,
626  void *reserved);
627 
630 void grpc_call_destroy(grpc_call *call);
631 
641  grpc_server *server, grpc_call **call, grpc_call_details *details,
642  grpc_metadata_array *request_metadata,
643  grpc_completion_queue *cq_bound_to_call,
644  grpc_completion_queue *cq_for_notification, void *tag_new);
645 
653 void *grpc_server_register_method(grpc_server *server, const char *method,
654  const char *host);
655 
660  grpc_server *server, void *registered_method, grpc_call **call,
661  gpr_timespec *deadline, grpc_metadata_array *request_metadata,
662  grpc_byte_buffer **optional_payload,
663  grpc_completion_queue *cq_bound_to_call,
664  grpc_completion_queue *cq_for_notification, void *tag_new);
665 
670 grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved);
671 
678  void *reserved);
679 
683 int grpc_server_add_insecure_http2_port(grpc_server *server, const char *addr);
684 
686 void grpc_server_start(grpc_server *server);
687 
696  grpc_completion_queue *cq, void *tag);
697 
701 
706 void grpc_server_destroy(grpc_server *server);
707 
716 int grpc_tracer_set_enabled(const char *name, int enabled);
717 
719 int grpc_header_key_is_legal(const char *key, size_t length);
720 
723 int grpc_header_nonbin_value_is_legal(const char *value, size_t length);
724 
726 int grpc_is_binary_header(const char *key, size_t length);
727 
728 #ifdef __cplusplus
729 }
730 #endif
731 
732 #endif /* GRPC_GRPC_H */
completion queue for notification has not been registered with the server
Definition: grpc.h:204
Operation completion.
Definition: grpc.h:242
void * tag
The tag passed to grpc_call_start_batch etc to start this operation.
Definition: grpc.h:256
grpc_op_type op
Operation type, as defined by grpc_op_type.
Definition: grpc.h:327
grpc_connectivity_state
Connectivity state of a channel.
Definition: grpc.h:159
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:295
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:387
grpc_op_type
Definition: grpc.h:280
gpr_uint32 flags
Write flags bitset for grpc_begin_messages.
Definition: grpc.h:329
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:353
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:114
grpc_arg * args
Definition: grpc.h:116
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc.h:66
grpc_arg_type
Type specifier for grpc_arg.
Definition: grpc.h:74
gpr_uint32 flags
Definition: grpc.h:225
the flags value was illegal for this call
Definition: grpc.h:197
grpc_status_code status
Definition: grpc.h:345
union grpc_op::@7 data
gpr_timespec deadline
Definition: grpc.h:273
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:77
The result of an operation.
Definition: grpc.h:248
int integer
Definition: grpc.h:97
int grpc_is_binary_header(const char *key, size_t length)
Check whether a metadata key corresponds to a binary value.
Send status from the server: one and only one instance MUST be sent from the server unless the call w...
Definition: grpc.h:301
char * string
Definition: grpc.h:96
void grpc_call_details_init(grpc_call_details *details)
char * host
Definition: grpc.h:271
int success
non-zero if the operation was successful, 0 upon failure.
Definition: grpc.h:253
void grpc_call_destroy(grpc_call *call)
Destroy a call.
size_t count
Definition: grpc.h:338
A single argument...
Definition: grpc.h:92
Definition: grpc.h:268
something failed, we don't know what
Definition: grpc.h:180
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc.h:71
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:357
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.
void grpc_channel_ping(grpc_channel *channel, grpc_completion_queue *cq, void *tag, void *reserved)
Ping the channels peer (load balanced channels will select one sub-channel to ping); if the channel i...
Definition: grpc.h:259
uint32_t gpr_uint32
Definition: port_platform.h:322
void * p
Definition: grpc.h:99
size_t * status_details_capacity
Definition: grpc.h:388
Receive a message: 0 or more of these operations can occur for each call.
Definition: grpc.h:310
size_t method_capacity
Definition: grpc.h:270
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:366
struct grpc_alarm grpc_alarm
An alarm associated with a completion queue.
Definition: grpc.h:60
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:262
this method must be called before invoke
Definition: grpc.h:188
invalid message was passed to this call
Definition: grpc.h:201
Send initial metadata: one and only one instance MUST be sent for each call, unless the call was canc...
Definition: grpc.h:285
Definition: grpc.h:76
grpc_call_error
Result of a grpc call.
Definition: grpc.h:176
grpc_metadata * trailing_metadata
Definition: grpc.h:344
size_t capacity
Definition: grpc.h:261
const char * key
Definition: grpc.h:222
void grpc_server_cancel_all_calls(grpc_server *server)
Cancel all in-progress calls.
invalid metadata was passed to this call
Definition: grpc.h:199
const char * status_details
Definition: grpc.h:346
void * obfuscated[4]
Definition: grpc.h:231
struct grpc_channel grpc_channel
The Channel interface allows creation of Call objects.
Definition: grpc.h:63
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:221
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:182
Shutting down.
Definition: grpc.h:238
Receive initial metadata: one and only one MUST be made on the client, must not be made on the server...
Definition: grpc.h:306
Operation data: one field for each op type (except SEND_CLOSE_FROM_CLIENT which has no arguments) ...
Definition: grpc.h:325
void * reserved
Definition: grpc.h:274
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:289
int grpc_header_nonbin_value_is_legal(const char *value, size_t length)
Check whether a non-binary metadata value is legal (will be accepted by core)
everything went ok
Definition: grpc.h:178
this call is already finished (writes_done or write_status has already been called) ...
Definition: grpc.h:193
void grpc_alarm_destroy(grpc_alarm *alarm)
Destroy the given completion queue alarm, cancelling it in the process.
struct grpc_event grpc_event
The result of an operation.
grpc_completion_type
The type of completion (for grpc_event)
Definition: grpc.h:236
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:343
grpc_server * grpc_server_create(const grpc_channel_args *args, void *reserved)
Create a server.
size_t host_capacity
Definition: grpc.h:272
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:206
int grpc_header_key_is_legal(const char *key, size_t length)
Check whether a metadata key is legal (will be accepted by core)
size_t num_args
Definition: grpc.h:115
size_t count
Definition: grpc.h:260
void grpc_alarm_cancel(grpc_alarm *alarm)
Cancel a completion queue alarm.
Receive status on the client: one and only one must be made on the client.
Definition: grpc.h:316
channel is ready for work
Definition: grpc.h:165
char * key
Definition: grpc.h:94
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:167
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:223
this method is not available on the client
Definition: grpc.h:184
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:161
channel has seen a failure that it cannot recover from
Definition: grpc.h:169
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:240
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' ...
grpc_alarm * grpc_alarm_create(grpc_completion_queue *cq, gpr_timespec deadline, void *tag)
Create a completion queue alarm instance associated to cq.
void grpc_init(void)
Initialize the grpc library.
void * reserved
Reserved for future usage.
Definition: grpc.h:331
channel is connecting
Definition: grpc.h:163
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:341
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:365
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:224
there is already an outstanding read/write operation on the call
Definition: grpc.h:195
Receive close on the server: one and only one must be made on the server.
Definition: grpc.h:320
grpc_completion_type type
The type of the completion.
Definition: grpc.h:250
this method must be called before server_accept
Definition: grpc.h:186
grpc_arg_type type
Definition: grpc.h:93
void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Begin destruction of a completion queue.
Definition: grpc.h:75
grpc_metadata * metadata
Definition: grpc.h:339
int * cancelled
out argument, set to 1 if the call failed in any way (seen as a cancellation on the server)...
Definition: grpc.h:393
this method must be called after invoke
Definition: grpc.h:190
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:269
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)