diff options
Diffstat (limited to 'src/node/ext/server.cc')
-rw-r--r-- | src/node/ext/server.cc | 65 |
1 files changed, 59 insertions, 6 deletions
diff --git a/src/node/ext/server.cc b/src/node/ext/server.cc index 6b8ccef9b1..ee3e1087ce 100644 --- a/src/node/ext/server.cc +++ b/src/node/ext/server.cc @@ -31,6 +31,8 @@ * */ +#include <memory> + #include "server.h" #include <node.h> @@ -41,17 +43,20 @@ #include <vector> #include "grpc/grpc.h" #include "grpc/grpc_security.h" +#include "grpc/support/log.h" #include "call.h" #include "completion_queue_async_worker.h" -#include "tag.h" #include "server_credentials.h" +#include "timeval.h" namespace grpc { namespace node { +using std::unique_ptr; using v8::Arguments; using v8::Array; using v8::Boolean; +using v8::Date; using v8::Exception; using v8::Function; using v8::FunctionTemplate; @@ -67,6 +72,49 @@ using v8::Value; Persistent<Function> Server::constructor; Persistent<FunctionTemplate> Server::fun_tpl; +class NewCallOp : public Op { + public: + NewCallOp() { + call = NULL; + grpc_call_details_init(&details); + grpc_metadata_array_init(&request_metadata); + } + + ~NewCallOp() { + grpc_call_details_destroy(&details); + grpc_metadata_array_destroy(&request_metadata); + } + + Handle<Value> GetNodeValue() const { + NanEscapableScope(); + if (call == NULL) { + return NanEscapeScope(NanNull()); + } + Handle<Object> obj = NanNew<Object>(); + obj->Set(NanNew("call"), Call::WrapStruct(call)); + obj->Set(NanNew("method"), NanNew(details.method)); + obj->Set(NanNew("host"), NanNew(details.host)); + obj->Set(NanNew("deadline"), + NanNew<Date>(TimespecToMilliseconds(details.deadline))); + obj->Set(NanNew("metadata"), ParseMetadata(&request_metadata)); + return NanEscapeScope(obj); + } + + bool ParseOp(Handle<Value> value, grpc_op *out, + shared_ptr<Resources> resources) { + return true; + } + + grpc_call *call; + grpc_call_details details; + grpc_metadata_array request_metadata; + + protected: + std::string GetTypeString() const { + return "new call"; + } +}; + Server::Server(grpc_server *server) : wrapped_server(server) {} Server::~Server() { grpc_server_destroy(wrapped_server); } @@ -175,13 +223,18 @@ NAN_METHOD(Server::RequestCall) { return NanThrowTypeError("requestCall can only be called on a Server"); } Server *server = ObjectWrap::Unwrap<Server>(args.This()); - grpc_call_error error = grpc_server_request_call_old( - server->wrapped_server, CreateTag(args[0], NanNull())); - if (error == GRPC_CALL_OK) { - CompletionQueueAsyncWorker::Next(); - } else { + NewCallOp *op = new NewCallOp(); + unique_ptr<OpVec> ops(new OpVec()); + ops->push_back(unique_ptr<Op>(op)); + grpc_call_error error = grpc_server_request_call( + server->wrapped_server, &op->call, &op->details, &op->request_metadata, + CompletionQueueAsyncWorker::GetQueue(), + new struct tag(new NanCallback(args[0].As<Function>()), ops.release(), + shared_ptr<Resources>(nullptr))); + if (error != GRPC_CALL_OK) { return NanThrowError("requestCall failed", error); } + CompletionQueueAsyncWorker::Next(); NanReturnUndefined(); } |