byte_buffer.cc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <string.h>
  34. #include <node.h>
  35. #include <nan.h>
  36. #include "grpc/grpc.h"
  37. #include "grpc/support/slice.h"
  38. #include "byte_buffer.h"
  39. namespace grpc {
  40. namespace node {
  41. using v8::Context;
  42. using v8::Function;
  43. using v8::Handle;
  44. using v8::Object;
  45. using v8::Number;
  46. using v8::Value;
  47. grpc_byte_buffer *BufferToByteBuffer(Handle<Value> buffer) {
  48. NanScope();
  49. int length = ::node::Buffer::Length(buffer);
  50. char *data = ::node::Buffer::Data(buffer);
  51. gpr_slice slice = gpr_slice_malloc(length);
  52. memcpy(GPR_SLICE_START_PTR(slice), data, length);
  53. grpc_byte_buffer *byte_buffer(grpc_byte_buffer_create(&slice, 1));
  54. gpr_slice_unref(slice);
  55. return byte_buffer;
  56. }
  57. Handle<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) {
  58. NanEscapableScope();
  59. if (buffer == NULL) {
  60. return NanEscapeScope(NanNull());
  61. }
  62. size_t length = grpc_byte_buffer_length(buffer);
  63. char *result = reinterpret_cast<char *>(calloc(length, sizeof(char)));
  64. size_t offset = 0;
  65. grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer);
  66. gpr_slice next;
  67. while (grpc_byte_buffer_reader_next(reader, &next) != 0) {
  68. memcpy(result + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next));
  69. offset += GPR_SLICE_LENGTH(next);
  70. }
  71. return NanEscapeScope(MakeFastBuffer(NanNewBufferHandle(result, length)));
  72. }
  73. Handle<Value> MakeFastBuffer(Handle<Value> slowBuffer) {
  74. NanEscapableScope();
  75. Handle<Object> globalObj = NanGetCurrentContext()->Global();
  76. Handle<Function> bufferConstructor = Handle<Function>::Cast(
  77. globalObj->Get(NanNew("Buffer")));
  78. Handle<Value> consArgs[3] = {
  79. slowBuffer,
  80. NanNew<Number>(::node::Buffer::Length(slowBuffer)),
  81. NanNew<Number>(0)
  82. };
  83. Handle<Object> fastBuffer = bufferConstructor->NewInstance(3, consArgs);
  84. return NanEscapeScope(fastBuffer);
  85. }
  86. } // namespace node
  87. } // namespace grpc