1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | #ifndef NET_GRPC_NODE_CALL_H_
|
20 | #define NET_GRPC_NODE_CALL_H_
|
21 |
|
22 | #include <memory>
|
23 | #include <vector>
|
24 |
|
25 | #include <nan.h>
|
26 | #include <node.h>
|
27 | #include "grpc/grpc.h"
|
28 | #include "grpc/support/log.h"
|
29 |
|
30 | #include "channel.h"
|
31 |
|
32 | namespace grpc {
|
33 | namespace node {
|
34 |
|
35 | using std::unique_ptr;
|
36 | using std::shared_ptr;
|
37 |
|
38 | v8::Local<v8::Value> nanErrorWithCode(const char *msg, grpc_call_error code);
|
39 |
|
40 | v8::Local<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array);
|
41 |
|
42 | bool CreateMetadataArray(v8::Local<v8::Object> metadata,
|
43 | grpc_metadata_array *array);
|
44 |
|
45 | void DestroyMetadataArray(grpc_metadata_array *array);
|
46 |
|
47 |
|
48 | class Call : public Nan::ObjectWrap {
|
49 | public:
|
50 | static void Init(v8::Local<v8::Object> exports);
|
51 | static bool HasInstance(v8::Local<v8::Value> val);
|
52 |
|
53 | static v8::Local<v8::Value> WrapStruct(grpc_call *call);
|
54 |
|
55 | grpc_call *GetWrappedCall();
|
56 |
|
57 | void CompleteBatch(bool is_final_op);
|
58 |
|
59 | private:
|
60 | explicit Call(grpc_call *call);
|
61 | ~Call();
|
62 |
|
63 |
|
64 | Call(const Call &);
|
65 | Call &operator=(const Call &);
|
66 |
|
67 | void DestroyCall();
|
68 |
|
69 | static NAN_METHOD(New);
|
70 | static NAN_METHOD(StartBatch);
|
71 | static NAN_METHOD(Cancel);
|
72 | static NAN_METHOD(CancelWithStatus);
|
73 | static NAN_METHOD(GetPeer);
|
74 | static NAN_METHOD(SetCredentials);
|
75 | static Nan::Callback *constructor;
|
76 |
|
77 | static Nan::Persistent<v8::FunctionTemplate> fun_tpl;
|
78 |
|
79 | grpc_call *wrapped_call;
|
80 |
|
81 | int pending_batches;
|
82 | |
83 |
|
84 |
|
85 | bool has_final_op_completed;
|
86 | char *peer;
|
87 | };
|
88 |
|
89 | class Op {
|
90 | public:
|
91 | virtual v8::Local<v8::Value> GetNodeValue() const = 0;
|
92 | virtual bool ParseOp(v8::Local<v8::Value> value, grpc_op *out) = 0;
|
93 | virtual ~Op();
|
94 | v8::Local<v8::Value> GetOpType() const;
|
95 | virtual bool IsFinalOp() = 0;
|
96 | virtual void OnComplete(bool success) = 0;
|
97 |
|
98 | protected:
|
99 | virtual std::string GetTypeString() const = 0;
|
100 | };
|
101 |
|
102 | typedef std::vector<unique_ptr<Op>> OpVec;
|
103 | struct tag {
|
104 | tag(Nan::Callback *callback, OpVec *ops, Call *call,
|
105 | v8::Local<v8::Value> call_value);
|
106 | ~tag();
|
107 | Nan::Callback *callback;
|
108 | Nan::AsyncResource *async_resource;
|
109 | OpVec *ops;
|
110 | Call *call;
|
111 | Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>>
|
112 | call_persist;
|
113 | };
|
114 |
|
115 | void DestroyTag(void *tag);
|
116 |
|
117 | void CompleteTag(void *tag, const char *error_message);
|
118 |
|
119 | }
|
120 | }
|
121 |
|
122 | #endif
|