murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 34 | #include <memory> |
| 35 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 36 | #include "server.h" |
| 37 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 38 | #include <nan.h> |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 39 | #include <node.h> |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 40 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 41 | #include <vector> |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 42 | #include "call.h" |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 43 | #include "completion_queue.h" |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 44 | #include "completion_queue_async_worker.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 45 | #include "grpc/grpc.h" |
| 46 | #include "grpc/grpc_security.h" |
murgatroid99 | 7765906 | 2015-02-11 09:26:25 -0800 | [diff] [blame] | 47 | #include "grpc/support/log.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 48 | #include "server_credentials.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 49 | #include "slice.h" |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 50 | #include "timeval.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 51 | |
| 52 | namespace grpc { |
| 53 | namespace node { |
| 54 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 55 | using Nan::Callback; |
| 56 | using Nan::EscapableHandleScope; |
| 57 | using Nan::HandleScope; |
| 58 | using Nan::Maybe; |
| 59 | using Nan::MaybeLocal; |
| 60 | using Nan::ObjectWrap; |
| 61 | using Nan::Persistent; |
| 62 | using Nan::Utf8String; |
| 63 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 64 | using std::unique_ptr; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 65 | using v8::Array; |
| 66 | using v8::Boolean; |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 67 | using v8::Date; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 68 | using v8::Exception; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 69 | using v8::External; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 70 | using v8::Function; |
| 71 | using v8::FunctionTemplate; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 72 | using v8::Local; |
| 73 | using v8::Number; |
| 74 | using v8::Object; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 75 | using v8::String; |
| 76 | using v8::Value; |
| 77 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 78 | Nan::Callback *Server::constructor; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 79 | Persistent<FunctionTemplate> Server::fun_tpl; |
| 80 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 81 | class NewCallOp : public Op { |
| 82 | public: |
| 83 | NewCallOp() { |
| 84 | call = NULL; |
| 85 | grpc_call_details_init(&details); |
| 86 | grpc_metadata_array_init(&request_metadata); |
| 87 | } |
| 88 | |
| 89 | ~NewCallOp() { |
| 90 | grpc_call_details_destroy(&details); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 91 | grpc_metadata_array_destroy(&request_metadata); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 92 | } |
| 93 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 94 | Local<Value> GetNodeValue() const { |
| 95 | Nan::EscapableHandleScope scope; |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 96 | if (call == NULL) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 97 | return scope.Escape(Nan::Null()); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 98 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 99 | Local<Object> obj = Nan::New<Object>(); |
| 100 | Nan::Set(obj, Nan::New("call").ToLocalChecked(), Call::WrapStruct(call)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 101 | // TODO(murgatroid99): Use zero-copy string construction instead |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 102 | Nan::Set(obj, Nan::New("method").ToLocalChecked(), |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 103 | CopyStringFromSlice(details.method)); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 104 | Nan::Set(obj, Nan::New("host").ToLocalChecked(), |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 105 | CopyStringFromSlice(details.host)); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 106 | Nan::Set(obj, Nan::New("deadline").ToLocalChecked(), |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 107 | Nan::New<Date>(TimespecToMilliseconds(details.deadline)) |
| 108 | .ToLocalChecked()); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 109 | Nan::Set(obj, Nan::New("metadata").ToLocalChecked(), |
| 110 | ParseMetadata(&request_metadata)); |
| 111 | return scope.Escape(obj); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame^] | 114 | bool ParseOp(Local<Value> value, grpc_op *out) { return true; } |
| 115 | bool IsFinalOp() { return false; } |
| 116 | void OnComplete(bool success) {} |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 117 | |
| 118 | grpc_call *call; |
| 119 | grpc_call_details details; |
| 120 | grpc_metadata_array request_metadata; |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 121 | |
| 122 | protected: |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 123 | std::string GetTypeString() const { return "new_call"; } |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 124 | }; |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 125 | |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame^] | 126 | class TryShutdownOp : public Op { |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 127 | public: |
| 128 | TryShutdownOp(Server *server, Local<Value> server_value) : server(server) { |
| 129 | server_persist.Reset(server_value); |
| 130 | } |
| 131 | Local<Value> GetNodeValue() const { |
| 132 | EscapableHandleScope scope; |
| 133 | return scope.Escape(Nan::New(server_persist)); |
| 134 | } |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame^] | 135 | bool ParseOp(Local<Value> value, grpc_op *out) { return true; } |
| 136 | bool IsFinalOp() { return false; } |
murgatroid99 | 017a335 | 2017-04-11 14:34:04 -0700 | [diff] [blame] | 137 | void OnComplete(bool success) { |
| 138 | if (success) { |
| 139 | server->DestroyWrappedServer(); |
| 140 | } |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 141 | } |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame^] | 142 | |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 143 | protected: |
| 144 | std::string GetTypeString() const { return "try_shutdown"; } |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame^] | 145 | |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 146 | private: |
| 147 | Server *server; |
| 148 | Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>> |
| 149 | server_persist; |
| 150 | }; |
| 151 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 152 | void Server::Init(Local<Object> exports) { |
| 153 | HandleScope scope; |
| 154 | Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New); |
| 155 | tpl->SetClassName(Nan::New("Server").ToLocalChecked()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 156 | tpl->InstanceTemplate()->SetInternalFieldCount(1); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 157 | Nan::SetPrototypeMethod(tpl, "requestCall", RequestCall); |
| 158 | Nan::SetPrototypeMethod(tpl, "addHttp2Port", AddHttp2Port); |
| 159 | Nan::SetPrototypeMethod(tpl, "start", Start); |
| 160 | Nan::SetPrototypeMethod(tpl, "tryShutdown", TryShutdown); |
| 161 | Nan::SetPrototypeMethod(tpl, "forceShutdown", ForceShutdown); |
| 162 | fun_tpl.Reset(tpl); |
| 163 | Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked(); |
| 164 | Nan::Set(exports, Nan::New("Server").ToLocalChecked(), ctr); |
| 165 | constructor = new Callback(ctr); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 166 | } |
| 167 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 168 | bool Server::HasInstance(Local<Value> val) { |
| 169 | HandleScope scope; |
| 170 | return Nan::New(fun_tpl)->HasInstance(val); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 171 | } |
| 172 | |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 173 | void Server::DestroyWrappedServer() { |
| 174 | if (this->wrapped_server != NULL) { |
| 175 | grpc_server_destroy(this->wrapped_server); |
| 176 | this->wrapped_server = NULL; |
| 177 | } |
| 178 | } |
| 179 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 180 | NAN_METHOD(Server::New) { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 181 | /* If this is not a constructor call, make a constructor call and return |
| 182 | the result */ |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 183 | if (!info.IsConstructCall()) { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 184 | const int argc = 1; |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 185 | Local<Value> argv[argc] = {info[0]}; |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 186 | MaybeLocal<Object> maybe_instance = |
murgatroid99 | c0a64cd | 2016-08-15 13:14:16 -0700 | [diff] [blame] | 187 | Nan::NewInstance(constructor->GetFunction(), argc, argv); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 188 | if (maybe_instance.IsEmpty()) { |
| 189 | // There's probably a pending exception |
| 190 | return; |
| 191 | } else { |
| 192 | info.GetReturnValue().Set(maybe_instance.ToLocalChecked()); |
| 193 | return; |
| 194 | } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 195 | } |
| 196 | grpc_server *wrapped_server; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 197 | grpc_completion_queue *queue = GetCompletionQueue(); |
murgatroid99 | 75a2bba | 2015-10-12 16:12:04 -0700 | [diff] [blame] | 198 | grpc_channel_args *channel_args; |
| 199 | if (!ParseChannelArgs(info[0], &channel_args)) { |
| 200 | DeallocateChannelArgs(channel_args); |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 201 | return Nan::ThrowTypeError( |
| 202 | "Server options must be an object with " |
| 203 | "string keys and integer or string values"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 204 | } |
murgatroid99 | 75a2bba | 2015-10-12 16:12:04 -0700 | [diff] [blame] | 205 | wrapped_server = grpc_server_create(channel_args, NULL); |
| 206 | DeallocateChannelArgs(channel_args); |
Nicolas "Pixel" Noble | 8e6bab5 | 2015-08-07 01:40:49 +0200 | [diff] [blame] | 207 | grpc_server_register_completion_queue(wrapped_server, queue, NULL); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 208 | Server *server = new Server(wrapped_server); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 209 | server->Wrap(info.This()); |
| 210 | info.GetReturnValue().Set(info.This()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | NAN_METHOD(Server::RequestCall) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 214 | if (!HasInstance(info.This())) { |
| 215 | return Nan::ThrowTypeError("requestCall can only be called on a Server"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 216 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 217 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 218 | NewCallOp *op = new NewCallOp(); |
murgatroid99 | d3f9f9f | 2015-02-13 12:21:59 -0800 | [diff] [blame] | 219 | unique_ptr<OpVec> ops(new OpVec()); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 220 | ops->push_back(unique_ptr<Op>(op)); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 221 | grpc_call_error error = grpc_server_request_call( |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 222 | server->wrapped_server, &op->call, &op->details, &op->request_metadata, |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame^] | 223 | GetCompletionQueue(), GetCompletionQueue(), |
| 224 | new struct tag(new Callback(info[0].As<Function>()), ops.release(), NULL, |
| 225 | Nan::Null())); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 226 | if (error != GRPC_CALL_OK) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 227 | return Nan::ThrowError(nanErrorWithCode("requestCall failed", error)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 228 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 229 | CompletionQueueNext(); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | NAN_METHOD(Server::AddHttp2Port) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 233 | if (!HasInstance(info.This())) { |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 234 | return Nan::ThrowTypeError("addHttp2Port can only be called on a Server"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 235 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 236 | if (!info[0]->IsString()) { |
| 237 | return Nan::ThrowTypeError( |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 238 | "addHttp2Port's first argument must be a String"); |
murgatroid99 | da02a67 | 2015-03-02 17:28:02 -0800 | [diff] [blame] | 239 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 240 | if (!ServerCredentials::HasInstance(info[1])) { |
| 241 | return Nan::ThrowTypeError( |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 242 | "addHttp2Port's second argument must be ServerCredentials"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 243 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 244 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 245 | ServerCredentials *creds_object = ObjectWrap::Unwrap<ServerCredentials>( |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 246 | Nan::To<Object>(info[1]).ToLocalChecked()); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 247 | grpc_server_credentials *creds = creds_object->GetWrappedServerCredentials(); |
| 248 | int port; |
| 249 | if (creds == NULL) { |
Craig Tiller | c5ae3eb | 2015-08-03 10:42:22 -0700 | [diff] [blame] | 250 | port = grpc_server_add_insecure_http2_port(server->wrapped_server, |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 251 | *Utf8String(info[0])); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 252 | } else { |
| 253 | port = grpc_server_add_secure_http2_port(server->wrapped_server, |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 254 | *Utf8String(info[0]), creds); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 255 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 256 | info.GetReturnValue().Set(Nan::New<Number>(port)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | NAN_METHOD(Server::Start) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 260 | Nan::HandleScope scope; |
| 261 | if (!HasInstance(info.This())) { |
| 262 | return Nan::ThrowTypeError("start can only be called on a Server"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 263 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 264 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 265 | grpc_server_start(server->wrapped_server); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 266 | } |
| 267 | |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 268 | NAN_METHOD(Server::TryShutdown) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 269 | Nan::HandleScope scope; |
| 270 | if (!HasInstance(info.This())) { |
| 271 | return Nan::ThrowTypeError("tryShutdown can only be called on a Server"); |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 272 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 273 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | 803392e | 2017-04-11 12:13:08 -0700 | [diff] [blame] | 274 | if (server->wrapped_server == NULL) { |
| 275 | // Server is already shut down. Call callback immediately. |
| 276 | Nan::Callback callback(info[0].As<Function>()); |
| 277 | callback.Call(0, {}); |
| 278 | return; |
| 279 | } |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 280 | TryShutdownOp *op = new TryShutdownOp(server, info.This()); |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 281 | unique_ptr<OpVec> ops(new OpVec()); |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 282 | ops->push_back(unique_ptr<Op>(op)); |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 283 | grpc_server_shutdown_and_notify( |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 284 | server->wrapped_server, GetCompletionQueue(), |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 285 | new struct tag(new Nan::Callback(info[0].As<Function>()), ops.release(), |
murgatroid99 | 130568e | 2017-04-04 13:43:49 -0700 | [diff] [blame] | 286 | NULL, Nan::Null())); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 287 | CompletionQueueNext(); |
murgatroid99 | cd35c4c | 2015-05-28 13:39:25 -0700 | [diff] [blame] | 288 | } |
| 289 | |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 290 | NAN_METHOD(Server::ForceShutdown) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 291 | Nan::HandleScope scope; |
| 292 | if (!HasInstance(info.This())) { |
| 293 | return Nan::ThrowTypeError("forceShutdown can only be called on a Server"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 294 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 295 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | cd35c4c | 2015-05-28 13:39:25 -0700 | [diff] [blame] | 296 | server->ShutdownServer(); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | } // namespace node |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 300 | } // namespace grpc |