cgrpc.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_server;
  57. /** Result of a grpc call. If the caller satisfies the prerequisites of a
  58. particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
  59. Receiving any other value listed here is an indication of a bug in the
  60. caller. */
  61. typedef enum grpc_call_error {
  62. /** everything went ok */
  63. GRPC_CALL_OK = 0,
  64. /** something failed, we don't know what */
  65. GRPC_CALL_ERROR,
  66. /** this method is not available on the server */
  67. GRPC_CALL_ERROR_NOT_ON_SERVER,
  68. /** this method is not available on the client */
  69. GRPC_CALL_ERROR_NOT_ON_CLIENT,
  70. /** this method must be called before server_accept */
  71. GRPC_CALL_ERROR_ALREADY_ACCEPTED,
  72. /** this method must be called before invoke */
  73. GRPC_CALL_ERROR_ALREADY_INVOKED,
  74. /** this method must be called after invoke */
  75. GRPC_CALL_ERROR_NOT_INVOKED,
  76. /** this call is already finished
  77. (writes_done or write_status has already been called) */
  78. GRPC_CALL_ERROR_ALREADY_FINISHED,
  79. /** there is already an outstanding read/write operation on the call */
  80. GRPC_CALL_ERROR_TOO_MANY_OPERATIONS,
  81. /** the flags value was illegal for this call */
  82. GRPC_CALL_ERROR_INVALID_FLAGS,
  83. /** invalid metadata was passed to this call */
  84. GRPC_CALL_ERROR_INVALID_METADATA,
  85. /** invalid message was passed to this call */
  86. GRPC_CALL_ERROR_INVALID_MESSAGE,
  87. /** completion queue for notification has not been registered with the
  88. server */
  89. GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE,
  90. /** this batch of operations leads to more operations than allowed */
  91. GRPC_CALL_ERROR_BATCH_TOO_BIG,
  92. /** payload type requested is not the type registered */
  93. GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH
  94. } grpc_call_error;
  95. /** The type of completion (for grpc_event) */
  96. typedef enum grpc_completion_type {
  97. /** Shutting down */
  98. GRPC_QUEUE_SHUTDOWN,
  99. /** No event before timeout */
  100. GRPC_QUEUE_TIMEOUT,
  101. /** Operation completion */
  102. GRPC_OP_COMPLETE
  103. } grpc_completion_type;
  104. #endif
  105. // directly expose a few grpc library functions
  106. void grpc_init();
  107. void grpc_shutdown();
  108. const char *grpc_version_string();
  109. // client support
  110. cgrpc_client *cgrpc_client_create(const char *address);
  111. void cgrpc_client_destroy(cgrpc_client *client);
  112. cgrpc_call *cgrpc_client_create_call(cgrpc_client *client,
  113. const char *method,
  114. const char *host,
  115. double timeout);
  116. cgrpc_completion_queue *cgrpc_client_completion_queue(cgrpc_client *client);
  117. // server support
  118. cgrpc_server *cgrpc_server_create(const char *address);
  119. void cgrpc_server_destroy(cgrpc_server *s);
  120. void cgrpc_server_start(cgrpc_server *s);
  121. cgrpc_completion_queue *cgrpc_server_get_completion_queue(cgrpc_server *s);
  122. // completion queues
  123. grpc_completion_type cgrpc_completion_queue_get_next_event(cgrpc_completion_queue *cq,
  124. double timeout);
  125. void cgrpc_completion_queue_drain(cgrpc_completion_queue *cq);
  126. // server request handlers
  127. cgrpc_handler *cgrpc_handler_create_with_server(cgrpc_server *server);
  128. void cgrpc_handler_destroy(cgrpc_handler *h);
  129. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h);
  130. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h);
  131. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  132. cgrpc_metadata_array *metadata,
  133. long tag);
  134. grpc_completion_type cgrpc_handler_wait_for_request(cgrpc_handler *h,
  135. cgrpc_metadata_array *metadata,
  136. double timeout);
  137. const char *cgrpc_handler_host(cgrpc_handler *h);
  138. const char *cgrpc_handler_method(cgrpc_handler *h);
  139. const char *cgrpc_handler_call_peer(cgrpc_handler *h);
  140. // call support
  141. void cgrpc_call_destroy(cgrpc_call *call);
  142. void cgrpc_call_reserve_space_for_operations(cgrpc_call *call, int max_operations);
  143. void cgrpc_call_add_operation(cgrpc_call *call, cgrpc_observer *observer);
  144. grpc_call_error cgrpc_call_perform(cgrpc_call *call, long tag);
  145. // metadata support
  146. cgrpc_metadata_array *cgrpc_metadata_array_create();
  147. void cgrpc_metadata_array_destroy(cgrpc_metadata_array *array);
  148. size_t cgrpc_metadata_array_get_count(cgrpc_metadata_array *array);
  149. const char *cgrpc_metadata_array_get_key_at_index(cgrpc_metadata_array *array, size_t index);
  150. const char *cgrpc_metadata_array_get_value_at_index(cgrpc_metadata_array *array, size_t index);
  151. void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *dest, cgrpc_metadata_array *src);
  152. void cgrpc_metadata_array_append_metadata(cgrpc_metadata_array *metadata, const char *key, const char *value);
  153. // mutex support
  154. cgrpc_mutex *cgrpc_mutex_create();
  155. void cgrpc_mutex_destroy(cgrpc_mutex *mu);
  156. void cgrpc_mutex_lock(cgrpc_mutex *mu);
  157. void cgrpc_mutex_unlock(cgrpc_mutex *mu);
  158. // byte buffer support
  159. void cgrpc_byte_buffer_destroy(cgrpc_byte_buffer *bb);
  160. cgrpc_byte_buffer *cgrpc_byte_buffer_create_with_string(const char *string);
  161. const char *cgrpc_byte_buffer_as_string(cgrpc_byte_buffer *bb);
  162. // observers
  163. // constructors
  164. cgrpc_observer_send_initial_metadata *cgrpc_observer_create_send_initial_metadata(cgrpc_metadata_array *metadata);
  165. cgrpc_observer_send_message *cgrpc_observer_create_send_message();
  166. cgrpc_observer_send_close_from_client *cgrpc_observer_create_send_close_from_client();
  167. cgrpc_observer_send_status_from_server *cgrpc_observer_create_send_status_from_server(cgrpc_metadata_array *metadata);
  168. cgrpc_observer_recv_initial_metadata *cgrpc_observer_create_recv_initial_metadata();
  169. cgrpc_observer_recv_message *cgrpc_observer_create_recv_message();
  170. cgrpc_observer_recv_status_on_client *cgrpc_observer_create_recv_status_on_client();
  171. cgrpc_observer_recv_close_on_server *cgrpc_observer_create_recv_close_on_server();
  172. // destructor
  173. void cgrpc_observer_destroy(cgrpc_observer *observer);
  174. // GRPC_OP_SEND_INITIAL_METADATA
  175. // GRPC_OP_SEND_MESSAGE
  176. void cgrpc_observer_send_message_set_message(cgrpc_observer_send_message *observer,
  177. cgrpc_byte_buffer *message);
  178. // GRPC_OP_SEND_CLOSE_FROM_CLIENT
  179. // -- no special handlers --
  180. // GRPC_OP_SEND_STATUS_FROM_SERVER
  181. void cgrpc_observer_send_status_from_server_set_status
  182. (cgrpc_observer_send_status_from_server *observer,
  183. int status);
  184. void cgrpc_observer_send_status_from_server_set_status_details
  185. (cgrpc_observer_send_status_from_server *observer,
  186. const char *statusDetails);
  187. // GRPC_OP_RECV_INITIAL_METADATA
  188. cgrpc_metadata_array *cgrpc_observer_recv_initial_metadata_get_metadata
  189. (cgrpc_observer_recv_initial_metadata *observer);
  190. // GRPC_OP_RECV_MESSAGE
  191. cgrpc_byte_buffer *cgrpc_observer_recv_message_get_message
  192. (cgrpc_observer_recv_message *observer);
  193. // GRPC_OP_RECV_STATUS_ON_CLIENT
  194. cgrpc_metadata_array *cgrpc_observer_recv_status_on_client_get_metadata
  195. (cgrpc_observer_recv_status_on_client *observer);
  196. long cgrpc_observer_recv_status_on_client_get_status
  197. (cgrpc_observer_recv_status_on_client *observer);
  198. const char *cgrpc_observer_recv_status_on_client_get_status_details
  199. (cgrpc_observer_recv_status_on_client *observer);
  200. // GRPC_OP_RECV_CLOSE_ON_SERVER
  201. int cgrpc_observer_recv_close_on_server_was_cancelled
  202. (cgrpc_observer_recv_close_on_server *observer);
  203. #endif