call.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. *
  3. * Copyright 2015, 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. #ifndef NET_GRPC_NODE_CALL_H_
  34. #define NET_GRPC_NODE_CALL_H_
  35. #include <memory>
  36. #include <vector>
  37. #include <node.h>
  38. #include <nan.h>
  39. #include "grpc/grpc.h"
  40. #include "grpc/support/log.h"
  41. #include "channel.h"
  42. namespace grpc {
  43. namespace node {
  44. using std::unique_ptr;
  45. using std::shared_ptr;
  46. v8::Handle<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array);
  47. class PersistentHolder {
  48. public:
  49. explicit PersistentHolder(v8::Persistent<v8::Value> *persist) :
  50. persist(persist) {
  51. }
  52. ~PersistentHolder() {
  53. NanDisposePersistent(*persist);
  54. delete persist;
  55. }
  56. private:
  57. v8::Persistent<v8::Value> *persist;
  58. };
  59. struct Resources {
  60. std::vector<unique_ptr<NanUtf8String> > strings;
  61. std::vector<unique_ptr<PersistentHolder> > handles;
  62. };
  63. class Op {
  64. public:
  65. virtual v8::Handle<v8::Value> GetNodeValue() const = 0;
  66. virtual bool ParseOp(v8::Handle<v8::Value> value, grpc_op *out,
  67. shared_ptr<Resources> resources) = 0;
  68. v8::Handle<v8::Value> GetOpType() const;
  69. protected:
  70. virtual std::string GetTypeString() const = 0;
  71. };
  72. typedef std::vector<unique_ptr<Op>> OpVec;
  73. struct tag {
  74. tag(NanCallback *callback, OpVec *ops,
  75. shared_ptr<Resources> resources);
  76. ~tag();
  77. NanCallback *callback;
  78. OpVec *ops;
  79. shared_ptr<Resources> resources;
  80. };
  81. v8::Handle<v8::Value> GetTagNodeValue(void *tag);
  82. NanCallback *GetTagCallback(void *tag);
  83. void DestroyTag(void *tag);
  84. /* Wrapper class for grpc_call structs. */
  85. class Call : public ::node::ObjectWrap {
  86. public:
  87. static void Init(v8::Handle<v8::Object> exports);
  88. static bool HasInstance(v8::Handle<v8::Value> val);
  89. /* Wrap a grpc_call struct in a javascript object */
  90. static v8::Handle<v8::Value> WrapStruct(grpc_call *call);
  91. private:
  92. explicit Call(grpc_call *call);
  93. ~Call();
  94. // Prevent copying
  95. Call(const Call &);
  96. Call &operator=(const Call &);
  97. static NAN_METHOD(New);
  98. static NAN_METHOD(StartBatch);
  99. static NAN_METHOD(Cancel);
  100. static NanCallback *constructor;
  101. // Used for typechecking instances of this javascript class
  102. static v8::Persistent<v8::FunctionTemplate> fun_tpl;
  103. grpc_call *wrapped_call;
  104. };
  105. } // namespace node
  106. } // namespace grpc
  107. #endif // NET_GRPC_NODE_CALL_H_