OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 34 #include <string.h> |
| 35 |
| 36 #include <node.h> |
| 37 #include <nan.h> |
| 38 #include "grpc/grpc.h" |
| 39 #include "grpc/byte_buffer_reader.h" |
| 40 #include "grpc/support/slice.h" |
| 41 |
| 42 #include "byte_buffer.h" |
| 43 |
| 44 namespace grpc { |
| 45 namespace node { |
| 46 |
| 47 |
| 48 using v8::Context; |
| 49 using v8::Function; |
| 50 using v8::Local; |
| 51 using v8::Object; |
| 52 using v8::Number; |
| 53 using v8::Value; |
| 54 |
| 55 grpc_byte_buffer *BufferToByteBuffer(Local<Value> buffer) { |
| 56 Nan::HandleScope scope; |
| 57 int length = ::node::Buffer::Length(buffer); |
| 58 char *data = ::node::Buffer::Data(buffer); |
| 59 gpr_slice slice = gpr_slice_malloc(length); |
| 60 memcpy(GPR_SLICE_START_PTR(slice), data, length); |
| 61 grpc_byte_buffer *byte_buffer(grpc_raw_byte_buffer_create(&slice, 1)); |
| 62 gpr_slice_unref(slice); |
| 63 return byte_buffer; |
| 64 } |
| 65 |
| 66 namespace { |
| 67 void delete_buffer(char *data, void *hint) { delete[] data; } |
| 68 } |
| 69 |
| 70 Local<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) { |
| 71 Nan::EscapableHandleScope scope; |
| 72 if (buffer == NULL) { |
| 73 return scope.Escape(Nan::Null()); |
| 74 } |
| 75 size_t length = grpc_byte_buffer_length(buffer); |
| 76 char *result = new char[length]; |
| 77 size_t offset = 0; |
| 78 grpc_byte_buffer_reader reader; |
| 79 grpc_byte_buffer_reader_init(&reader, buffer); |
| 80 gpr_slice next; |
| 81 while (grpc_byte_buffer_reader_next(&reader, &next) != 0) { |
| 82 memcpy(result + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); |
| 83 offset += GPR_SLICE_LENGTH(next); |
| 84 gpr_slice_unref(next); |
| 85 } |
| 86 return scope.Escape(MakeFastBuffer( |
| 87 Nan::NewBuffer(result, length, delete_buffer, NULL).ToLocalChecked())); |
| 88 } |
| 89 |
| 90 Local<Value> MakeFastBuffer(Local<Value> slowBuffer) { |
| 91 Nan::EscapableHandleScope scope; |
| 92 Local<Object> globalObj = Nan::GetCurrentContext()->Global(); |
| 93 Local<Function> bufferConstructor = Local<Function>::Cast( |
| 94 globalObj->Get(Nan::New("Buffer").ToLocalChecked())); |
| 95 Local<Value> consArgs[3] = { |
| 96 slowBuffer, |
| 97 Nan::New<Number>(::node::Buffer::Length(slowBuffer)), |
| 98 Nan::New<Number>(0) |
| 99 }; |
| 100 Local<Object> fastBuffer = bufferConstructor->NewInstance(3, consArgs); |
| 101 return scope.Escape(fastBuffer); |
| 102 } |
| 103 } // namespace node |
| 104 } // namespace grpc |
OLD | NEW |