Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, 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 191 : grpc_byte_buffer *BufferToByteBuffer(Local<Value> buffer) {
56 : Nan::HandleScope scope;
57 191 : int length = ::node::Buffer::Length(buffer);
58 191 : char *data = ::node::Buffer::Data(buffer);
59 191 : gpr_slice slice = gpr_slice_malloc(length);
60 191 : memcpy(GPR_SLICE_START_PTR(slice), data, length);
61 191 : grpc_byte_buffer *byte_buffer(grpc_raw_byte_buffer_create(&slice, 1));
62 191 : gpr_slice_unref(slice);
63 382 : return byte_buffer;
64 : }
65 :
66 254 : Local<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) {
67 : Nan::EscapableHandleScope scope;
68 254 : if (buffer == NULL) {
69 69 : return scope.Escape(Nan::Null());
70 : }
71 185 : size_t length = grpc_byte_buffer_length(buffer);
72 185 : char *result = reinterpret_cast<char *>(calloc(length, sizeof(char)));
73 185 : size_t offset = 0;
74 : grpc_byte_buffer_reader reader;
75 185 : grpc_byte_buffer_reader_init(&reader, buffer);
76 : gpr_slice next;
77 880 : while (grpc_byte_buffer_reader_next(&reader, &next) != 0) {
78 510 : memcpy(result + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next));
79 510 : offset += GPR_SLICE_LENGTH(next);
80 510 : gpr_slice_unref(next);
81 : }
82 : return scope.Escape(MakeFastBuffer(
83 740 : Nan::NewBuffer(result, length).ToLocalChecked()));
84 : }
85 :
86 189 : Local<Value> MakeFastBuffer(Local<Value> slowBuffer) {
87 : Nan::EscapableHandleScope scope;
88 189 : Local<Object> globalObj = Nan::GetCurrentContext()->Global();
89 : Local<Function> bufferConstructor = Local<Function>::Cast(
90 567 : globalObj->Get(Nan::New("Buffer").ToLocalChecked()));
91 : Local<Value> consArgs[3] = {
92 : slowBuffer,
93 : Nan::New<Number>(::node::Buffer::Length(slowBuffer)),
94 : Nan::New<Number>(0)
95 567 : };
96 189 : Local<Object> fastBuffer = bufferConstructor->NewInstance(3, consArgs);
97 378 : return scope.Escape(fastBuffer);
98 : }
99 : } // namespace node
100 : } // namespace grpc
|