Add native tag completion callbacks, dispose of server after tryShutdown succeeds

This commit is contained in:
murgatroid99 2017-04-10 15:43:09 -07:00
parent 25d8d5b609
commit da129b2b4a
5 changed files with 63 additions and 3 deletions

View File

@ -217,6 +217,8 @@ class SendMetadataOp : public Op {
bool IsFinalOp() {
return false;
}
void OnComplete() {
}
protected:
std::string GetTypeString() const {
return "send_metadata";
@ -260,6 +262,8 @@ class SendMessageOp : public Op {
bool IsFinalOp() {
return false;
}
void OnComplete() {
}
protected:
std::string GetTypeString() const {
return "send_message";
@ -280,6 +284,8 @@ class SendClientCloseOp : public Op {
bool IsFinalOp() {
return false;
}
void OnComplete() {
}
protected:
std::string GetTypeString() const {
return "client_close";
@ -349,6 +355,8 @@ class SendServerStatusOp : public Op {
bool IsFinalOp() {
return true;
}
void OnComplete() {
}
protected:
std::string GetTypeString() const {
return "send_status";
@ -381,6 +389,8 @@ class GetMetadataOp : public Op {
bool IsFinalOp() {
return false;
}
void OnComplete() {
}
protected:
std::string GetTypeString() const {
@ -413,6 +423,8 @@ class ReadMessageOp : public Op {
bool IsFinalOp() {
return false;
}
void OnComplete() {
}
protected:
std::string GetTypeString() const {
@ -454,6 +466,8 @@ class ClientStatusOp : public Op {
bool IsFinalOp() {
return true;
}
void OnComplete() {
}
protected:
std::string GetTypeString() const {
return "status";
@ -478,6 +492,8 @@ class ServerCloseResponseOp : public Op {
bool IsFinalOp() {
return false;
}
void OnComplete() {
}
protected:
std::string GetTypeString() const {
@ -517,16 +533,17 @@ void CompleteTag(void *tag, const char *error_message) {
callback->Call(1, argv);
}
bool is_final_op = false;
if (tag_struct->call == NULL) {
return;
}
for (vector<unique_ptr<Op> >::iterator it = tag_struct->ops->begin();
it != tag_struct->ops->end(); ++it) {
Op *op_ptr = it->get();
op_ptr->OnComplete();
if (op_ptr->IsFinalOp()) {
is_final_op = true;
}
}
if (tag_struct->call == NULL) {
return;
}
tag_struct->call->CompleteBatch(is_final_op);
}

View File

@ -106,6 +106,7 @@ class Op {
virtual ~Op();
v8::Local<v8::Value> GetOpType() const;
virtual bool IsFinalOp() = 0;
virtual void OnComplete() = 0;
protected:
virtual std::string GetTypeString() const = 0;

View File

@ -117,6 +117,8 @@ class NewCallOp : public Op {
bool IsFinalOp() {
return false;
}
void OnComplete() {
}
grpc_call *call;
grpc_call_details details;
@ -126,6 +128,32 @@ class NewCallOp : public Op {
std::string GetTypeString() const { return "new_call"; }
};
class TryShutdownOp: public Op {
public:
TryShutdownOp(Server *server, Local<Value> server_value) : server(server) {
server_persist.Reset(server_value);
}
Local<Value> GetNodeValue() const {
EscapableHandleScope scope;
return scope.Escape(Nan::New(server_persist));
}
bool ParseOp(Local<Value> value, grpc_op *out) {
return true;
}
bool IsFinalOp() {
return false;
}
void OnComplete() {
server->DestroyWrappedServer();
}
protected:
std::string GetTypeString() const { return "try_shutdown"; }
private:
Server *server;
Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>>
server_persist;
};
void Server::Init(Local<Object> exports) {
HandleScope scope;
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
@ -147,6 +175,13 @@ bool Server::HasInstance(Local<Value> val) {
return Nan::New(fun_tpl)->HasInstance(val);
}
void Server::DestroyWrappedServer() {
if (this->wrapped_server != NULL) {
grpc_server_destroy(this->wrapped_server);
this->wrapped_server = NULL;
}
}
NAN_METHOD(Server::New) {
/* If this is not a constructor call, make a constructor call and return
the result */
@ -242,7 +277,9 @@ NAN_METHOD(Server::TryShutdown) {
return Nan::ThrowTypeError("tryShutdown can only be called on a Server");
}
Server *server = ObjectWrap::Unwrap<Server>(info.This());
TryShutdownOp *op = new TryShutdownOp(server, info.This());
unique_ptr<OpVec> ops(new OpVec());
ops->push_back(unique_ptr<Op>(op));
grpc_server_shutdown_and_notify(
server->wrapped_server, GetCompletionQueue(),
new struct tag(new Nan::Callback(info[0].As<Function>()), ops.release(),

View File

@ -53,6 +53,8 @@ class Server : public Nan::ObjectWrap {
JavaScript constructor */
static bool HasInstance(v8::Local<v8::Value> val);
void DestroyWrappedServer();
private:
explicit Server(grpc_server *server);
~Server();

View File

@ -76,6 +76,8 @@ class ServerShutdownOp : public Op {
bool IsFinalOp() {
return false;
}
void OnComplete() {
}
grpc_server *server;
@ -104,6 +106,7 @@ NAN_METHOD(ServerShutdownCallback) {
}
void Server::ShutdownServer() {
Nan::HandleScope scope;
if (this->wrapped_server != NULL) {
if (shutdown_callback == NULL) {
Local<FunctionTemplate>callback_tpl =