UNPKG

3.34 kBtext/x-cView Raw
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
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
32namespace grpc {
33namespace node {
34
35using std::unique_ptr;
36using std::shared_ptr;
37
38v8::Local<v8::Value> nanErrorWithCode(const char *msg, grpc_call_error code);
39
40v8::Local<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array);
41
42bool CreateMetadataArray(v8::Local<v8::Object> metadata,
43 grpc_metadata_array *array);
44
45void DestroyMetadataArray(grpc_metadata_array *array);
46
47/* Wrapper class for grpc_call structs. */
48class 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 /* Wrap a grpc_call struct in a javascript object */
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 // Prevent copying
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 // Used for typechecking instances of this javascript class
77 static Nan::Persistent<v8::FunctionTemplate> fun_tpl;
78
79 grpc_call *wrapped_call;
80 // The number of ops that were started but not completed on this call
81 int pending_batches;
82 /* Indicates whether the "final" op on a call has completed. For a client
83 call, this is GRPC_OP_RECV_STATUS_ON_CLIENT and for a server call, this
84 is GRPC_OP_SEND_STATUS_FROM_SERVER */
85 bool has_final_op_completed;
86 char *peer;
87};
88
89class 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
102typedef std::vector<unique_ptr<Op>> OpVec;
103struct 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
115void DestroyTag(void *tag);
116
117void CompleteTag(void *tag, const char *error_message);
118
119} // namespace node
120} // namespace grpc
121
122#endif // NET_GRPC_NODE_CALL_H_