server.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <stdlib.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. cgrpc_server *cgrpc_server_create(const char *address) {
  40. cgrpc_server *server = (cgrpc_server *) malloc(sizeof (cgrpc_server));
  41. server->server = grpc_server_create(NULL, NULL);
  42. server->completion_queue = grpc_completion_queue_create(NULL);
  43. grpc_server_register_completion_queue(server->server, server->completion_queue, NULL);
  44. // prepare the server to listen
  45. server->port = grpc_server_add_insecure_http2_port(server->server, address);
  46. return server;
  47. }
  48. void cgrpc_server_stop(cgrpc_server *server) {
  49. grpc_server_shutdown_and_notify(server->server,
  50. server->completion_queue,
  51. cgrpc_create_tag(0));
  52. }
  53. void cgrpc_server_destroy(cgrpc_server *server) {
  54. grpc_server_shutdown_and_notify(server->server,
  55. server->completion_queue,
  56. cgrpc_create_tag(1000));
  57. grpc_event completion_event =
  58. grpc_completion_queue_pluck(server->completion_queue,
  59. cgrpc_create_tag(1000),
  60. cgrpc_deadline_in_seconds_from_now(5),
  61. NULL);
  62. assert(completion_event.type == GRPC_OP_COMPLETE);
  63. grpc_server_destroy(server->server);
  64. server->server = NULL;
  65. grpc_completion_queue_shutdown(server->completion_queue);
  66. cgrpc_completion_queue_drain(server->completion_queue);
  67. grpc_completion_queue_destroy(server->completion_queue);
  68. }
  69. void cgrpc_server_start(cgrpc_server *server) {
  70. grpc_server_start(server->server);
  71. }
  72. cgrpc_completion_queue *cgrpc_server_get_completion_queue(cgrpc_server *s) {
  73. return s->completion_queue;
  74. }