handler.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. grpc_completion_queue_attributes attr;
  29. attr.version = 1;
  30. attr.cq_completion_type = GRPC_CQ_CURRENT_VERSION;
  31. attr.cq_polling_type = GRPC_CQ_DEFAULT_POLLING;
  32. grpc_completion_queue_factory *factory = grpc_completion_queue_factory_lookup(&attr);
  33. handler->completion_queue = grpc_completion_queue_create(factory, &attr, NULL);
  34. return handler;
  35. }
  36. void cgrpc_handler_destroy(cgrpc_handler *h) {
  37. grpc_completion_queue_shutdown(h->completion_queue);
  38. cgrpc_completion_queue_drain(h->completion_queue);
  39. grpc_completion_queue_destroy(h->completion_queue);
  40. grpc_metadata_array_destroy(&(h->request_metadata_recv));
  41. grpc_call_details_destroy(&(h->call_details));
  42. if (h->server_call) {
  43. //grpc_call_destroy(h->server_call);
  44. }
  45. free(h);
  46. }
  47. const char *cgrpc_handler_host(cgrpc_handler *h) {
  48. return (const char *) GRPC_SLICE_START_PTR(h->call_details.host);
  49. }
  50. const char *cgrpc_handler_method(cgrpc_handler *h) {
  51. return (const char *) GRPC_SLICE_START_PTR(h->call_details.method);
  52. }
  53. const char *cgrpc_handler_call_peer(cgrpc_handler *h) {
  54. return grpc_call_get_peer(h->server_call);
  55. }
  56. cgrpc_call *cgrpc_handler_get_call(cgrpc_handler *h) {
  57. cgrpc_call *call = (cgrpc_call *) malloc(sizeof(cgrpc_call));
  58. memset(call, 0, sizeof(cgrpc_call));
  59. call->call = h->server_call;
  60. return call;
  61. }
  62. cgrpc_completion_queue *cgrpc_handler_get_completion_queue(cgrpc_handler *h) {
  63. return h->completion_queue;
  64. }
  65. grpc_call_error cgrpc_handler_request_call(cgrpc_handler *h,
  66. cgrpc_metadata_array *metadata,
  67. long tag) {
  68. return grpc_server_request_call(h->server->server,
  69. &(h->server_call),
  70. &(h->call_details),
  71. metadata,
  72. h->completion_queue,
  73. h->server->completion_queue,
  74. cgrpc_create_tag(tag));
  75. }