handler.c 4.5 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 <assert.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. cgrpc_handler *cgrpc_handler_create_with_server(cgrpc_server *server) {
  40. cgrpc_handler *handler = (cgrpc_handler *) malloc(sizeof (cgrpc_handler));
  41. memset(handler, 0, sizeof(cgrpc_handler));
  42. handler->server = server;
  43. grpc_metadata_array_init(&(handler->request_metadata_recv));
  44. grpc_call_details_init(&(handler->call_details));
  45. handler->completion_queue = grpc_completion_queue_create(NULL);
  46. return handler;
  47. }
  48. void cgrpc_handler_destroy(cgrpc_handler *h) {
  49. grpc_completion_queue_shutdown(h->completion_queue);
  50. cgrpc_completion_queue_drain(h->completion_queue);
  51. grpc_completion_queue_destroy(h->completion_queue);
  52. grpc_metadata_array_destroy(&(h->request_metadata_recv));
  53. grpc_call_details_destroy(&(h->call_details));
  54. if (h->server_call) {
  55. grpc_call_destroy(h->server_call);
  56. }
  57. free(h);
  58. }
  59. const char *cgrpc_handler_host(cgrpc_handler *h) {
  60. return h->call_details.host;
  61. }
  62. const char *cgrpc_handler_method(cgrpc_handler *h) {
  63. return h->call_details.method;
  64. }
  65. const char *cgrpc_handler_call_peer(cgrpc_handler *h) {
  66. return grpc_call_get_peer(h->server_call);
  67. }
  68. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h) {
  69. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  70. memset(call, 0, sizeof(cgrpc_call));
  71. call->call = h->server_call;
  72. return call;
  73. }
  74. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h) {
  75. return h->completion_queue;
  76. }
  77. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  78. cgrpc_metadata_array *metadata,
  79. long tag) {
  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. }
  88. grpc_completion_type cgrpc_handler_wait_for_request(cgrpc_handler *h,
  89. cgrpc_metadata_array *metadata,
  90. double timeout) {
  91. void *tag101 = cgrpc_create_tag(101);
  92. grpc_call_error error = grpc_server_request_call(h->server->server,
  93. &(h->server_call),
  94. &(h->call_details),
  95. metadata,
  96. h->completion_queue,
  97. h->server->completion_queue,
  98. tag101);
  99. assert(error == GRPC_CALL_OK);
  100. // wait for a request
  101. return cgrpc_completion_queue_get_next_event(h->server->completion_queue, timeout);
  102. }