OLD | NEW |
(Empty) | |
| 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 |
| 34 #ifndef NET_GRPC_NODE_CALL_H_ |
| 35 #define NET_GRPC_NODE_CALL_H_ |
| 36 |
| 37 #include <memory> |
| 38 #include <vector> |
| 39 |
| 40 #include <node.h> |
| 41 #include <nan.h> |
| 42 #include "grpc/grpc.h" |
| 43 #include "grpc/support/log.h" |
| 44 |
| 45 #include "channel.h" |
| 46 |
| 47 |
| 48 namespace grpc { |
| 49 namespace node { |
| 50 |
| 51 using std::unique_ptr; |
| 52 using std::shared_ptr; |
| 53 |
| 54 typedef Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>> Per
sistentValue; |
| 55 |
| 56 v8::Local<v8::Value> nanErrorWithCode(const char *msg, grpc_call_error code); |
| 57 |
| 58 v8::Local<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array); |
| 59 |
| 60 struct Resources { |
| 61 std::vector<unique_ptr<Nan::Utf8String> > strings; |
| 62 std::vector<unique_ptr<PersistentValue> > handles; |
| 63 }; |
| 64 |
| 65 bool CreateMetadataArray(v8::Local<v8::Object> metadata, |
| 66 grpc_metadata_array *array, |
| 67 shared_ptr<Resources> resources); |
| 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, |
| 73 shared_ptr<Resources> resources) = 0; |
| 74 virtual ~Op(); |
| 75 v8::Local<v8::Value> GetOpType() const; |
| 76 |
| 77 protected: |
| 78 virtual std::string GetTypeString() const = 0; |
| 79 }; |
| 80 |
| 81 typedef std::vector<unique_ptr<Op>> OpVec; |
| 82 struct tag { |
| 83 tag(Nan::Callback *callback, OpVec *ops, |
| 84 shared_ptr<Resources> resources); |
| 85 ~tag(); |
| 86 Nan::Callback *callback; |
| 87 OpVec *ops; |
| 88 shared_ptr<Resources> resources; |
| 89 }; |
| 90 |
| 91 v8::Local<v8::Value> GetTagNodeValue(void *tag); |
| 92 |
| 93 Nan::Callback *GetTagCallback(void *tag); |
| 94 |
| 95 void DestroyTag(void *tag); |
| 96 |
| 97 /* Wrapper class for grpc_call structs. */ |
| 98 class Call : public Nan::ObjectWrap { |
| 99 public: |
| 100 static void Init(v8::Local<v8::Object> exports); |
| 101 static bool HasInstance(v8::Local<v8::Value> val); |
| 102 /* Wrap a grpc_call struct in a javascript object */ |
| 103 static v8::Local<v8::Value> WrapStruct(grpc_call *call); |
| 104 |
| 105 private: |
| 106 explicit Call(grpc_call *call); |
| 107 ~Call(); |
| 108 |
| 109 // Prevent copying |
| 110 Call(const Call &); |
| 111 Call &operator=(const Call &); |
| 112 |
| 113 static NAN_METHOD(New); |
| 114 static NAN_METHOD(StartBatch); |
| 115 static NAN_METHOD(Cancel); |
| 116 static NAN_METHOD(CancelWithStatus); |
| 117 static NAN_METHOD(GetPeer); |
| 118 static NAN_METHOD(SetCredentials); |
| 119 static Nan::Callback *constructor; |
| 120 // Used for typechecking instances of this javascript class |
| 121 static Nan::Persistent<v8::FunctionTemplate> fun_tpl; |
| 122 |
| 123 grpc_call *wrapped_call; |
| 124 }; |
| 125 |
| 126 } // namespace node |
| 127 } // namespace grpc |
| 128 |
| 129 #endif // NET_GRPC_NODE_CALL_H_ |
OLD | NEW |