channel.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "internal.h"
  34. #include "cgrpc.h"
  35. #include <grpc/support/string_util.h>
  36. #include <grpc/support/alloc.h>
  37. #include <assert.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. cgrpc_channel *cgrpc_channel_create(const char *address) {
  42. cgrpc_channel *c = (cgrpc_channel *) malloc(sizeof (cgrpc_channel));
  43. // create the channel
  44. grpc_channel_args channel_args;
  45. channel_args.num_args = 0;
  46. c->channel = grpc_insecure_channel_create(address, &channel_args, NULL);
  47. c->completion_queue = grpc_completion_queue_create(NULL);
  48. return c;
  49. }
  50. cgrpc_channel *cgrpc_channel_create_secure(const char *address,
  51. const char *pem_root_certs,
  52. const char *host) {
  53. cgrpc_channel *c = (cgrpc_channel *) malloc(sizeof (cgrpc_channel));
  54. // create the channel
  55. int argMax = 2;
  56. grpc_channel_args *channelArgs = gpr_malloc(sizeof(grpc_channel_args));
  57. channelArgs->args = gpr_malloc(argMax * sizeof(grpc_arg));
  58. int argCount = 1;
  59. grpc_arg *arg = &channelArgs->args[0];
  60. arg->type = GRPC_ARG_STRING;
  61. arg->key = gpr_strdup("grpc.primary_user_agent");
  62. arg->value.string = gpr_strdup("grpc-swift/0.0.1");
  63. if (host) {
  64. argCount++;
  65. arg = &channelArgs->args[1];
  66. arg->type = GRPC_ARG_STRING;
  67. arg->key = gpr_strdup("grpc.ssl_target_name_override");
  68. arg->value.string = gpr_strdup(host);
  69. }
  70. channelArgs->num_args = argCount;
  71. grpc_channel_credentials *creds = grpc_ssl_credentials_create(pem_root_certs, NULL, NULL);
  72. c->channel = grpc_secure_channel_create(creds, address, channelArgs, NULL);
  73. c->completion_queue = grpc_completion_queue_create(NULL);
  74. return c;
  75. }
  76. void cgrpc_channel_destroy(cgrpc_channel *c) {
  77. grpc_channel_destroy(c->channel);
  78. c->channel = NULL;
  79. grpc_completion_queue_shutdown(c->completion_queue);
  80. cgrpc_completion_queue_drain(c->completion_queue);
  81. grpc_completion_queue_destroy(c->completion_queue);
  82. free(c);
  83. }
  84. cgrpc_call *cgrpc_channel_create_call(cgrpc_channel *channel,
  85. const char *method,
  86. const char *host,
  87. double timeout) {
  88. // create call
  89. gpr_timespec deadline = cgrpc_deadline_in_seconds_from_now(timeout);
  90. grpc_call *channel_call = grpc_channel_create_call(channel->channel,
  91. NULL,
  92. GRPC_PROPAGATE_DEFAULTS,
  93. channel->completion_queue,
  94. method,
  95. host,
  96. deadline,
  97. NULL);
  98. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  99. memset(call, 0, sizeof(cgrpc_call));
  100. call->call = channel_call;
  101. return call;
  102. }
  103. cgrpc_completion_queue *cgrpc_channel_completion_queue(cgrpc_channel *channel) {
  104. return channel->completion_queue;
  105. }