channel.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. cgrpc_channel *c = (cgrpc_channel *) malloc(sizeof (cgrpc_channel));
  26. // create the channel
  27. grpc_channel_args channel_args;
  28. channel_args.num_args = 0;
  29. c->channel = grpc_insecure_channel_create(address, &channel_args, NULL);
  30. grpc_completion_queue_attributes attr;
  31. attr.version = 1;
  32. attr.cq_completion_type = GRPC_CQ_CURRENT_VERSION;
  33. attr.cq_polling_type = GRPC_CQ_DEFAULT_POLLING;
  34. grpc_completion_queue_factory *factory = grpc_completion_queue_factory_lookup(&attr);
  35. c->completion_queue = grpc_completion_queue_create(factory, &attr, NULL);
  36. return c;
  37. }
  38. cgrpc_channel *cgrpc_channel_create_secure(const char *address,
  39. const char *pem_root_certs,
  40. const char *host) {
  41. cgrpc_channel *c = (cgrpc_channel *) malloc(sizeof (cgrpc_channel));
  42. // create the channel
  43. int argMax = 2;
  44. grpc_channel_args *channelArgs = gpr_malloc(sizeof(grpc_channel_args));
  45. channelArgs->args = gpr_malloc(argMax * sizeof(grpc_arg));
  46. int argCount = 1;
  47. grpc_arg *arg = &channelArgs->args[0];
  48. arg->type = GRPC_ARG_STRING;
  49. arg->key = gpr_strdup("grpc.primary_user_agent");
  50. arg->value.string = gpr_strdup("grpc-swift/0.0.1");
  51. if (host) {
  52. argCount++;
  53. arg = &channelArgs->args[1];
  54. arg->type = GRPC_ARG_STRING;
  55. arg->key = gpr_strdup("grpc.ssl_target_name_override");
  56. arg->value.string = gpr_strdup(host);
  57. }
  58. channelArgs->num_args = argCount;
  59. grpc_channel_credentials *creds = grpc_ssl_credentials_create(pem_root_certs, NULL, NULL);
  60. c->channel = grpc_secure_channel_create(creds, address, channelArgs, NULL);
  61. grpc_completion_queue_attributes attr;
  62. attr.version = 1;
  63. attr.cq_completion_type = GRPC_CQ_CURRENT_VERSION;
  64. attr.cq_polling_type = GRPC_CQ_DEFAULT_POLLING;
  65. grpc_completion_queue_factory *factory = grpc_completion_queue_factory_lookup(&attr);
  66. c->completion_queue = grpc_completion_queue_create(factory, &attr, NULL);
  67. return c;
  68. }
  69. void cgrpc_channel_destroy(cgrpc_channel *c) {
  70. grpc_channel_destroy(c->channel);
  71. c->channel = NULL;
  72. grpc_completion_queue_shutdown(c->completion_queue);
  73. cgrpc_completion_queue_drain(c->completion_queue);
  74. grpc_completion_queue_destroy(c->completion_queue);
  75. free(c);
  76. }
  77. cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
  78. const char *method,
  79. const char *host,
  80. double timeout) {
  81. // create call
  82. grpc_slice host_slice = grpc_slice_from_copied_string(host);
  83. gpr_timespec deadline = cgrpc_deadline_in_seconds_from_now(timeout);
  84. grpc_call *channel_call = grpc_channel_create_call(channel->channel,
  85. NULL,
  86. GRPC_PROPAGATE_DEFAULTS,
  87. channel->completion_queue,
  88. grpc_slice_from_copied_string(method),
  89. &host_slice, // this might crash
  90. deadline,
  91. NULL);
  92. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  93. memset(call, 0, sizeof(cgrpc_call));
  94. call->call = channel_call;
  95. return call;
  96. }
  97. cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel) {
  98. return channel->completion_queue;
  99. }