3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef NET_GRPC_NODE_CALL_H_
20 #define NET_GRPC_NODE_CALL_H_
27 #include "grpc/grpc.h"
28 #include "grpc/support/log.h"
35 using std::unique_ptr;
36 using std::shared_ptr;
38 v8::Local<v8::Value> nanErrorWithCode(const char *msg, grpc_call_error code);
40 v8::Local<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array);
42 bool CreateMetadataArray(v8::Local<v8::Object> metadata,
43 grpc_metadata_array *array);
45 void DestroyMetadataArray(grpc_metadata_array *array);
47 /* Wrapper class for grpc_call structs. */
48 class Call : public Nan::ObjectWrap {
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);
55 grpc_call *GetWrappedCall();
57 void CompleteBatch(bool is_final_op);
60 explicit Call(grpc_call *call);
65 Call &operator=(const Call &);
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;
79 grpc_call *wrapped_call;
80 // The number of ops that were started but not completed on this call
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;
91 virtual v8::Local<v8::Value> GetNodeValue() const = 0;
92 virtual bool ParseOp(v8::Local<v8::Value> value, grpc_op *out) = 0;
94 v8::Local<v8::Value> GetOpType() const;
95 virtual bool IsFinalOp() = 0;
96 virtual void OnComplete(bool success) = 0;
99 virtual std::string GetTypeString() const = 0;
102 typedef std::vector<unique_ptr<Op>> OpVec;
104 tag(Nan::Callback *callback, OpVec *ops, Call *call,
105 v8::Local<v8::Value> call_value);
107 Nan::Callback *callback;
108 Nan::AsyncResource *async_resource;
111 Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>>
115 void DestroyTag(void *tag);
117 void CompleteTag(void *tag, const char *error_message);
122 #endif // NET_GRPC_NODE_CALL_H_