channel.c 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "internal.h"
  17. #include "cgrpc.h"
  18. #include <grpc/support/string_util.h>
  19. #include <grpc/support/alloc.h>
  20. #include <assert.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. cgrpc_channel *cgrpc_channel_create(const char *address,
  25. grpc_arg *args,
  26. int number_args) {
  27. cgrpc_channel *c = (cgrpc_channel *) malloc(sizeof (cgrpc_channel));
  28. grpc_channel_args *channel_args = gpr_malloc(sizeof(grpc_channel_args));
  29. channel_args->args = args;
  30. channel_args->num_args = number_args;
  31. // create the channel
  32. c->channel = grpc_insecure_channel_create(address, channel_args, NULL);
  33. c->completion_queue = grpc_completion_queue_create_for_next(NULL);
  34. gpr_free(channel_args);
  35. return c;
  36. }
  37. cgrpc_channel *cgrpc_channel_create_secure(const char *address,
  38. const char *pem_root_certs,
  39. grpc_arg *args,
  40. int number_args) {
  41. cgrpc_channel *c = (cgrpc_channel *) malloc(sizeof (cgrpc_channel));
  42. // create the channel
  43. grpc_channel_args *channel_args = gpr_malloc(sizeof(grpc_channel_args));
  44. channel_args->args = args;
  45. channel_args->num_args = number_args;
  46. grpc_channel_credentials *creds = grpc_ssl_credentials_create(pem_root_certs, NULL, NULL);
  47. c->channel = grpc_secure_channel_create(creds, address, channel_args, NULL);
  48. c->completion_queue = grpc_completion_queue_create_for_next(NULL);
  49. gpr_free(channel_args);
  50. return c;
  51. }
  52. void cgrpc_channel_destroy(cgrpc_channel *c) {
  53. grpc_channel_destroy(c->channel);
  54. c->channel = NULL;
  55. free(c);
  56. }
  57. grpc_slice host_slice;
  58. cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
  59. const char *method,
  60. const char *host,
  61. double timeout) {
  62. // create call
  63. host_slice = grpc_slice_from_copied_string(host);
  64. gpr_timespec deadline = cgrpc_deadline_in_seconds_from_now(timeout);
  65. // The resulting call will have a retain call of +1. We'll release it in `cgrpc_call_destroy()`.
  66. grpc_call *channel_call = grpc_channel_create_call(channel->channel,
  67. NULL,
  68. GRPC_PROPAGATE_DEFAULTS,
  69. channel->completion_queue,
  70. grpc_slice_from_copied_string(method),
  71. &host_slice,
  72. deadline,
  73. NULL);
  74. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  75. memset(call, 0, sizeof(cgrpc_call));
  76. call->call = channel_call;
  77. return call;
  78. }
  79. cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel) {
  80. return channel->completion_queue;
  81. }
  82. grpc_connectivity_state cgrpc_channel_check_connectivity_state(cgrpc_channel *channel, int try_to_connect) {
  83. return grpc_channel_check_connectivity_state(channel->channel, try_to_connect);
  84. }