handler.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <assert.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. cgrpc_handler *cgrpc_handler_create_with_server(cgrpc_server *server) {
  23. cgrpc_handler *handler = (cgrpc_handler *) malloc(sizeof (cgrpc_handler));
  24. memset(handler, 0, sizeof(cgrpc_handler));
  25. handler->server = server;
  26. grpc_metadata_array_init(&(handler->request_metadata_recv));
  27. grpc_call_details_init(&(handler->call_details));
  28. handler->completion_queue = grpc_completion_queue_create_for_next(NULL);
  29. return handler;
  30. }
  31. void cgrpc_handler_destroy(cgrpc_handler *h) {
  32. grpc_completion_queue_shutdown(h->completion_queue);
  33. cgrpc_completion_queue_drain(h->completion_queue);
  34. grpc_completion_queue_destroy(h->completion_queue);
  35. grpc_metadata_array_destroy(&(h->request_metadata_recv));
  36. grpc_call_details_destroy(&(h->call_details));
  37. if (h->server_call) {
  38. grpc_call_unref(h->server_call);
  39. }
  40. free(h);
  41. }
  42. char *cgrpc_handler_copy_host(cgrpc_handler *h) {
  43. size_t length = GRPC_SLICE_LENGTH(h->call_details.host);
  44. char *str = (char *) malloc(length + 1);
  45. memcpy(str, GRPC_SLICE_START_PTR(h->call_details.host), length);
  46. str[length] = 0;
  47. return str;
  48. }
  49. char *cgrpc_handler_copy_method(cgrpc_handler *h) {
  50. size_t length = GRPC_SLICE_LENGTH(h->call_details.method);
  51. char *str = (char *) malloc(length + 1);
  52. memcpy(str, GRPC_SLICE_START_PTR(h->call_details.method), length);
  53. str[length] = 0;
  54. return str;
  55. }
  56. char *cgrpc_handler_call_peer(cgrpc_handler *h) {
  57. return grpc_call_get_peer(h->server_call);
  58. }
  59. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h) {
  60. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  61. memset(call, 0, sizeof(cgrpc_call));
  62. call->call = h->server_call;
  63. if (call->call) {
  64. // This retain will be balanced by `cgrpc_call_destroy()`.
  65. grpc_call_ref(call->call);
  66. }
  67. return call;
  68. }
  69. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h) {
  70. return h->completion_queue;
  71. }
  72. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  73. cgrpc_metadata_array *metadata,
  74. long tag) {
  75. if (h->server_call != NULL) {
  76. return GRPC_CALL_OK;
  77. }
  78. // This fills `h->server_call` with a call with retain count of +1.
  79. // We'll release that retain in `cgrpc_handler_destroy()`.
  80. return grpc_server_request_call(h->server->server,
  81. &(h->server_call),
  82. &(h->call_details),
  83. metadata,
  84. h->completion_queue,
  85. h->server->completion_queue,
  86. cgrpc_create_tag(tag));
  87. }