cgrpc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_client;
  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. // client support
  121. cgrpc_client *cgrpc_client_create(const char *address);
  122. void cgrpc_client_destroy(cgrpc_client *client);
  123. cgrpc_call *cgrpc_client_create_call(cgrpc_client *client,
  124. const char *method,
  125. const char *host,
  126. double timeout);
  127. cgrpc_completion_queue *cgrpc_client_completion_queue(cgrpc_client *client);
  128. // server support
  129. cgrpc_server *cgrpc_server_create(const char *address);
  130. void cgrpc_server_destroy(cgrpc_server *s);
  131. void cgrpc_server_start(cgrpc_server *s);
  132. cgrpc_completion_queue *cgrpc_server_get_completion_queue(cgrpc_server *s);
  133. // completion queues
  134. grpc_event cgrpc_completion_queue_get_next_event(cgrpc_completion_queue *cq,
  135. double timeout);
  136. void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
  137. // server request handlers
  138. cgrpc_handler *cgrpc_handler_create_with_server(cgrpc_server *server);
  139. void cgrpc_handler_destroy(cgrpc_handler *h);
  140. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h);
  141. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h);
  142. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  143. cgrpc_metadata_array *metadata,
  144. long tag);
  145. grpc_event cgrpc_handler_wait_for_request(cgrpc_handler *h,
  146. cgrpc_metadata_array *metadata,
  147. double timeout);
  148. const char *cgrpc_handler_host(cgrpc_handler *h);
  149. const char *cgrpc_handler_method(cgrpc_handler *h);
  150. const char *cgrpc_handler_call_peer(cgrpc_handler *h);
  151. // call support
  152. void cgrpc_call_destroy(cgrpc_call *call);
  153. grpc_call_error cgrpc_call_perform(cgrpc_call *call, cgrpc_operations *operations, int64_t tag);
  154. // operations
  155. cgrpc_operations *cgrpc_operations_create();
  156. void cgrpc_operations_reserve_space_for_operations(cgrpc_operations *call, int max_operations);
  157. void cgrpc_operations_add_operation(cgrpc_operations *call, cgrpc_observer *observer);
  158. // metadata support
  159. cgrpc_metadata_array *cgrpc_metadata_array_create();
  160. void cgrpc_metadata_array_destroy(cgrpc_metadata_array *array);
  161. size_t cgrpc_metadata_array_get_count(cgrpc_metadata_array *array);
  162. const char *cgrpc_metadata_array_get_key_at_index(cgrpc_metadata_array *array, size_t index);
  163. const char *cgrpc_metadata_array_get_value_at_index(cgrpc_metadata_array *array, size_t index);
  164. void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *dest, cgrpc_metadata_array *src);
  165. void cgrpc_metadata_array_append_metadata(cgrpc_metadata_array *metadata, const char *key, const char *value);
  166. // mutex support
  167. cgrpc_mutex *cgrpc_mutex_create();
  168. void cgrpc_mutex_destroy(cgrpc_mutex *mu);
  169. void cgrpc_mutex_lock(cgrpc_mutex *mu);
  170. void cgrpc_mutex_unlock(cgrpc_mutex *mu);
  171. // byte buffer support
  172. void cgrpc_byte_buffer_destroy(cgrpc_byte_buffer *bb);
  173. cgrpc_byte_buffer *cgrpc_byte_buffer_create_with_string(const char *string);
  174. cgrpc_byte_buffer *cgrpc_byte_buffer_create_with_data(const void *source, size_t len);
  175. const char *cgrpc_byte_buffer_as_string(cgrpc_byte_buffer *bb);
  176. const void *cgrpc_byte_buffer_as_data(cgrpc_byte_buffer *bb, size_t *length);
  177. // event support
  178. int64_t cgrpc_event_tag(grpc_event ev);
  179. // observers
  180. // constructors
  181. cgrpc_observer_send_initial_metadata *cgrpc_observer_create_send_initial_metadata(cgrpc_metadata_array *metadata);
  182. cgrpc_observer_send_message *cgrpc_observer_create_send_message();
  183. cgrpc_observer_send_close_from_client *cgrpc_observer_create_send_close_from_client();
  184. cgrpc_observer_send_status_from_server *cgrpc_observer_create_send_status_from_server(cgrpc_metadata_array *metadata);
  185. cgrpc_observer_recv_initial_metadata *cgrpc_observer_create_recv_initial_metadata();
  186. cgrpc_observer_recv_message *cgrpc_observer_create_recv_message();
  187. cgrpc_observer_recv_status_on_client *cgrpc_observer_create_recv_status_on_client();
  188. cgrpc_observer_recv_close_on_server *cgrpc_observer_create_recv_close_on_server();
  189. // destructor
  190. void cgrpc_observer_destroy(cgrpc_observer *observer);
  191. // GRPC_OP_SEND_INITIAL_METADATA
  192. // GRPC_OP_SEND_MESSAGE
  193. void cgrpc_observer_send_message_set_message(cgrpc_observer_send_message *observer,
  194. cgrpc_byte_buffer *message);
  195. // GRPC_OP_SEND_CLOSE_FROM_CLIENT
  196. // -- no special handlers --
  197. // GRPC_OP_SEND_STATUS_FROM_SERVER
  198. void cgrpc_observer_send_status_from_server_set_status
  199. (cgrpc_observer_send_status_from_server *observer,
  200. int status);
  201. void cgrpc_observer_send_status_from_server_set_status_details
  202. (cgrpc_observer_send_status_from_server *observer,
  203. const char *statusDetails);
  204. // GRPC_OP_RECV_INITIAL_METADATA
  205. cgrpc_metadata_array *cgrpc_observer_recv_initial_metadata_get_metadata
  206. (cgrpc_observer_recv_initial_metadata *observer);
  207. // GRPC_OP_RECV_MESSAGE
  208. cgrpc_byte_buffer *cgrpc_observer_recv_message_get_message
  209. (cgrpc_observer_recv_message *observer);
  210. // GRPC_OP_RECV_STATUS_ON_CLIENT
  211. cgrpc_metadata_array *cgrpc_observer_recv_status_on_client_get_metadata
  212. (cgrpc_observer_recv_status_on_client *observer);
  213. long cgrpc_observer_recv_status_on_client_get_status
  214. (cgrpc_observer_recv_status_on_client *observer);
  215. const char *cgrpc_observer_recv_status_on_client_get_status_details
  216. (cgrpc_observer_recv_status_on_client *observer);
  217. // GRPC_OP_RECV_CLOSE_ON_SERVER
  218. int cgrpc_observer_recv_close_on_server_was_cancelled
  219. (cgrpc_observer_recv_close_on_server *observer);
  220. #endif