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" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 44 | #include "grpc/grpc.h" |
| 45 | #include "grpc/grpc_security.h" |
murgatroid99 | 7765906 | 2015-02-11 09:26:25 -0800 | [diff] [blame] | 46 | #include "grpc/support/log.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 47 | #include "server_credentials.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 48 | #include "slice.h" |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 49 | #include "timeval.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 50 | |
| 51 | namespace grpc { |
| 52 | namespace node { |
| 53 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 54 | using Nan::Callback; |
| 55 | using Nan::EscapableHandleScope; |
| 56 | using Nan::HandleScope; |
| 57 | using Nan::Maybe; |
| 58 | using Nan::MaybeLocal; |
| 59 | using Nan::ObjectWrap; |
| 60 | using Nan::Persistent; |
| 61 | using Nan::Utf8String; |
| 62 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 63 | using std::unique_ptr; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 64 | using v8::Array; |
| 65 | using v8::Boolean; |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 66 | using v8::Date; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 67 | using v8::Exception; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 68 | using v8::External; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 69 | using v8::Function; |
| 70 | using v8::FunctionTemplate; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 71 | using v8::Local; |
| 72 | using v8::Number; |
| 73 | using v8::Object; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 74 | using v8::String; |
| 75 | using v8::Value; |
| 76 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 77 | Nan::Callback *Server::constructor; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 78 | Persistent<FunctionTemplate> Server::fun_tpl; |
| 79 | |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 80 | static Callback *shutdown_callback = NULL; |
| 81 | |
| 82 | class ServerShutdownOp : public Op { |
| 83 | public: |
murgatroid99 | ea87dfb | 2017-04-25 12:51:40 -0700 | [diff] [blame] | 84 | ServerShutdownOp(grpc_server *server) : server(server) {} |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 85 | |
murgatroid99 | ea87dfb | 2017-04-25 12:51:40 -0700 | [diff] [blame] | 86 | ~ServerShutdownOp() {} |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 87 | |
murgatroid99 | ea87dfb | 2017-04-25 12:51:40 -0700 | [diff] [blame] | 88 | Local<Value> GetNodeValue() const { return Nan::Null(); } |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 89 | |
murgatroid99 | ea87dfb | 2017-04-25 12:51:40 -0700 | [diff] [blame] | 90 | bool ParseOp(Local<Value> value, grpc_op *out) { return true; } |
| 91 | bool IsFinalOp() { return false; } |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 92 | void OnComplete(bool success) { |
| 93 | /* Because cancel_all_calls was called, we assume that shutdown_and_notify |
| 94 | completes successfully */ |
| 95 | grpc_server_destroy(server); |
| 96 | } |
| 97 | |
| 98 | grpc_server *server; |
| 99 | |
| 100 | protected: |
| 101 | std::string GetTypeString() const { return "shutdown"; } |
| 102 | }; |
| 103 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 104 | class NewCallOp : public Op { |
| 105 | public: |
| 106 | NewCallOp() { |
| 107 | call = NULL; |
| 108 | grpc_call_details_init(&details); |
| 109 | grpc_metadata_array_init(&request_metadata); |
| 110 | } |
| 111 | |
| 112 | ~NewCallOp() { |
| 113 | grpc_call_details_destroy(&details); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 114 | grpc_metadata_array_destroy(&request_metadata); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 115 | } |
| 116 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 117 | Local<Value> GetNodeValue() const { |
| 118 | Nan::EscapableHandleScope scope; |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 119 | if (call == NULL) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 120 | return scope.Escape(Nan::Null()); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 121 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 122 | Local<Object> obj = Nan::New<Object>(); |
| 123 | Nan::Set(obj, Nan::New("call").ToLocalChecked(), Call::WrapStruct(call)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 124 | // TODO(murgatroid99): Use zero-copy string construction instead |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 125 | Nan::Set(obj, Nan::New("method").ToLocalChecked(), |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 126 | CopyStringFromSlice(details.method)); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 127 | Nan::Set(obj, Nan::New("host").ToLocalChecked(), |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 128 | CopyStringFromSlice(details.host)); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 129 | Nan::Set(obj, Nan::New("deadline").ToLocalChecked(), |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 130 | Nan::New<Date>(TimespecToMilliseconds(details.deadline)) |
| 131 | .ToLocalChecked()); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 132 | Nan::Set(obj, Nan::New("metadata").ToLocalChecked(), |
| 133 | ParseMetadata(&request_metadata)); |
| 134 | return scope.Escape(obj); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 137 | bool ParseOp(Local<Value> value, grpc_op *out) { return true; } |
| 138 | bool IsFinalOp() { return false; } |
| 139 | void OnComplete(bool success) {} |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 140 | |
| 141 | grpc_call *call; |
| 142 | grpc_call_details details; |
| 143 | grpc_metadata_array request_metadata; |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 144 | |
| 145 | protected: |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 146 | std::string GetTypeString() const { return "new_call"; } |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 147 | }; |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 148 | |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 149 | class TryShutdownOp : public Op { |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 150 | public: |
| 151 | TryShutdownOp(Server *server, Local<Value> server_value) : server(server) { |
| 152 | server_persist.Reset(server_value); |
| 153 | } |
| 154 | Local<Value> GetNodeValue() const { |
| 155 | EscapableHandleScope scope; |
| 156 | return scope.Escape(Nan::New(server_persist)); |
| 157 | } |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 158 | bool ParseOp(Local<Value> value, grpc_op *out) { return true; } |
| 159 | bool IsFinalOp() { return false; } |
murgatroid99 | 017a335 | 2017-04-11 14:34:04 -0700 | [diff] [blame] | 160 | void OnComplete(bool success) { |
| 161 | if (success) { |
| 162 | server->DestroyWrappedServer(); |
| 163 | } |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 164 | } |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 165 | |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 166 | protected: |
| 167 | std::string GetTypeString() const { return "try_shutdown"; } |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 168 | |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 169 | private: |
| 170 | Server *server; |
| 171 | Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>> |
| 172 | server_persist; |
| 173 | }; |
| 174 | |
murgatroid99 | ea87dfb | 2017-04-25 12:51:40 -0700 | [diff] [blame] | 175 | Server::Server(grpc_server *server) : wrapped_server(server) {} |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 176 | |
murgatroid99 | ea87dfb | 2017-04-25 12:51:40 -0700 | [diff] [blame] | 177 | Server::~Server() { this->ShutdownServer(); } |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 178 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 179 | void Server::Init(Local<Object> exports) { |
| 180 | HandleScope scope; |
| 181 | Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New); |
| 182 | tpl->SetClassName(Nan::New("Server").ToLocalChecked()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 183 | tpl->InstanceTemplate()->SetInternalFieldCount(1); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 184 | Nan::SetPrototypeMethod(tpl, "requestCall", RequestCall); |
| 185 | Nan::SetPrototypeMethod(tpl, "addHttp2Port", AddHttp2Port); |
| 186 | Nan::SetPrototypeMethod(tpl, "start", Start); |
| 187 | Nan::SetPrototypeMethod(tpl, "tryShutdown", TryShutdown); |
| 188 | Nan::SetPrototypeMethod(tpl, "forceShutdown", ForceShutdown); |
| 189 | fun_tpl.Reset(tpl); |
| 190 | Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked(); |
| 191 | Nan::Set(exports, Nan::New("Server").ToLocalChecked(), ctr); |
| 192 | constructor = new Callback(ctr); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 193 | } |
| 194 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 195 | bool Server::HasInstance(Local<Value> val) { |
| 196 | HandleScope scope; |
| 197 | return Nan::New(fun_tpl)->HasInstance(val); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 198 | } |
| 199 | |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 200 | void Server::DestroyWrappedServer() { |
| 201 | if (this->wrapped_server != NULL) { |
| 202 | grpc_server_destroy(this->wrapped_server); |
| 203 | this->wrapped_server = NULL; |
| 204 | } |
| 205 | } |
| 206 | |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 207 | NAN_METHOD(ServerShutdownCallback) { |
| 208 | if (!info[0]->IsNull()) { |
| 209 | return Nan::ThrowError("forceShutdown failed somehow"); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void Server::ShutdownServer() { |
| 214 | Nan::HandleScope scope; |
| 215 | if (this->wrapped_server != NULL) { |
| 216 | if (shutdown_callback == NULL) { |
murgatroid99 | ea87dfb | 2017-04-25 12:51:40 -0700 | [diff] [blame] | 217 | Local<FunctionTemplate> callback_tpl = |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 218 | Nan::New<FunctionTemplate>(ServerShutdownCallback); |
murgatroid99 | ea87dfb | 2017-04-25 12:51:40 -0700 | [diff] [blame] | 219 | shutdown_callback = |
| 220 | new Callback(Nan::GetFunction(callback_tpl).ToLocalChecked()); |
murgatroid99 | f94f64f | 2017-04-24 13:35:21 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | ServerShutdownOp *op = new ServerShutdownOp(this->wrapped_server); |
| 224 | unique_ptr<OpVec> ops(new OpVec()); |
| 225 | ops->push_back(unique_ptr<Op>(op)); |
| 226 | |
| 227 | grpc_server_shutdown_and_notify( |
| 228 | this->wrapped_server, GetCompletionQueue(), |
| 229 | new struct tag(new Callback(**shutdown_callback), ops.release(), NULL, |
| 230 | Nan::Null())); |
| 231 | grpc_server_cancel_all_calls(this->wrapped_server); |
| 232 | CompletionQueueNext(); |
| 233 | this->wrapped_server = NULL; |
| 234 | } |
| 235 | } |
| 236 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 237 | NAN_METHOD(Server::New) { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 238 | /* If this is not a constructor call, make a constructor call and return |
| 239 | the result */ |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 240 | if (!info.IsConstructCall()) { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 241 | const int argc = 1; |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 242 | Local<Value> argv[argc] = {info[0]}; |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 243 | MaybeLocal<Object> maybe_instance = |
murgatroid99 | c0a64cd | 2016-08-15 13:14:16 -0700 | [diff] [blame] | 244 | Nan::NewInstance(constructor->GetFunction(), argc, argv); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 245 | if (maybe_instance.IsEmpty()) { |
| 246 | // There's probably a pending exception |
| 247 | return; |
| 248 | } else { |
| 249 | info.GetReturnValue().Set(maybe_instance.ToLocalChecked()); |
| 250 | return; |
| 251 | } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 252 | } |
| 253 | grpc_server *wrapped_server; |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 254 | grpc_completion_queue *queue = GetCompletionQueue(); |
murgatroid99 | 75a2bba | 2015-10-12 16:12:04 -0700 | [diff] [blame] | 255 | grpc_channel_args *channel_args; |
| 256 | if (!ParseChannelArgs(info[0], &channel_args)) { |
| 257 | DeallocateChannelArgs(channel_args); |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 258 | return Nan::ThrowTypeError( |
| 259 | "Server options must be an object with " |
| 260 | "string keys and integer or string values"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 261 | } |
murgatroid99 | 75a2bba | 2015-10-12 16:12:04 -0700 | [diff] [blame] | 262 | wrapped_server = grpc_server_create(channel_args, NULL); |
| 263 | DeallocateChannelArgs(channel_args); |
Nicolas "Pixel" Noble | 8e6bab5 | 2015-08-07 01:40:49 +0200 | [diff] [blame] | 264 | grpc_server_register_completion_queue(wrapped_server, queue, NULL); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 265 | Server *server = new Server(wrapped_server); |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 266 | server->Wrap(info.This()); |
| 267 | info.GetReturnValue().Set(info.This()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | NAN_METHOD(Server::RequestCall) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 271 | if (!HasInstance(info.This())) { |
| 272 | return Nan::ThrowTypeError("requestCall can only be called on a Server"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 273 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 274 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 275 | NewCallOp *op = new NewCallOp(); |
murgatroid99 | d3f9f9f | 2015-02-13 12:21:59 -0800 | [diff] [blame] | 276 | unique_ptr<OpVec> ops(new OpVec()); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 277 | ops->push_back(unique_ptr<Op>(op)); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 278 | grpc_call_error error = grpc_server_request_call( |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 279 | server->wrapped_server, &op->call, &op->details, &op->request_metadata, |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 280 | GetCompletionQueue(), GetCompletionQueue(), |
| 281 | new struct tag(new Callback(info[0].As<Function>()), ops.release(), NULL, |
| 282 | Nan::Null())); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 283 | if (error != GRPC_CALL_OK) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 284 | return Nan::ThrowError(nanErrorWithCode("requestCall failed", error)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 285 | } |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 286 | CompletionQueueNext(); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | NAN_METHOD(Server::AddHttp2Port) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 290 | if (!HasInstance(info.This())) { |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 291 | return Nan::ThrowTypeError("addHttp2Port can only be called on a Server"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 292 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 293 | if (!info[0]->IsString()) { |
| 294 | return Nan::ThrowTypeError( |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 295 | "addHttp2Port's first argument must be a String"); |
murgatroid99 | da02a67 | 2015-03-02 17:28:02 -0800 | [diff] [blame] | 296 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 297 | if (!ServerCredentials::HasInstance(info[1])) { |
| 298 | return Nan::ThrowTypeError( |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 299 | "addHttp2Port's second argument must be ServerCredentials"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 300 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 301 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 302 | ServerCredentials *creds_object = ObjectWrap::Unwrap<ServerCredentials>( |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 303 | Nan::To<Object>(info[1]).ToLocalChecked()); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 304 | grpc_server_credentials *creds = creds_object->GetWrappedServerCredentials(); |
| 305 | int port; |
| 306 | if (creds == NULL) { |
Craig Tiller | c5ae3eb | 2015-08-03 10:42:22 -0700 | [diff] [blame] | 307 | port = grpc_server_add_insecure_http2_port(server->wrapped_server, |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 308 | *Utf8String(info[0])); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 309 | } else { |
| 310 | port = grpc_server_add_secure_http2_port(server->wrapped_server, |
Craig Tiller | 325a059 | 2016-05-25 06:31:12 -0700 | [diff] [blame] | 311 | *Utf8String(info[0]), creds); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 312 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 313 | info.GetReturnValue().Set(Nan::New<Number>(port)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | NAN_METHOD(Server::Start) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 317 | Nan::HandleScope scope; |
| 318 | if (!HasInstance(info.This())) { |
| 319 | return Nan::ThrowTypeError("start can only be called on a Server"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 320 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 321 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 322 | grpc_server_start(server->wrapped_server); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 323 | } |
| 324 | |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 325 | NAN_METHOD(Server::TryShutdown) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 326 | Nan::HandleScope scope; |
| 327 | if (!HasInstance(info.This())) { |
| 328 | return Nan::ThrowTypeError("tryShutdown can only be called on a Server"); |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 329 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 330 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | 803392e | 2017-04-11 12:13:08 -0700 | [diff] [blame] | 331 | if (server->wrapped_server == NULL) { |
| 332 | // Server is already shut down. Call callback immediately. |
| 333 | Nan::Callback callback(info[0].As<Function>()); |
| 334 | callback.Call(0, {}); |
| 335 | return; |
| 336 | } |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 337 | TryShutdownOp *op = new TryShutdownOp(server, info.This()); |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 338 | unique_ptr<OpVec> ops(new OpVec()); |
murgatroid99 | 42cfaa9 | 2017-04-10 15:43:09 -0700 | [diff] [blame] | 339 | ops->push_back(unique_ptr<Op>(op)); |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 340 | grpc_server_shutdown_and_notify( |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 341 | server->wrapped_server, GetCompletionQueue(), |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 342 | new struct tag(new Nan::Callback(info[0].As<Function>()), ops.release(), |
murgatroid99 | 130568e | 2017-04-04 13:43:49 -0700 | [diff] [blame] | 343 | NULL, Nan::Null())); |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 344 | CompletionQueueNext(); |
murgatroid99 | cd35c4c | 2015-05-28 13:39:25 -0700 | [diff] [blame] | 345 | } |
| 346 | |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 347 | NAN_METHOD(Server::ForceShutdown) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 348 | Nan::HandleScope scope; |
| 349 | if (!HasInstance(info.This())) { |
| 350 | return Nan::ThrowTypeError("forceShutdown can only be called on a Server"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 351 | } |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 352 | Server *server = ObjectWrap::Unwrap<Server>(info.This()); |
murgatroid99 | cd35c4c | 2015-05-28 13:39:25 -0700 | [diff] [blame] | 353 | server->ShutdownServer(); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | } // namespace node |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 357 | } // namespace grpc |