handler.c 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }