completion_queue_uv.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #ifdef GRPC_UV
  34. #include <uv.h>
  35. #include <node.h>
  36. #include <v8.h>
  37. #include <grpc/grpc.h>
  38. #include "call.h"
  39. #include "completion_queue.h"
  40. namespace grpc {
  41. namespace node {
  42. using v8::Local;
  43. using v8::Object;
  44. using v8::Value;
  45. grpc_completion_queue *queue;
  46. uv_prepare_t prepare;
  47. int pending_batches;
  48. void drain_completion_queue(uv_prepare_t *handle) {
  49. Nan::HandleScope scope;
  50. grpc_event event;
  51. (void)handle;
  52. do {
  53. event = grpc_completion_queue_next(
  54. queue, gpr_inf_past(GPR_CLOCK_MONOTONIC), NULL);
  55. if (event.type == GRPC_OP_COMPLETE) {
  56. Nan::Callback *callback = grpc::node::GetTagCallback(event.tag);
  57. if (event.success) {
  58. Local<Value> argv[] = {Nan::Null(),
  59. grpc::node::GetTagNodeValue(event.tag)};
  60. callback->Call(2, argv);
  61. } else {
  62. Local<Value> argv[] = {Nan::Error(
  63. "The async function encountered an error")};
  64. callback->Call(1, argv);
  65. }
  66. grpc::node::CompleteTag(event.tag);
  67. grpc::node::DestroyTag(event.tag);
  68. pending_batches--;
  69. if (pending_batches == 0) {
  70. uv_prepare_stop(&prepare);
  71. }
  72. }
  73. } while (event.type != GRPC_QUEUE_TIMEOUT);
  74. }
  75. grpc_completion_queue *GetCompletionQueue() {
  76. return queue;
  77. }
  78. void CompletionQueueNext() {
  79. if (pending_batches == 0) {
  80. GPR_ASSERT(!uv_is_active((uv_handle_t *)&prepare));
  81. uv_prepare_start(&prepare, drain_completion_queue);
  82. }
  83. pending_batches++;
  84. }
  85. void CompletionQueueInit(Local<Object> exports) {
  86. queue = grpc_completion_queue_create(NULL);
  87. uv_prepare_init(uv_default_loop(), &prepare);
  88. pending_batches = 0;
  89. }
  90. } // namespace node
  91. } // namespace grpc
  92. #endif /* GRPC_UV */