call.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef NET_GRPC_NODE_CALL_H_
  19. #define NET_GRPC_NODE_CALL_H_
  20. #include <memory>
  21. #include <vector>
  22. #include <nan.h>
  23. #include <node.h>
  24. #include "grpc/grpc.h"
  25. #include "grpc/support/log.h"
  26. #include "channel.h"
  27. namespace grpc {
  28. namespace node {
  29. using std::unique_ptr;
  30. using std::shared_ptr;
  31. v8::Local<v8::Value> nanErrorWithCode(const char *msg, grpc_call_error code);
  32. v8::Local<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array);
  33. bool CreateMetadataArray(v8::Local<v8::Object> metadata,
  34. grpc_metadata_array *array);
  35. void DestroyMetadataArray(grpc_metadata_array *array);
  36. /* Wrapper class for grpc_call structs. */
  37. class Call : public Nan::ObjectWrap {
  38. public:
  39. static void Init(v8::Local<v8::Object> exports);
  40. static bool HasInstance(v8::Local<v8::Value> val);
  41. /* Wrap a grpc_call struct in a javascript object */
  42. static v8::Local<v8::Value> WrapStruct(grpc_call *call);
  43. void CompleteBatch(bool is_final_op);
  44. private:
  45. explicit Call(grpc_call *call);
  46. ~Call();
  47. // Prevent copying
  48. Call(const Call &);
  49. Call &operator=(const Call &);
  50. void DestroyCall();
  51. static NAN_METHOD(New);
  52. static NAN_METHOD(StartBatch);
  53. static NAN_METHOD(Cancel);
  54. static NAN_METHOD(CancelWithStatus);
  55. static NAN_METHOD(GetPeer);
  56. static NAN_METHOD(SetCredentials);
  57. static Nan::Callback *constructor;
  58. // Used for typechecking instances of this javascript class
  59. static Nan::Persistent<v8::FunctionTemplate> fun_tpl;
  60. grpc_call *wrapped_call;
  61. // The number of ops that were started but not completed on this call
  62. int pending_batches;
  63. /* Indicates whether the "final" op on a call has completed. For a client
  64. call, this is GRPC_OP_RECV_STATUS_ON_CLIENT and for a server call, this
  65. is GRPC_OP_SEND_STATUS_FROM_SERVER */
  66. bool has_final_op_completed;
  67. char *peer;
  68. };
  69. class Op {
  70. public:
  71. virtual v8::Local<v8::Value> GetNodeValue() const = 0;
  72. virtual bool ParseOp(v8::Local<v8::Value> value, grpc_op *out) = 0;
  73. virtual ~Op();
  74. v8::Local<v8::Value> GetOpType() const;
  75. virtual bool IsFinalOp() = 0;
  76. virtual void OnComplete(bool success) = 0;
  77. protected:
  78. virtual std::string GetTypeString() const = 0;
  79. };
  80. typedef std::vector<unique_ptr<Op>> OpVec;
  81. struct tag {
  82. tag(Nan::Callback *callback, OpVec *ops, Call *call,
  83. v8::Local<v8::Value> call_value);
  84. ~tag();
  85. Nan::Callback *callback;
  86. OpVec *ops;
  87. Call *call;
  88. Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>>
  89. call_persist;
  90. };
  91. void DestroyTag(void *tag);
  92. void CompleteTag(void *tag, const char *error_message);
  93. } // namespace node
  94. } // namespace grpc
  95. #endif // NET_GRPC_NODE_CALL_H_