cgrpc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. *
  3. * Copyright 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. #ifndef cgrpc_h
  34. #define cgrpc_h
  35. #import <stdlib.h>
  36. // This file lists C functions and types used to build Swift gRPC support
  37. #ifndef cgrpc_internal_h
  38. // all domain types are opaque pointers
  39. typedef void cgrpc_byte_buffer;
  40. typedef void cgrpc_call;
  41. typedef void cgrpc_channel;
  42. typedef void cgrpc_completion_queue;
  43. typedef void cgrpc_handler;
  44. typedef void cgrpc_metadata;
  45. typedef void cgrpc_metadata_array;
  46. typedef void cgrpc_mutex;
  47. typedef void cgrpc_observer;
  48. typedef void cgrpc_observer_send_initial_metadata;
  49. typedef void cgrpc_observer_send_message;
  50. typedef void cgrpc_observer_send_close_from_client;
  51. typedef void cgrpc_observer_send_status_from_server;
  52. typedef void cgrpc_observer_recv_initial_metadata;
  53. typedef void cgrpc_observer_recv_message;
  54. typedef void cgrpc_observer_recv_status_on_client;
  55. typedef void cgrpc_observer_recv_close_on_server;
  56. typedef void cgrpc_operations;
  57. typedef void cgrpc_server;
  58. /** Result of a grpc call. If the caller satisfies the prerequisites of a
  59. particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
  60. Receiving any other value listed here is an indication of a bug in the
  61. caller. */
  62. typedef enum grpc_call_error {
  63. /** everything went ok */
  64. GRPC_CALL_OK = 0,
  65. /** something failed, we don't know what */
  66. GRPC_CALL_ERROR,
  67. /** this method is not available on the server */
  68. GRPC_CALL_ERROR_NOT_ON_SERVER,
  69. /** this method is not available on the client */
  70. GRPC_CALL_ERROR_NOT_ON_CLIENT,
  71. /** this method must be called before server_accept */
  72. GRPC_CALL_ERROR_ALREADY_ACCEPTED,
  73. /** this method must be called before invoke */
  74. GRPC_CALL_ERROR_ALREADY_INVOKED,
  75. /** this method must be called after invoke */
  76. GRPC_CALL_ERROR_NOT_INVOKED,
  77. /** this call is already finished
  78. (writes_done or write_status has already been called) */
  79. GRPC_CALL_ERROR_ALREADY_FINISHED,
  80. /** there is already an outstanding read/write operation on the call */
  81. GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
  82. /** the flags value was illegal for this call */
  83. GRPC_CALL_ERROR_INVALID_FLAGS,
  84. /** invalid metadata was passed to this call */
  85. GRPC_CALL_ERROR_INVALID_METADATA,
  86. /** invalid message was passed to this call */
  87. GRPC_CALL_ERROR_INVALID_MESSAGE,
  88. /** completion queue for notification has not been registered with the
  89. server */
  90. GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE,
  91. /** this batch of operations leads to more operations than allowed */
  92. GRPC_CALL_ERROR_BATCH_TOO_BIG,
  93. /** payload type requested is not the type registered */
  94. GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH
  95. } grpc_call_error;
  96. /** The type of completion (for grpc_event) */
  97. typedef enum grpc_completion_type {
  98. /** Shutting down */
  99. GRPC_QUEUE_SHUTDOWN,
  100. /** No event before timeout */
  101. GRPC_QUEUE_TIMEOUT,
  102. /** Operation completion */
  103. GRPC_OP_COMPLETE
  104. } grpc_completion_type;
  105. typedef struct grpc_event {
  106. /** The type of the completion. */
  107. grpc_completion_type type;
  108. /** non-zero if the operation was successful, 0 upon failure.
  109. Only GRPC_OP_COMPLETE can succeed or fail. */
  110. int success;
  111. /** The tag passed to grpc_call_start_batch etc to start this operation.
  112. Only GRPC_OP_COMPLETE has a tag. */
  113. void *tag;
  114. } grpc_event;
  115. #endif
  116. // directly expose a few grpc library functions
  117. void grpc_init();
  118. void grpc_shutdown();
  119. const char *grpc_version_string();
  120. // channel support
  121. cgrpc_channel *cgrpc_channel_create(const char *address);
  122. cgrpc_channel *cgrpc_channel_create_secure(const char *address,
  123. const char *pem_root_certs,
  124. const char *host);
  125. void cgrpc_channel_destroy(cgrpc_channel *channel);
  126. cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
  127. const char *method,
  128. const char *host,
  129. double timeout);
  130. cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel);
  131. // server support
  132. cgrpc_server *cgrpc_server_create(const char *address);
  133. cgrpc_server *cgrpc_server_create_secure(const char *address,
  134. const char *private_key,
  135. const char *cert_chain);
  136. void cgrpc_server_stop(cgrpc_server *server);
  137. void cgrpc_server_destroy(cgrpc_server *s);
  138. void cgrpc_server_start(cgrpc_server *s);
  139. cgrpc_completion_queue *cgrpc_server_get_completion_queue(cgrpc_server *s);
  140. // completion queues
  141. grpc_event cgrpc_completion_queue_get_next_event(cgrpc_completion_queue *cq,
  142. double timeout);
  143. void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
  144. void cgrpc_completion_queue_shutdown(cgrpc_completion_queue *cq);
  145. // server request handlers
  146. cgrpc_handler *cgrpc_handler_create_with_server(cgrpc_server *server);
  147. void cgrpc_handler_destroy(cgrpc_handler *h);
  148. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h);
  149. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h);
  150. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  151. cgrpc_metadata_array *metadata,
  152. long tag);
  153. const char *cgrpc_handler_host(cgrpc_handler *h);
  154. const char *cgrpc_handler_method(cgrpc_handler *h);
  155. const char *cgrpc_handler_call_peer(cgrpc_handler *h);
  156. // call support
  157. void cgrpc_call_destroy(cgrpc_call *call);
  158. grpc_call_error cgrpc_call_perform(cgrpc_call *call, cgrpc_operations *operations, int64_t tag);
  159. // operations
  160. cgrpc_operations *cgrpc_operations_create();
  161. void cgrpc_operations_destroy(cgrpc_operations *operations);
  162. void cgrpc_operations_reserve_space_for_operations(cgrpc_operations *call, int max_operations);
  163. void cgrpc_operations_add_operation(cgrpc_operations *call, cgrpc_observer *observer);
  164. // metadata support
  165. cgrpc_metadata_array *cgrpc_metadata_array_create();
  166. void cgrpc_metadata_array_destroy(cgrpc_metadata_array *array);
  167. size_t cgrpc_metadata_array_get_count(cgrpc_metadata_array *array);
  168. const char *cgrpc_metadata_array_get_key_at_index(cgrpc_metadata_array *array, size_t index);
  169. const char *cgrpc_metadata_array_get_value_at_index(cgrpc_metadata_array *array, size_t index);
  170. void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *dest, cgrpc_metadata_array *src);
  171. void cgrpc_metadata_array_append_metadata(cgrpc_metadata_array *metadata, const char *key, const char *value);
  172. // mutex support
  173. cgrpc_mutex *cgrpc_mutex_create();
  174. void cgrpc_mutex_destroy(cgrpc_mutex *mu);
  175. void cgrpc_mutex_lock(cgrpc_mutex *mu);
  176. void cgrpc_mutex_unlock(cgrpc_mutex *mu);
  177. // byte buffer support
  178. void cgrpc_byte_buffer_destroy(cgrpc_byte_buffer *bb);
  179. cgrpc_byte_buffer *cgrpc_byte_buffer_create_by_copying_data(const void *source, size_t len);
  180. const void *cgrpc_byte_buffer_copy_data(cgrpc_byte_buffer *bb, size_t *length);
  181. // event support
  182. int64_t cgrpc_event_tag(grpc_event ev);
  183. // observers
  184. // constructors
  185. cgrpc_observer_send_initial_metadata *cgrpc_observer_create_send_initial_metadata(cgrpc_metadata_array *metadata);
  186. cgrpc_observer_send_message *cgrpc_observer_create_send_message();
  187. cgrpc_observer_send_close_from_client *cgrpc_observer_create_send_close_from_client();
  188. cgrpc_observer_send_status_from_server *cgrpc_observer_create_send_status_from_server(cgrpc_metadata_array *metadata);
  189. cgrpc_observer_recv_initial_metadata *cgrpc_observer_create_recv_initial_metadata();
  190. cgrpc_observer_recv_message *cgrpc_observer_create_recv_message();
  191. cgrpc_observer_recv_status_on_client *cgrpc_observer_create_recv_status_on_client();
  192. cgrpc_observer_recv_close_on_server *cgrpc_observer_create_recv_close_on_server();
  193. // destructor
  194. void cgrpc_observer_destroy(cgrpc_observer *observer);
  195. // GRPC_OP_SEND_INITIAL_METADATA
  196. // GRPC_OP_SEND_MESSAGE
  197. void cgrpc_observer_send_message_set_message(cgrpc_observer_send_message *observer,
  198. cgrpc_byte_buffer *message);
  199. // GRPC_OP_SEND_CLOSE_FROM_CLIENT
  200. // -- no special handlers --
  201. // GRPC_OP_SEND_STATUS_FROM_SERVER
  202. void cgrpc_observer_send_status_from_server_set_status
  203. (cgrpc_observer_send_status_from_server *observer,
  204. int status);
  205. void cgrpc_observer_send_status_from_server_set_status_details
  206. (cgrpc_observer_send_status_from_server *observer,
  207. const char *statusDetails);
  208. // GRPC_OP_RECV_INITIAL_METADATA
  209. cgrpc_metadata_array *cgrpc_observer_recv_initial_metadata_get_metadata
  210. (cgrpc_observer_recv_initial_metadata *observer);
  211. // GRPC_OP_RECV_MESSAGE
  212. cgrpc_byte_buffer *cgrpc_observer_recv_message_get_message
  213. (cgrpc_observer_recv_message *observer);
  214. // GRPC_OP_RECV_STATUS_ON_CLIENT
  215. cgrpc_metadata_array *cgrpc_observer_recv_status_on_client_get_metadata
  216. (cgrpc_observer_recv_status_on_client *observer);
  217. long cgrpc_observer_recv_status_on_client_get_status
  218. (cgrpc_observer_recv_status_on_client *observer);
  219. const char *cgrpc_observer_recv_status_on_client_get_status_details
  220. (cgrpc_observer_recv_status_on_client *observer);
  221. // GRPC_OP_RECV_CLOSE_ON_SERVER
  222. int cgrpc_observer_recv_close_on_server_was_cancelled
  223. (cgrpc_observer_recv_close_on_server *observer);
  224. #endif